Merge branch 'MDL-36900-master' of git://github.com/FMCorz/moodle
[moodle.git] / question / type / multichoice / backup / moodle1 / lib.php
1 <?php
3 // This file is part of Moodle - http://moodle.org/
4 //
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
18 /**
19  * @package    qtype
20  * @subpackage multichoice
21  * @copyright  2011 David Mudrak <david@moodle.com>
22  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23  */
25 defined('MOODLE_INTERNAL') || die();
27 /**
28  * Multichoice question type conversion handler
29  */
30 class moodle1_qtype_multichoice_handler extends moodle1_qtype_handler {
32     /**
33      * @return array
34      */
35     public function get_question_subpaths() {
36         return array(
37             'ANSWERS/ANSWER',
38             'MULTICHOICE',
39         );
40     }
42     /**
43      * Appends the multichoice specific information to the question
44      */
45     public function process_question(array $data, array $raw) {
47         // convert and write the answers first
48         if (isset($data['answers'])) {
49             $this->write_answers($data['answers'], $this->pluginname);
50         }
52         // convert and write the multichoice
53         if (!isset($data['multichoice'])) {
54             // This should never happen, but it can do if the 1.9 site contained
55             // corrupt data/
56             $data['multichoice'] = array(array(
57                 'single'                         => 1,
58                 'shuffleanswers'                 => 1,
59                 'correctfeedback'                => '',
60                 'correctfeedbackformat'          => FORMAT_HTML,
61                 'partiallycorrectfeedback'       => '',
62                 'partiallycorrectfeedbackformat' => FORMAT_HTML,
63                 'incorrectfeedback'              => '',
64                 'incorrectfeedbackformat'        => FORMAT_HTML,
65                 'answernumbering'                => 'abc',
66             ));
67         }
68         $this->write_multichoice($data['multichoice'], $data['oldquestiontextformat'], $data['id']);
69     }
71     /**
72      * Converts the multichoice info and writes it into the question.xml
73      *
74      * @param array $multichoices the grouped structure
75      * @param int $oldquestiontextformat - {@see moodle1_question_bank_handler::process_question()}
76      * @param int $questionid question id
77      */
78     protected function write_multichoice(array $multichoices, $oldquestiontextformat, $questionid) {
79         global $CFG;
81         // the grouped array is supposed to have just one element - let us use foreach anyway
82         // just to be sure we do not loose anything
83         foreach ($multichoices as $multichoice) {
84             // append an artificial 'id' attribute (is not included in moodle.xml)
85             $multichoice['id'] = $this->converter->get_nextid();
87             // replay the upgrade step 2009021801
88             $multichoice['correctfeedbackformat']               = 0;
89             $multichoice['partiallycorrectfeedbackformat']      = 0;
90             $multichoice['incorrectfeedbackformat']             = 0;
92             if ($CFG->texteditors !== 'textarea' and $oldquestiontextformat == FORMAT_MOODLE) {
93                 $multichoice['correctfeedback']                 = text_to_html($multichoice['correctfeedback'], false, false, true);
94                 $multichoice['correctfeedbackformat']           = FORMAT_HTML;
95                 $multichoice['partiallycorrectfeedback']        = text_to_html($multichoice['partiallycorrectfeedback'], false, false, true);
96                 $multichoice['partiallycorrectfeedbackformat']  = FORMAT_HTML;
97                 $multichoice['incorrectfeedback']               = text_to_html($multichoice['incorrectfeedback'], false, false, true);
98                 $multichoice['incorrectfeedbackformat']         = FORMAT_HTML;
99             } else {
100                 $multichoice['correctfeedbackformat']           = $oldquestiontextformat;
101                 $multichoice['partiallycorrectfeedbackformat']  = $oldquestiontextformat;
102                 $multichoice['incorrectfeedbackformat']         = $oldquestiontextformat;
103             }
105             $multichoice['correctfeedback'] = $this->migrate_files(
106                     $multichoice['correctfeedback'], 'question', 'correctfeedback', $questionid);
107             $multichoice['partiallycorrectfeedback'] = $this->migrate_files(
108                     $multichoice['partiallycorrectfeedback'], 'question', 'partiallycorrectfeedback', $questionid);
109             $multichoice['incorrectfeedback'] = $this->migrate_files(
110                     $multichoice['incorrectfeedback'], 'question', 'incorrectfeedback', $questionid);
112             $this->write_xml('multichoice', $multichoice, array('/multichoice/id'));
113         }
114     }