web service MDL-12886
[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 {
f24493ec 14 protected $excludedqtypes = null;
15 protected $manualqtypes = null;
516cf3eb 16
8b192edb 17 // Caches questions available as randoms sorted by category
18 // This is a 2-d array. The first key is question category, and the
19 // second is whether to include subcategories.
f24493ec 20 private $catrandoms = array();
516cf3eb 21
22 function name() {
23 return 'random';
24 }
25
a2156789 26 function menu_name() {
8b192edb 27 // Don't include this question type in the 'add new question' menu.
a2156789 28 return false;
29 }
30
f24493ec 31 function show_analysis_of_responses() {
32 return true;
33 }
34
35 function is_manual_graded() {
36 return true;
37 }
38
39 function is_question_manual_graded($question, $otherquestionsinuse) {
40 global $DB;
41 // We take our best shot at working whether a particular question is manually
42 // graded follows: We look to see if any of the questions that this random
43 // question might select if of a manually graded type. If a category contains
44 // a mixture of manual and non-manual questions, and if all the attempts so
45 // far selected non-manual ones, this will give the wrong answer, but we
46 // don't care. Even so, this is an expensive calculation!
47 $this->init_qtype_lists();
48 if (!$this->manualqtypes) {
49 return false;
50 }
51 if ($question->questiontext) {
52 $categorylist = question_categorylist($question->category);
53 } else {
54 $categorylist = $question->category;
55 }
56 return $DB->record_exists_select('question',
57 "category IN ($categorylist)
58 AND parent = 0
59 AND hidden = 0
60 AND id NOT IN ($otherquestionsinuse)
61 AND qtype IN ($this->manualqtypes)");
62 }
63
a2156789 64 function is_usable_by_random() {
65 return false;
66 }
67
f24493ec 68 /**
69 * This method needs to be called before the ->excludedqtypes and
70 * ->manualqtypes fields can be used.
71 */
72 function init_qtype_lists() {
73 global $QTYPES;
74 if (is_null($this->excludedqtypes)) {
75 $excludedqtypes = array();
76 $manualqtypes = array();
77 foreach ($QTYPES as $qtype) {
78 $quotedname = "'" . $qtype->name() . "'";
79 if (!$qtype->is_usable_by_random()) {
80 $excludedqtypes[] = $quotedname;
81 } else if ($qtype->is_manual_graded()) {
82 $manualqtypes[] = $quotedname;
83 }
84 }
85 $this->excludedqtypes = implode(',', $excludedqtypes);
86 $this->manualqtypes = implode(',', $manualqtypes);
87 }
88 }
89
bcc234b0 90 function display_question_editing_page(&$mform, $question, $wizardnow){
91 list($heading, $langmodule) = $this->get_heading(empty($question->id));
92 print_heading_with_help($heading, $this->name(), $langmodule);
93 $mform->display();
94 }
95
516cf3eb 96 function get_question_options(&$question) {
97 // Don't do anything here, because the random question has no options.
98 // Everything is handled by the create- or restore_session_and_responses
99 // functions.
100 return true;
101 }
102
f59dba84 103 /**
104 * Random questions always get a question name that is Random (cateogryname).
105 * This function is a centralised place to calculate that, given the category.
106 */
107 function question_name($category) {
108 return get_string('random', 'quiz') .' ('. $category->name .')';
109 }
110
5348b899 111 function save_question($question, $form, $course) {
112 $form->name = '';
113 // Name is not a required field for random questions, but parent::save_question
114 // Assumes that it is.
115 return parent::save_question($question, $form, $course);
116 }
117
24e8b9b6 118 function save_question_options($question) {
f34488b2 119 global $DB;
24e8b9b6 120
121 // No options, as such, but we set the parent field to the question's
122 // own id. Setting the parent field has the effect of hiding this
123 // question in various places.
124 $updateobject = new stdClass;
125 $updateobject->id = $question->id;
126 $updateobject->parent = $question->id;
127
128 // We also force the question name to be 'Random (categoryname)'.
f34488b2 129 if (!$category = $DB->get_record('question_categories', array('id' => $question->category))) {
2471ef86 130 print_error('cannotretrieveqcat', 'question');
f59dba84 131 }
24e8b9b6 132 $updateobject->name = $this->question_name($category);
133 return $DB->update_record('question', $updateobject);
516cf3eb 134 }
135
339ef4c2 136 /**
137 * Get all the usable questions from a particular question category.
138 *
139 * @param integer $categoryid the id of a question category.
140 * @param boolean whether to include questions from subcategories.
141 * @param string $questionsinuse comma-separated list of question ids to exclude from consideration.
142 * @return array of question records.
143 */
144 function get_usable_questions_from_category($categoryid, $subcategories, $questionsinuse) {
f24493ec 145 global $DB;
146 $this->init_qtype_lists();
339ef4c2 147 if ($subcategories) {
148 $categorylist = question_categorylist($categoryid);
149 } else {
150 $categorylist = $categoryid;
151 }
152 if (!$catrandoms = $DB->get_records_select('question',
153 "category IN ($categorylist)
f24493ec 154 AND parent = 0
155 AND hidden = 0
339ef4c2 156 AND id NOT IN ($questionsinuse)
f24493ec 157 AND qtype NOT IN ($this->excludedqtypes)", null, '', 'id')) {
339ef4c2 158 $catrandoms = array();
159 }
160 return $catrandoms;
161 }
162
516cf3eb 163 function create_session_and_responses(&$question, &$state, $cmoptions, $attempt) {
339ef4c2 164 global $QTYPES, $DB;
516cf3eb 165 // Choose a random question from the category:
166 // We need to make sure that no question is used more than once in the
167 // quiz. Therfore the following need to be excluded:
168 // 1. All questions that are explicitly assigned to the quiz
169 // 2. All random questions
170 // 3. All questions that are already chosen by an other random question
5dba6590 171 // 4. Deleted questions
516cf3eb 172 if (!isset($cmoptions->questionsinuse)) {
173 $cmoptions->questionsinuse = $attempt->layout;
174 }
175
176 if (!isset($this->catrandoms[$question->category][$question->questiontext])) {
41d37309 177 $catrandoms = $this->get_usable_questions_from_category($question->category,
339ef4c2 178 $question->questiontext == "1", $cmoptions->questionsinuse);
41d37309 179 $this->catrandoms[$question->category][$question->questiontext] = swapshuffle_assoc($catrandoms);
516cf3eb 180 }
181
339ef4c2 182 while ($wrappedquestion = array_pop(
183 $this->catrandoms[$question->category][$question->questiontext])) {
516cf3eb 184 if (!ereg("(^|,)$wrappedquestion->id(,|$)", $cmoptions->questionsinuse)) {
185 /// $randomquestion is not in use and will therefore be used
186 /// as the randomquestion here...
f34488b2 187 $wrappedquestion = $DB->get_record('question', array('id' => $wrappedquestion->id));
f02c6f01 188 global $QTYPES;
189 $QTYPES[$wrappedquestion->qtype]
339ef4c2 190 ->get_question_options($wrappedquestion);
f02c6f01 191 $QTYPES[$wrappedquestion->qtype]
339ef4c2 192 ->create_session_and_responses($wrappedquestion,
193 $state, $cmoptions, $attempt);
516cf3eb 194 $wrappedquestion->name_prefix = $question->name_prefix;
339ef4c2 195 $wrappedquestion->maxgrade = $question->maxgrade;
516cf3eb 196 $cmoptions->questionsinuse .= ",$wrappedquestion->id";
197 $state->options->question = &$wrappedquestion;
198 return true;
199 }
200 }
201 $question->questiontext = '<span class="notifyproblem">'.
339ef4c2 202 get_string('toomanyrandom', 'quiz'). '</span>';
dfa47f96 203 $question->qtype = 'description';
516cf3eb 204 $state->responses = array('' => '');
205 return true;
206 }
207
208 function restore_session_and_responses(&$question, &$state) {
209 /// The raw response records for random questions come in two flavours:
210 /// ---- 1 ----
211 /// For responses stored by Moodle version 1.5 and later the answer
212 /// field has the pattern random#-* where the # part is the numeric
213 /// question id of the actual question shown in the quiz attempt
214 /// and * represents the student response to that actual question.
215 /// ---- 2 ----
216 /// For responses stored by older Moodle versions - the answer field is
217 /// simply the question id of the actual question. The student response
218 /// to the actual question is stored in a separate response record.
219 /// -----------------------
220 /// This means that prior to Moodle version 1.5, random questions needed
221 /// two response records for storing the response to a single question.
222 /// From version 1.5 and later the question type random works like all
223 /// the other question types in that it now only needs one response
224 /// record per question.
f34488b2 225 global $QTYPES, $DB;
516cf3eb 226 if (!ereg('^random([0-9]+)-(.*)$', $state->responses[''], $answerregs)) {
227 if (empty($state->responses[''])) {
228 // This is the case if there weren't enough questions available in the category.
229 $question->questiontext = '<span class="notifyproblem">'.
230 get_string('toomanyrandom', 'quiz'). '</span>';
dfa47f96 231 $question->qtype = 'description';
516cf3eb 232 return true;
233 }
234 // this must be an old-style state which stores only the id for the wrapped question
f34488b2 235 if (!$wrappedquestion = $DB->get_record('question', array('id' => $state->responses['']))) {
516cf3eb 236 notify("Can not find wrapped question {$state->responses['']}");
237 }
238 // In the old model the actual response was stored in a separate entry in
239 // the state table and fortunately there was only a single state per question
f34488b2 240 if (!$state->responses[''] = $DB->get_field('question_states', 'answer', array('attempt' => $state->attempt, 'question' => $wrappedquestion->id))) {
516cf3eb 241 notify("Wrapped state missing");
242 }
243 } else {
f34488b2 244 if (!$wrappedquestion = $DB->get_record('question', array('id' => $answerregs[1]))) {
4572d78f 245 // The teacher must have deleted this question by mistake
246 // Convert it into a description type question with an explanation to the student
247 $wrappedquestion = clone($question);
248 $wrappedquestion->id = $answerregs[1];
249 $wrappedquestion->questiontext = get_string('questiondeleted', 'quiz');
f57c6242 250 $wrappedquestion->qtype = 'missingtype';
516cf3eb 251 }
252 $state->responses[''] = (false === $answerregs[2]) ? '' : $answerregs[2];
253 }
254
f02c6f01 255 if (!$QTYPES[$wrappedquestion->qtype]
516cf3eb 256 ->get_question_options($wrappedquestion)) {
257 return false;
258 }
259
f02c6f01 260 if (!$QTYPES[$wrappedquestion->qtype]
516cf3eb 261 ->restore_session_and_responses($wrappedquestion, $state)) {
262 return false;
263 }
264 $wrappedquestion->name_prefix = $question->name_prefix;
265 $wrappedquestion->maxgrade = $question->maxgrade;
266 $state->options->question = &$wrappedquestion;
267 return true;
268 }
269
270 function save_session_and_responses(&$question, &$state) {
f34488b2 271 global $QTYPES, $DB;
516cf3eb 272 $wrappedquestion = &$state->options->question;
273
274 // Trick the wrapped question into pretending to be the random one.
275 $realqid = $wrappedquestion->id;
276 $wrappedquestion->id = $question->id;
f02c6f01 277 $QTYPES[$wrappedquestion->qtype]
516cf3eb 278 ->save_session_and_responses($wrappedquestion, $state);
279
280 // Read what the wrapped question has just set the answer field to
281 // (if anything)
f34488b2 282 $response = $DB->get_field('question_states', 'answer', array('id' => $state->id));
516cf3eb 283 if(false === $response) {
284 return false;
285 }
286
287 // Prefix the answer field...
288 $response = "random$realqid-$response";
289
290 // ... and save it again.
f34488b2 291 if (!$DB->set_field('question_states', 'answer', $response, array('id' => $state->id))) {
516cf3eb 292 }
293
294 // Restore the real id
295 $wrappedquestion->id = $realqid;
296 return true;
297 }
298
299 function get_correct_responses(&$question, &$state) {
f02c6f01 300 global $QTYPES;
516cf3eb 301 $wrappedquestion = &$state->options->question;
f02c6f01 302 return $QTYPES[$wrappedquestion->qtype]
516cf3eb 303 ->get_correct_responses($wrappedquestion, $state);
304 }
305
306 // ULPGC ecastro
307 function get_all_responses(&$question, &$state){
f02c6f01 308 global $QTYPES;
516cf3eb 309 $wrappedquestion = &$state->options->question;
f02c6f01 310 return $QTYPES[$wrappedquestion->qtype]
516cf3eb 311 ->get_all_responses($wrappedquestion, $state);
312 }
313
314 // ULPGC ecastro
315 function get_actual_response(&$question, &$state){
f02c6f01 316 global $QTYPES;
516cf3eb 317 $wrappedquestion = &$state->options->question;
f02c6f01 318 return $QTYPES[$wrappedquestion->qtype]
516cf3eb 319 ->get_actual_response($wrappedquestion, $state);
320 }
321
99ba746d 322 function get_html_head_contributions(&$question, &$state) {
323 global $QTYPES;
324 $wrappedquestion = &$state->options->question;
325 return $QTYPES[$wrappedquestion->qtype]
326 ->get_html_head_contributions($wrappedquestion, $state);
327 }
516cf3eb 328
329 function print_question(&$question, &$state, &$number, $cmoptions, $options) {
f02c6f01 330 global $QTYPES;
516cf3eb 331 $wrappedquestion = &$state->options->question;
8bb3fac1 332 $wrappedquestion->randomquestionid = $question->id;
f02c6f01 333 $QTYPES[$wrappedquestion->qtype]
516cf3eb 334 ->print_question($wrappedquestion, $state, $number, $cmoptions, $options);
335 }
336
337 function grade_responses(&$question, &$state, $cmoptions) {
f02c6f01 338 global $QTYPES;
516cf3eb 339 $wrappedquestion = &$state->options->question;
f02c6f01 340 return $QTYPES[$wrappedquestion->qtype]
516cf3eb 341 ->grade_responses($wrappedquestion, $state, $cmoptions);
342 }
343
344 function get_texsource(&$question, &$state, $cmoptions, $type) {
f02c6f01 345 global $QTYPES;
516cf3eb 346 $wrappedquestion = &$state->options->question;
f02c6f01 347 return $QTYPES[$wrappedquestion->qtype]
516cf3eb 348 ->get_texsource($wrappedquestion, $state, $cmoptions, $type);
349 }
350
351 function compare_responses(&$question, $state, $teststate) {
f02c6f01 352 global $QTYPES;
bb080d20 353 $wrappedquestion = &$teststate->options->question;
f02c6f01 354 return $QTYPES[$wrappedquestion->qtype]
516cf3eb 355 ->compare_responses($wrappedquestion, $state, $teststate);
356 }
357
3ff8a187 358 function restore_recode_answer($state, $restore) {
359 // The answer looks like 'randomXX-ANSWER', where XX is
360 // the id of the used question and ANSWER the actual
361 // response to that question.
362 // However, there may still be old-style states around,
363 // which store the id of the wrapped question in the
364 // state of the random question and store the response
365 // in a separate state for the wrapped question
366
f34488b2 367 global $QTYPES, $DB;
3ff8a187 368 $answer_field = "";
369
370 if (ereg('^random([0-9]+)-(.*)$', $state->answer, $answerregs)) {
371 // Recode the question id in $answerregs[1]
372 // Get the question from backup_ids
373 if(!$wrapped = backup_getid($restore->backup_unique_code,"question",$answerregs[1])) {
374 echo 'Could not recode question in random-'.$answerregs[1].'<br />';
375 return($answer_field);
376 }
377 // Get the question type for recursion
f34488b2 378 if (!$wrappedquestion->qtype = $DB->get_field('question', 'qtype', array('id' => $wrapped->new_id))) {
3ff8a187 379 echo 'Could not get qtype while recoding question random-'.$answerregs[1].'<br />';
380 return($answer_field);
381 }
382 $newstate = $state;
383 $newstate->question = $wrapped->new_id;
384 $newstate->answer = $answerregs[2];
385 $answer_field = 'random'.$wrapped->new_id.'-';
386
387 // Recode the answer field in $answerregs[2] depending on
388 // the qtype of question with id $answerregs[1]
389 $answer_field .= $QTYPES[$wrappedquestion->qtype]->restore_recode_answer($newstate, $restore);
390 } else {
391 // Handle old-style states
392 $answer_link = backup_getid($restore->backup_unique_code,"question",$state->answer);
393 if ($answer_link) {
394 $answer_field = $answer_link->new_id;
395 }
396 }
397
398 return $answer_field;
399 }
455c3efa 400
401 /**
402 * For random question type return empty string which means won't calculate.
403 * @param object $question
404 * @return mixed either a integer score out of 1 that the average random
405 * guess by a student might give or an empty string which means will not
406 * calculate.
407 */
408 function get_random_guess_score($question) {
409 return '';
410 }
3ff8a187 411
516cf3eb 412}
413//// END OF CLASS ////
414
415//////////////////////////////////////////////////////////////////////////
416//// INITIATION - Without this line the question type is not in use... ///
417//////////////////////////////////////////////////////////////////////////
a2156789 418question_register_questiontype(new random_qtype());
516cf3eb 419
420?>