MDL-52162 question: Removing unnecessary dependency
[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 /**
30  * Upgrade code for the multi-answer question type.
31  * @param int $oldversion the version we are upgrading from.
32  */
33 function xmldb_qtype_multianswer_upgrade($oldversion) {
34     global $CFG, $DB;
36     $dbman = $DB->get_manager();
38     // Moodle v2.2.0 release upgrade line
39     // Put any upgrade step following this.
41     // Moodle v2.3.0 release upgrade line
42     // Put any upgrade step following this.
44     // Moodle v2.4.0 release upgrade line
45     // Put any upgrade step following this.
47     // Moodle v2.5.0 release upgrade line.
48     // Put any upgrade step following this.
51     // Moodle v2.6.0 release upgrade line.
52     // Put any upgrade step following this.
54     // Moodle v2.7.0 release upgrade line.
55     // Put any upgrade step following this.
57     // Moodle v2.8.0 release upgrade line.
58     // Put any upgrade step following this.
60     // Moodle v2.9.0 release upgrade line.
61     // Put any upgrade step following this.
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);
77                             if (isset($answerregs[ANSWER_REGEX_ANSWER_TYPE_MULTICHOICE]) &&
78                                     $answerregs[ANSWER_REGEX_ANSWER_TYPE_MULTICHOICE] !== '') {
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     }
99     return true;