MDL-20636 a bit of clode clean-up in calculated*.
[moodle.git] / question / engine / questionattemptstep.php
CommitLineData
dcd03928 1<?php
dcd03928
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
17/**
18 * This file defines the question attempt step class, and a few related classes.
19 *
20 * @package moodlecore
21 * @subpackage questionengine
22 * @copyright 2009 The Open University
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26
27defined('MOODLE_INTERNAL') || die();
28
29
30/**
31 * Stores one step in a {@link question_attempt}.
32 *
33 * The most important attributes of a step are the state, which is one of the
34 * {@link question_state} constants, the fraction, which may be null, or a
35 * number bewteen the attempt's minfraction and 1.0, and the array of submitted
36 * data, about which more later.
37 *
38 * A step also tracks the time it was created, and the user responsible for
39 * creating it.
40 *
41 * The submitted data is basically just an array of name => value pairs, with
42 * certain conventions about the to divide the variables into four = two times two
43 * categories.
44 *
45 * Variables may either belong to the behaviour, in which case the
46 * name starts with a '-', or they may belong to the question type in which case
47 * they name does not start with a '-'.
48 *
49 * Second, variables may either be ones that came form the original request, in
50 * which case the name does not start with an _, or they are cached values that
51 * were created during processing, in which case the name does start with an _.
52 *
53 * That is, each name will start with one of '', '_'. '-' or '-_'. The remainder
54 * of the name should match the regex [a-z][a-z0-9]*.
55 *
56 * These variables can be accessed with {@link get_behaviour_var()} and {@link get_qt_var()},
57 * - to be clear, ->get_behaviour_var('x') gets the variable with name '-x' -
58 * and values whose names start with '_' can be set using {@link set_behaviour_var()}
59 * and {@link set_qt_var()}. There are some other methods like {@link has_behaviour_var()}
60 * to check wether a varaible with a particular name is set, and {@link get_behaviour_data()}
61 * to get all the behaviour data as an associative array.
62 *
63 * @copyright 2009 The Open University
64 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
65 */
66class question_attempt_step {
9c197f44
TH
67 /**
68 * @var integer if this attempts is stored in the question_attempts table,
69 * the id of that row.
70 */
dcd03928
TH
71 private $id = null;
72
9c197f44
TH
73 /**
74 * @var question_state one of the {@link question_state} constants.
75 * The state after this step.
76 */
dcd03928
TH
77 private $state;
78
79 /** @var null|number the fraction (grade on a scale of minfraction .. 1.0) or null. */
80 private $fraction = null;
81
82 /** @var integer the timestamp when this step was created. */
83 private $timecreated;
84
85 /** @var integer the id of the user resonsible for creating this step. */
86 private $userid;
87
88 /** @var array name => value pairs. The submitted data. */
89 private $data;
90
91 /** @var array name => array of {@link stored_file}s. Caches the contents of file areas. */
92 private $files = array();
93
94 /**
95 * You should not need to call this constructor in your own code. Steps are
96 * normally created by {@link question_attempt} methods like
97 * {@link question_attempt::process_action()}.
98 * @param array $data the submitted data that defines this step.
99 * @param int $timestamp the time to record for the action. (If not given, use now.)
100 * @param int $userid the user to attribute the aciton to. (If not given, use the current user.)
101 */
102 public function __construct($data = array(), $timecreated = null, $userid = null) {
103 global $USER;
104 $this->state = question_state::$unprocessed;
105 $this->data = $data;
106 if (is_null($timecreated)) {
107 $this->timecreated = time();
108 } else {
109 $this->timecreated = $timecreated;
110 }
111 if (is_null($userid)) {
112 $this->userid = $USER->id;
113 } else {
114 $this->userid = $userid;
115 }
116 }
117
118 /** @return question_state The state after this step. */
119 public function get_state() {
120 return $this->state;
121 }
122
123 /**
124 * Set the state. Normally only called by behaviours.
125 * @param question_state $state one of the {@link question_state} constants.
126 */
127 public function set_state($state) {
128 $this->state = $state;
129 }
130
131 /**
132 * @return null|number the fraction (grade on a scale of minfraction .. 1.0)
133 * or null if this step has not been marked.
134 */
135 public function get_fraction() {
136 return $this->fraction;
137 }
138
139 /**
140 * Set the fraction. Normally only called by behaviours.
141 * @param null|number $fraction the fraction to set.
142 */
143 public function set_fraction($fraction) {
144 $this->fraction = $fraction;
145 }
146
147 /** @return int the id of the user resonsible for creating this step. */
148 public function get_user_id() {
149 return $this->userid;
150 }
151
152 /** @return int the timestamp when this step was created. */
153 public function get_timecreated() {
154 return $this->timecreated;
155 }
156
157 /**
158 * @param string $name the name of a question type variable to look for in the submitted data.
159 * @return bool whether a variable with this name exists in the question type data.
160 */
161 public function has_qt_var($name) {
162 return array_key_exists($name, $this->data);
163 }
164
165 /**
166 * @param string $name the name of a question type variable to look for in the submitted data.
167 * @return string the requested variable, or null if the variable is not set.
168 */
169 public function get_qt_var($name) {
170 if (!$this->has_qt_var($name)) {
171 return null;
172 }
173 return $this->data[$name];
174 }
175
176 /**
177 * Set a cached question type variable.
178 * @param string $name the name of the variable to set. Must match _[a-z][a-z0-9]*.
179 * @param string $value the value to set.
180 */
181 public function set_qt_var($name, $value) {
182 if ($name[0] != '_') {
9c197f44
TH
183 throw new coding_exception('Cannot set question type data ' . $name .
184 ' on an attempt step. You can only set variables with names begining with _.');
dcd03928
TH
185 }
186 $this->data[$name] = $value;
187 }
188
189 /**
190 * Get the latest set of files for a particular question type variable of
191 * type question_attempt::PARAM_FILES.
192 *
193 * @param string $name the name of the associated variable.
194 * @return array of {@link stored_files}.
195 */
196 public function get_qt_files($name, $contextid) {
197 if (array_key_exists($name, $this->files)) {
198 return $this->files[$name];
199 }
200
201 if (!$this->has_qt_var($name)) {
202 $this->files[$name] = array();
203 return array();
204 }
205
206 $fs = get_file_storage();
207 $this->files[$name] = $fs->get_area_files($contextid, 'question',
208 'response_' . $name, $this->id, 'sortorder', false);
209
210 return $this->files[$name];
211 }
212
213 /**
214 * Prepare a draft file are for the files belonging the a response variable
215 * of this step.
216 *
217 * @param string $name the variable name the files belong to.
218 * @param int $contextid the id of the context the quba belongs to.
219 * @return int the draft itemid.
220 */
221 public function prepare_response_files_draft_itemid($name, $contextid) {
222 list($draftid, $notused) = $this->prepare_response_files_draft_itemid_with_text(
223 $name, $contextid, null);
224 return $draftid;
225 }
226
227 /**
228 * Prepare a draft file are for the files belonging the a response variable
229 * of this step, while rewriting the URLs in some text.
230 *
231 * @param string $name the variable name the files belong to.
232 * @param int $contextid the id of the context the quba belongs to.
233 * @param string $text the text to update the URLs in.
234 * @return array(int, string) the draft itemid and the text with URLs rewritten.
235 */
236 public function prepare_response_files_draft_itemid_with_text($name, $contextid, $text) {
237 $draftid = 0; // Will be filled in by file_prepare_draft_area.
238 $newtext = file_prepare_draft_area($draftid, $contextid, 'question',
239 'response_' . $name, $this->id, null, $text);
240 return array($draftid, $newtext);
241 }
242
243 /**
244 * Rewrite the @@PLUGINFILE@@ tokens in a response variable from this step
245 * that contains links to file. Normally you should probably call
246 * {@link question_attempt::rewrite_response_pluginfile_urls()} instead of
247 * calling this method directly.
248 *
249 * @param string $text the text to update the URLs in.
250 * @param int $contextid the id of the context the quba belongs to.
251 * @param string $name the variable name the files belong to.
252 * @param array $extra extra file path components.
253 * @return string the rewritten text.
254 */
255 public function rewrite_response_pluginfile_urls($text, $contextid, $name, $extras) {
256 return question_rewrite_question_urls($text, 'pluginfile.php', $contextid,
257 'question', 'response_' . $name, $extras, $this->id);
258 }
259
260 /**
261 * Get all the question type variables.
262 * @param array name => value pairs.
263 */
264 public function get_qt_data() {
265 $result = array();
266 foreach ($this->data as $name => $value) {
267 if ($name[0] != '-' && $name[0] != ':') {
268 $result[$name] = $value;
269 }
270 }
271 return $result;
272 }
273
274 /**
275 * @param string $name the name of an behaviour variable to look for in the submitted data.
276 * @return bool whether a variable with this name exists in the question type data.
277 */
278 public function has_behaviour_var($name) {
279 return array_key_exists('-' . $name, $this->data);
280 }
281
282 /**
283 * @param string $name the name of an behaviour variable to look for in the submitted data.
284 * @return string the requested variable, or null if the variable is not set.
285 */
286 public function get_behaviour_var($name) {
287 if (!$this->has_behaviour_var($name)) {
288 return null;
289 }
290 return $this->data['-' . $name];
291 }
292
293 /**
294 * Set a cached behaviour variable.
295 * @param string $name the name of the variable to set. Must match _[a-z][a-z0-9]*.
296 * @param string $value the value to set.
297 */
298 public function set_behaviour_var($name, $value) {
299 if ($name[0] != '_') {
9c197f44
TH
300 throw new coding_exception('Cannot set question type data ' . $name .
301 ' on an attempt step. You can only set variables with names begining with _.');
dcd03928
TH
302 }
303 return $this->data['-' . $name] = $value;
304 }
305
306 /**
307 * Get all the behaviour variables.
308 * @param array name => value pairs.
309 */
310 public function get_behaviour_data() {
311 $result = array();
312 foreach ($this->data as $name => $value) {
313 if ($name[0] == '-') {
314 $result[substr($name, 1)] = $value;
315 }
316 }
317 return $result;
318 }
319
320 /**
321 * Get all the submitted data, but not the cached data. behaviour
322 * variables have the - at the start of their name. This is only really
323 * intended for use by {@link question_attempt::regrade()}, it should not
324 * be considered part of the public API.
325 * @param array name => value pairs.
326 */
327 public function get_submitted_data() {
328 $result = array();
329 foreach ($this->data as $name => $value) {
330 if ($name[0] == '_' || ($name[0] == '-' && $name[1] == '_')) {
331 continue;
332 }
333 $result[$name] = $value;
334 }
335 return $result;
336 }
337
338 /**
339 * Get all the data. behaviour variables have the - at the start of
340 * their name. This is only intended for internal use, for example by
341 * {@link question_engine_data_mapper::insert_question_attempt_step()},
342 * however, it can ocasionally be useful in test code. It should not be
343 * considered part of the public API of this class.
344 * @param array name => value pairs.
345 */
346 public function get_all_data() {
347 return $this->data;
348 }
349
350 /**
351 * Create a question_attempt_step from records loaded from the database.
352 * @param array $records Raw records loaded from the database.
353 * @param int $stepid The id of the records to extract.
354 * @return question_attempt_step The newly constructed question_attempt_step.
355 */
356 public static function load_from_records(&$records, $attemptstepid) {
357 $currentrec = current($records);
358 while ($currentrec->attemptstepid != $attemptstepid) {
359 $currentrec = next($records);
360 if (!$currentrec) {
9c197f44
TH
361 throw new coding_exception('Question attempt step ' . $attemptstepid .
362 ' not found in the database.');
dcd03928
TH
363 }
364 }
365
366 $record = $currentrec;
367 $data = array();
368 while ($currentrec && $currentrec->attemptstepid == $attemptstepid) {
369 if ($currentrec->name) {
370 $data[$currentrec->name] = $currentrec->value;
371 }
372 $currentrec = next($records);
373 }
374
375 $step = new question_attempt_step_read_only($data, $record->timecreated, $record->userid);
376 $step->state = question_state::get($record->state);
377 $step->id = $record->attemptstepid;
378 if (!is_null($record->fraction)) {
379 $step->fraction = $record->fraction + 0;
380 }
381 return $step;
382 }
383}
384
385
386/**
387 * A subclass with a bit of additional funcitonality, for pending steps.
388 *
389 * @copyright 2010 The Open University
390 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
391 */
392class question_attempt_pending_step extends question_attempt_step {
393 /** @var string . */
394 protected $newresponsesummary = null;
395
396 /**
397 * If as a result of processing this step, the response summary for the
398 * question attempt should changed, you should call this method to set the
399 * new summary.
400 * @param string $responsesummary the new response summary.
401 */
402 public function set_new_response_summary($responsesummary) {
403 $this->newresponsesummary = $responsesummary;
404 }
405
406 /** @return string the new response summary, if any. */
407 public function get_new_response_summary() {
408 return $this->newresponsesummary;
409 }
410
411 /** @return string whether this step changes the response summary. */
412 public function response_summary_changed() {
413 return !is_null($this->newresponsesummary);
414 }
415}
416
417
418/**
419 * A subclass of {@link question_attempt_step} that cannot be modified.
420 *
421 * @copyright 2009 The Open University
422 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
423 */
424class question_attempt_step_read_only extends question_attempt_step {
425 public function set_state($state) {
426 throw new coding_exception('Cannot modify a question_attempt_step_read_only.');
427 }
428 public function set_fraction($fraction) {
429 throw new coding_exception('Cannot modify a question_attempt_step_read_only.');
430 }
431 public function set_qt_var($name, $value) {
432 throw new coding_exception('Cannot modify a question_attempt_step_read_only.');
433 }
434 public function set_behaviour_var($name, $value) {
435 throw new coding_exception('Cannot modify a question_attempt_step_read_only.');
436 }
437}
438
439
440/**
441 * A null {@link question_attempt_step} returned from
442 * {@link question_attempt::get_last_step()} etc. when a an attempt has just been
443 * created and there is no acutal step.
444 *
445 * @copyright 2009 The Open University
446 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
447 */
448class question_null_step {
449 public function get_state() {
450 return question_state::$notstarted;
451 }
452
453 public function set_state($state) {
454 throw new coding_exception('This question has not been started.');
455 }
456
457 public function get_fraction() {
458 return null;
459 }
460}