diff --git a/question/format/gift/tests/giftformat_test.php b/question/format/gift/tests/giftformat_test.php index 54fcdb308c9..514b1771f25 100644 --- a/question/format/gift/tests/giftformat_test.php +++ b/question/format/gift/tests/giftformat_test.php @@ -743,7 +743,7 @@ class qformat_gift_test extends question_testcase { 'qtype' => 'shortanswer', 'options' => (object) array( 'id' => 123, - 'question' => 666, + 'questionid' => 666, 'usecase' => 1, 'answers' => array( 1 => (object) array( @@ -803,7 +803,7 @@ class qformat_gift_test extends question_testcase { 'qtype' => 'shortanswer', 'options' => (object) array( 'id' => 123, - 'question' => 666, + 'questionid' => 666, 'usecase' => 1, 'answers' => array( 1 => (object) array( diff --git a/question/type/multianswer/questiontype.php b/question/type/multianswer/questiontype.php index 032364ed74f..7df126e4936 100644 --- a/question/type/multianswer/questiontype.php +++ b/question/type/multianswer/questiontype.php @@ -113,8 +113,8 @@ class qtype_multianswer extends question_type { array('question' => $oldwrappedquestion->id)); break; case 'shortanswer': - $DB->delete_records('question_shortanswer', - array('question' => $oldwrappedquestion->id)); + $DB->delete_records('qtype_shortanswer_options', + array('questionid' => $oldwrappedquestion->id)); break; case 'numerical': $DB->delete_records('question_numerical', diff --git a/question/type/shortanswer/backup/moodle2/backup_qtype_shortanswer_plugin.class.php b/question/type/shortanswer/backup/moodle2/backup_qtype_shortanswer_plugin.class.php index 99c094aeccf..39570e2e943 100644 --- a/question/type/shortanswer/backup/moodle2/backup_qtype_shortanswer_plugin.class.php +++ b/question/type/shortanswer/backup/moodle2/backup_qtype_shortanswer_plugin.class.php @@ -52,15 +52,14 @@ class backup_qtype_shortanswer_plugin extends backup_qtype_plugin { $this->add_question_question_answers($pluginwrapper); // Now create the qtype own structures - $shortanswer = new backup_nested_element('shortanswer', array('id'), array( - 'answers', 'usecase')); + $shortanswer = new backup_nested_element('shortanswer', array('id'), array('usecase')); // Now the own qtype tree $pluginwrapper->add_child($shortanswer); // set source to populate the data - $shortanswer->set_source_table('question_shortanswer', - array('question' => backup::VAR_PARENTID)); + $shortanswer->set_source_table('qtype_shortanswer_options', + array('questionid' => backup::VAR_PARENTID)); // don't need to annotate ids nor files diff --git a/question/type/shortanswer/backup/moodle2/restore_qtype_shortanswer_plugin.class.php b/question/type/shortanswer/backup/moodle2/restore_qtype_shortanswer_plugin.class.php index 9f1b67b81bc..665a5cdd522 100644 --- a/question/type/shortanswer/backup/moodle2/restore_qtype_shortanswer_plugin.class.php +++ b/question/type/shortanswer/backup/moodle2/restore_qtype_shortanswer_plugin.class.php @@ -68,20 +68,11 @@ class restore_qtype_shortanswer_plugin extends restore_qtype_plugin { $questioncreated = $this->get_mappingid('question_created', $oldquestionid) ? true : false; // If the question has been created by restore, we need to create its - // question_shortanswer too, if they are defined (the gui should ensure this). - if ($questioncreated && !empty($data->answers)) { - // Adjust some columns - $data->question = $newquestionid; - // Map sequence of question_answer ids - $answersarr = explode(',', $data->answers); - foreach ($answersarr as $key => $answer) { - $answersarr[$key] = $this->get_mappingid('question_answer', $answer); - } - $data->answers = implode(',', $answersarr); - // Insert record - $newitemid = $DB->insert_record('question_shortanswer', $data); - // Create mapping - $this->set_mapping('question_shortanswer', $oldid, $newitemid); + // qtype_shortanswer_options too, if they are defined (the gui should ensure this). + if ($questioncreated) { + $data->questionid = $newquestionid; + $newitemid = $DB->insert_record('qtype_shortanswer_options', $data); + $this->set_mapping('qtype_shortanswer_options', $oldid, $newitemid); } } } diff --git a/question/type/shortanswer/db/install.xml b/question/type/shortanswer/db/install.xml index eb410f6de15..97df0fd5178 100644 --- a/question/type/shortanswer/db/install.xml +++ b/question/type/shortanswer/db/install.xml @@ -1,19 +1,18 @@ - - +
- - - - + + + - - + +
diff --git a/question/type/shortanswer/db/upgrade.php b/question/type/shortanswer/db/upgrade.php new file mode 100755 index 00000000000..cadf88a29f5 --- /dev/null +++ b/question/type/shortanswer/db/upgrade.php @@ -0,0 +1,109 @@ +. + +/** + * Short-answer question type upgrade code. + * + * @package qtype + * @subpackage shortanswer + * @copyright 2011 The Open University + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + + +defined('MOODLE_INTERNAL') || die(); + + +/** + * Upgrade code for the essay question type. + * @param int $oldversion the version we are upgrading from. + */ +function xmldb_qtype_shortanswer_upgrade($oldversion) { + global $CFG, $DB; + + $dbman = $DB->get_manager(); + + // Moodle v2.4.0 release upgrade line + // Put any upgrade step following this + + if ($oldversion < 2013011800) { + + // Define field answers to be dropped from question_shortanswer. + $table = new xmldb_table('question_shortanswer'); + $field = new xmldb_field('answers'); + + // Conditionally launch drop field answers. + if ($dbman->field_exists($table, $field)) { + $dbman->drop_field($table, $field); + } + + // Shortanswer savepoint reached. + upgrade_plugin_savepoint(true, 2013011800, 'qtype', 'shortanswer'); + } + + if ($oldversion < 2013011801) { + + // Define key question (foreign) to be dropped form question_shortanswer + $table = new xmldb_table('question_shortanswer'); + $key = new xmldb_key('question', XMLDB_KEY_FOREIGN, array('question'), 'question', array('id')); + + // Launch drop key question + $dbman->drop_key($table, $key); + + // shortanswer savepoint reached + upgrade_plugin_savepoint(true, 2013011801, 'qtype', 'shortanswer'); + } + + if ($oldversion < 2013011802) { + + // Rename field question on table question_shortanswer to questionid. + $table = new xmldb_table('question_shortanswer'); + $field = new xmldb_field('question', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'id'); + + // Launch rename field question. + $dbman->rename_field($table, $field, 'questionid'); + + // Shortanswer savepoint reached. + upgrade_plugin_savepoint(true, 2013011802, 'qtype', 'shortanswer'); + } + + if ($oldversion < 2013011803) { + + // Define key questionid (foreign-unique) to be added to question_shortanswer + $table = new xmldb_table('question_shortanswer'); + $key = new xmldb_key('questionid', XMLDB_KEY_FOREIGN_UNIQUE, array('questionid'), 'question', array('id')); + + // Launch add key questionid + $dbman->add_key($table, $key); + + // shortanswer savepoint reached + upgrade_plugin_savepoint(true, 2013011803, 'qtype', 'shortanswer'); + } + + if ($oldversion < 2013011804) { + + // Define table qtype_shortanswer_options to be renamed to qtype_shortanswer_options + $table = new xmldb_table('question_shortanswer'); + + // Launch rename table for qtype_shortanswer_options + $dbman->rename_table($table, 'qtype_shortanswer_options'); + + // shortanswer savepoint reached + upgrade_plugin_savepoint(true, 2013011804, 'qtype', 'shortanswer'); + } + + return true; +} diff --git a/question/type/shortanswer/questiontype.php b/question/type/shortanswer/questiontype.php index 2cf77d7c06f..5aa5d3da70d 100644 --- a/question/type/shortanswer/questiontype.php +++ b/question/type/shortanswer/questiontype.php @@ -39,11 +39,7 @@ require_once($CFG->dirroot . '/question/type/shortanswer/question.php'); */ class qtype_shortanswer extends question_type { public function extra_question_fields() { - return array('question_shortanswer', 'answers', 'usecase'); - } - - public function questionid_column_name() { - return 'question'; + return array('qtype_shortanswer_options', 'usecase'); } public function move_files($questionid, $oldcontextid, $newcontextid) { @@ -67,7 +63,6 @@ class qtype_shortanswer extends question_type { $oldanswers = $DB->get_records('question_answers', array('question' => $question->id), 'id ASC'); - $answers = array(); $maxfraction = -1; // Insert all the new answers @@ -95,13 +90,11 @@ class qtype_shortanswer extends question_type { $answer->feedbackformat = $question->feedback[$key]['format']; $DB->update_record('question_answers', $answer); - $answers[] = $answer->id; if ($question->fraction[$key] > $maxfraction) { $maxfraction = $question->fraction[$key]; } } - $question->answers = implode(',', $answers); $parentresult = parent::save_question_options($question); if ($parentresult !== null) { // Parent function returns null if all is OK diff --git a/question/type/shortanswer/tests/tupgradelibnewqe_test.php b/question/type/shortanswer/tests/upgradelibnewqe_test.php similarity index 100% rename from question/type/shortanswer/tests/tupgradelibnewqe_test.php rename to question/type/shortanswer/tests/upgradelibnewqe_test.php diff --git a/question/type/shortanswer/version.php b/question/type/shortanswer/version.php index a7ef9c223bc..196e9be0824 100644 --- a/question/type/shortanswer/version.php +++ b/question/type/shortanswer/version.php @@ -26,7 +26,7 @@ defined('MOODLE_INTERNAL') || die(); $plugin->component = 'qtype_shortanswer'; -$plugin->version = 2012112900; +$plugin->version = 2013011804; $plugin->requires = 2012112900;