MDL-51855 mod_question: fix offset error during upgrade
[moodle.git] / question / type / multianswer / db / upgrade.php
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.
13 //
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  * 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  */
27 defined('MOODLE_INTERNAL') || die();
29 require_once($CFG->dirroot . '/question/type/multianswer/questiontype.php');
31 /**
32  * Upgrade code for the multi-answer question type.
33  * @param int $oldversion the version we are upgrading from.
34  */
35 function xmldb_qtype_multianswer_upgrade($oldversion) {
36     global $CFG, $DB;
38     $dbman = $DB->get_manager();
40     // Moodle v2.2.0 release upgrade line
41     // Put any upgrade step following this.
43     // Moodle v2.3.0 release upgrade line
44     // Put any upgrade step following this.
46     // Moodle v2.4.0 release upgrade line
47     // Put any upgrade step following this.
49     // Moodle v2.5.0 release upgrade line.
50     // Put any upgrade step following this.
53     // Moodle v2.6.0 release upgrade line.
54     // Put any upgrade step following this.
56     // Moodle v2.7.0 release upgrade line.
57     // Put any upgrade step following this.
59     // Moodle v2.8.0 release upgrade line.
60     // Put any upgrade step following this.
62     // Moodle v2.9.0 release upgrade line.
63     // Put any upgrade step following this.
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);
79                             if (isset($answerregs[ANSWER_REGEX_ANSWER_TYPE_MULTICHOICE]) &&
80                                     $answerregs[ANSWER_REGEX_ANSWER_TYPE_MULTICHOICE] !== '') {
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     }
101     return true;