quiz editing: MDL-17296 refactor print_timing_information to avoid duplicating code...
[moodle.git] / question / type / random / questiontype.php
CommitLineData
516cf3eb 1<?php // $Id$
1976496e 2/**
8b192edb 3 * Class for the random question type.
f34488b2 4 *
5 * The random question type does not have any options. When the question is
8b192edb 6 * attempted, it picks a question at random from the category it is in (and
7 * optionally its subcategories). For details see create_session_and_responses.
8 * Then all other method calls as delegated to that other question.
f34488b2 9 *
1976496e 10 * @package questionbank
11 * @subpackage questiontypes
53a4d39f 12 */
7518b645 13class random_qtype extends default_questiontype {
516cf3eb 14
8b192edb 15 // Caches questions available as randoms sorted by category
16 // This is a 2-d array. The first key is question category, and the
17 // second is whether to include subcategories.
516cf3eb 18 var $catrandoms = array();
19
20 function name() {
21 return 'random';
22 }
23
a2156789 24 function menu_name() {
8b192edb 25 // Don't include this question type in the 'add new question' menu.
a2156789 26 return false;
27 }
28
29 function is_usable_by_random() {
30 return false;
31 }
32
516cf3eb 33 function get_question_options(&$question) {
34 // Don't do anything here, because the random question has no options.
35 // Everything is handled by the create- or restore_session_and_responses
36 // functions.
37 return true;
38 }
39
f59dba84 40 /**
41 * Random questions always get a question name that is Random (cateogryname).
42 * This function is a centralised place to calculate that, given the category.
43 */
44 function question_name($category) {
45 return get_string('random', 'quiz') .' ('. $category->name .')';
46 }
47
48 function save_question($question, $form, $course) {
f34488b2 49 global $DB;
f59dba84 50 // If the category is changing, set things up as default_questiontype::save_question expects.
51 list($formcategory, $unused) = explode(',', $form->category);
52 if (isset($question->id) && $formcategory != $question->category) {
53 $form->categorymoveto = $form->category;
54 }
55 $form->name = '';
56 $question = parent::save_question($question, $form, $course);
f34488b2 57 if (!$category = $DB->get_record('question_categories', array('id' => $question->category))) {
2471ef86 58 print_error('cannotretrieveqcat', 'question');
f59dba84 59 }
60 $question->name = $this->question_name($category);
04c8148a 61 if (!$DB->set_field('question', 'name', $question->name, array('id' => $question->id))) {
2471ef86 62 print_error('cannotupdaterandomqname', 'question');
f59dba84 63 }
64 return $question;
65 }
66
516cf3eb 67 function save_question_options($question) {
f34488b2 68 global $DB;
8b192edb 69 // No options, but we set the parent field to the question's own id.
70 // Setting the parent field has the effect of hiding this question in
71 // various places.
f34488b2 72 return ($DB->set_field('question', 'parent', $question->id, array('id' => $question->id)) ? true : false);
516cf3eb 73 }
74
339ef4c2 75 /**
76 * Get all the usable questions from a particular question category.
77 *
78 * @param integer $categoryid the id of a question category.
79 * @param boolean whether to include questions from subcategories.
80 * @param string $questionsinuse comma-separated list of question ids to exclude from consideration.
81 * @return array of question records.
82 */
83 function get_usable_questions_from_category($categoryid, $subcategories, $questionsinuse) {
84 global $QTYPE_EXCLUDE_FROM_RANDOM, $DB;
85 if ($subcategories) {
86 $categorylist = question_categorylist($categoryid);
87 } else {
88 $categorylist = $categoryid;
89 }
90 if (!$catrandoms = $DB->get_records_select('question',
91 "category IN ($categorylist)
92 AND parent = '0'
93 AND hidden = '0'
94 AND id NOT IN ($questionsinuse)
95 AND qtype NOT IN ($QTYPE_EXCLUDE_FROM_RANDOM)", null, '', 'id')) {
96 $catrandoms = array();
97 }
98 return $catrandoms;
99 }
100
516cf3eb 101 function create_session_and_responses(&$question, &$state, $cmoptions, $attempt) {
339ef4c2 102 global $QTYPES, $DB;
516cf3eb 103 // Choose a random question from the category:
104 // We need to make sure that no question is used more than once in the
105 // quiz. Therfore the following need to be excluded:
106 // 1. All questions that are explicitly assigned to the quiz
107 // 2. All random questions
108 // 3. All questions that are already chosen by an other random question
5dba6590 109 // 4. Deleted questions
516cf3eb 110 if (!isset($cmoptions->questionsinuse)) {
111 $cmoptions->questionsinuse = $attempt->layout;
112 }
113
114 if (!isset($this->catrandoms[$question->category][$question->questiontext])) {
41d37309 115 $catrandoms = $this->get_usable_questions_from_category($question->category,
339ef4c2 116 $question->questiontext == "1", $cmoptions->questionsinuse);
41d37309 117 $this->catrandoms[$question->category][$question->questiontext] = swapshuffle_assoc($catrandoms);
516cf3eb 118 }
119
339ef4c2 120 while ($wrappedquestion = array_pop(
121 $this->catrandoms[$question->category][$question->questiontext])) {
516cf3eb 122 if (!ereg("(^|,)$wrappedquestion->id(,|$)", $cmoptions->questionsinuse)) {
123 /// $randomquestion is not in use and will therefore be used
124 /// as the randomquestion here...
f34488b2 125 $wrappedquestion = $DB->get_record('question', array('id' => $wrappedquestion->id));
f02c6f01 126 global $QTYPES;
127 $QTYPES[$wrappedquestion->qtype]
339ef4c2 128 ->get_question_options($wrappedquestion);
f02c6f01 129 $QTYPES[$wrappedquestion->qtype]
339ef4c2 130 ->create_session_and_responses($wrappedquestion,
131 $state, $cmoptions, $attempt);
516cf3eb 132 $wrappedquestion->name_prefix = $question->name_prefix;
339ef4c2 133 $wrappedquestion->maxgrade = $question->maxgrade;
516cf3eb 134 $cmoptions->questionsinuse .= ",$wrappedquestion->id";
135 $state->options->question = &$wrappedquestion;
136 return true;
137 }
138 }
139 $question->questiontext = '<span class="notifyproblem">'.
339ef4c2 140 get_string('toomanyrandom', 'quiz'). '</span>';
dfa47f96 141 $question->qtype = 'description';
516cf3eb 142 $state->responses = array('' => '');
143 return true;
144 }
145
146 function restore_session_and_responses(&$question, &$state) {
147 /// The raw response records for random questions come in two flavours:
148 /// ---- 1 ----
149 /// For responses stored by Moodle version 1.5 and later the answer
150 /// field has the pattern random#-* where the # part is the numeric
151 /// question id of the actual question shown in the quiz attempt
152 /// and * represents the student response to that actual question.
153 /// ---- 2 ----
154 /// For responses stored by older Moodle versions - the answer field is
155 /// simply the question id of the actual question. The student response
156 /// to the actual question is stored in a separate response record.
157 /// -----------------------
158 /// This means that prior to Moodle version 1.5, random questions needed
159 /// two response records for storing the response to a single question.
160 /// From version 1.5 and later the question type random works like all
161 /// the other question types in that it now only needs one response
162 /// record per question.
f34488b2 163 global $QTYPES, $DB;
516cf3eb 164 if (!ereg('^random([0-9]+)-(.*)$', $state->responses[''], $answerregs)) {
165 if (empty($state->responses[''])) {
166 // This is the case if there weren't enough questions available in the category.
167 $question->questiontext = '<span class="notifyproblem">'.
168 get_string('toomanyrandom', 'quiz'). '</span>';
dfa47f96 169 $question->qtype = 'description';
516cf3eb 170 return true;
171 }
172 // this must be an old-style state which stores only the id for the wrapped question
f34488b2 173 if (!$wrappedquestion = $DB->get_record('question', array('id' => $state->responses['']))) {
516cf3eb 174 notify("Can not find wrapped question {$state->responses['']}");
175 }
176 // In the old model the actual response was stored in a separate entry in
177 // the state table and fortunately there was only a single state per question
f34488b2 178 if (!$state->responses[''] = $DB->get_field('question_states', 'answer', array('attempt' => $state->attempt, 'question' => $wrappedquestion->id))) {
516cf3eb 179 notify("Wrapped state missing");
180 }
181 } else {
f34488b2 182 if (!$wrappedquestion = $DB->get_record('question', array('id' => $answerregs[1]))) {
4572d78f 183 // The teacher must have deleted this question by mistake
184 // Convert it into a description type question with an explanation to the student
185 $wrappedquestion = clone($question);
186 $wrappedquestion->id = $answerregs[1];
187 $wrappedquestion->questiontext = get_string('questiondeleted', 'quiz');
f57c6242 188 $wrappedquestion->qtype = 'missingtype';
516cf3eb 189 }
190 $state->responses[''] = (false === $answerregs[2]) ? '' : $answerregs[2];
191 }
192
f02c6f01 193 if (!$QTYPES[$wrappedquestion->qtype]
516cf3eb 194 ->get_question_options($wrappedquestion)) {
195 return false;
196 }
197
f02c6f01 198 if (!$QTYPES[$wrappedquestion->qtype]
516cf3eb 199 ->restore_session_and_responses($wrappedquestion, $state)) {
200 return false;
201 }
202 $wrappedquestion->name_prefix = $question->name_prefix;
203 $wrappedquestion->maxgrade = $question->maxgrade;
204 $state->options->question = &$wrappedquestion;
205 return true;
206 }
207
208 function save_session_and_responses(&$question, &$state) {
f34488b2 209 global $QTYPES, $DB;
516cf3eb 210 $wrappedquestion = &$state->options->question;
211
212 // Trick the wrapped question into pretending to be the random one.
213 $realqid = $wrappedquestion->id;
214 $wrappedquestion->id = $question->id;
f02c6f01 215 $QTYPES[$wrappedquestion->qtype]
516cf3eb 216 ->save_session_and_responses($wrappedquestion, $state);
217
218 // Read what the wrapped question has just set the answer field to
219 // (if anything)
f34488b2 220 $response = $DB->get_field('question_states', 'answer', array('id' => $state->id));
516cf3eb 221 if(false === $response) {
222 return false;
223 }
224
225 // Prefix the answer field...
226 $response = "random$realqid-$response";
227
228 // ... and save it again.
f34488b2 229 if (!$DB->set_field('question_states', 'answer', $response, array('id' => $state->id))) {
516cf3eb 230 }
231
232 // Restore the real id
233 $wrappedquestion->id = $realqid;
234 return true;
235 }
236
237 function get_correct_responses(&$question, &$state) {
f02c6f01 238 global $QTYPES;
516cf3eb 239 $wrappedquestion = &$state->options->question;
f02c6f01 240 return $QTYPES[$wrappedquestion->qtype]
516cf3eb 241 ->get_correct_responses($wrappedquestion, $state);
242 }
243
244 // ULPGC ecastro
245 function get_all_responses(&$question, &$state){
f02c6f01 246 global $QTYPES;
516cf3eb 247 $wrappedquestion = &$state->options->question;
f02c6f01 248 return $QTYPES[$wrappedquestion->qtype]
516cf3eb 249 ->get_all_responses($wrappedquestion, $state);
250 }
251
252 // ULPGC ecastro
253 function get_actual_response(&$question, &$state){
f02c6f01 254 global $QTYPES;
516cf3eb 255 $wrappedquestion = &$state->options->question;
f02c6f01 256 return $QTYPES[$wrappedquestion->qtype]
516cf3eb 257 ->get_actual_response($wrappedquestion, $state);
258 }
259
99ba746d 260 function get_html_head_contributions(&$question, &$state) {
261 global $QTYPES;
262 $wrappedquestion = &$state->options->question;
263 return $QTYPES[$wrappedquestion->qtype]
264 ->get_html_head_contributions($wrappedquestion, $state);
265 }
516cf3eb 266
267 function print_question(&$question, &$state, &$number, $cmoptions, $options) {
f02c6f01 268 global $QTYPES;
516cf3eb 269 $wrappedquestion = &$state->options->question;
8bb3fac1 270 $wrappedquestion->randomquestionid = $question->id;
f02c6f01 271 $QTYPES[$wrappedquestion->qtype]
516cf3eb 272 ->print_question($wrappedquestion, $state, $number, $cmoptions, $options);
273 }
274
275 function grade_responses(&$question, &$state, $cmoptions) {
f02c6f01 276 global $QTYPES;
516cf3eb 277 $wrappedquestion = &$state->options->question;
f02c6f01 278 return $QTYPES[$wrappedquestion->qtype]
516cf3eb 279 ->grade_responses($wrappedquestion, $state, $cmoptions);
280 }
281
282 function get_texsource(&$question, &$state, $cmoptions, $type) {
f02c6f01 283 global $QTYPES;
516cf3eb 284 $wrappedquestion = &$state->options->question;
f02c6f01 285 return $QTYPES[$wrappedquestion->qtype]
516cf3eb 286 ->get_texsource($wrappedquestion, $state, $cmoptions, $type);
287 }
288
289 function compare_responses(&$question, $state, $teststate) {
f02c6f01 290 global $QTYPES;
bb080d20 291 $wrappedquestion = &$teststate->options->question;
f02c6f01 292 return $QTYPES[$wrappedquestion->qtype]
516cf3eb 293 ->compare_responses($wrappedquestion, $state, $teststate);
294 }
295
3ff8a187 296 function restore_recode_answer($state, $restore) {
297 // The answer looks like 'randomXX-ANSWER', where XX is
298 // the id of the used question and ANSWER the actual
299 // response to that question.
300 // However, there may still be old-style states around,
301 // which store the id of the wrapped question in the
302 // state of the random question and store the response
303 // in a separate state for the wrapped question
304
f34488b2 305 global $QTYPES, $DB;
3ff8a187 306 $answer_field = "";
307
308 if (ereg('^random([0-9]+)-(.*)$', $state->answer, $answerregs)) {
309 // Recode the question id in $answerregs[1]
310 // Get the question from backup_ids
311 if(!$wrapped = backup_getid($restore->backup_unique_code,"question",$answerregs[1])) {
312 echo 'Could not recode question in random-'.$answerregs[1].'<br />';
313 return($answer_field);
314 }
315 // Get the question type for recursion
f34488b2 316 if (!$wrappedquestion->qtype = $DB->get_field('question', 'qtype', array('id' => $wrapped->new_id))) {
3ff8a187 317 echo 'Could not get qtype while recoding question random-'.$answerregs[1].'<br />';
318 return($answer_field);
319 }
320 $newstate = $state;
321 $newstate->question = $wrapped->new_id;
322 $newstate->answer = $answerregs[2];
323 $answer_field = 'random'.$wrapped->new_id.'-';
324
325 // Recode the answer field in $answerregs[2] depending on
326 // the qtype of question with id $answerregs[1]
327 $answer_field .= $QTYPES[$wrappedquestion->qtype]->restore_recode_answer($newstate, $restore);
328 } else {
329 // Handle old-style states
330 $answer_link = backup_getid($restore->backup_unique_code,"question",$state->answer);
331 if ($answer_link) {
332 $answer_field = $answer_link->new_id;
333 }
334 }
335
336 return $answer_field;
337 }
455c3efa 338
339 /**
340 * For random question type return empty string which means won't calculate.
341 * @param object $question
342 * @return mixed either a integer score out of 1 that the average random
343 * guess by a student might give or an empty string which means will not
344 * calculate.
345 */
346 function get_random_guess_score($question) {
347 return '';
348 }
3ff8a187 349
516cf3eb 350}
351//// END OF CLASS ////
352
353//////////////////////////////////////////////////////////////////////////
354//// INITIATION - Without this line the question type is not in use... ///
355//////////////////////////////////////////////////////////////////////////
a2156789 356question_register_questiontype(new random_qtype());
516cf3eb 357
358?>