Merge branch 'MDL-70065_310' of https://github.com/timhunt/moodle into MOODLE_310_STABLE
[moodle.git] / question / type / gapselect / questiontype.php
CommitLineData
0d24b17a 1<?php
0d24b17a
TH
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
0d24b17a
TH
17/**
18 * Question type class for the select missing words question type.
19 *
458700d8 20 * @package qtype_gapselect
9df0480d
TH
21 * @copyright 2011 The Open University
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
0d24b17a
TH
23 */
24
25
b28ad86a
TH
26defined('MOODLE_INTERNAL') || die();
27
0d24b17a
TH
28require_once($CFG->libdir . '/questionlib.php');
29require_once($CFG->dirroot . '/question/engine/lib.php');
30require_once($CFG->dirroot . '/question/format/xml/format.php');
0d24b17a
TH
31require_once($CFG->dirroot . '/question/type/gapselect/questiontypebase.php');
32
9df0480d 33
0d24b17a
TH
34/**
35 * The select missing words question type class.
36 *
9df0480d
TH
37 * @copyright 2011 The Open University
38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
0d24b17a
TH
39 */
40class qtype_gapselect extends qtype_gapselect_base {
41 protected function choice_options_to_feedback($choice) {
721ef2ea 42 return $choice['choicegroup'];
0d24b17a
TH
43 }
44
45 protected function make_choice($choicedata) {
46 return new qtype_gapselect_choice($choicedata->answer, $choicedata->feedback);
47 }
48
49 protected function feedback_to_choice_options($feedback) {
50 return array('selectgroup' => $feedback);
51 }
52
53
54 protected function choice_group_key() {
55 return 'selectgroup';
56 }
57
344d774e 58 public function import_from_xml($data, $question, qformat_xml $format, $extra=null) {
0d24b17a
TH
59 if (!isset($data['@']['type']) || $data['@']['type'] != 'gapselect') {
60 return false;
61 }
62
63 $question = $format->import_headers($data);
64 $question->qtype = 'gapselect';
65
66 $question->shuffleanswers = $format->trans_single(
67 $format->getpath($data, array('#', 'shuffleanswers', 0, '#'), 1));
68
69 if (!empty($data['#']['selectoption'])) {
70 // Modern XML format.
71 $selectoptions = $data['#']['selectoption'];
72 $question->answer = array();
73 $question->selectgroup = array();
74
75 foreach ($data['#']['selectoption'] as $selectoptionxml) {
76 $question->choices[] = array(
060e0294
TH
77 'answer' => $format->getpath($selectoptionxml,
78 array('#', 'text', 0, '#'), '', true),
79 'choicegroup' => $format->getpath($selectoptionxml,
80 array('#', 'group', 0, '#'), 1),
0d24b17a
TH
81 );
82 }
83
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,
aa6a0ff0 90 'choicegroup' => $ans->feedback,
0d24b17a
TH
91 );
92 }
93 }
94
95 $format->import_combined_feedback($question, $data, true);
5283ee02
TH
96 $format->import_hints($question, $data, true, false,
97 $format->get_format($question->questiontextformat));
0d24b17a
TH
98
99 return $question;
100 }
101
344d774e 102 public function export_to_xml($question, qformat_xml $format, $extra = null) {
0d24b17a
TH
103 $output = '';
104
060e0294
TH
105 $output .= ' <shuffleanswers>' . $question->options->shuffleanswers .
106 "</shuffleanswers>\n";
0d24b17a 107
1371d090
TH
108 $output .= $format->write_combined_feedback($question->options,
109 $question->id,
110 $question->contextid);
0d24b17a
TH
111
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 }
118
119 return $output;
120 }
0d24b17a 121}