Merge branch 'MDL-70041-310' of git://github.com/mihailges/moodle into MOODLE_310_STABLE
[moodle.git] / question / type / gapselect / questiontype.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  * Question type class for the select missing words question type.
19  *
20  * @package    qtype_gapselect
21  * @copyright  2011 The Open University
22  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23  */
26 defined('MOODLE_INTERNAL') || die();
28 require_once($CFG->libdir . '/questionlib.php');
29 require_once($CFG->dirroot . '/question/engine/lib.php');
30 require_once($CFG->dirroot . '/question/format/xml/format.php');
31 require_once($CFG->dirroot . '/question/type/gapselect/questiontypebase.php');
34 /**
35  * The select missing words question type class.
36  *
37  * @copyright  2011 The Open University
38  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39  */
40 class qtype_gapselect extends qtype_gapselect_base {
41     protected function choice_options_to_feedback($choice) {
42         return $choice['choicegroup'];
43     }
45     protected function make_choice($choicedata) {
46         return new qtype_gapselect_choice($choicedata->answer, $choicedata->feedback);
47     }
49     protected function feedback_to_choice_options($feedback) {
50         return array('selectgroup' => $feedback);
51     }
54     protected function choice_group_key() {
55         return 'selectgroup';
56     }
58     public function import_from_xml($data, $question, qformat_xml $format, $extra=null) {
59         if (!isset($data['@']['type']) || $data['@']['type'] != 'gapselect') {
60             return false;
61         }
63         $question = $format->import_headers($data);
64         $question->qtype = 'gapselect';
66         $question->shuffleanswers = $format->trans_single(
67                 $format->getpath($data, array('#', 'shuffleanswers', 0, '#'), 1));
69         if (!empty($data['#']['selectoption'])) {
70             // Modern XML format.
71             $selectoptions = $data['#']['selectoption'];
72             $question->answer = array();
73             $question->selectgroup = array();
75             foreach ($data['#']['selectoption'] as $selectoptionxml) {
76                 $question->choices[] = array(
77                     'answer'      => $format->getpath($selectoptionxml,
78                                                       array('#', 'text', 0, '#'), '', true),
79                     'choicegroup' => $format->getpath($selectoptionxml,
80                                                       array('#', 'group', 0, '#'), 1),
81                 );
82             }
84         } else {
85             // Legacy format containing PHP serialisation.
86             foreach ($data['#']['answer'] as $answerxml) {
87                 $ans = $format->import_answer($answerxml);
88                 $question->choices[] = array(
89                     'answer' => $ans->answer,
90                     'choicegroup' => $ans->feedback,
91                 );
92             }
93         }
95         $format->import_combined_feedback($question, $data, true);
96         $format->import_hints($question, $data, true, false,
97                 $format->get_format($question->questiontextformat));
99         return $question;
100     }
102     public function export_to_xml($question, qformat_xml $format, $extra = null) {
103         $output = '';
105         $output .= '    <shuffleanswers>' . $question->options->shuffleanswers .
106                 "</shuffleanswers>\n";
108         $output .= $format->write_combined_feedback($question->options,
109                                                     $question->id,
110                                                     $question->contextid);
112         foreach ($question->options->answers as $answer) {
113             $output .= "    <selectoption>\n";
114             $output .= $format->writetext($answer->answer, 3);
115             $output .= "      <group>{$answer->feedback}</group>\n";
116             $output .= "    </selectoption>\n";
117         }
119         return $output;
120     }