MDL-63812 qtype_gapselect: test data generator to standard qtype pattern
[moodle.git] / question / type / gapselect / questiontypebase.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 embedded element in question text question types.
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
b28ad86a
TH
25defined('MOODLE_INTERNAL') || die();
26
0d24b17a
TH
27require_once($CFG->libdir . '/questionlib.php');
28require_once($CFG->dirroot . '/question/engine/lib.php');
29require_once($CFG->dirroot . '/question/format/xml/format.php');
30
31
32/**
33 * The embedded element in question text question type class.
34 *
9df0480d
TH
35 * @copyright 2011 The Open University
36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
0d24b17a 37 */
43df6cac 38abstract class qtype_gapselect_base extends question_type {
0d24b17a
TH
39 /**
40 * Choices are stored in the question_answers table, and any options need to
41 * be put into the feedback field somehow. This method is responsible for
42 * converting all the options to a single string for this purpose. It is used
43 * by {@link save_question_options()}.
44 * @param array $choice the form data relating to this choice.
45 * @return string ready to store in the database.
46 */
47 protected abstract function choice_options_to_feedback($choice);
48
49 public function save_question_options($question) {
721ef2ea
TH
50 global $DB;
51 $context = $question->context;
0d24b17a
TH
52 $result = new stdClass();
53
721ef2ea
TH
54 $oldanswers = $DB->get_records('question_answers',
55 array('question' => $question->id), 'id ASC');
0d24b17a 56
8d6fb0c6 57 // Insert all the new answers.
0d24b17a
TH
58 foreach ($question->choices as $key => $choice) {
59
60 if (trim($choice['answer']) == '') {
61 continue;
62 }
63
64 $feedback = $this->choice_options_to_feedback($choice);
65
721ef2ea 66 if ($answer = array_shift($oldanswers)) {
0d24b17a 67 $answer->answer = $choice['answer'];
0d24b17a 68 $answer->feedback = $feedback;
721ef2ea
TH
69 $DB->update_record('question_answers', $answer);
70
0d24b17a 71 } else {
721ef2ea 72 $answer = new stdClass();
0d24b17a 73 $answer->question = $question->id;
721ef2ea
TH
74 $answer->answer = $choice['answer'];
75 $answer->answerformat = FORMAT_HTML;
0d24b17a
TH
76 $answer->fraction = 0;
77 $answer->feedback = $feedback;
721ef2ea
TH
78 $answer->feedbackformat = 0;
79 $DB->insert_record('question_answers', $answer);
0d24b17a
TH
80 }
81 }
82
8d6fb0c6 83 // Delete old answer records.
060e0294 84 foreach ($oldanswers as $oa) {
dd9ae7a0 85 $DB->delete_records('question_answers', array('id' => $oa->id));
0d24b17a
TH
86 }
87
060e0294
TH
88 $options = $DB->get_record('question_' . $this->name(),
89 array('questionid' => $question->id));
0d24b17a 90 if (!$options) {
721ef2ea 91 $options = new stdClass();
0d24b17a 92 $options->questionid = $question->id;
721ef2ea
TH
93 $options->correctfeedback = '';
94 $options->partiallycorrectfeedback = '';
95 $options->incorrectfeedback = '';
96 $options->id = $DB->insert_record('question_' . $this->name(), $options);
0d24b17a
TH
97 }
98
99 $options->shuffleanswers = !empty($question->shuffleanswers);
721ef2ea
TH
100 $options = $this->save_combined_feedback_helper($options, $question, $context, true);
101 $DB->update_record('question_' . $this->name(), $options);
0d24b17a
TH
102
103 $this->save_hints($question, true);
0d24b17a
TH
104 }
105
106 public function get_question_options($question) {
721ef2ea
TH
107 global $DB;
108 $question->options = $DB->get_record('question_'.$this->name(),
109 array('questionid' => $question->id), '*', MUST_EXIST);
0d24b17a 110 parent::get_question_options($question);
0d24b17a
TH
111 }
112
721ef2ea
TH
113 public function delete_question($questionid, $contextid) {
114 global $DB;
115 $DB->delete_records('question_'.$this->name(), array('questionid' => $questionid));
116 return parent::delete_question($questionid, $contextid);
0d24b17a
TH
117 }
118
119 /**
120 * Used by {@link initialise_question_instance()} to set up the choice-specific data.
121 * @param object $choicedata as loaded from the question_answers table.
122 * @return object an appropriate object for representing the choice.
123 */
124 protected abstract function make_choice($choicedata);
125
126 protected function initialise_question_instance(question_definition $question, $questiondata) {
127 parent::initialise_question_instance($question, $questiondata);
128
129 $question->shufflechoices = $questiondata->options->shuffleanswers;
130
43df6cac 131 $this->initialise_combined_feedback($question, $questiondata, true);
0d24b17a
TH
132
133 $question->choices = array();
647d9373 134 $choiceindexmap = array();
0d24b17a
TH
135
136 // Store the choices in arrays by group.
137 $i = 1;
138 foreach ($questiondata->options->answers as $choicedata) {
139 $choice = $this->make_choice($choicedata);
140
141 if (array_key_exists($choice->choice_group(), $question->choices)) {
142 $question->choices[$choice->choice_group()][] = $choice;
143 } else {
144 $question->choices[$choice->choice_group()][1] = $choice;
145 }
146
147 end($question->choices[$choice->choice_group()]);
148 $choiceindexmap[$i] = array($choice->choice_group(),
149 key($question->choices[$choice->choice_group()]));
150 $i += 1;
151 }
152
153 $question->places = array();
154 $question->textfragments = array();
155 $question->rightchoices = array();
156 // Break up the question text, and store the fragments, places and right answers.
157
060e0294
TH
158 $bits = preg_split('/\[\[(\d+)]]/', $question->questiontext,
159 null, PREG_SPLIT_DELIM_CAPTURE);
0d24b17a
TH
160 $question->textfragments[0] = array_shift($bits);
161 $i = 1;
162
163 while (!empty($bits)) {
164 $choice = array_shift($bits);
165
166 list($group, $choiceindex) = $choiceindexmap[$choice];
167 $question->places[$i] = $group;
168 $question->rightchoices[$i] = $choiceindex;
169
170 $question->textfragments[$i] = array_shift($bits);
171 $i += 1;
172 }
173 }
174
175 protected function make_hint($hint) {
176 return question_hint_with_parts::load_from_record($hint);
177 }
178
179 public function get_random_guess_score($questiondata) {
180 $question = $this->make_question($questiondata);
181 return $question->get_random_guess_score();
182 }
183
184 /**
185 * This function should reverse {@link choice_options_to_feedback()}.
186 * @param string $feedback the data loaded from the database.
187 * @return array the choice options.
188 */
189 protected abstract function feedback_to_choice_options($feedback);
190
191 /**
192 * This method gets the choices (answers)
193 * in a 2 dimentional array.
194 *
195 * @param object $question
196 * @return array of groups
197 */
198 protected function get_array_of_choices($question) {
199 $subquestions = $question->options->answers;
200 $count = 0;
060e0294 201 foreach ($subquestions as $key => $subquestion) {
0d24b17a
TH
202 $answers[$count]['id'] = $subquestion->id;
203 $answers[$count]['answer'] = $subquestion->answer;
204 $answers[$count]['fraction'] = $subquestion->fraction;
205 $answers[$count] += $this->feedback_to_choice_options($subquestion->feedback);
647d9373 206 $answers[$count]['choice'] = $count + 1;
0d24b17a
TH
207 ++$count;
208 }
209 return $answers;
210 }
211
43df6cac
TH
212 /**
213 * This method gets the choices (answers) and sort them by groups
0d24b17a
TH
214 * in a 2 dimentional array.
215 *
216 * @param object $question
8bc1d28b 217 * @param object $state Question state object
0d24b17a
TH
218 * @return array of groups
219 */
220 protected function get_array_of_groups($question, $state) {
221 $answers = $this->get_array_of_choices($question);
222 $arr = array();
647d9373 223 for ($group = 1; $group < count($answers); $group++) {
43df6cac
TH
224 $players = $this->get_group_of_players($question, $state, $answers, $group);
225 if ($players) {
647d9373 226 $arr[$group] = $players;
0d24b17a
TH
227 }
228 }
229 return $arr;
230 }
231
232 /**
233 * This method gets the correct answers in a 2 dimentional array.
234 *
235 * @param object $question
236 * @return array of groups
237 */
238 protected function get_correct_answers($question) {
239 $arrayofchoices = $this->get_array_of_choices($question);
240 $arrayofplaceholdeers = $this->get_array_of_placeholders($question);
241
721ef2ea 242 $correctplayers = array();
060e0294
TH
243 foreach ($arrayofplaceholdeers as $ph) {
244 foreach ($arrayofchoices as $key => $choice) {
245 if ($key + 1 == $ph) {
647d9373 246 $correctplayers[] = $choice;
0d24b17a
TH
247 }
248 }
249 }
721ef2ea 250 return $correctplayers;
0d24b17a
TH
251 }
252
647d9373
TH
253 /**
254 * Return the list of groups used in a question.
255 * @param stdClass $question the question data.
256 * @return array the groups used, or false if an error occurs.
257 */
0d24b17a
TH
258 protected function get_array_of_placeholders($question) {
259 $qtext = $question->questiontext;
260 $error = '<b> ERROR</b>: Please check the form for this question. ';
060e0294 261 if (!$qtext) {
0d24b17a
TH
262 echo $error . 'The question text is empty!';
263 return false;
264 }
265
8d6fb0c6 266 // Get the slots.
0d24b17a
TH
267 $slots = $this->getEmbeddedTextArray($question);
268
060e0294 269 if (!$slots) {
0d24b17a
TH
270 echo $error . 'The question text is not in the correct format!';
271 return false;
272 }
273
274 $output = array();
275 foreach ($slots as $slot) {
8d6fb0c6 276 $output[] = substr($slot, 2, strlen($slot) - 4); // 2 is for '[[' and 4 is for '[[]]'.
0d24b17a
TH
277 }
278 return $output;
060e0294 279 }
0d24b17a 280
721ef2ea
TH
281 protected function get_group_of_players($question, $state, $subquestions, $group) {
282 $goupofanswers = array();
283 foreach ($subquestions as $key => $subquestion) {
284 if ($subquestion[$this->choice_group_key()] == $group) {
285 $goupofanswers[] = $subquestion;
0d24b17a
TH
286 }
287 }
288
8d6fb0c6 289 // Shuffle answers within this group.
0d24b17a 290 if ($question->options->shuffleanswers == 1) {
0d24b17a
TH
291 shuffle($goupofanswers);
292 }
293 return $goupofanswers;
294 }
295
296 public function get_possible_responses($questiondata) {
297 $question = $this->make_question($questiondata);
298
299 $parts = array();
300 foreach ($question->places as $place => $group) {
301 $choices = array();
302
303 foreach ($question->choices[$group] as $i => $choice) {
304 $choices[$i] = new question_possible_response(
4ffe7ac4 305 html_to_text($choice->text, 0, false),
647d9373 306 ($question->rightchoices[$place] == $i) / count($question->places));
0d24b17a
TH
307 }
308 $choices[null] = question_possible_response::no_response();
309
310 $parts[$place] = $choices;
311 }
312
313 return $parts;
314 }
315
046d8165 316 public function move_files($questionid, $oldcontextid, $newcontextid) {
721ef2ea 317 parent::move_files($questionid, $oldcontextid, $newcontextid);
ac6beb75 318 $this->move_files_in_combined_feedback($questionid, $oldcontextid, $newcontextid);
995d770e 319 $this->move_files_in_hints($questionid, $oldcontextid, $newcontextid);
721ef2ea 320 }
0d24b17a 321
721ef2ea
TH
322 protected function delete_files($questionid, $contextid) {
323 parent::delete_files($questionid, $contextid);
ac6beb75 324 $this->delete_files_in_combined_feedback($questionid, $contextid);
995d770e 325 $this->delete_files_in_hints($questionid, $contextid);
721ef2ea 326 }
0d24b17a 327}