From 6bb88b0306af8691ae7e4f6545b9e9ebd0520acf Mon Sep 17 00:00:00 2001 From: Jean-Michel Vedrine Date: Sat, 27 Apr 2019 16:08:43 +0200 Subject: change ref field name in variable table --- backup/moodle1/lib.php | 2 +- .../moodle2/backup_qtype_algebra_plugin.class.php | 2 +- .../moodle2/restore_qtype_algebra_plugin.class.php | 4 +-- db/install.xml | 38 ++++++++++---------- db/upgrade.php | 42 ++++++++++++++++++++++ questiontype.php | 12 +++---- version.php | 2 +- 7 files changed, 70 insertions(+), 32 deletions(-) diff --git a/backup/moodle1/lib.php b/backup/moodle1/lib.php index 9ffe528..31a544d 100644 --- a/backup/moodle1/lib.php +++ b/backup/moodle1/lib.php @@ -23,7 +23,7 @@ defined('MOODLE_INTERNAL') || die(); /** - * Multichoice question type conversion handler + * Algebra question type conversion handler. */ class moodle1_qtype_algebra_handler extends moodle1_qtype_handler { diff --git a/backup/moodle2/backup_qtype_algebra_plugin.class.php b/backup/moodle2/backup_qtype_algebra_plugin.class.php index 4f7708f..099a41c 100644 --- a/backup/moodle2/backup_qtype_algebra_plugin.class.php +++ b/backup/moodle2/backup_qtype_algebra_plugin.class.php @@ -63,7 +63,7 @@ class backup_qtype_algebra_plugin extends backup_qtype_plugin { // Set source to populate the data. $algebra->set_source_table('qtype_algebra_options', array('questionid' => backup::VAR_PARENTID)); - $algebravariable->set_source_table('qtype_algebra_variables', array('question' => backup::VAR_PARENTID)); + $algebravariable->set_source_table('qtype_algebra_variables', array('questionid' => backup::VAR_PARENTID)); // Don't need to annotate ids nor files. diff --git a/backup/moodle2/restore_qtype_algebra_plugin.class.php b/backup/moodle2/restore_qtype_algebra_plugin.class.php index 013b70c..a4b4c70 100644 --- a/backup/moodle2/restore_qtype_algebra_plugin.class.php +++ b/backup/moodle2/restore_qtype_algebra_plugin.class.php @@ -62,7 +62,7 @@ class restore_qtype_algebra_plugin extends restore_qtype_plugin { $newquestionid = $this->get_new_parentid('question'); $questioncreated = $this->get_mappingid('question_created', $oldquestionid) ? true : false; - // If the question has been created by restore, we need to create its qtype_algebra too. + // If the question has been created by restore, we need to create its qtype_algebra_options too. if ($questioncreated) { // Adjust some columns. $data->questionid = $newquestionid; @@ -91,7 +91,7 @@ class restore_qtype_algebra_plugin extends restore_qtype_plugin { // If the question has been created by restore, we need to create its qtype_algebra_variables too. if ($questioncreated) { // Adjust some columns. - $data->question = $newquestionid; + $data->questionid = $newquestionid; // Insert record. $newitemid = $DB->insert_record('qtype_algebra_variables', $data); // Create mapping. diff --git a/db/install.xml b/db/install.xml index 5e2802c..b612af2 100644 --- a/db/install.xml +++ b/db/install.xml @@ -4,33 +4,33 @@ xsi:noNamespaceSchemaLocation="../../../../lib/xmldb/xmldb.xsd" > - +
- - - - - - - - + + + + + + + + - - + +
- +
- - - - - + + + + + - - + +
diff --git a/db/upgrade.php b/db/upgrade.php index 48cc159..f1db600 100644 --- a/db/upgrade.php +++ b/db/upgrade.php @@ -80,6 +80,48 @@ function xmldb_qtype_algebra_upgrade($oldversion=0) { } upgrade_plugin_savepoint(true, 2012061702, 'qtype', 'algebra'); } + + if ($oldversion < 2019042705) { + + // Define key question (foreign) to be dropped form qtype_algebra_variables. + $table = new xmldb_table('qtype_algebra_variables'); + $key = new xmldb_key('question', XMLDB_KEY_FOREIGN, array('question'), 'question', array('id')); + + // Launch drop key question. + $dbman->drop_key($table, $key); + + // Record that qtype_algebra savepoint was reached. + upgrade_plugin_savepoint(true, 2019042705, 'qtype', 'algebra'); + } + + if ($oldversion < 2019042706) { + + // Rename field question on table qtype_algebra_variables to questionid. + $table = new xmldb_table('qtype_algebra_variables'); + $field = new xmldb_field('question', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'id'); + + // Launch rename field question. + if ($dbman->field_exists($table, $field)) { + $dbman->rename_field($table, $field, 'questionid'); + } + + // Record that qtype_algebra savepoint was reached. + upgrade_plugin_savepoint(true, 2019042706, 'qtype', 'algebra'); + } + + if ($oldversion < 2019042707) { + + // Define key questionid (foreign-unique) to be added to qtype_algebra_variables. + $table = new xmldb_table('qtype_algebra_variables'); + $key = new xmldb_key('questionid', XMLDB_KEY_FOREIGN_UNIQUE, array('questionid'), 'question', array('id')); + + // Launch add key questionid. + $dbman->add_key($table, $key); + + // Record that qtype_algebra savepoint was reached. + upgrade_plugin_savepoint(true, 2019042707, 'qtype', 'algebra'); + } + return true; } diff --git a/questiontype.php b/questiontype.php index 01f86c4..c3a0674 100644 --- a/questiontype.php +++ b/questiontype.php @@ -54,10 +54,6 @@ class qtype_algebra extends question_type { ); } - public function questionid_column_name() { - return 'questionid'; - } - public function move_files($questionid, $oldcontextid, $newcontextid) { parent::move_files($questionid, $oldcontextid, $newcontextid); $this->move_files_in_answers($questionid, $oldcontextid, $newcontextid); @@ -73,7 +69,7 @@ class qtype_algebra extends question_type { public function delete_question($questionid, $contextid) { global $DB; $DB->delete_records('qtype_algebra_options', array('questionid' => $questionid)); - $DB->delete_records('qtype_algebra_variables', array('question' => $questionid)); + $DB->delete_records('qtype_algebra_variables', array('questionid' => $questionid)); parent::delete_question($questionid, $contextid); } @@ -94,7 +90,7 @@ class qtype_algebra extends question_type { // Create the results class. $result = new stdClass; // Get all the old answers from the database as an array. - if (!$oldvars = $DB->get_records('qtype_algebra_variables', array('question' => $question->id), 'id ASC')) { + if (!$oldvars = $DB->get_records('qtype_algebra_variables', array('questionid' => $question->id), 'id ASC')) { $oldvars = array(); } // Create an array of the variable IDs for the question. @@ -122,7 +118,7 @@ class qtype_algebra extends question_type { // This is a completely new variable so we have to create a new record. $var = new stdClass; $var->name = trim($varname); - $var->question = $question->id; + $var->questionid = $question->id; $var->min = trim($question->varmin[$key]); $var->max = trim($question->varmax[$key]); // Insert a new record into the database table. @@ -305,7 +301,7 @@ class qtype_algebra extends question_type { return false; } // Now get the variables from the database as well. - $question->options->variables = $DB->get_records('qtype_algebra_variables', array('question' => $question->id)); + $question->options->variables = $DB->get_records('qtype_algebra_variables', array('questionid' => $question->id)); // Check that we have variables and if not then bail since this question type requires variables. if (count($question->options->variables) == 0) { diff --git a/version.php b/version.php index 7a28488..41195e0 100644 --- a/version.php +++ b/version.php @@ -23,7 +23,7 @@ defined('MOODLE_INTERNAL') || die(); $plugin->component = 'qtype_algebra'; -$plugin->version = 2019020201; +$plugin->version = 2019042707; $plugin->requires = 2016052300; $plugin->release = '1.8 for Moodle 3.0 ... 3.6'; -- cgit v1.2.3