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