MDL-47494 gapselect: Add lots of missing public/protected/private.
[moodle.git] / question / type / gapselect / questiontypebase.php
CommitLineData
0d24b17a
TH
1<?php
2
3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17
0d24b17a
TH
18/**
19 * Question type class for the embedded element in question text question types.
20 *
9df0480d 21 * @package qtype
0d24b17a 22 * @subpackage gapselect
9df0480d
TH
23 * @copyright 2011 The Open University
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
0d24b17a
TH
25 */
26
27
b28ad86a
TH
28defined('MOODLE_INTERNAL') || die();
29
0d24b17a
TH
30require_once($CFG->libdir . '/questionlib.php');
31require_once($CFG->dirroot . '/question/engine/lib.php');
32require_once($CFG->dirroot . '/question/format/xml/format.php');
33
34
35/**
36 * The embedded element in question text 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 40 */
43df6cac 41abstract class qtype_gapselect_base extends question_type {
0d24b17a
TH
42 /**
43 * Choices are stored in the question_answers table, and any options need to
44 * be put into the feedback field somehow. This method is responsible for
45 * converting all the options to a single string for this purpose. It is used
46 * by {@link save_question_options()}.
47 * @param array $choice the form data relating to this choice.
48 * @return string ready to store in the database.
49 */
50 protected abstract function choice_options_to_feedback($choice);
51
52 public function save_question_options($question) {
721ef2ea
TH
53 global $DB;
54 $context = $question->context;
0d24b17a
TH
55 $result = new stdClass();
56
721ef2ea
TH
57 $oldanswers = $DB->get_records('question_answers',
58 array('question' => $question->id), 'id ASC');
0d24b17a
TH
59
60 // Insert all the new answers
61 foreach ($question->choices as $key => $choice) {
62
63 if (trim($choice['answer']) == '') {
64 continue;
65 }
66
67 $feedback = $this->choice_options_to_feedback($choice);
68
721ef2ea 69 if ($answer = array_shift($oldanswers)) {
0d24b17a 70 $answer->answer = $choice['answer'];
0d24b17a 71 $answer->feedback = $feedback;
721ef2ea
TH
72 $DB->update_record('question_answers', $answer);
73
0d24b17a 74 } else {
721ef2ea 75 $answer = new stdClass();
0d24b17a 76 $answer->question = $question->id;
721ef2ea
TH
77 $answer->answer = $choice['answer'];
78 $answer->answerformat = FORMAT_HTML;
0d24b17a
TH
79 $answer->fraction = 0;
80 $answer->feedback = $feedback;
721ef2ea
TH
81 $answer->feedbackformat = 0;
82 $DB->insert_record('question_answers', $answer);
0d24b17a
TH
83 }
84 }
85
86 // Delete old answer records
721ef2ea
TH
87 foreach($oldanswers as $oa) {
88 delete_records('question_answers', 'id', $oa->id);
0d24b17a
TH
89 }
90
721ef2ea 91 $options = $DB->get_record('question_' . $this->name(), array('questionid' => $question->id));
0d24b17a 92 if (!$options) {
721ef2ea 93 $options = new stdClass();
0d24b17a 94 $options->questionid = $question->id;
721ef2ea
TH
95 $options->correctfeedback = '';
96 $options->partiallycorrectfeedback = '';
97 $options->incorrectfeedback = '';
98 $options->id = $DB->insert_record('question_' . $this->name(), $options);
0d24b17a
TH
99 }
100
101 $options->shuffleanswers = !empty($question->shuffleanswers);
721ef2ea
TH
102 $options = $this->save_combined_feedback_helper($options, $question, $context, true);
103 $DB->update_record('question_' . $this->name(), $options);
0d24b17a
TH
104
105 $this->save_hints($question, true);
0d24b17a
TH
106 }
107
108 public function get_question_options($question) {
721ef2ea
TH
109 global $DB;
110 $question->options = $DB->get_record('question_'.$this->name(),
111 array('questionid' => $question->id), '*', MUST_EXIST);
0d24b17a 112 parent::get_question_options($question);
0d24b17a
TH
113 }
114
721ef2ea
TH
115 public function delete_question($questionid, $contextid) {
116 global $DB;
117 $DB->delete_records('question_'.$this->name(), array('questionid' => $questionid));
118 return parent::delete_question($questionid, $contextid);
0d24b17a
TH
119 }
120
121 /**
122 * Used by {@link initialise_question_instance()} to set up the choice-specific data.
123 * @param object $choicedata as loaded from the question_answers table.
124 * @return object an appropriate object for representing the choice.
125 */
126 protected abstract function make_choice($choicedata);
127
128 protected function initialise_question_instance(question_definition $question, $questiondata) {
129 parent::initialise_question_instance($question, $questiondata);
130
131 $question->shufflechoices = $questiondata->options->shuffleanswers;
132
43df6cac 133 $this->initialise_combined_feedback($question, $questiondata, true);
0d24b17a
TH
134
135 $question->choices = array();
136 $choiceindexmap= array();
137
138 // Store the choices in arrays by group.
139 $i = 1;
140 foreach ($questiondata->options->answers as $choicedata) {
141 $choice = $this->make_choice($choicedata);
142
143 if (array_key_exists($choice->choice_group(), $question->choices)) {
144 $question->choices[$choice->choice_group()][] = $choice;
145 } else {
146 $question->choices[$choice->choice_group()][1] = $choice;
147 }
148
149 end($question->choices[$choice->choice_group()]);
150 $choiceindexmap[$i] = array($choice->choice_group(),
151 key($question->choices[$choice->choice_group()]));
152 $i += 1;
153 }
154
155 $question->places = array();
156 $question->textfragments = array();
157 $question->rightchoices = array();
158 // Break up the question text, and store the fragments, places and right answers.
159
160 $bits = preg_split('/\[\[(\d+)]]/', $question->questiontext, null, PREG_SPLIT_DELIM_CAPTURE);
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;
202 foreach ($subquestions as $key=>$subquestion) {
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();
0d24b17a
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. ';
256 if(!$qtext) {
257 echo $error . 'The question text is empty!';
258 return false;
259 }
260
261 //get the slots
262 $slots = $this->getEmbeddedTextArray($question);
263
264 if(!$slots) {
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) {
721ef2ea 271 $output[] = substr($slot, 2, strlen($slot) - 4); //2 is for '[[' and 4 is for '[[]]'.
0d24b17a
TH
272 }
273 return $output;
274 }
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
721ef2ea 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),
0d24b17a
TH
301 $question->rightchoices[$place] == $i);
302 }
303 $choices[null] = question_possible_response::no_response();
304
305 $parts[$place] = $choices;
306 }
307
308 return $parts;
309 }
310
046d8165 311 public function move_files($questionid, $oldcontextid, $newcontextid) {
721ef2ea
TH
312 parent::move_files($questionid, $oldcontextid, $newcontextid);
313
314 $fs = get_file_storage();
315 $fs->move_area_files_to_new_context($oldcontextid,
316 $newcontextid, 'question', 'correctfeedback', $questionid);
317 $fs->move_area_files_to_new_context($oldcontextid,
318 $newcontextid, 'question', 'partiallycorrectfeedback', $questionid);
319 $fs->move_area_files_to_new_context($oldcontextid,
320 $newcontextid, 'question', 'incorrectfeedback', $questionid);
321 }
0d24b17a 322
721ef2ea
TH
323 protected function delete_files($questionid, $contextid) {
324 parent::delete_files($questionid, $contextid);
325
326 $fs = get_file_storage();
327 $fs->delete_area_files($contextid, 'question', 'correctfeedback', $questionid);
328 $fs->delete_area_files($contextid, 'question', 'partiallycorrectfeedback', $questionid);
329 $fs->delete_area_files($contextid, 'question', 'incorrectfeedback', $questionid);
330 }
0d24b17a 331}