From e73cdf3714dff0c2aecee41e40391486cb15a60e Mon Sep 17 00:00:00 2001 From: Gordon Bateson Date: Fri, 23 Jan 2015 16:03:02 +0900 Subject: [PATCH] MDL-79863 qtype_ordering: qtype/ordering fix error upgrading when there are duplicate questionids in qtype_ordering_options table --- question/type/ordering/db/upgrade.php | 14 ++++++++++++++ question/type/ordering/version.php | 4 ++-- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/question/type/ordering/db/upgrade.php b/question/type/ordering/db/upgrade.php index ac6f0617861..017a0801488 100644 --- a/question/type/ordering/db/upgrade.php +++ b/question/type/ordering/db/upgrade.php @@ -125,6 +125,20 @@ function xmldb_qtype_ordering_upgrade($oldversion) { } } + // make sure there are no duplicate "questionid" fields in "qtype_ordering_options" table + $select = 'questionid, COUNT(*) AS countduplicates, MAX(id) AS maxid'; + $from = '{qtype_ordering_options}'; + $group = 'questionid'; + $having = 'countduplicates > ?'; + $params = array(1); + if ($records = $DB->get_records_sql("SELECT $select FROM $from GROUP BY $group HAVING $having", $params)) { + foreach ($records as $record) { + $select = 'id <> ? AND questionid = ?'; + $params = array($record->maxid, $record->questionid); + $DB->delete_records_select('qtype_ordering_options', $select, $params); + } + } + // restore index on questionid field $table = new xmldb_table('qtype_ordering_options'); $index = new xmldb_index('qtypordeopti_que_uix', XMLDB_INDEX_UNIQUE, array('questionid')); diff --git a/question/type/ordering/version.php b/question/type/ordering/version.php index 95166fd71b7..5e5845bbe4c 100644 --- a/question/type/ordering/version.php +++ b/question/type/ordering/version.php @@ -30,6 +30,6 @@ defined('MOODLE_INTERNAL') || die(); $plugin->cron = 0; $plugin->component = 'qtype_ordering'; $plugin->maturity = MATURITY_STABLE; // ALPHA=50, BETA=100, RC=150, STABLE=200 -$plugin->release = '2015-01-19 (15)'; -$plugin->version = 2015011915; +$plugin->release = '2015-01-23 (16)'; +$plugin->version = 2015012316; $plugin->requires = 2010112400; // Moodle 2.0