Skip to content

Commit

Permalink
Merge branch 'MDL-63692-master' of git://github.com/sarjona/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewnicols committed Nov 19, 2018
2 parents b2cb05a + 16913e6 commit 08cd156
Show file tree
Hide file tree
Showing 10 changed files with 2,262 additions and 117 deletions.
31 changes: 26 additions & 5 deletions favourites/classes/privacy/provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,11 @@ public static function get_favourites_info_for_user(int $userid, \context $conte
* @param \context $context The context to which deletion is scoped.
* @param string $component The favourite's component name.
* @param string $itemtype The favourite's itemtype.
* @param int $itemid Optional itemid associated with component.
* @throws \dml_exception if any errors are encountered during deletion.
*/
public static function delete_favourites_for_all_users(\context $context, string $component, string $itemtype) {
public static function delete_favourites_for_all_users(\context $context, string $component, string $itemtype,
int $itemid = 0) {
global $DB;

$params = [
Expand All @@ -184,6 +186,11 @@ public static function delete_favourites_for_all_users(\context $context, string
];

$select = "component = :component AND itemtype =:itemtype AND contextid = :contextid";

if (!empty($itemid)) {
$select .= " AND itemid = :itemid";
$params['itemid'] = $itemid;
}
$DB->delete_records_select('favourite', $select, $params);
}

Expand All @@ -193,10 +200,11 @@ public static function delete_favourites_for_all_users(\context $context, string
* @param \core_privacy\local\request\approved_userlist $userlist The approved contexts and user information
* to delete information for.
* @param string $itemtype The favourite's itemtype.
* @param int $itemid Optional itemid associated with component.
* @throws \dml_exception if any errors are encountered during deletion.
*/
public static function delete_favourites_for_userlist(\core_privacy\local\request\approved_userlist $userlist,
string $itemtype) {
string $itemtype, int $itemid = 0) {
global $DB;

$userids = $userlist->get_userids();
Expand All @@ -217,19 +225,26 @@ public static function delete_favourites_for_userlist(\core_privacy\local\reques
$params += $userparams;
$select = "component = :component AND itemtype = :itemtype AND contextid = :contextid AND userid $usersql";

if (!empty($itemid)) {
$select .= " AND itemid = :itemid";
$params['itemid'] = $itemid;
}

$DB->delete_records_select('favourite', $select, $params);
}

/**
* Delete all favourites for the specified user, in the specified contexts.
*
* @param approved_contextlist $contextlist The approved contexts and user information to delete information for.
* @param string $component
* @param string $itemtype
* @param string $component The favourite's component name.
* @param string $itemtype The favourite's itemtype.
* @param int $itemid Optional itemid associated with component.
* @throws \coding_exception
* @throws \dml_exception
*/
public static function delete_favourites_for_user(approved_contextlist $contextlist, string $component, string $itemtype) {
public static function delete_favourites_for_user(approved_contextlist $contextlist, string $component, string $itemtype,
int $itemid = 0) {
global $DB;

$userid = $contextlist->get_user()->id;
Expand All @@ -244,6 +259,12 @@ public static function delete_favourites_for_user(approved_contextlist $contextl
$params += $inparams;

$select = "userid = :userid AND component = :component AND itemtype =:itemtype AND contextid $insql";

if (!empty($itemid)) {
$select .= " AND itemid = :itemid";
$params['itemid'] = $itemid;
}

$DB->delete_records_select('favourite', $select, $params);
}
}
40 changes: 37 additions & 3 deletions group/classes/privacy/provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ public static function get_metadata(collection $collection) : collection {
'timeadded' => 'privacy:metadata:groups:timeadded',
], 'privacy:metadata:groups');

$collection->link_subsystem('core_message', 'privacy:metadata:core_message');

return $collection;
}

Expand All @@ -88,7 +90,7 @@ public static function export_groups(\context $context, string $component, array

$subcontext[] = get_string('groups', 'core_group');

$sql = "SELECT gm.id, gm.timeadded, gm.userid, g.name
$sql = "SELECT gm.id, gm.timeadded, gm.userid, g.name, gm.groupid
FROM {groups_members} gm
JOIN {groups} g ON gm.groupid = g.id
WHERE g.courseid = :courseid
Expand All @@ -107,7 +109,7 @@ public static function export_groups(\context $context, string $component, array

$groups = $DB->get_records_sql($sql, $params);

$groups = array_map(function($group) {
$groupstoexport = array_map(function($group) {
return (object) [
'name' => format_string($group->name),
'timeadded' => transform::datetime($group->timeadded),
Expand All @@ -117,8 +119,14 @@ public static function export_groups(\context $context, string $component, array
if (!empty($groups)) {
\core_privacy\local\request\writer::with_context($context)
->export_data($subcontext, (object) [
'groups' => $groups,
'groups' => $groupstoexport,
]);

foreach ($groups as $group) {
// Export associated conversations to this group.
\core_message\privacy\provider::export_conversations($USER->id, 'core_group', 'groups',
$context, [], $group->groupid);
}
}
}

Expand Down Expand Up @@ -148,6 +156,13 @@ public static function delete_groups_for_all_users(\context $context, string $co
$params['itemid'] = $itemid;
}

// Delete the group conversations.
$groups = $DB->get_records_select('groups_members', $select, $params);
foreach ($groups as $group) {
\core_message\privacy\provider::delete_conversations_for_all_users($context, 'core_group', 'groups', $group->groupid);
}

// Remove members from the group.
$DB->delete_records_select('groups_members', $select, $params);

// Purge the group and grouping cache for users.
Expand Down Expand Up @@ -191,6 +206,13 @@ public static function delete_groups_for_user(approved_contextlist $contextlist,
$params['itemid'] = $itemid;
}

// Delete the group conversations.
$groups = $DB->get_records_select('groups_members', $select, $params);
foreach ($groups as $group) {
\core_message\privacy\provider::delete_conversations_for_user($contextlist, 'core_group', 'groups', $group->groupid);
}

// Remove members from the group.
$DB->delete_records_select('groups_members', $select, $params);

// Invalidate the group and grouping cache for the user.
Expand Down Expand Up @@ -227,6 +249,9 @@ public static function get_group_members_in_context(userlist $userlist, string $
}

$userlist->add_from_sql('userid', $sql, $params);

// Get the users with some group conversation in this context.
\core_message\privacy\provider::add_conversations_in_context($userlist, 'core_group', 'groups', $itemid);
}

/**
Expand Down Expand Up @@ -258,6 +283,12 @@ public static function delete_groups_for_users(approved_userlist $userlist, stri
$params['itemid'] = $itemid;
}

// Delete the group conversations for these users.
$groups = $DB->get_records_select('groups_members', $select, $params);
foreach ($groups as $group) {
\core_message\privacy\provider::delete_conversations_for_users($userlist, 'core_group', 'groups', $group->groupid);
}

$DB->delete_records_select('groups_members', $select, $params);

// Invalidate the group and grouping cache for the user.
Expand Down Expand Up @@ -294,6 +325,9 @@ public static function get_contexts_for_group_member(int $userid, string $compon

$contextlist->add_from_sql($sql, $params);

// Get the contexts where the userid has group conversations.
\core_message\privacy\provider::add_contexts_for_conversations($contextlist, $userid, 'core_group', 'groups', $itemid);

return $contextlist;
}

Expand Down
148 changes: 145 additions & 3 deletions group/tests/privacy_provider_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,20 @@ public function test_get_metadata() {
$collection = new collection('core_group');
$newcollection = provider::get_metadata($collection);
$itemcollection = $newcollection->get_collection();
$this->assertCount(1, $itemcollection);

$table = reset($itemcollection);
$this->assertCount(2, $itemcollection);

$table = array_shift($itemcollection);
$this->assertEquals('groups_members', $table->get_name());
$this->assertEquals('privacy:metadata:groups', $table->get_summary());

$privacyfields = $table->get_privacy_fields();
$this->assertArrayHasKey('groupid', $privacyfields);
$this->assertArrayHasKey('userid', $privacyfields);
$this->assertArrayHasKey('timeadded', $privacyfields);

$table = array_shift($itemcollection);
$this->assertEquals('core_message', $table->get_name());
$this->assertEquals('privacy:metadata:core_message', $table->get_summary());
}

/**
Expand Down Expand Up @@ -508,6 +511,145 @@ public function test_delete_groups_for_user_for_component() {
);
}

/**
* Test for provider::delete_groups_for_users() to delete group memberships of a component.
*/
public function test_delete_groups_for_users_for_component() {
global $DB;

$this->resetAfterTest();

$course1 = $this->getDataGenerator()->create_course();
$course2 = $this->getDataGenerator()->create_course();
$course3 = $this->getDataGenerator()->create_course();

$group1a = $this->getDataGenerator()->create_group(array('courseid' => $course1->id));
$group1b = $this->getDataGenerator()->create_group(array('courseid' => $course1->id));
$group2a = $this->getDataGenerator()->create_group(array('courseid' => $course2->id));
$group2b = $this->getDataGenerator()->create_group(array('courseid' => $course2->id));
$group3a = $this->getDataGenerator()->create_group(array('courseid' => $course3->id));
$group3b = $this->getDataGenerator()->create_group(array('courseid' => $course3->id));

$user1 = $this->getDataGenerator()->create_user();
$user2 = $this->getDataGenerator()->create_user();

$this->getDataGenerator()->enrol_user($user1->id, $course1->id, null, 'self');
$this->getDataGenerator()->enrol_user($user1->id, $course2->id, null, 'self');
$this->getDataGenerator()->enrol_user($user1->id, $course3->id, null, 'self');
$this->getDataGenerator()->enrol_user($user2->id, $course1->id, null, 'self');
$this->getDataGenerator()->enrol_user($user2->id, $course2->id, null, 'self');
$this->getDataGenerator()->enrol_user($user2->id, $course3->id, null, 'self');

$this->getDataGenerator()->create_group_member(
array('groupid' => $group1a->id, 'userid' => $user1->id, 'component' => 'enrol_self'));
$this->getDataGenerator()->create_group_member(
array('groupid' => $group1b->id, 'userid' => $user2->id, 'component' => 'enrol_self'));
$this->getDataGenerator()->create_group_member(
array('groupid' => $group2a->id, 'userid' => $user1->id, 'component' => 'enrol_self'));
$this->getDataGenerator()->create_group_member(
array('groupid' => $group2b->id, 'userid' => $user2->id, 'component' => 'enrol_self'));
$this->getDataGenerator()->create_group_member(array('groupid' => $group3a->id, 'userid' => $user1->id));
$this->getDataGenerator()->create_group_member(array('groupid' => $group3b->id, 'userid' => $user2->id));

$this->assertEquals(
2,
$DB->count_records_sql("SELECT COUNT(gm.id)
FROM {groups_members} gm
JOIN {groups} g ON gm.groupid = g.id
WHERE g.courseid = ?", [$course1->id])
);
$this->assertEquals(
2,
$DB->count_records_sql("SELECT COUNT(gm.id)
FROM {groups_members} gm
JOIN {groups} g ON gm.groupid = g.id
WHERE g.courseid = ?", [$course2->id])
);
$this->assertEquals(
2,
$DB->count_records_sql("SELECT COUNT(gm.id)
FROM {groups_members} gm
JOIN {groups} g ON gm.groupid = g.id
WHERE g.courseid = ?", [$course2->id])
);
$this->assertEquals(
3,
$DB->count_records_sql("SELECT COUNT(gm.id)
FROM {groups_members} gm
JOIN {groups} g ON gm.groupid = g.id
WHERE gm.userid = ?", [$user1->id])
);

// Delete user1 and user2 from groups in course1.
$coursecontext1 = context_course::instance($course1->id);
$approveduserlist = new \core_privacy\local\request\approved_userlist($coursecontext1, 'core_group',
[$user1->id, $user2->id]);
provider::delete_groups_for_users($approveduserlist, 'enrol_self');

$this->assertEquals(
0,
$DB->count_records_sql("SELECT COUNT(gm.id)
FROM {groups_members} gm
JOIN {groups} g ON gm.groupid = g.id
WHERE g.courseid = ?", [$course1->id])
);
$this->assertEquals(
2,
$DB->count_records_sql("SELECT COUNT(gm.id)
FROM {groups_members} gm
JOIN {groups} g ON gm.groupid = g.id
WHERE g.courseid = ?", [$course2->id])
);
$this->assertEquals(
2,
$DB->count_records_sql("SELECT COUNT(gm.id)
FROM {groups_members} gm
JOIN {groups} g ON gm.groupid = g.id
WHERE g.courseid = ?", [$course3->id])
);
$this->assertEquals(
2,
$DB->count_records_sql("SELECT COUNT(gm.id)
FROM {groups_members} gm
JOIN {groups} g ON gm.groupid = g.id
WHERE gm.userid = ?", [$user1->id])
);

// Delete user1 and user2 from course3.
$coursecontext3 = context_course::instance($course3->id);
$approveduserlist = new \core_privacy\local\request\approved_userlist($coursecontext3, 'core_group',
[$user1->id, $user2->id]);
provider::delete_groups_for_users($approveduserlist, 'enrol_self');
$this->assertEquals(
0,
$DB->count_records_sql("SELECT COUNT(gm.id)
FROM {groups_members} gm
JOIN {groups} g ON gm.groupid = g.id
WHERE g.courseid = ?", [$course1->id])
);
$this->assertEquals(
2,
$DB->count_records_sql("SELECT COUNT(gm.id)
FROM {groups_members} gm
JOIN {groups} g ON gm.groupid = g.id
WHERE g.courseid = ?", [$course2->id])
);
$this->assertEquals(
2,
$DB->count_records_sql("SELECT COUNT(gm.id)
FROM {groups_members} gm
JOIN {groups} g ON gm.groupid = g.id
WHERE g.courseid = ?", [$course3->id])
);
$this->assertEquals(
2,
$DB->count_records_sql("SELECT COUNT(gm.id)
FROM {groups_members} gm
JOIN {groups} g ON gm.groupid = g.id
WHERE gm.userid = ?", [$user1->id])
);
}

/**
* Test for provider::delete_groups_for_user() to check deleting from cache.
*/
Expand Down
1 change: 1 addition & 0 deletions lang/en/group.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@
$string['potentialmembers'] = 'Potential members: {$a}';
$string['potentialmembs'] = 'Potential members';
$string['printerfriendly'] = 'Printer-friendly display';
$string['privacy:metadata:core_message'] = 'The group conversations';
$string['privacy:metadata:groups'] = 'A record of group membership.';
$string['privacy:metadata:groups:groupid'] = 'The ID of the group.';
$string['privacy:metadata:groups:timeadded'] = 'The timestamp indicating when the user was added to the group.';
Expand Down
2 changes: 2 additions & 0 deletions lang/en/message.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@
$string['permitted'] = 'Permitted';
$string['privacy'] = 'Privacy';
$string['privacy_desc'] = 'You can restrict who can message you';
$string['privacy:metadata:core_favourites'] = 'The conversations starred by the user';
$string['privacy:metadata:messages'] = 'Messages';
$string['privacy:metadata:messages:conversationid'] = 'The ID of the conversation';
$string['privacy:metadata:messages:fullmessage'] = 'The full message';
Expand Down Expand Up @@ -189,6 +190,7 @@
$string['privacy:metadata:notifications:useridto'] = 'The ID of the user who received the notification';
$string['privacy:metadata:preference:core_message_settings'] = 'Settings related to messaging';
$string['privacy:request:preference:set'] = 'The value of the setting \'{$a->name}\' was \'{$a->value}\'';
$string['privacy:export:conversationprefix'] = 'Conversation: ';
$string['processorsettings'] = 'Processor settings';
$string['removecontact'] = 'Remove contact';
$string['removecontactconfirm'] = 'Are you sure you want to remove {$a} from your contacts?';
Expand Down
Loading

0 comments on commit 08cd156

Please sign in to comment.