MDL-51855 mod_question: fix offset error during upgrade
[moodle.git] / question / type / multianswer / db / upgrade.php
CommitLineData
aeb15530 1<?php
d3603157 2// This file is part of Moodle - http://moodle.org/
dfdd5332 3//
d3603157
TH
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.
dfdd5332 8//
d3603157
TH
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.
dfdd5332 13//
d3603157
TH
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/>.
16
17/**
18 * Multi-answer question type upgrade code.
19 *
20 * @package qtype
21 * @subpackage multianswer
22 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
dfdd5332 26
a17b297d
TH
27defined('MOODLE_INTERNAL') || die();
28
bd156853 29require_once($CFG->dirroot . '/question/type/multianswer/questiontype.php');
a17b297d 30
d3603157
TH
31/**
32 * Upgrade code for the multi-answer question type.
33 * @param int $oldversion the version we are upgrading from.
34 */
775f811a 35function xmldb_qtype_multianswer_upgrade($oldversion) {
36 global $CFG, $DB;
dfdd5332 37
775f811a 38 $dbman = $DB->get_manager();
dfdd5332 39
46f2a936 40 // Moodle v2.2.0 release upgrade line
3d9645ae 41 // Put any upgrade step following this.
46f2a936 42
99c3c503 43 // Moodle v2.3.0 release upgrade line
3d9645ae 44 // Put any upgrade step following this.
99c3c503 45
6b9dfe73 46 // Moodle v2.4.0 release upgrade line
3d9645ae 47 // Put any upgrade step following this.
6b9dfe73 48
c9e54743
EL
49 // Moodle v2.5.0 release upgrade line.
50 // Put any upgrade step following this.
51
52
47578dbf
DW
53 // Moodle v2.6.0 release upgrade line.
54 // Put any upgrade step following this.
55
f94891cc
EL
56 // Moodle v2.7.0 release upgrade line.
57 // Put any upgrade step following this.
58
147bae8f
EL
59 // Moodle v2.8.0 release upgrade line.
60 // Put any upgrade step following this.
61
b758ab30
EL
62 // Moodle v2.9.0 release upgrade line.
63 // Put any upgrade step following this.
64
bd156853
PP
65 if ($oldversion < 2015100201) {
66 $rs = $DB->get_recordset_sql("SELECT q.id, q.category, qma.sequence
67 FROM {question} q
68 JOIN {question_multianswer} qma ON q.id = qma.question");
69 foreach ($rs as $q) {
70 if (!empty($q->sequence)) {
71 // Get relevant data indexed by positionkey from the multianswers table.
72 $wrappedquestions = $DB->get_records_list('question', 'id',
73 explode(',', $q->sequence), 'id ASC');
74 foreach ($wrappedquestions as $wrapped) {
75 if ($wrapped->qtype == 'multichoice') {
76 $options = $DB->get_record('qtype_multichoice_options', array('questionid' => $wrapped->id), '*');
77 if (isset($options->shuffleanswers)) {
78 preg_match('/'.ANSWER_REGEX.'/s', $wrapped->questiontext, $answerregs);
abc24912
SL
79 if (isset($answerregs[ANSWER_REGEX_ANSWER_TYPE_MULTICHOICE]) &&
80 $answerregs[ANSWER_REGEX_ANSWER_TYPE_MULTICHOICE] !== '') {
bd156853
PP
81 $DB->set_field('qtype_multichoice_options', 'shuffleanswers', '0',
82 array('id' => $options->id) );
83 }
84 } else {
85 $newrecord = new stdClass();
86 $newrecord->questionid = $wrapped->id;
87 $newrecord->correctfeedback = '';
88 $newrecord->partiallycorrectfeedback = '';
89 $newrecord->incorrectfeedback = '';
90 $DB->insert_record('qtype_multichoice_options', $newrecord);
91 }
92 }
93 }
94 }
95 }
96 $rs->close();
97 // Multianswer savepoint reached.
98 upgrade_plugin_savepoint(true, 2015100201, 'qtype', 'multianswer');
99 }
100
a4cdd6d2
PS
101 return true;
102}