diff --git a/lib/db/upgrade.php b/lib/db/upgrade.php index dd3643fffcf..9cf7394e91b 100644 --- a/lib/db/upgrade.php +++ b/lib/db/upgrade.php @@ -2268,5 +2268,30 @@ function xmldb_main_upgrade($oldversion) { upgrade_main_savepoint(true, 2018062800.03); } + if ($oldversion < 2018072500.00) { + // Find all duplicate top level categories per context. + $duplicates = $DB->get_records_sql("SELECT qc1.* + FROM {question_categories} qc1 + JOIN {question_categories} qc2 + ON qc1.contextid = qc2.contextid AND qc1.id <> qc2.id + WHERE qc1.parent = 0 AND qc2.parent = 0 + ORDER BY qc1.contextid, qc1.id"); + + // For each context, let the first top category to remain as top category and make the rest its children. + $currentcontextid = 0; + $chosentopid = 0; + foreach ($duplicates as $duplicate) { + if ($currentcontextid != $duplicate->contextid) { + $currentcontextid = $duplicate->contextid; + $chosentopid = $duplicate->id; + } else { + $DB->set_field('question_categories', 'parent', $chosentopid, ['id' => $duplicate->id]); + } + } + + // Main savepoint reached. + upgrade_main_savepoint(true, 2018072500.00); + } + return true; } diff --git a/version.php b/version.php index 94c593137c0..0ec04158ac3 100644 --- a/version.php +++ b/version.php @@ -29,7 +29,7 @@ defined('MOODLE_INTERNAL') || die(); -$version = 2018072000.00; // YYYYMMDD = weekly release date of this DEV branch. +$version = 2018072500.00; // YYYYMMDD = weekly release date of this DEV branch. // RR = release increments - 00 in DEV branches. // .XX = incremental changes.