From d2c4ff10bfd99c7ffd2862931b5feea599e33037 Mon Sep 17 00:00:00 2001 From: Shamim Rezaie Date: Tue, 30 Jan 2018 11:42:27 +1100 Subject: [PATCH] MDL-61132 Questions: Upgrade step to add Top to existing contexts Upgrade script to add a "Top" category as a real category to the question bank --- lib/db/upgrade.php | 38 ++++++++++++++++++++++++++++++++++++++ version.php | 4 ++-- 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/lib/db/upgrade.php b/lib/db/upgrade.php index 504fbfdafab..6b6dc90d3b2 100644 --- a/lib/db/upgrade.php +++ b/lib/db/upgrade.php @@ -1935,5 +1935,43 @@ function xmldb_main_upgrade($oldversion) { upgrade_main_savepoint(true, 2017122200.01); } + if ($oldversion < 2018020500.00) { + + $topcategory = new stdClass(); + $topcategory->name = 'top'; // A non-real name for the top category. It will be localised at the display time. + $topcategory->info = ''; + $topcategory->parent = 0; + $topcategory->sortorder = 0; + + // Get the total record count - used for the progress bar. + $total = $DB->count_records_sql("SELECT COUNT(DISTINCT contextid) FROM {question_categories} WHERE parent = 0"); + + // Get the records themselves - a list of contextids. + $rs = $DB->get_recordset_sql("SELECT DISTINCT contextid FROM {question_categories} WHERE parent = 0"); + + // For each context, create a single top-level category. + $i = 0; + $pbar = new progress_bar('createtopquestioncategories', 500, true); + foreach ($rs as $contextid => $notused) { + $topcategory->contextid = $contextid; + $topcategory->stamp = make_unique_id_code(); + + $topcategoryid = $DB->insert_record('question_categories', $topcategory); + + $DB->set_field_select('question_categories', 'parent', $topcategoryid, + 'contextid = ? AND id <> ? AND parent = 0', + array($contextid, $topcategoryid)); + + // Update progress. + $i++; + $pbar->update($i, $total, "Creating top-level question categories - $i/$total."); + } + + $rs->close(); + + // Main savepoint reached. + upgrade_main_savepoint(true, 2018020500.00); + } + return true; } diff --git a/version.php b/version.php index a8826708737..281e3faa583 100644 --- a/version.php +++ b/version.php @@ -29,11 +29,11 @@ defined('MOODLE_INTERNAL') || die(); -$version = 2018020100.00; // YYYYMMDD = weekly release date of this DEV branch. +$version = 2018020500.00; // YYYYMMDD = weekly release date of this DEV branch. // RR = release increments - 00 in DEV branches. // .XX = incremental changes. -$release = '3.5dev (Build: 20180201)'; // Human-friendly version name +$release = '3.5dev (Build: 20180205)'; // Human-friendly version name $branch = '35'; // This version's branch. $maturity = MATURITY_ALPHA; // This version's maturity level.