mirror of
https://github.com/moodle/moodle.git
synced 2025-08-04 08:26:37 +02:00
MDL-63633 block_comments: Add support for removal of context users
This issue is part of the MDL-62560 Epic.
This commit is contained in:
parent
4fc10f50fa
commit
eff881a41b
2 changed files with 164 additions and 1 deletions
|
@ -30,6 +30,8 @@ defined('MOODLE_INTERNAL') || die();
|
||||||
use core_privacy\local\metadata\collection;
|
use core_privacy\local\metadata\collection;
|
||||||
use core_privacy\local\request\approved_contextlist;
|
use core_privacy\local\request\approved_contextlist;
|
||||||
use core_privacy\local\request\contextlist;
|
use core_privacy\local\request\contextlist;
|
||||||
|
use core_privacy\local\request\userlist;
|
||||||
|
use core_privacy\local\request\approved_userlist;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Privacy Subsystem implementation for block_comments.
|
* Privacy Subsystem implementation for block_comments.
|
||||||
|
@ -40,7 +42,7 @@ use core_privacy\local\request\contextlist;
|
||||||
class provider implements
|
class provider implements
|
||||||
// The block_comments block stores user provided data.
|
// The block_comments block stores user provided data.
|
||||||
\core_privacy\local\metadata\provider,
|
\core_privacy\local\metadata\provider,
|
||||||
|
\core_privacy\local\request\core_userlist_provider,
|
||||||
// The block_comments block provides data directly to core.
|
// The block_comments block provides data directly to core.
|
||||||
\core_privacy\local\request\plugin\provider {
|
\core_privacy\local\request\plugin\provider {
|
||||||
|
|
||||||
|
@ -77,6 +79,27 @@ class provider implements
|
||||||
return $contextlist;
|
return $contextlist;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the list of users within a specific context.
|
||||||
|
*
|
||||||
|
* @param userlist $userlist The userlist containing the list of users who have data in this context/plugin combination.
|
||||||
|
*/
|
||||||
|
public static function get_users_in_context(userlist $userlist) {
|
||||||
|
$context = $userlist->get_context();
|
||||||
|
|
||||||
|
$params = [
|
||||||
|
'contextid' => $context->id,
|
||||||
|
'component' => 'block_comments',
|
||||||
|
];
|
||||||
|
|
||||||
|
$sql = "SELECT userid as userid
|
||||||
|
FROM {comments}
|
||||||
|
WHERE component = :component
|
||||||
|
AND contextid = :contextid";
|
||||||
|
|
||||||
|
$userlist->add_from_sql('userid', $sql, $params);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Export all user data for the specified user, in the specified contexts.
|
* Export all user data for the specified user, in the specified contexts.
|
||||||
*
|
*
|
||||||
|
@ -104,6 +127,15 @@ class provider implements
|
||||||
\core_comment\privacy\provider::delete_comments_for_all_users($context, 'block_comments');
|
\core_comment\privacy\provider::delete_comments_for_all_users($context, 'block_comments');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Delete multiple users within a single context.
|
||||||
|
*
|
||||||
|
* @param approved_userlist $userlist The approved context and user information to delete information for.
|
||||||
|
*/
|
||||||
|
public static function delete_data_for_users(approved_userlist $userlist) {
|
||||||
|
\core_comment\privacy\provider::delete_comments_for_users($userlist, 'block_comments');
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Delete all user data for the specified user, in the specified contexts.
|
* Delete all user data for the specified user, in the specified contexts.
|
||||||
*
|
*
|
||||||
|
|
|
@ -24,6 +24,7 @@
|
||||||
|
|
||||||
use core_privacy\local\metadata\collection;
|
use core_privacy\local\metadata\collection;
|
||||||
use block_comments\privacy\provider;
|
use block_comments\privacy\provider;
|
||||||
|
use core_privacy\local\request\approved_userlist;
|
||||||
|
|
||||||
defined('MOODLE_INTERNAL') || die();
|
defined('MOODLE_INTERNAL') || die();
|
||||||
|
|
||||||
|
@ -465,4 +466,134 @@ class block_comments_privacy_provider_testcase extends \core_privacy\tests\provi
|
||||||
$DB->count_records('comments', ['component' => 'block_comments', 'userid' => $this->student1->id])
|
$DB->count_records('comments', ['component' => 'block_comments', 'userid' => $this->student1->id])
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test that only users within a course context are fetched.
|
||||||
|
* @group qtesttt
|
||||||
|
*/
|
||||||
|
public function test_get_users_in_context() {
|
||||||
|
$component = 'block_comments';
|
||||||
|
|
||||||
|
$coursecontext1 = context_course::instance($this->course1->id);
|
||||||
|
$coursecontext2 = context_course::instance($this->course2->id);
|
||||||
|
|
||||||
|
$userlist1 = new \core_privacy\local\request\userlist($coursecontext1, $component);
|
||||||
|
provider::get_users_in_context($userlist1);
|
||||||
|
$this->assertCount(0, $userlist1);
|
||||||
|
|
||||||
|
$userlist2 = new \core_privacy\local\request\userlist($coursecontext2, $component);
|
||||||
|
provider::get_users_in_context($userlist2);
|
||||||
|
$this->assertCount(0, $userlist2);
|
||||||
|
|
||||||
|
$this->setUser($this->student12);
|
||||||
|
$this->add_comment('New comment', $coursecontext1);
|
||||||
|
$this->add_comment('New comment', $coursecontext2);
|
||||||
|
$this->setUser($this->student1);
|
||||||
|
$this->add_comment('New comment', $coursecontext1);
|
||||||
|
|
||||||
|
// The list of users should contain user12 and user1.
|
||||||
|
provider::get_users_in_context($userlist1);
|
||||||
|
$this->assertCount(2, $userlist1);
|
||||||
|
$this->assertTrue(in_array($this->student1->id, $userlist1->get_userids()));
|
||||||
|
$this->assertTrue(in_array($this->student12->id, $userlist1->get_userids()));
|
||||||
|
|
||||||
|
// The list of users should contain user12.
|
||||||
|
provider::get_users_in_context($userlist2);
|
||||||
|
$this->assertCount(1, $userlist2);
|
||||||
|
$expected = [$this->student12->id];
|
||||||
|
$actual = $userlist2->get_userids();
|
||||||
|
$this->assertEquals($expected, $actual);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test that data for users in approved userlist is deleted.
|
||||||
|
*/
|
||||||
|
public function test_delete_data_for_users() {
|
||||||
|
$component = 'block_comments';
|
||||||
|
|
||||||
|
$coursecontext1 = context_course::instance($this->course1->id);
|
||||||
|
$coursecontext2 = context_course::instance($this->course2->id);
|
||||||
|
|
||||||
|
$this->setUser($this->student12);
|
||||||
|
$this->add_comment('New comment', $coursecontext1);
|
||||||
|
$this->add_comment('New comment', $coursecontext2);
|
||||||
|
$this->setUser($this->student1);
|
||||||
|
$this->add_comment('New comment', $coursecontext1);
|
||||||
|
|
||||||
|
$userlist1 = new \core_privacy\local\request\userlist($coursecontext1, $component);
|
||||||
|
provider::get_users_in_context($userlist1);
|
||||||
|
$this->assertCount(2, $userlist1);
|
||||||
|
|
||||||
|
$userlist2 = new \core_privacy\local\request\userlist($coursecontext2, $component);
|
||||||
|
provider::get_users_in_context($userlist2);
|
||||||
|
$this->assertCount(1, $userlist2);
|
||||||
|
|
||||||
|
// Convert $userlist1 into an approved_contextlist.
|
||||||
|
$approvedlist1 = new approved_userlist($coursecontext1, $component, $userlist1->get_userids());
|
||||||
|
// Delete using delete_data_for_user.
|
||||||
|
provider::delete_data_for_users($approvedlist1);
|
||||||
|
|
||||||
|
// Re-fetch users in coursecontext1.
|
||||||
|
$userlist1 = new \core_privacy\local\request\userlist($coursecontext1, $component);
|
||||||
|
provider::get_users_in_context($userlist1);
|
||||||
|
// The user data in coursecontext1 should be deleted.
|
||||||
|
$this->assertCount(0, $userlist1);
|
||||||
|
|
||||||
|
// Re-fetch users in coursecontext2.
|
||||||
|
$userlist2 = new \core_privacy\local\request\userlist($coursecontext2, $component);
|
||||||
|
provider::get_users_in_context($userlist2);
|
||||||
|
// The user data in coursecontext2 should be still present.
|
||||||
|
$this->assertCount(1, $userlist2);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test for provider::delete_data_for_user() when there are also comments from other plugins.
|
||||||
|
*/
|
||||||
|
public function test_delete_data_for_users_with_comments_from_other_plugins() {
|
||||||
|
$component = 'block_comments';
|
||||||
|
|
||||||
|
$assigngenerator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
|
||||||
|
$instance = $assigngenerator->create_instance(['course' => $this->course1]);
|
||||||
|
$cm = get_coursemodule_from_instance('assign', $instance->id);
|
||||||
|
$assigncontext = \context_module::instance($cm->id);
|
||||||
|
$assign = new \assign($assigncontext, $cm, $this->course1);
|
||||||
|
|
||||||
|
// Add a comments block in the assignment page.
|
||||||
|
$this->add_comments_block_in_context($assigncontext);
|
||||||
|
|
||||||
|
$submission = $assign->get_user_submission($this->student1->id, true);
|
||||||
|
|
||||||
|
$options = new stdClass();
|
||||||
|
$options->area = 'submission_comments';
|
||||||
|
$options->course = $assign->get_course();
|
||||||
|
$options->context = $assigncontext;
|
||||||
|
$options->itemid = $submission->id;
|
||||||
|
$options->component = 'assignsubmission_comments';
|
||||||
|
$options->showcount = true;
|
||||||
|
$options->displaycancel = true;
|
||||||
|
|
||||||
|
$comment = new comment($options);
|
||||||
|
$comment->set_post_permission(true);
|
||||||
|
|
||||||
|
$this->setUser($this->student1);
|
||||||
|
$comment->add('Comment from student 1');
|
||||||
|
|
||||||
|
$this->add_comment('New comment', $assigncontext);
|
||||||
|
$this->add_comment('New comment', $assigncontext);
|
||||||
|
|
||||||
|
$userlist1 = new \core_privacy\local\request\userlist($assigncontext, $component);
|
||||||
|
provider::get_users_in_context($userlist1);
|
||||||
|
$this->assertCount(1, $userlist1);
|
||||||
|
|
||||||
|
// Convert $userlist1 into an approved_contextlist.
|
||||||
|
$approvedlist = new approved_userlist($assigncontext, $component, $userlist1->get_userids());
|
||||||
|
// Delete using delete_data_for_user.
|
||||||
|
provider::delete_data_for_users($approvedlist);
|
||||||
|
|
||||||
|
// Re-fetch users in assigncontext.
|
||||||
|
$userlist1 = new \core_privacy\local\request\userlist($assigncontext, $component);
|
||||||
|
provider::get_users_in_context($userlist1);
|
||||||
|
// The user data in assigncontext should be deleted.
|
||||||
|
$this->assertCount(0, $userlist1);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue