Skip to content
Browse files

MDL-17812 shortanswer qtype: rename DB stuff to match coding guidelines.

We should be leading by example in Moodle core.
  • Loading branch information...
1 parent a5ec499 commit b4cb095798f8624eac9046cb2ff7f3bc02cfd4be @timhunt timhunt committed Jan 18, 2013
View
4 question/format/gift/tests/giftformat_test.php
@@ -743,7 +743,7 @@ public function test_export_shortanswer() {
'qtype' => 'shortanswer',
'options' => (object) array(
'id' => 123,
- 'question' => 666,
+ 'questionid' => 666,
'usecase' => 1,
'answers' => array(
1 => (object) array(
@@ -803,7 +803,7 @@ public function test_export_shortanswer_with_general_feedback() {
'qtype' => 'shortanswer',
'options' => (object) array(
'id' => 123,
- 'question' => 666,
+ 'questionid' => 666,
'usecase' => 1,
'answers' => array(
1 => (object) array(
View
4 question/type/multianswer/questiontype.php
@@ -113,8 +113,8 @@ public function save_question_options($question) {
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',
View
7 question/type/shortanswer/backup/moodle2/backup_qtype_shortanswer_plugin.class.php
@@ -52,15 +52,14 @@ protected function define_question_plugin_structure() {
$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
View
19 question/type/shortanswer/backup/moodle2/restore_qtype_shortanswer_plugin.class.php
@@ -68,20 +68,11 @@ public function process_shortanswer($data) {
$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);
}
}
}
View
15 question/type/shortanswer/db/install.xml
@@ -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>
View
109 question/type/shortanswer/db/upgrade.php
@@ -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;
+}
View
9 question/type/shortanswer/questiontype.php
@@ -39,11 +39,7 @@
*/
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 @@ public function save_question_options($question) {
$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 @@ public function save_question_options($question) {
$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
View
0 ...ortanswer/tests/tupgradelibnewqe_test.php → ...hortanswer/tests/upgradelibnewqe_test.php
File renamed without changes.
View
2 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;

0 comments on commit b4cb095

Please sign in to comment.
Something went wrong with that request. Please try again.