MDL-58256 group: prevents users to be added to a group twice

This commit is contained in:
Justus Dieckmann
2019-01-22 13:05:34 +01:00
committed by Sara Arjona
parent 279ec1f986
commit 609203ada6
3 changed files with 30 additions and 2 deletions
+2 -1
View File
@@ -2230,6 +2230,7 @@
<KEY NAME="primary" TYPE="primary" FIELDS="id"/>
<KEY NAME="groupid" TYPE="foreign" FIELDS="groupid" REFTABLE="groups" REFFIELDS="id"/>
<KEY NAME="userid" TYPE="foreign" FIELDS="userid" REFTABLE="user" REFFIELDS="id"/>
<KEY NAME="useridgroupid" TYPE="unique" FIELDS="userid, groupid" COMMENT="Unique key"/>
</KEYS>
</TABLE>
<TABLE NAME="groupings_groups" COMMENT="Link a grouping to a group (note, groups can be in multiple groupings ONLY in a course). WAS: groups_groupings_groups">
@@ -3863,4 +3864,4 @@
</INDEXES>
</TABLE>
</TABLES>
</XMLDB>
</XMLDB>
+27
View File
@@ -2373,5 +2373,32 @@ function xmldb_main_upgrade($oldversion) {
upgrade_main_savepoint(true, 2018051704.03);
}
if ($oldversion < 2018051704.04) {
// Remove duplicate entries from group memberships.
// Find records with multiple userid/groupid combinations and find the highest ID.
// Later we will remove all those entries.
$sql = "
SELECT MIN(id) as minid, userid, groupid
FROM {groups_members}
GROUP BY userid, groupid
HAVING COUNT(id) > 1";
if ($duplicatedrows = $DB->get_recordset_sql($sql)) {
foreach ($duplicatedrows as $row) {
$DB->delete_records_select('groups_members',
'userid = :userid AND groupid = :groupid AND id <> :minid', (array)$row);
}
}
$duplicatedrows->close();
// Define key useridgroupid (unique) to be added to group_members.
$table = new xmldb_table('groups_members');
$key = new xmldb_key('useridgroupid', XMLDB_KEY_UNIQUE, array('userid', 'groupid'));
// Launch add key useridgroupid.
$dbman->add_key($table, $key);
// Main savepoint reached.
upgrade_main_savepoint(true, 2018051704.04);
}
return true;
}
+1 -1
View File
@@ -29,7 +29,7 @@
defined('MOODLE_INTERNAL') || die();
$version = 2018051704.03; // 20180517 = branching date YYYYMMDD - do not modify!
$version = 2018051704.04; // 20180517 = branching date YYYYMMDD - do not modify!
// RR = release increments - 00 in DEV branches.
// .XX = incremental changes.