From 6336bd914a7adb446dc48b88a0f58bbcec795a17 Mon Sep 17 00:00:00 2001 From: Henning Bostelmann Date: Tue, 13 Sep 2011 01:11:19 +0100 Subject: [PATCH] MDL-29350 Prevent duplication of groupings when copying activities This patch also includes a database upgrade to correct data produced as a result of this bug. --- backup/moodle2/restore_stepslib.php | 9 ++++++++- lib/db/upgrade.php | 15 +++++++++++++++ version.php | 2 +- 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/backup/moodle2/restore_stepslib.php b/backup/moodle2/restore_stepslib.php index 3044c7ff808..c9705a98b33 100644 --- a/backup/moodle2/restore_stepslib.php +++ b/backup/moodle2/restore_stepslib.php @@ -802,7 +802,14 @@ class restore_groups_structure_step extends restore_structure_step { $data->groupingid = $this->get_new_parentid('grouping'); // Use new parentid $data->groupid = $this->get_mappingid('group', $data->groupid); // Get from mappings - $DB->insert_record('groupings_groups', $data); // No need to set this mapping (no child info nor files) + + $params = array(); + $params['groupingid'] = $data->groupingid; + $params['groupid'] = $data->groupid; + + if (!$DB->record_exists('groupings_groups', $params)) { + $DB->insert_record('groupings_groups', $data); // No need to set this mapping (no child info nor files) + } } protected function after_execute() { diff --git a/lib/db/upgrade.php b/lib/db/upgrade.php index 83adec74439..097cfb10066 100644 --- a/lib/db/upgrade.php +++ b/lib/db/upgrade.php @@ -6916,6 +6916,21 @@ FROM upgrade_main_savepoint(true, 2011110200.02); } + if ($oldversion < 2011111500.01) { + // Remove duplicate entries from groupings_groups table + $sql = 'SELECT MIN(id) AS firstid, groupingid, groupid FROM {groupings_groups} '. + 'GROUP BY groupingid, groupid HAVING COUNT(id)>1'; + $badrecs = $DB->get_records_sql($sql); + foreach ($badrecs as $badrec) { + $where = 'groupingid = ? and groupid = ? and id > ?'; + $params = array($badrec->groupingid, $badrec->groupid, $badrec->firstid); + $DB->delete_records_select('groupings_groups', $where, $params); + } + + // Main savepoint reached + upgrade_main_savepoint(true, 2011111500.01); + } + return true; } diff --git a/version.php b/version.php index eb0a3297af2..19816d1a81f 100644 --- a/version.php +++ b/version.php @@ -30,7 +30,7 @@ defined('MOODLE_INTERNAL') || die(); -$version = 2011111500.00; // YYYYMMDD = weekly release date of this DEV branch +$version = 2011111500.01; // YYYYMMDD = weekly release date of this DEV branch // RR = release increments - 00 in DEV branches // .XX = incremental changes -- 2.11.4.GIT