Skip to content

Commit

Permalink
MDL-58256 group: prevents users to be added to a group twice
Browse files Browse the repository at this point in the history
  • Loading branch information
Justus Dieckmann authored and sarjona committed Jan 22, 2019
1 parent 7d4a900 commit 8516feb
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
5 changes: 3 additions & 2 deletions lib/db/install.xml
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" ?>
<XMLDB PATH="lib/db" VERSION="20190111" COMMENT="XMLDB file for core Moodle tables"
<XMLDB PATH="lib/db" VERSION="20190122" COMMENT="XMLDB file for core Moodle tables"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../../lib/xmldb/xmldb.xsd"
>
Expand Down Expand Up @@ -2282,6 +2282,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">
Expand Down Expand Up @@ -4096,4 +4097,4 @@
</INDEXES>
</TABLE>
</TABLES>
</XMLDB>
</XMLDB>
27 changes: 27 additions & 0 deletions lib/db/upgrade.php
Expand Up @@ -2692,5 +2692,32 @@ function xmldb_main_upgrade($oldversion) {
upgrade_main_savepoint(true, 2019011801.02);
}

if ($oldversion < 2019011801.03) {
// 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, 2019011801.03);
}

return true;
}
2 changes: 1 addition & 1 deletion version.php
Expand Up @@ -29,7 +29,7 @@

defined('MOODLE_INTERNAL') || die();

$version = 2019011801.02; // YYYYMMDD = weekly release date of this DEV branch.
$version = 2019011801.03; // YYYYMMDD = weekly release date of this DEV branch.
// RR = release increments - 00 in DEV branches.
// .XX = incremental changes.

Expand Down

0 comments on commit 8516feb

Please sign in to comment.