MDL-20636 improve the fomatting of the generated code.
[moodle.git] / question / engine / questionusage.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 usage 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 * This class keeps track of a group of questions that are being attempted,
32 * and which state, and so on, each one is currently in.
33 *
34 * A quiz attempt or a lesson attempt could use an instance of this class to
35 * keep track of all the questions in the attempt and process student submissions.
36 * It is basically a collection of {@question_attempt} objects.
37 *
38 * The questions being attempted as part of this usage are identified by an integer
39 * that is passed into many of the methods as $slot. ($question->id is not
40 * used so that the same question can be used more than once in an attempt.)
41 *
42 * Normally, calling code should be able to do everything it needs to be calling
43 * methods of this class. You should not normally need to get individual
44 * {@question_attempt} objects and play around with their inner workind, in code
45 * that it outside the quetsion engine.
46 *
47 * Instances of this class correspond to rows in the question_usages table.
48 *
49 * @copyright 2009 The Open University
50 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
51 */
52class question_usage_by_activity {
53 /**
54 * @var integer|string the id for this usage. If this usage was loaded from
55 * the database, then this is the database id. Otherwise a unique random
56 * string is used.
57 */
58 protected $id = null;
59
60 /**
61 * @var string name of an archetypal behaviour, that should be used
62 * by questions in this usage if possible.
63 */
64 protected $preferredbehaviour = null;
65
66 /** @var object the context this usage belongs to. */
67 protected $context;
68
69 /** @var string plugin name of the plugin this usage belongs to. */
70 protected $owningcomponent;
71
72 /** @var array {@link question_attempt}s that make up this usage. */
73 protected $questionattempts = array();
74
75 /** @var question_usage_observer that tracks changes to this usage. */
76 protected $observer;
77
78 /**
79 * Create a new instance. Normally, calling code should use
80 * {@link question_engine::make_questions_usage_by_activity()} or
81 * {@link question_engine::load_questions_usage_by_activity()} rather than
82 * calling this constructor directly.
83 *
84 * @param string $component the plugin creating this attempt. For example mod_quiz.
85 * @param object $context the context this usage belongs to.
86 */
87 public function __construct($component, $context) {
88 $this->owningcomponent = $component;
89 $this->context = $context;
90 $this->observer = new question_usage_null_observer();
91 }
92
93 /**
94 * @param string $behaviour the name of an archetypal behaviour, that should
95 * be used by questions in this usage if possible.
96 */
97 public function set_preferred_behaviour($behaviour) {
98 $this->preferredbehaviour = $behaviour;
99 $this->observer->notify_modified();
100 }
101
102 /** @return string the name of the preferred behaviour. */
103 public function get_preferred_behaviour() {
104 return $this->preferredbehaviour;
105 }
106
107 /** @return object the context this usage belongs to. */
108 public function get_owning_context() {
109 return $this->context;
110 }
111
112 /** @return string the name of the plugin that owns this attempt. */
113 public function get_owning_component() {
114 return $this->owningcomponent;
115 }
116
117 /** @return int|string If this usage came from the database, then the id
118 * from the question_usages table is returned. Otherwise a random string is
119 * returned. */
120 public function get_id() {
121 if (is_null($this->id)) {
122 $this->id = random_string(10);
123 }
124 return $this->id;
125 }
126
127 /** @return question_usage_observer that is tracking changes made to this usage. */
128 public function get_observer() {
129 return $this->observer;
130 }
131
132 /**
133 * For internal use only. Used by {@link question_engine_data_mapper} to set
134 * the id when a usage is saved to the database.
135 * @param int $id the newly determined id for this usage.
136 */
137 public function set_id_from_database($id) {
138 $this->id = $id;
139 foreach ($this->questionattempts as $qa) {
140 $qa->set_usage_id($id);
141 }
142 }
143
144 /**
145 * Add another question to this usage.
146 *
147 * The added question is not started until you call {@link start_question()}
148 * on it.
149 *
150 * @param question_definition $question the question to add.
151 * @param number $maxmark the maximum this question will be marked out of in
152 * this attempt (optional). If not given, $question->defaultmark is used.
153 * @return int the number used to identify this question within this usage.
154 */
155 public function add_question(question_definition $question, $maxmark = null) {
156 $qa = new question_attempt($question, $this->get_id(), $this->observer, $maxmark);
157 if (count($this->questionattempts) == 0) {
158 $this->questionattempts[1] = $qa;
159 } else {
160 $this->questionattempts[] = $qa;
161 }
162 $qa->set_number_in_usage(end(array_keys($this->questionattempts)));
163 $this->observer->notify_attempt_added($qa);
164 return $qa->get_slot();
165 }
166
167 /**
168 * Get the question_definition for a question in this attempt.
169 * @param int $slot the number used to identify this question within this usage.
170 * @return question_definition the requested question object.
171 */
172 public function get_question($slot) {
173 return $this->get_question_attempt($slot)->get_question();
174 }
175
176 /** @return array all the identifying numbers of all the questions in this usage. */
177 public function get_slots() {
178 return array_keys($this->questionattempts);
179 }
180
181 /** @return int the identifying number of the first question that was added to this usage. */
182 public function get_first_question_number() {
183 reset($this->questionattempts);
184 return key($this->questionattempts);
185 }
186
187 /** @return int the number of questions that are currently in this usage. */
188 public function question_count() {
189 return count($this->questionattempts);
190 }
191
192 /**
193 * Note the part of the {@link question_usage_by_activity} comment that explains
194 * that {@link question_attempt} objects should be considered part of the inner
195 * workings of the question engine, and should not, if possible, be accessed directly.
196 *
197 * @return question_attempt_iterator for iterating over all the questions being
198 * attempted. as part of this usage.
199 */
200 public function get_attempt_iterator() {
201 return new question_attempt_iterator($this);
202 }
203
204 /**
205 * Check whether $number actually corresponds to a question attempt that is
206 * part of this usage. Throws an exception if not.
207 *
208 * @param int $slot a number allegedly identifying a question within this usage.
209 */
210 protected function check_slot($slot) {
211 if (!array_key_exists($slot, $this->questionattempts)) {
9c197f44
TH
212 throw new coding_exception('There is no question_attempt number ' . $slot .
213 ' in this attempt.');
dcd03928
TH
214 }
215 }
216
217 /**
218 * Note the part of the {@link question_usage_by_activity} comment that explains
219 * that {@link question_attempt} objects should be considered part of the inner
220 * workings of the question engine, and should not, if possible, be accessed directly.
221 *
222 * @param int $slot the number used to identify this question within this usage.
223 * @return question_attempt the corresponding {@link question_attempt} object.
224 */
225 public function get_question_attempt($slot) {
226 $this->check_slot($slot);
227 return $this->questionattempts[$slot];
228 }
229
230 /**
231 * Get the current state of the attempt at a question.
232 * @param int $slot the number used to identify this question within this usage.
233 * @return question_state.
234 */
235 public function get_question_state($slot) {
236 return $this->get_question_attempt($slot)->get_state();
237 }
238
239 /**
240 * @param int $slot the number used to identify this question within this usage.
241 * @param bool $showcorrectness Whether right/partial/wrong states should
242 * be distinguised.
243 * @return string A brief textual description of the current state.
244 */
245 public function get_question_state_string($slot, $showcorrectness) {
246 return $this->get_question_attempt($slot)->get_state_string($showcorrectness);
247 }
248
249 /**
250 * Get the time of the most recent action performed on a question.
251 * @param int $slot the number used to identify this question within this usage.
252 * @return int timestamp.
253 */
254 public function get_question_action_time($slot) {
255 return $this->get_question_attempt($slot)->get_last_action_time();
256 }
257
258 /**
259 * Get the current fraction awarded for the attempt at a question.
260 * @param int $slot the number used to identify this question within this usage.
261 * @return number|null The current fraction for this question, or null if one has
262 * not been assigned yet.
263 */
264 public function get_question_fraction($slot) {
265 return $this->get_question_attempt($slot)->get_fraction();
266 }
267
268 /**
269 * Get the current mark awarded for the attempt at a question.
270 * @param int $slot the number used to identify this question within this usage.
271 * @return number|null The current mark for this question, or null if one has
272 * not been assigned yet.
273 */
274 public function get_question_mark($slot) {
275 return $this->get_question_attempt($slot)->get_mark();
276 }
277
278 /**
279 * Get the maximum mark possible for the attempt at a question.
280 * @param int $slot the number used to identify this question within this usage.
281 * @return number the available marks for this question.
282 */
283 public function get_question_max_mark($slot) {
284 return $this->get_question_attempt($slot)->get_max_mark();
285 }
286
287 /**
288 * Get the current mark awarded for the attempt at a question.
289 * @param int $slot the number used to identify this question within this usage.
290 * @return number|null The current mark for this question, or null if one has
291 * not been assigned yet.
292 */
293 public function get_total_mark() {
294 $mark = 0;
295 foreach ($this->questionattempts as $qa) {
296 if ($qa->get_max_mark() > 0 && $qa->get_state() == question_state::$needsgrading) {
297 return null;
298 }
299 $mark += $qa->get_mark();
300 }
301 return $mark;
302 }
303
304 /**
305 * @return string a simple textual summary of the question that was asked.
306 */
307 public function get_question_summary($slot) {
308 return $this->get_question_attempt($slot)->get_question_summary();
309 }
310
311 /**
312 * @return string a simple textual summary of response given.
313 */
314 public function get_response_summary($slot) {
315 return $this->get_question_attempt($slot)->get_response_summary();
316 }
317
318 /**
319 * @return string a simple textual summary of the correct resonse.
320 */
321 public function get_right_answer_summary($slot) {
322 return $this->get_question_attempt($slot)->get_right_answer_summary();
323 }
324
325 /**
326 * Get the {@link core_question_renderer}, in collaboration with appropriate
327 * {@link qbehaviour_renderer} and {@link qtype_renderer} subclasses, to generate the
328 * HTML to display this question.
329 * @param int $slot the number used to identify this question within this usage.
330 * @param question_display_options $options controls how the question is rendered.
331 * @param string|null $number The question number to display. 'i' is a special
332 * value that gets displayed as Information. Null means no number is displayed.
333 * @return string HTML fragment representing the question.
334 */
335 public function render_question($slot, $options, $number = null) {
336 $options->context = $this->context;
337 return $this->get_question_attempt($slot)->render($options, $number);
338 }
339
340 /**
341 * Generate any bits of HTML that needs to go in the <head> tag when this question
342 * is displayed in the body.
343 * @param int $slot the number used to identify this question within this usage.
344 * @return string HTML fragment.
345 */
346 public function render_question_head_html($slot) {
347 $options->context = $this->context;
348 return $this->get_question_attempt($slot)->render_head_html();
349 }
350
351 /**
352 * Like {@link render_question()} but displays the question at the past step
353 * indicated by $seq, rather than showing the latest step.
354 *
355 * @param int $slot the number used to identify this question within this usage.
356 * @param int $seq the seq number of the past state to display.
357 * @param question_display_options $options controls how the question is rendered.
358 * @param string|null $number The question number to display. 'i' is a special
359 * value that gets displayed as Information. Null means no number is displayed.
360 * @return string HTML fragment representing the question.
361 */
362 public function render_question_at_step($slot, $seq, $options, $number = null) {
363 $options->context = $this->context;
9c197f44
TH
364 return $this->get_question_attempt($slot)->render_at_step(
365 $seq, $options, $number, $this->preferredbehaviour);
dcd03928
TH
366 }
367
368 /**
369 * Checks whether the users is allow to be served a particular file.
370 * @param int $slot the number used to identify this question within this usage.
371 * @param question_display_options $options the options that control display of the question.
372 * @param string $component the name of the component we are serving files for.
373 * @param string $filearea the name of the file area.
374 * @param array $args the remaining bits of the file path.
375 * @param bool $forcedownload whether the user must be forced to download the file.
376 * @return bool true if the user can access this file.
377 */
9c197f44
TH
378 public function check_file_access($slot, $options, $component, $filearea,
379 $args, $forcedownload) {
380 return $this->get_question_attempt($slot)->check_file_access(
381 $options, $component, $filearea, $args, $forcedownload);
dcd03928
TH
382 }
383
384 /**
385 * Replace a particular question_attempt with a different one.
386 *
387 * For internal use only. Used when reloading the state of a question from the
388 * database.
389 *
390 * @param array $records Raw records loaded from the database.
391 * @param int $questionattemptid The id of the question_attempt to extract.
392 * @return question_attempt The newly constructed question_attempt_step.
393 */
394 public function replace_loaded_question_attempt_info($slot, $qa) {
395 $this->check_slot($slot);
396 $this->questionattempts[$slot] = $qa;
397 }
398
399 /**
400 * You should probably not use this method in code outside the question engine.
401 * The main reason for exposing it was for the benefit of unit tests.
402 * @param int $slot the number used to identify this question within this usage.
403 * @return string return the prefix that is pre-pended to field names in the HTML
404 * that is output.
405 */
406 public function get_field_prefix($slot) {
407 return $this->get_question_attempt($slot)->get_field_prefix();
408 }
409
410 /**
411 * Start the attempt at a question that has been added to this usage.
412 * @param int $slot the number used to identify this question within this usage.
413 */
414 public function start_question($slot) {
415 $qa = $this->get_question_attempt($slot);
416 $qa->start($this->preferredbehaviour);
417 $this->observer->notify_attempt_modified($qa);
418 }
419
420 /**
421 * Start the attempt at all questions that has been added to this usage.
e35ba43c
TH
422 * @param int $timestamp optional, the timstamp to record for this action. Defaults to now.
423 * @param int $userid optional, the user to attribute this action to. Defaults to the current user.
dcd03928 424 */
e35ba43c 425 public function start_all_questions($timestamp = null, $userid = null) {
dcd03928
TH
426 foreach ($this->questionattempts as $qa) {
427 $qa->start($this->preferredbehaviour);
428 $this->observer->notify_attempt_modified($qa);
429 }
430 }
431
432 /**
433 * Start the attempt at a question, starting from the point where the previous
434 * question_attempt $oldqa had reached. This is used by the quiz 'Each attempt
435 * builds on last' mode.
436 * @param int $slot the number used to identify this question within this usage.
437 * @param question_attempt $oldqa a previous attempt at this quetsion that
438 * defines the starting point.
439 */
440 public function start_question_based_on($slot, question_attempt $oldqa) {
441 $qa = $this->get_question_attempt($slot);
442 $qa->start_based_on($oldqa);
443 $this->observer->notify_attempt_modified($qa);
444 }
445
446 /**
447 * Process all the question actions in the current request.
448 *
449 * If there is a parameter slots included in the post data, then only
450 * those question numbers will be processed, otherwise all questions in this
451 * useage will be.
452 *
453 * This function also does {@link update_question_flags()}.
454 *
455 * @param int $timestamp optional, use this timestamp as 'now'.
456 * @param array $postdata optional, only intended for testing. Use this data
457 * instead of the data from $_POST.
458 */
459 public function process_all_actions($timestamp = null, $postdata = null) {
460 $slots = question_attempt::get_submitted_var('slots', PARAM_SEQUENCE, $postdata);
461 if (is_null($slots)) {
462 $slots = $this->get_slots();
463 } else if (!$slots) {
464 $slots = array();
465 } else {
466 $slots = explode(',', $slots);
467 }
468 foreach ($slots as $slot) {
469 if (!$this->validate_sequence_number($slot, $postdata)) {
470 continue;
471 }
472 $submitteddata = $this->extract_responses($slot, $postdata);
473 $this->process_action($slot, $submitteddata, $timestamp);
474 }
475 $this->update_question_flags($postdata);
476 }
477
478 /**
479 * Get the submitted data from the current request that belongs to this
480 * particular question.
481 *
482 * @param int $slot the number used to identify this question within this usage.
483 * @param $postdata optional, only intended for testing. Use this data
484 * instead of the data from $_POST.
485 * @return array submitted data specific to this question.
486 */
487 public function extract_responses($slot, $postdata = null) {
488 return $this->get_question_attempt($slot)->get_submitted_data($postdata);
489 }
490
491 /**
492 * Process a specific action on a specific question.
493 * @param int $slot the number used to identify this question within this usage.
494 * @param $submitteddata the submitted data that constitutes the action.
495 */
496 public function process_action($slot, $submitteddata, $timestamp = null) {
497 $qa = $this->get_question_attempt($slot);
498 $qa->process_action($submitteddata, $timestamp);
499 $this->observer->notify_attempt_modified($qa);
500 }
501
502 /**
503 * Check that the sequence number, that detects weird things like the student
504 * clicking back, is OK. If the sequence check variable is not present, returns
505 * false. If the check variable is present and correct, returns true. If the
506 * variable is present and wrong, throws an exception.
507 * @param int $slot the number used to identify this question within this usage.
508 * @param array $submitteddata the submitted data that constitutes the action.
509 * @return bool true if the check variable is present and correct. False if it
510 * is missing. (Throws an exception if the check fails.)
511 */
512 public function validate_sequence_number($slot, $postdata = null) {
513 $qa = $this->get_question_attempt($slot);
514 $sequencecheck = $qa->get_submitted_var(
515 $qa->get_control_field_name('sequencecheck'), PARAM_INT, $postdata);
516 if (is_null($sequencecheck)) {
517 return false;
518 } else if ($sequencecheck != $qa->get_num_steps()) {
519 throw new question_out_of_sequence_exception($this->id, $slot, $postdata);
520 } else {
521 return true;
522 }
523 }
524 /**
525 * Update the flagged state for all question_attempts in this usage, if their
526 * flagged state was changed in the request.
527 *
528 * @param $postdata optional, only intended for testing. Use this data
529 * instead of the data from $_POST.
530 */
531 public function update_question_flags($postdata = null) {
532 foreach ($this->questionattempts as $qa) {
533 $flagged = $qa->get_submitted_var(
534 $qa->get_flag_field_name(), PARAM_BOOL, $postdata);
535 if (!is_null($flagged) && $flagged != $qa->is_flagged()) {
536 $qa->set_flagged($flagged);
537 }
538 }
539 }
540
541 /**
542 * Get the correct response to a particular question. Passing the results of
543 * this method to {@link process_action()} will probably result in full marks.
544 * @param int $slot the number used to identify this question within this usage.
545 * @return array that constitutes a correct response to this question.
546 */
547 public function get_correct_response($slot) {
548 return $this->get_question_attempt($slot)->get_correct_response();
549 }
550
551 /**
552 * Finish the active phase of an attempt at a question.
553 *
554 * This is an external act of finishing the attempt. Think, for example, of
555 * the 'Submit all and finish' button in the quiz. Some behaviours,
556 * (for example, immediatefeedback) give a way of finishing the active phase
557 * of a question attempt as part of a {@link process_action()} call.
558 *
559 * After the active phase is over, the only changes possible are things like
560 * manual grading, or changing the flag state.
561 *
562 * @param int $slot the number used to identify this question within this usage.
563 */
564 public function finish_question($slot, $timestamp = null) {
565 $qa = $this->get_question_attempt($slot);
566 $qa->finish($timestamp);
567 $this->observer->notify_attempt_modified($qa);
568 }
569
570 /**
571 * Finish the active phase of an attempt at a question. See {@link finish_question()}
572 * for a fuller description of what 'finish' means.
573 */
574 public function finish_all_questions($timestamp = null) {
575 foreach ($this->questionattempts as $qa) {
576 $qa->finish($timestamp);
577 $this->observer->notify_attempt_modified($qa);
578 }
579 }
580
581 /**
582 * Perform a manual grading action on a question attempt.
583 * @param int $slot the number used to identify this question within this usage.
584 * @param string $comment the comment being added to the question attempt.
585 * @param number $mark the mark that is being assigned. Can be null to just
586 * add a comment.
587 */
588 public function manual_grade($slot, $comment, $mark) {
589 $qa = $this->get_question_attempt($slot);
590 $qa->manual_grade($comment, $mark);
591 $this->observer->notify_attempt_modified($qa);
592 }
593
594 /**
595 * Regrade a question in this usage. This replays the sequence of submitted
596 * actions to recompute the outcomes.
597 * @param int $slot the number used to identify this question within this usage.
598 * @param bool $finished whether the question attempt should be forced to be finished
599 * after the regrade, or whether it may still be in progress (default false).
600 * @param number $newmaxmark (optional) if given, will change the max mark while regrading.
601 */
602 public function regrade_question($slot, $finished = false, $newmaxmark = null) {
603 $oldqa = $this->get_question_attempt($slot);
604 if (is_null($newmaxmark)) {
605 $newmaxmark = $oldqa->get_max_mark();
606 }
607
608 $this->observer->notify_delete_attempt_steps($oldqa);
609
610 $newqa = new question_attempt($oldqa->get_question(), $oldqa->get_usage_id(),
611 $this->observer, $newmaxmark);
612 $newqa->set_database_id($oldqa->get_database_id());
613 $newqa->regrade($oldqa, $finished);
614
615 $this->questionattempts[$slot] = $newqa;
616 $this->observer->notify_attempt_modified($newqa);
617 }
618
619 /**
620 * Regrade all the questions in this usage (without changing their max mark).
621 * @param bool $finished whether each question should be forced to be finished
622 * after the regrade, or whether it may still be in progress (default false).
623 */
624 public function regrade_all_questions($finished = false) {
625 foreach ($this->questionattempts as $slot => $notused) {
626 $this->regrade_question($slot, $finished);
627 }
628 }
629
630 /**
631 * Create a question_usage_by_activity from records loaded from the database.
632 *
633 * For internal use only.
634 *
635 * @param array $records Raw records loaded from the database.
636 * @param int $questionattemptid The id of the question_attempt to extract.
637 * @return question_attempt The newly constructed question_attempt_step.
638 */
639 public static function load_from_records(&$records, $qubaid) {
640 $record = current($records);
641 while ($record->qubaid != $qubaid) {
642 $record = next($records);
643 if (!$record) {
644 throw new coding_exception("Question usage $qubaid not found in the database.");
645 }
646 }
647
648 $quba = new question_usage_by_activity($record->component,
649 get_context_instance_by_id($record->contextid));
650 $quba->set_id_from_database($record->qubaid);
651 $quba->set_preferred_behaviour($record->preferredbehaviour);
652
653 $quba->observer = new question_engine_unit_of_work($quba);
654
655 while ($record && $record->qubaid == $qubaid && !is_null($record->slot)) {
656 $quba->questionattempts[$record->slot] =
657 question_attempt::load_from_records($records,
658 $record->questionattemptid, $quba->observer,
659 $quba->get_preferred_behaviour());
660 $record = current($records);
661 }
662
663 return $quba;
664 }
665}
666
667
668/**
669 * A class abstracting access to the
670 * {@link question_usage_by_activity::$questionattempts} array.
671 *
672 * This class snapshots the list of {@link question_attempts} to iterate over
673 * when it is created. If a question is added to the usage mid-iteration, it
674 * will now show up.
675 *
676 * To create an instance of this class, use
677 * {@link question_usage_by_activity::get_attempt_iterator()}
678 *
679 * @copyright 2009 The Open University
680 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
681 */
682class question_attempt_iterator implements Iterator, ArrayAccess {
683 /** @var question_usage_by_activity that we are iterating over. */
684 protected $quba;
685 /** @var array of question numbers. */
686 protected $slots;
687
688 /**
9c197f44
TH
689 * To create an instance of this class, use
690 * {@link question_usage_by_activity::get_attempt_iterator()}.
dcd03928
TH
691 * @param $quba the usage to iterate over.
692 */
693 public function __construct(question_usage_by_activity $quba) {
694 $this->quba = $quba;
695 $this->slots = $quba->get_slots();
696 $this->rewind();
697 }
698
699 /** @return question_attempt_step */
700 public function current() {
701 return $this->offsetGet(current($this->slots));
702 }
703 /** @return int */
704 public function key() {
705 return current($this->slots);
706 }
707 public function next() {
708 next($this->slots);
709 }
710 public function rewind() {
711 reset($this->slots);
712 }
713 /** @return bool */
714 public function valid() {
715 return current($this->slots) !== false;
716 }
717
718 /** @return bool */
719 public function offsetExists($slot) {
720 return in_array($slot, $this->slots);
721 }
722 /** @return question_attempt_step */
723 public function offsetGet($slot) {
724 return $this->quba->get_question_attempt($slot);
725 }
726 public function offsetSet($slot, $value) {
9c197f44
TH
727 throw new coding_exception('You are only allowed read-only access to ' .
728 'question_attempt::states through a question_attempt_step_iterator. Cannot set.');
dcd03928
TH
729 }
730 public function offsetUnset($slot) {
9c197f44
TH
731 throw new coding_exception('You are only allowed read-only access to ' .
732 'question_attempt::states through a question_attempt_step_iterator. Cannot unset.');
dcd03928
TH
733 }
734}
735
736
737/**
738 * Interface for things that want to be notified of signficant changes to a
739 * {@link question_usage_by_activity}.
740 *
741 * A question behaviour controls the flow of actions a student can
742 * take as they work through a question, and later, as a teacher manually grades it.
743 *
744 * @copyright 2009 The Open University
745 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
746 */
747interface question_usage_observer {
748 /** Called when a field of the question_usage_by_activity is changed. */
749 public function notify_modified();
750
751 /**
752 * Called when the fields of a question attempt in this usage are modified.
753 * @param question_attempt $qa the newly added question attempt.
754 */
755 public function notify_attempt_modified(question_attempt $qa);
756
757 /**
758 * Called when a new question attempt is added to this usage.
759 * @param question_attempt $qa the newly added question attempt.
760 */
761 public function notify_attempt_added(question_attempt $qa);
762
763 /**
764 * Called we want to delete the old step records for an attempt, prior to
765 * inserting newones. This is used by regrading.
766 * @param question_attempt $qa the question attempt to delete the steps for.
767 */
768 public function notify_delete_attempt_steps(question_attempt $qa);
769
770 /**
771 * Called when a new step is added to a question attempt in this usage.
772 * @param $step the new step.
773 * @param $qa the usage it is being added to.
774 * @param $seq the sequence number of the new step.
775 */
776 public function notify_step_added(question_attempt_step $step, question_attempt $qa, $seq);
777}
778
779
780/**
781 * Null implmentation of the {@link question_usage_watcher} interface.
782 * Does nothing.
783 *
784 * @copyright 2009 The Open University
785 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
786 */
787class question_usage_null_observer implements question_usage_observer {
788 public function notify_modified() {
789 }
790 public function notify_attempt_modified(question_attempt $qa) {
791 }
792 public function notify_attempt_added(question_attempt $qa) {
793 }
794 public function notify_delete_attempt_steps(question_attempt $qa) {
795 }
796 public function notify_step_added(question_attempt_step $step, question_attempt $qa, $seq) {
797 }
798}