Follow-up for MDL-1149 Accessibility: Current week/topic is NOT indicated non-visuall...
[moodle.git] / question / type / random / questiontype.php
CommitLineData
516cf3eb 1<?php // $Id$
1976496e 2/**
8b192edb 3 * Class for the random question type.
4 *
5 * The random question type does not have any options. When the question is
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.
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
40 function save_question_options($question) {
8b192edb 41 // No options, but we set the parent field to the question's own id.
42 // Setting the parent field has the effect of hiding this question in
43 // various places.
4f48fb42 44 return (set_field('question', 'parent', $question->id, 'id',
516cf3eb 45 $question->id) ? true : false);
46 }
47
48 function create_session_and_responses(&$question, &$state, $cmoptions, $attempt) {
a2156789 49 global $QTYPE_EXCLUDE_FROM_RANDOM;
516cf3eb 50 // Choose a random question from the category:
51 // We need to make sure that no question is used more than once in the
52 // quiz. Therfore the following need to be excluded:
53 // 1. All questions that are explicitly assigned to the quiz
54 // 2. All random questions
55 // 3. All questions that are already chosen by an other random question
5dba6590 56 // 4. Deleted questions
516cf3eb 57 if (!isset($cmoptions->questionsinuse)) {
58 $cmoptions->questionsinuse = $attempt->layout;
59 }
60
61 if (!isset($this->catrandoms[$question->category][$question->questiontext])) {
62 // Need to fetch random questions from category $question->category"
63 // (Note: $this refers to the questiontype, not the question.)
64 global $CFG;
516cf3eb 65 if ($question->questiontext == "1") {
66 // recurse into subcategories
dc1f00de 67 $categorylist = question_categorylist($question->category);
516cf3eb 68 } else {
69 $categorylist = $question->category;
70 }
5dba6590 71 if ($catrandoms = get_records_select('question',
72 "category IN ($categorylist)
516cf3eb 73 AND parent = '0'
5dba6590 74 AND hidden = '0'
516cf3eb 75 AND id NOT IN ($cmoptions->questionsinuse)
5dba6590 76 AND qtype NOT IN ($QTYPE_EXCLUDE_FROM_RANDOM)", '', 'id')) {
516cf3eb 77 $this->catrandoms[$question->category][$question->questiontext] =
5dba6590 78 draw_rand_array($catrandoms, count($catrandoms));
516cf3eb 79 } else {
80 $this->catrandoms[$question->category][$question->questiontext] = array();
81 }
82 }
83
84 while ($wrappedquestion =
85 array_pop($this->catrandoms[$question->category][$question->questiontext])) {
86 if (!ereg("(^|,)$wrappedquestion->id(,|$)", $cmoptions->questionsinuse)) {
87 /// $randomquestion is not in use and will therefore be used
88 /// as the randomquestion here...
4f48fb42 89 $wrappedquestion = get_record('question', 'id', $wrappedquestion->id);
f02c6f01 90 global $QTYPES;
91 $QTYPES[$wrappedquestion->qtype]
516cf3eb 92 ->get_question_options($wrappedquestion);
f02c6f01 93 $QTYPES[$wrappedquestion->qtype]
516cf3eb 94 ->create_session_and_responses($wrappedquestion,
95 $state, $cmoptions, $attempt);
96 $wrappedquestion->name_prefix = $question->name_prefix;
97 $wrappedquestion->maxgrade = $question->maxgrade;
98 $cmoptions->questionsinuse .= ",$wrappedquestion->id";
99 $state->options->question = &$wrappedquestion;
100 return true;
101 }
102 }
103 $question->questiontext = '<span class="notifyproblem">'.
104 get_string('toomanyrandom', 'quiz'). '</span>';
dfa47f96 105 $question->qtype = 'description';
516cf3eb 106 $state->responses = array('' => '');
107 return true;
108 }
109
110 function restore_session_and_responses(&$question, &$state) {
111 /// The raw response records for random questions come in two flavours:
112 /// ---- 1 ----
113 /// For responses stored by Moodle version 1.5 and later the answer
114 /// field has the pattern random#-* where the # part is the numeric
115 /// question id of the actual question shown in the quiz attempt
116 /// and * represents the student response to that actual question.
117 /// ---- 2 ----
118 /// For responses stored by older Moodle versions - the answer field is
119 /// simply the question id of the actual question. The student response
120 /// to the actual question is stored in a separate response record.
121 /// -----------------------
122 /// This means that prior to Moodle version 1.5, random questions needed
123 /// two response records for storing the response to a single question.
124 /// From version 1.5 and later the question type random works like all
125 /// the other question types in that it now only needs one response
126 /// record per question.
f02c6f01 127 global $QTYPES;
516cf3eb 128 if (!ereg('^random([0-9]+)-(.*)$', $state->responses[''], $answerregs)) {
129 if (empty($state->responses[''])) {
130 // This is the case if there weren't enough questions available in the category.
131 $question->questiontext = '<span class="notifyproblem">'.
132 get_string('toomanyrandom', 'quiz'). '</span>';
dfa47f96 133 $question->qtype = 'description';
516cf3eb 134 return true;
135 }
136 // this must be an old-style state which stores only the id for the wrapped question
4f48fb42 137 if (!$wrappedquestion = get_record('question', 'id', $state->responses[''])) {
516cf3eb 138 notify("Can not find wrapped question {$state->responses['']}");
139 }
140 // In the old model the actual response was stored in a separate entry in
141 // the state table and fortunately there was only a single state per question
4f48fb42 142 if (!$state->responses[''] = get_field('question_states', 'answer', 'attempt', $state->attempt, 'question', $wrappedquestion->id)) {
516cf3eb 143 notify("Wrapped state missing");
144 }
145 } else {
4f48fb42 146 if (!$wrappedquestion = get_record('question', 'id', $answerregs[1])) {
4572d78f 147 // The teacher must have deleted this question by mistake
148 // Convert it into a description type question with an explanation to the student
149 $wrappedquestion = clone($question);
150 $wrappedquestion->id = $answerregs[1];
151 $wrappedquestion->questiontext = get_string('questiondeleted', 'quiz');
f57c6242 152 $wrappedquestion->qtype = 'missingtype';
516cf3eb 153 }
154 $state->responses[''] = (false === $answerregs[2]) ? '' : $answerregs[2];
155 }
156
f02c6f01 157 if (!$QTYPES[$wrappedquestion->qtype]
516cf3eb 158 ->get_question_options($wrappedquestion)) {
159 return false;
160 }
161
f02c6f01 162 if (!$QTYPES[$wrappedquestion->qtype]
516cf3eb 163 ->restore_session_and_responses($wrappedquestion, $state)) {
164 return false;
165 }
166 $wrappedquestion->name_prefix = $question->name_prefix;
167 $wrappedquestion->maxgrade = $question->maxgrade;
168 $state->options->question = &$wrappedquestion;
169 return true;
170 }
171
172 function save_session_and_responses(&$question, &$state) {
f02c6f01 173 global $QTYPES;
516cf3eb 174 $wrappedquestion = &$state->options->question;
175
176 // Trick the wrapped question into pretending to be the random one.
177 $realqid = $wrappedquestion->id;
178 $wrappedquestion->id = $question->id;
f02c6f01 179 $QTYPES[$wrappedquestion->qtype]
516cf3eb 180 ->save_session_and_responses($wrappedquestion, $state);
181
182 // Read what the wrapped question has just set the answer field to
183 // (if anything)
4f48fb42 184 $response = get_field('question_states', 'answer', 'id', $state->id);
516cf3eb 185 if(false === $response) {
186 return false;
187 }
188
189 // Prefix the answer field...
190 $response = "random$realqid-$response";
191
192 // ... and save it again.
4f48fb42 193 if (!set_field('question_states', 'answer', addslashes($response), 'id', $state->id)) {
516cf3eb 194 return false;
195 }
196
197 // Restore the real id
198 $wrappedquestion->id = $realqid;
199 return true;
200 }
201
202 function get_correct_responses(&$question, &$state) {
f02c6f01 203 global $QTYPES;
516cf3eb 204 $wrappedquestion = &$state->options->question;
f02c6f01 205 return $QTYPES[$wrappedquestion->qtype]
516cf3eb 206 ->get_correct_responses($wrappedquestion, $state);
207 }
208
209 // ULPGC ecastro
210 function get_all_responses(&$question, &$state){
f02c6f01 211 global $QTYPES;
516cf3eb 212 $wrappedquestion = &$state->options->question;
f02c6f01 213 return $QTYPES[$wrappedquestion->qtype]
516cf3eb 214 ->get_all_responses($wrappedquestion, $state);
215 }
216
217 // ULPGC ecastro
218 function get_actual_response(&$question, &$state){
f02c6f01 219 global $QTYPES;
516cf3eb 220 $wrappedquestion = &$state->options->question;
f02c6f01 221 return $QTYPES[$wrappedquestion->qtype]
516cf3eb 222 ->get_actual_response($wrappedquestion, $state);
223 }
224
99ba746d 225 function get_html_head_contributions(&$question, &$state) {
226 global $QTYPES;
227 $wrappedquestion = &$state->options->question;
228 return $QTYPES[$wrappedquestion->qtype]
229 ->get_html_head_contributions($wrappedquestion, $state);
230 }
516cf3eb 231
232 function print_question(&$question, &$state, &$number, $cmoptions, $options) {
f02c6f01 233 global $QTYPES;
516cf3eb 234 $wrappedquestion = &$state->options->question;
f02c6f01 235 $QTYPES[$wrappedquestion->qtype]
516cf3eb 236 ->print_question($wrappedquestion, $state, $number, $cmoptions, $options);
237 }
238
239 function grade_responses(&$question, &$state, $cmoptions) {
f02c6f01 240 global $QTYPES;
516cf3eb 241 $wrappedquestion = &$state->options->question;
f02c6f01 242 return $QTYPES[$wrappedquestion->qtype]
516cf3eb 243 ->grade_responses($wrappedquestion, $state, $cmoptions);
244 }
245
246 function get_texsource(&$question, &$state, $cmoptions, $type) {
f02c6f01 247 global $QTYPES;
516cf3eb 248 $wrappedquestion = &$state->options->question;
f02c6f01 249 return $QTYPES[$wrappedquestion->qtype]
516cf3eb 250 ->get_texsource($wrappedquestion, $state, $cmoptions, $type);
251 }
252
253 function compare_responses(&$question, $state, $teststate) {
f02c6f01 254 global $QTYPES;
bb080d20 255 $wrappedquestion = &$teststate->options->question;
f02c6f01 256 return $QTYPES[$wrappedquestion->qtype]
516cf3eb 257 ->compare_responses($wrappedquestion, $state, $teststate);
258 }
259
260}
261//// END OF CLASS ////
262
263//////////////////////////////////////////////////////////////////////////
264//// INITIATION - Without this line the question type is not in use... ///
265//////////////////////////////////////////////////////////////////////////
a2156789 266question_register_questiontype(new random_qtype());
516cf3eb 267
268?>