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