From 8516febdd7c11fc8d399765e821d35db7eff349e Mon Sep 17 00:00:00 2001 From: Justus Dieckmann Date: Thu, 20 Dec 2018 20:19:38 +0100 Subject: [PATCH] MDL-58256 group: prevents users to be added to a group twice --- lib/db/install.xml | 5 +++-- lib/db/upgrade.php | 27 +++++++++++++++++++++++++++ version.php | 2 +- 3 files changed, 31 insertions(+), 3 deletions(-) diff --git a/lib/db/install.xml b/lib/db/install.xml index e52e7d70bf4..e106f9c18fa 100644 --- a/lib/db/install.xml +++ b/lib/db/install.xml @@ -1,5 +1,5 @@ - @@ -2282,6 +2282,7 @@ + @@ -4096,4 +4097,4 @@
-
\ No newline at end of file + diff --git a/lib/db/upgrade.php b/lib/db/upgrade.php index 2b71b073e20..cf8ad44ffe4 100644 --- a/lib/db/upgrade.php +++ b/lib/db/upgrade.php @@ -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; } diff --git a/version.php b/version.php index 7f7991fd1d7..e2c4dc079ba 100644 --- a/version.php +++ b/version.php @@ -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.