Merge branch 'MDL-17812' of git://github.com/timhunt/moodle
authorDan Poltawski <dan@moodle.com>
Tue, 22 Jan 2013 03:11:53 +0000 (11:11 +0800)
committerDan Poltawski <dan@moodle.com>
Tue, 22 Jan 2013 03:11:53 +0000 (11:11 +0800)
question/format/gift/tests/giftformat_test.php
question/type/multianswer/questiontype.php
question/type/shortanswer/backup/moodle2/backup_qtype_shortanswer_plugin.class.php
question/type/shortanswer/backup/moodle2/restore_qtype_shortanswer_plugin.class.php
question/type/shortanswer/db/install.xml
question/type/shortanswer/db/upgrade.php [new file with mode: 0755]
question/type/shortanswer/questiontype.php
question/type/shortanswer/tests/upgradelibnewqe_test.php [moved from question/type/shortanswer/tests/tupgradelibnewqe_test.php with 100% similarity]
question/type/shortanswer/version.php

index 54fcdb3..514b177 100644 (file)
@@ -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(
index 032364e..7df126e 100644 (file)
@@ -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',
index 99c094a..39570e2 100644 (file)
@@ -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
 
index 9f1b67b..665a5cd 100644 (file)
@@ -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);
         }
     }
 }
index eb410f6..97df0fd 100644 (file)
@@ -1,19 +1,18 @@
 <?xml version="1.0" encoding="UTF-8" ?>
-<XMLDB PATH="question/type/shortanswer/db" VERSION="20120122" COMMENT="XMLDB file for Moodle question/type/shortanswer"
+<XMLDB PATH="question/type/shortanswer/db" VERSION="20130118" COMMENT="XMLDB file for Moodle question/type/shortanswer"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:noNamespaceSchemaLocation="../../../../lib/xmldb/xmldb.xsd"
 >
   <TABLES>
-    <TABLE NAME="question_shortanswer" COMMENT="Options for short answer questions">
+    <TABLE NAME="qtype_shortanswer_options" COMMENT="Options for short answer questions">
       <FIELDS>
-        <FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true" NEXT="question"/>
-        <FIELD NAME="question" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false" COMMENT="Foreign key references question.id." PREVIOUS="id" NEXT="answers"/>
-        <FIELD NAME="answers" TYPE="char" LENGTH="255" NOTNULL="true" SEQUENCE="false" COMMENT="Redundant. Comma-separated list of question_answer ids. SELECT id FROM question_answers WHERE question = ? ORDER BY id." PREVIOUS="question" NEXT="usecase"/>
-        <FIELD NAME="usecase" TYPE="int" LENGTH="2" NOTNULL="true" DEFAULT="0" SEQUENCE="false" COMMENT="Whether answers are matched case-sensitively." PREVIOUS="answers"/>
+        <FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true" NEXT="questionid"/>
+        <FIELD NAME="questionid" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false" COMMENT="Foreign key references question.id." PREVIOUS="id" NEXT="usecase"/>
+        <FIELD NAME="usecase" TYPE="int" LENGTH="2" NOTNULL="true" DEFAULT="0" SEQUENCE="false" COMMENT="Whether answers are matched case-sensitively." PREVIOUS="questionid"/>
       </FIELDS>
       <KEYS>
-        <KEY NAME="primary" TYPE="primary" FIELDS="id" NEXT="question"/>
-        <KEY NAME="question" TYPE="foreign" FIELDS="question" REFTABLE="question" REFFIELDS="id" PREVIOUS="primary"/>
+        <KEY NAME="primary" TYPE="primary" FIELDS="id" NEXT="questionid"/>
+        <KEY NAME="questionid" TYPE="foreign-unique" FIELDS="questionid" REFTABLE="question" REFFIELDS="id" PREVIOUS="primary"/>
       </KEYS>
     </TABLE>
   </TABLES>
diff --git a/question/type/shortanswer/db/upgrade.php b/question/type/shortanswer/db/upgrade.php
new file mode 100755 (executable)
index 0000000..cadf88a
--- /dev/null
@@ -0,0 +1,109 @@
+<?php
+// This file is part of Moodle - http://moodle.org/
+//
+// Moodle is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// Moodle is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
+
+/**
+ * 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;
+}
index 2cf77d7..5aa5d3d 100644 (file)
@@ -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
index a7ef9c2..196e9be 100644 (file)
@@ -26,7 +26,7 @@
 defined('MOODLE_INTERNAL') || die();
 
 $plugin->component = 'qtype_shortanswer';
-$plugin->version   = 2012112900;
+$plugin->version   = 2013011804;
 
 $plugin->requires  = 2012112900;