From a871a7a757608cb692e3047f69222c207c178e19 Mon Sep 17 00:00:00 2001 From: Eric Merrill Date: Mon, 2 Jun 2014 11:54:11 -0400 Subject: [PATCH] MDL-45763 question Random qtype should use '0' or '1' question text. Some time ago, it when from using '0' for no-subcategories to blank. Change to properly define '0' or '1', update old questions, and make sure to fix newly restored questions. --- mod/quiz/editlib.php | 4 +- .../restore_qtype_random_plugin.class.php | 46 +++++++++++++ question/type/random/db/upgrade.php | 68 +++++++++++++++++++ question/type/random/questiontype.php | 5 ++ question/type/random/version.php | 2 +- 5 files changed, 122 insertions(+), 3 deletions(-) create mode 100644 question/type/random/db/upgrade.php diff --git a/mod/quiz/editlib.php b/mod/quiz/editlib.php index 47932922f83..66efafd50fb 100644 --- a/mod/quiz/editlib.php +++ b/mod/quiz/editlib.php @@ -213,7 +213,7 @@ function quiz_add_random_questions($quiz, $addonpage, $categoryid, $number, SELECT * FROM {quiz_slots} WHERE questionid = q.id) - ORDER BY id", array($category->id, $includesubcategories))) { + ORDER BY id", array($category->id, ($includesubcategories ? '1' : '0')))) { // Take as many of these as needed. while (($existingquestion = array_shift($existingquestions)) && $number > 0) { quiz_add_quiz_question($existingquestion->id, $quiz, $addonpage); @@ -228,7 +228,7 @@ function quiz_add_random_questions($quiz, $addonpage, $categoryid, $number, // More random questions are needed, create them. for ($i = 0; $i < $number; $i += 1) { $form = new stdClass(); - $form->questiontext = array('text' => $includesubcategories, 'format' => 0); + $form->questiontext = array('text' => ($includesubcategories ? '1' : '0'), 'format' => 0); $form->category = $category->id . ',' . $category->contextid; $form->defaultmark = 1; $form->hidden = 1; diff --git a/question/type/random/backup/moodle2/restore_qtype_random_plugin.class.php b/question/type/random/backup/moodle2/restore_qtype_random_plugin.class.php index 7418cd174eb..44746315fa1 100644 --- a/question/type/random/backup/moodle2/restore_qtype_random_plugin.class.php +++ b/question/type/random/backup/moodle2/restore_qtype_random_plugin.class.php @@ -33,6 +33,33 @@ defined('MOODLE_INTERNAL') || die(); * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class restore_qtype_random_plugin extends restore_qtype_plugin { + + /** + * Define the plugin structure. + * + * @return array Array of {@link restore_path_elements}. + */ + protected function define_question_plugin_structure() { + $paths = array(); + + // We have to specify a path here if we want after_execute_question to be called. + $elename = 'donothing'; + $elepath = $this->get_pathfor('/'); + + $paths[] = new restore_path_element($elename, $elepath); + + return $paths; // And we return the interesting paths. + } + + /** + * Required function to process path. Should never be called. + * + * @param object $data Data elements. + */ + public function process_donothing($data) { + // Intentionally blank. + } + /** * Given one question_states record, return the answer * recoded pointing to all the restored stuff for random questions @@ -69,4 +96,23 @@ class restore_qtype_random_plugin extends restore_qtype_plugin { } return $result; } + + /** + * After restoring, make sure questiontext is set properly. + */ + public function after_execute_question() { + global $DB; + + // Update any blank random questiontexts to 0. + $sql = "UPDATE {question} + SET questiontext = '0' + WHERE qtype = 'random' + AND " . $DB->sql_compare_text('questiontext') . " = ? + AND id IN (SELECT bi.newitemid + FROM {backup_ids_temp} AS bi + WHERE bi.backupid = ? + AND bi.itemname = 'question_created')"; + + $DB->execute($sql, array('', $this->get_restoreid())); + } } diff --git a/question/type/random/db/upgrade.php b/question/type/random/db/upgrade.php new file mode 100644 index 00000000000..469e4fc44f4 --- /dev/null +++ b/question/type/random/db/upgrade.php @@ -0,0 +1,68 @@ +. + +/** + * Random question type upgrade code. + * + * @package qtype_random + * @copyright 2014 Eric Merrill + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + + +defined('MOODLE_INTERNAL') || die(); + + +/** + * Upgrade code for the random question type. + * @param int $oldversion the version we are upgrading from. + */ +function xmldb_qtype_random_upgrade($oldversion) { + global $CFG, $DB; + + $dbman = $DB->get_manager(); + + // Moodle v2.2.0 release upgrade line + // Put any upgrade step following this. + + // Moodle v2.3.0 release upgrade line + // Put any upgrade step following this. + + // Moodle v2.4.0 release upgrade line + // Put any upgrade step following this. + + // Moodle v2.5.0 release upgrade line. + // Put any upgrade step following this. + + // Moodle v2.6.0 release upgrade line. + // Put any upgrade step following this. + + // Moodle v2.7.0 release upgrade line. + // Put any upgrade step following this. + + if ($oldversion < 2014060200) { + $sql = "UPDATE {question} + SET questiontext = '0' + WHERE qtype = 'random' + AND " . $DB->sql_compare_text('questiontext') . " = ?"; + $DB->execute($sql, array('')); + + // Record that qtype_random savepoint was reached. + upgrade_plugin_savepoint(true, 2014060200, 'qtype', 'random'); + } + + return true; +} diff --git a/question/type/random/questiontype.php b/question/type/random/questiontype.php index c57e360d35c..fbb85a75578 100644 --- a/question/type/random/questiontype.php +++ b/question/type/random/questiontype.php @@ -151,6 +151,11 @@ class qtype_random extends question_type { public function save_question($question, $form) { $form->name = ''; + if ($form->questiontext) { + $form->questiontext = '1'; + } else { + $form->questiontext = '0'; + } $form->questiontextformat = FORMAT_MOODLE; $form->tags = array(); diff --git a/question/type/random/version.php b/question/type/random/version.php index c1f08c34170..4f6080e97bc 100644 --- a/question/type/random/version.php +++ b/question/type/random/version.php @@ -26,7 +26,7 @@ defined('MOODLE_INTERNAL') || die(); $plugin->component = 'qtype_random'; -$plugin->version = 2014051200; +$plugin->version = 2014060200; $plugin->requires = 2014050800;