MDL-52162 question: Removing unnecessary dependency
[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
d3603157
TH
29/**
30 * Upgrade code for the multi-answer question type.
31 * @param int $oldversion the version we are upgrading from.
32 */
775f811a 33function xmldb_qtype_multianswer_upgrade($oldversion) {
34 global $CFG, $DB;
dfdd5332 35
775f811a 36 $dbman = $DB->get_manager();
dfdd5332 37
46f2a936 38 // Moodle v2.2.0 release upgrade line
3d9645ae 39 // Put any upgrade step following this.
46f2a936 40
99c3c503 41 // Moodle v2.3.0 release upgrade line
3d9645ae 42 // Put any upgrade step following this.
99c3c503 43
6b9dfe73 44 // Moodle v2.4.0 release upgrade line
3d9645ae 45 // Put any upgrade step following this.
6b9dfe73 46
c9e54743
EL
47 // Moodle v2.5.0 release upgrade line.
48 // Put any upgrade step following this.
49
50
47578dbf
DW
51 // Moodle v2.6.0 release upgrade line.
52 // Put any upgrade step following this.
53
f94891cc
EL
54 // Moodle v2.7.0 release upgrade line.
55 // Put any upgrade step following this.
56
147bae8f
EL
57 // Moodle v2.8.0 release upgrade line.
58 // Put any upgrade step following this.
59
b758ab30
EL
60 // Moodle v2.9.0 release upgrade line.
61 // Put any upgrade step following this.
62
bd156853
PP
63 if ($oldversion < 2015100201) {
64 $rs = $DB->get_recordset_sql("SELECT q.id, q.category, qma.sequence
65 FROM {question} q
66 JOIN {question_multianswer} qma ON q.id = qma.question");
67 foreach ($rs as $q) {
68 if (!empty($q->sequence)) {
69 // Get relevant data indexed by positionkey from the multianswers table.
70 $wrappedquestions = $DB->get_records_list('question', 'id',
71 explode(',', $q->sequence), 'id ASC');
72 foreach ($wrappedquestions as $wrapped) {
73 if ($wrapped->qtype == 'multichoice') {
74 $options = $DB->get_record('qtype_multichoice_options', array('questionid' => $wrapped->id), '*');
75 if (isset($options->shuffleanswers)) {
76 preg_match('/'.ANSWER_REGEX.'/s', $wrapped->questiontext, $answerregs);
abc24912
SL
77 if (isset($answerregs[ANSWER_REGEX_ANSWER_TYPE_MULTICHOICE]) &&
78 $answerregs[ANSWER_REGEX_ANSWER_TYPE_MULTICHOICE] !== '') {
bd156853
PP
79 $DB->set_field('qtype_multichoice_options', 'shuffleanswers', '0',
80 array('id' => $options->id) );
81 }
82 } else {
83 $newrecord = new stdClass();
84 $newrecord->questionid = $wrapped->id;
85 $newrecord->correctfeedback = '';
86 $newrecord->partiallycorrectfeedback = '';
87 $newrecord->incorrectfeedback = '';
88 $DB->insert_record('qtype_multichoice_options', $newrecord);
89 }
90 }
91 }
92 }
93 }
94 $rs->close();
95 // Multianswer savepoint reached.
96 upgrade_plugin_savepoint(true, 2015100201, 'qtype', 'multianswer');
97 }
98
a4cdd6d2
PS
99 return true;
100}