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/>.
16
17/**
18 * @package    qtype
19 * @subpackage multichoice
20 * @copyright  2011 David Mudrak <david@moodle.com>
21 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22 */
23
24defined('MOODLE_INTERNAL') || die();
25
26/**
27 * Multichoice question type conversion handler
28 */
29class moodle1_qtype_multichoice_handler extends moodle1_qtype_handler {
30
31    /**
32     * @return array
33     */
34    public function get_question_subpaths() {
35        return array(
36            'ANSWERS/ANSWER',
37            'MULTICHOICE',
38        );
39    }
40
41    /**
42     * Appends the multichoice specific information to the question
43     */
44    public function process_question(array $data, array $raw) {
45
46        // Convert and write the answers first.
47        if (isset($data['answers'])) {
48            $this->write_answers($data['answers'], $this->pluginname);
49        }
50
51        // Convert and write the multichoice.
52        if (!isset($data['multichoice'])) {
53            // This should never happen, but it can do if the 1.9 site contained
54            // corrupt data.
55            $data['multichoice'] = array(array(
56                'single'                         => 1,
57                'shuffleanswers'                 => 1,
58                'correctfeedback'                => '',
59                'correctfeedbackformat'          => FORMAT_HTML,
60                'partiallycorrectfeedback'       => '',
61                'partiallycorrectfeedbackformat' => FORMAT_HTML,
62                'incorrectfeedback'              => '',
63                'incorrectfeedbackformat'        => FORMAT_HTML,
64                'answernumbering'                => 'abc',
65                'showstandardinstruction'        => 0
66            ));
67        }
68        $this->write_multichoice($data['multichoice'], $data['oldquestiontextformat'], $data['id']);
69    }
70
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;
80
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();
86
87            // Replay the upgrade step 2009021801.
88            $multichoice['correctfeedbackformat']               = 0;
89            $multichoice['partiallycorrectfeedbackformat']      = 0;
90            $multichoice['incorrectfeedbackformat']             = 0;
91
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            }
104
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);
111
112            $this->write_xml('multichoice', $multichoice, array('/multichoice/id'));
113        }
114    }
115}
116