MDL-28169 Installation Added 2.1 upgrade (idiot proof) lines to upgrade scripts.
[moodle.git] / question / type / multichoice / db / upgrade.php
blob2345bc8beb5a15bf0556bdac590394dbadaddfe7
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 /**
18 * Multiple choice question type upgrade code.
20 * @package qtype
21 * @subpackage multichoice
22 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 defined('MOODLE_INTERNAL') || die();
30 /**
31 * Upgrade code for the multiple choice question type.
32 * @param int $oldversion the version we are upgrading from.
34 function xmldb_qtype_multichoice_upgrade($oldversion) {
35 global $CFG, $DB;
37 $dbman = $DB->get_manager();
39 // This upgrade actually belongs to the random question type,
40 // but that does not have a DB upgrade script. Therefore, multichoice
41 // is doing it.
42 // Rename random questions to give them more helpful names.
43 if ($oldversion < 2008021800) {
44 require_once($CFG->dirroot . '/question/type/random/questiontype.php');
45 $randomqtype = new qtype_random();
47 // Get all categories containing random questions.
48 $categories = $DB->get_recordset_sql("
49 SELECT qc.id, qc.name
50 FROM {question_categories} qc
51 JOIN {question} q ON q.category = qc.id
52 WHERE q.qtype = 'random'
53 GROUP BY qc.id, qc.name");
55 // Rename the random qusetions in those categories.
56 $where = "qtype = 'random' AND category = ? AND " .
57 $DB->sql_compare_text('questiontext') . " = " . $DB->sql_compare_text('?');
58 foreach ($categories as $cat) {
59 $randomqname = $randomqtype->question_name($cat, false);
60 $DB->set_field_select('question', 'name', $randomqname, $where, array($cat->id, '0'));
62 $randomqname = $randomqtype->question_name($cat, true);
63 $DB->set_field_select('question', 'name', $randomqname, $where, array($cat->id, '1'));
66 upgrade_plugin_savepoint(true, 2008021800, 'qtype', 'multichoice');
69 if ($oldversion < 2009021801) {
71 // Define field correctfeedbackformat to be added to question_multichoice
72 $table = new xmldb_table('question_multichoice');
73 $field = new xmldb_field('correctfeedbackformat', XMLDB_TYPE_INTEGER, '2', null,
74 XMLDB_NOTNULL, null, '0', 'correctfeedback');
76 // Conditionally launch add field correctfeedbackformat
77 if (!$dbman->field_exists($table, $field)) {
78 $dbman->add_field($table, $field);
81 // Define field partiallycorrectfeedbackformat to be added to question_multichoice
82 $field = new xmldb_field('partiallycorrectfeedbackformat', XMLDB_TYPE_INTEGER, '2', null,
83 XMLDB_NOTNULL, null, '0', 'partiallycorrectfeedback');
85 // Conditionally launch add field partiallycorrectfeedbackformat
86 if (!$dbman->field_exists($table, $field)) {
87 $dbman->add_field($table, $field);
90 // Define field incorrectfeedbackformat to be added to question_multichoice
91 $field = new xmldb_field('incorrectfeedbackformat', XMLDB_TYPE_INTEGER, '2', null,
92 XMLDB_NOTNULL, null, '0', 'incorrectfeedback');
94 // Conditionally launch add field incorrectfeedbackformat
95 if (!$dbman->field_exists($table, $field)) {
96 $dbman->add_field($table, $field);
99 // In the past, the correctfeedback, partiallycorrectfeedback,
100 // incorrectfeedback columns were assumed to contain content of the same
101 // form as questiontextformat. If we are using the HTML editor, then
102 // convert FORMAT_MOODLE content to FORMAT_HTML.
103 $rs = $DB->get_recordset_sql('
104 SELECT qm.*, q.oldquestiontextformat
105 FROM {question_multichoice} qm
106 JOIN {question} q ON qm.question = q.id');
107 foreach ($rs as $record) {
108 if ($CFG->texteditors !== 'textarea' &&
109 $record->oldquestiontextformat == FORMAT_MOODLE) {
110 $record->correctfeedback = text_to_html(
111 $record->correctfeedback, false, false, true);
112 $record->correctfeedbackformat = FORMAT_HTML;
113 $record->partiallycorrectfeedback = text_to_html(
114 $record->partiallycorrectfeedback, false, false, true);
115 $record->partiallycorrectfeedbackformat = FORMAT_HTML;
116 $record->incorrectfeedback = text_to_html(
117 $record->incorrectfeedback, false, false, true);
118 $record->incorrectfeedbackformat = FORMAT_HTML;
119 } else {
120 $record->correctfeedbackformat = $record->oldquestiontextformat;
121 $record->partiallycorrectfeedback = $record->oldquestiontextformat;
122 $record->incorrectfeedbackformat = $record->oldquestiontextformat;
124 $DB->update_record('question_multichoice', $record);
126 $rs->close();
128 // multichoice savepoint reached
129 upgrade_plugin_savepoint(true, 2009021801, 'qtype', 'multichoice');
132 // Add new shownumcorrect field. If this is true, then when the user gets a
133 // multiple-response question partially correct, tell them how many choices
134 // they got correct alongside the feedback.
135 if ($oldversion < 2011011200) {
137 // Define field shownumcorrect to be added to question_multichoice
138 $table = new xmldb_table('question_multichoice');
139 $field = new xmldb_field('shownumcorrect', XMLDB_TYPE_INTEGER, '2', null,
140 XMLDB_NOTNULL, null, '0', 'answernumbering');
142 // Launch add field shownumcorrect
143 if (!$dbman->field_exists($table, $field)) {
144 $dbman->add_field($table, $field);
147 // multichoice savepoint reached
148 upgrade_plugin_savepoint(true, 2011011200, 'qtype', 'multichoice');
151 // Moodle v2.1.0 release upgrade line
152 // Put any upgrade step following this
154 return true;