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

We should be leading by example in Moodle core.
This commit is contained in:
Tim Hunt
2013-01-18 15:53:35 +00:00
parent a5ec499521
commit b4cb095798
9 changed files with 130 additions and 39 deletions
@@ -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(
+2 -2
View 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',
@@ -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
@@ -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);
}
}
}
+7 -8
View 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>
+109
View File
@@ -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;
}
+1 -8
View 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
+1 -1
View File
@@ -26,7 +26,7 @@
defined('MOODLE_INTERNAL') || die();
$plugin->component = 'qtype_shortanswer';
$plugin->version = 2012112900;
$plugin->version = 2013011804;
$plugin->requires = 2012112900;