Document print_side_block, and eliminate a bit of duplicated code.
[moodle.git] / mod / quiz / attemptlib.php
CommitLineData
36e413e3 1<?php
36e413e3 2/**
3 * This class handles loading all the information about a quiz attempt into memory,
4 * and making it available for attemtp.php, summary.php and review.php.
5 * Initially, it only loads a minimal amout of information about each attempt - loading
6 * extra information only when necessary or when asked. The class tracks which questions
7 * are loaded.
b10c38a3 8 *//** */
36e413e3 9
66d07f81 10if (!defined('MOODLE_INTERNAL')) {
11 die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page.
12}
36e413e3 13
14/**
b10c38a3 15 * Class for quiz exceptions. Just saves a couple of arguments on the
16 * constructor for a moodle_exception.
36e413e3 17 */
18class moodle_quiz_exception extends moodle_exception {
19 function __construct($quizobj, $errorcode, $a = NULL, $link = '', $debuginfo = null) {
20 if (!$link) {
21 $link = $quizobj->view_url();
22 }
23 parent::__construct($errorcode, 'quiz', $link, $a, $debuginfo);
24 }
25}
26
b10c38a3 27/**
28 * A base class for holding and accessing information about a quiz and its questions,
29 * before details of a particular attempt are loaded.
30 */
36e413e3 31class quiz {
32 // Fields initialised in the constructor.
33 protected $course;
34 protected $cm;
35 protected $quiz;
36 protected $context;
78e7a3dd 37 protected $questionids; // All question ids in order that they appear in the quiz.
38 protected $pagequestionids; // array page no => array of questionids on the page in order.
36e413e3 39
40 // Fields set later if that data is needed.
78e7a3dd 41 protected $questions = null;
36e413e3 42 protected $accessmanager = null;
b10c38a3 43 protected $ispreviewuser = null;
36e413e3 44
45 // Constructor =========================================================================
b10c38a3 46 /**
47 * Constructor, assuming we already have the necessary data loaded.
48 *
49 * @param object $quiz the row from the quiz table.
50 * @param object $cm the course_module object for this quiz.
51 * @param object $course the row from the course table for the course we belong to.
52 */
36e413e3 53 function __construct($quiz, $cm, $course) {
54 $this->quiz = $quiz;
55 $this->cm = $cm;
56 $this->course = $course;
57 $this->context = get_context_instance(CONTEXT_MODULE, $cm->id);
78e7a3dd 58 $this->determine_layout();
36e413e3 59 }
60
61 // Functions for loading more data =====================================================
62 public function load_questions_on_page($page) {
78e7a3dd 63 $this->load_questions($this->pagequestionids[$page]);
64 }
65
66 public function preload_questions() {
67 if (empty($this->questionids)) {
68 throw new moodle_quiz_exception($this, 'noquestions', $this->edit_url());
69 }
70 $this->questions = question_preload_questions($this->questionids,
71 'qqi.grade AS maxgrade, qqi.id AS instance',
72 '{quiz_question_instances} qqi ON qqi.quiz = :quizid AND q.id = qqi.question',
73 array('quizid' => $this->quiz->id));
74 $this->number_questions();
36e413e3 75 }
76
77 /**
78 * Load some or all of the queestions for this quiz.
79 *
78e7a3dd 80 * @param array $questionids question ids of the questions to load. null for all.
36e413e3 81 */
78e7a3dd 82 public function load_questions($questionids = null) {
83 if (is_null($questionids)) {
84 $questionids = $this->questionids;
85 }
86 $questionstoprocess = array();
87 foreach ($questionids as $id) {
88 $questionstoprocess[$id] = $this->questions[$id];
36e413e3 89 }
78e7a3dd 90 if (!get_question_options($questionstoprocess)) {
91 throw new moodle_quiz_exception($this, 'loadingquestionsfailed', implode(', ', $questionids));
36e413e3 92 }
36e413e3 93 }
94
95 // Simple getters ======================================================================
b10c38a3 96 /** @return integer the course id. */
36e413e3 97 public function get_courseid() {
98 return $this->course->id;
99 }
100
78e7a3dd 101 /** @return object the row of the course table. */
102 public function get_course() {
103 return $this->course;
104 }
105
b10c38a3 106 /** @return integer the quiz id. */
36e413e3 107 public function get_quizid() {
108 return $this->quiz->id;
109 }
110
b10c38a3 111 /** @return object the row of the quiz table. */
36e413e3 112 public function get_quiz() {
113 return $this->quiz;
114 }
115
b10c38a3 116 /** @return string the name of this quiz. */
36e413e3 117 public function get_quiz_name() {
118 return $this->quiz->name;
119 }
120
b9b3aa94 121 /** @return integer the number of attempts allowed at this quiz (0 = infinite). */
122 public function get_num_attempts_allowed() {
123 return $this->quiz->attempts;
124 }
125
b10c38a3 126 /** @return integer the course_module id. */
36e413e3 127 public function get_cmid() {
128 return $this->cm->id;
129 }
130
b10c38a3 131 /** @return object the course_module object. */
36e413e3 132 public function get_cm() {
133 return $this->cm;
134 }
135
b10c38a3 136 /**
137 * @return boolean wether the current user is someone who previews the quiz,
138 * rather than attempting it.
139 */
140 public function is_preview_user() {
141 if (is_null($this->ispreviewuser)) {
142 $this->ispreviewuser = has_capability('mod/quiz:preview', $this->context);
143 }
144 return $this->ispreviewuser;
145 }
146
78e7a3dd 147 /**
148 * @return integer number fo pages in this quiz.
149 */
150 public function get_num_pages() {
151 return count($this->pagequestionids);
152 }
153
154
155 /**
156 * @param int $page page number
157 * @return boolean true if this is the last page of the quiz.
158 */
159 public function is_last_page($page) {
160 return $page == count($this->pagequestionids) - 1;
161 }
162
b10c38a3 163 /**
164 * @param integer $id the question id.
165 * @return object the question object with that id.
166 */
36e413e3 167 public function get_question($id) {
168 $this->ensure_question_loaded($id);
169 return $this->questions[$id];
170 }
171
78e7a3dd 172 /**
173 * @param array $questionids question ids of the questions to load. null for all.
174 */
175 public function get_questions($questionids = null) {
176 if (is_null($questionids)) {
177 $questionids = $this->questionids;
178 }
179 $questions = array();
180 foreach ($questionids as $id) {
181 $questions[$id] = $this->questions[$id];
182 $this->ensure_question_loaded($id);
183 }
184 return $questions;
185 }
186
187 /**
188 * Return the list of question ids for either a given page of the quiz, or for the
189 * whole quiz.
190 *
191 * @param mixed $page string 'all' or integer page number.
192 * @return array the reqested list of question ids.
193 */
194 public function get_question_ids($page = 'all') {
793f3a0e 195 if ($page === 'all') {
78e7a3dd 196 $list = $this->questionids;
197 } else {
e153801c 198 $list = $this->pagequestionids[$page];
78e7a3dd 199 }
200 // Clone the array, so our private arrays cannot be modified.
201 $result = array();
202 foreach ($list as $id) {
203 $result[] = $id;
204 }
205 return $result;
206 }
207
b10c38a3 208 /**
209 * @param integer $timenow the current time as a unix timestamp.
210 * @return object and instance of the quiz_access_manager class for this quiz at this time.
211 */
36e413e3 212 public function get_access_manager($timenow) {
213 if (is_null($this->accessmanager)) {
78e7a3dd 214 $this->accessmanager = new quiz_access_manager($this, $timenow,
36e413e3 215 has_capability('mod/quiz:ignoretimelimits', $this->context, NULL, false));
216 }
217 return $this->accessmanager;
218 }
219
78e7a3dd 220 /**
221 * Wrapper round the has_capability funciton that automatically passes in the quiz context.
222 */
223 public function has_capability($capability, $userid = NULL, $doanything = true) {
224 return has_capability($capability, $this->context, $userid, $doanything);
225 }
226
227 /**
228 * Wrapper round the require_capability funciton that automatically passes in the quiz context.
229 */
230 public function require_capability($capability, $userid = NULL, $doanything = true) {
231 return require_capability($capability, $this->context, $userid, $doanything);
232 }
233
36e413e3 234 // URLs related to this attempt ========================================================
b10c38a3 235 /**
236 * @return string the URL of this quiz's view page.
237 */
36e413e3 238 public function view_url() {
239 global $CFG;
240 return $CFG->wwwroot . '/mod/quiz/view.php?id=' . $this->cm->id;
241 }
242
78e7a3dd 243 /**
244 * @return string the URL of this quiz's edit page.
245 */
246 public function edit_url() {
247 global $CFG;
248 return $CFG->wwwroot . '/mod/quiz/edit.php?cmid=' . $this->cm->id;
249 }
250
251 /**
252 * @param integer $attemptid the id of an attempt.
253 * @return string the URL of that attempt.
254 */
255 public function attempt_url($attemptid) {
256 global $CFG;
257 return $CFG->wwwroot . '/mod/quiz/attempt.php?attempt=' . $attemptid . '&amp;page=0';
258 }
259
260 /**
261 * @return string the URL of this quiz's edit page. Needs to be POSTed to with a cmid parameter.
262 */
263 public function start_attempt_url() {
264 global $CFG;
265 return $CFG->wwwroot . '/mod/quiz/startattempt.php';
266 }
267
268 /**
269 * @param integer $attemptid the id of an attempt.
270 * @return string the URL of the review of that attempt.
271 */
272 public function review_url($attemptid) {
273 global $CFG;
274 return $CFG->wwwroot . '/mod/quiz/review.php?attempt=' . $attemptid;
275 }
276
36e413e3 277 // Bits of content =====================================================================
b10c38a3 278 /**
279 * @return string the HTML snipped that needs to be supplied to print_header_simple
280 * as the $button parameter.
281 */
36e413e3 282 public function update_module_button() {
283 if (has_capability('moodle/course:manageactivities',
284 get_context_instance(CONTEXT_COURSE, $this->course->id))) {
285 return update_module_button($this->cm->id, $this->course->id, get_string('modulename', 'quiz'));
286 } else {
287 return '';
288 }
289 }
290
b10c38a3 291 /**
292 * @param string $title the name of this particular quiz page.
293 * @return array the data that needs to be sent to print_header_simple as the $navigation
294 * parameter.
295 */
36e413e3 296 public function navigation($title) {
297 return build_navigation($title, $this->cm);
298 }
299
300 // Private methods =====================================================================
b10c38a3 301 // Check that the definition of a particular question is loaded, and if not throw an exception.
78e7a3dd 302 protected function ensure_question_loaded($id) {
303 if (isset($this->questions[$id]->_partiallyloaded)) {
36e413e3 304 throw new moodle_quiz_exception($this, 'questionnotloaded', $id);
305 }
306 }
78e7a3dd 307
308 private function determine_layout() {
309 $this->questionids = array();
310 $this->pagequestionids = array();
311
312 // Get the appropriate layout string (from quiz or attempt).
313 $layout = $this->get_layout_string();
314 if (empty($layout)) {
315 // Nothing to do.
316 return;
317 }
318
319 // Break up the layout string into pages.
320 $pagelayouts = explode(',0', $layout);
321
322 // Strip off any empty last page (normally there is one).
323 if (end($pagelayouts) == '') {
324 array_pop($pagelayouts);
325 }
326
327 // File the ids into the arrays.
328 $this->questionids = array();
329 $this->pagequestionids = array();
330 foreach ($pagelayouts as $page => $pagelayout) {
331 $pagelayout = trim($pagelayout, ',');
332 if ($pagelayout == '') continue;
333 $this->pagequestionids[$page] = explode(',', $pagelayout);
334 foreach ($this->pagequestionids[$page] as $id) {
335 $this->questionids[] = $id;
336 }
337 }
338 }
339
340 // Number the questions.
341 private function number_questions() {
342 $number = 1;
343 foreach ($this->pagequestionids as $page => $questionids) {
344 foreach ($questionids as $id) {
345 if ($this->questions[$id]->length > 0) {
346 $this->questions[$id]->_number = $number;
347 $number += $this->questions[$id]->length;
348 } else {
349 $this->questions[$id]->_number = get_string('infoshort', 'quiz');
350 }
351 $this->questions[$id]->_page = $page;
352 }
353 }
354 }
355
356 /**
357 * @return string the layout of this quiz. Used by number_questions to
358 * work out which questions are on which pages.
359 */
360 protected function get_layout_string() {
361 return $this->quiz->questions;
362 }
36e413e3 363}
364
b10c38a3 365/**
366 * This class extends the quiz class to hold data about the state of a particular attempt,
367 * in addition to the data about the quiz.
368 */
36e413e3 369class quiz_attempt extends quiz {
370 // Fields initialised in the constructor.
371 protected $attempt;
372
373 // Fields set later if that data is needed.
36e413e3 374 protected $states = array();
4fc3d7e5 375 protected $reviewoptions = null;
36e413e3 376
377 // Constructor =========================================================================
b10c38a3 378 /**
379 * Constructor from just an attemptid.
380 *
381 * @param integer $attemptid the id of the attempt to load. We automatically load the
382 * associated quiz, course, etc.
383 */
36e413e3 384 function __construct($attemptid) {
385 global $DB;
386 if (!$this->attempt = quiz_load_attempt($attemptid)) {
387 throw new moodle_exception('invalidattemptid', 'quiz');
388 }
389 if (!$quiz = $DB->get_record('quiz', array('id' => $this->attempt->quiz))) {
390 throw new moodle_exception('invalidquizid', 'quiz');
391 }
392 if (!$course = $DB->get_record('course', array('id' => $quiz->course))) {
393 throw new moodle_exception('invalidcoursemodule');
394 }
395 if (!$cm = get_coursemodule_from_instance('quiz', $quiz->id, $course->id)) {
396 throw new moodle_exception('invalidcoursemodule');
397 }
398 parent::__construct($quiz, $cm, $course);
78e7a3dd 399 $this->preload_questions();
36e413e3 400 }
401
402 // Functions for loading more data =====================================================
36e413e3 403 /**
78e7a3dd 404 * Load the state of a number of questions that have already been loaded.
36e413e3 405 *
78e7a3dd 406 * @param array $questionids question ids to process. Blank = all.
36e413e3 407 */
78e7a3dd 408 public function load_question_states($questionids = null) {
409 if (is_null($questionids)) {
410 $questionids = $this->questionids;
36e413e3 411 }
78e7a3dd 412 $questionstoprocess = array();
413 foreach ($questionids as $id) {
414 $this->ensure_question_loaded($id);
415 $questionstoprocess[$id] = $this->questions[$id];
416 }
417 if (!$newstates = get_question_states($questionstoprocess, $this->quiz, $this->attempt)) {
36e413e3 418 throw new moodle_quiz_exception($this, 'cannotrestore');
419 }
66d07f81 420 $this->states = $newstates + $this->states;
36e413e3 421 }
422
36e413e3 423 // Simple getters ======================================================================
b10c38a3 424 /** @return integer the attempt id. */
36e413e3 425 public function get_attemptid() {
426 return $this->attempt->id;
427 }
428
b10c38a3 429 /** @return object the row from the quiz_attempts table. */
36e413e3 430 public function get_attempt() {
431 return $this->attempt;
432 }
433
78e7a3dd 434 /** @return integer the number of this attemp (is it this user's first, second, ... attempt). */
435 public function get_attempt_number() {
436 return $this->attempt->attempt;
437 }
438
b10c38a3 439 /** @return integer the id of the user this attempt belongs to. */
36e413e3 440 public function get_userid() {
441 return $this->attempt->userid;
442 }
443
b10c38a3 444 /** @return boolean whether this attemp has been finished (true) or is still in progress (false). */
36e413e3 445 public function is_finished() {
446 return $this->attempt->timefinish != 0;
447 }
448
4fc3d7e5 449 /** @return boolean whether this attemp is a preview attempt. */
450 public function is_preview() {
451 return $this->attempt->preview;
452 }
453
78e7a3dd 454 public function get_question_state($questionid) {
455 $this->ensure_state_loaded($questionid);
456 return $this->states[$questionid];
457 }
458
b10c38a3 459 /**
460 * Wrapper that calls quiz_get_reviewoptions with the appropriate arguments.
461 *
4fc3d7e5 462 * @return object the review options for this user on this attempt.
b10c38a3 463 */
36e413e3 464 public function get_review_options() {
465 if (is_null($this->reviewoptions)) {
466 $this->reviewoptions = quiz_get_reviewoptions($this->quiz, $this->attempt, $this->context);
467 }
468 return $this->reviewoptions;
469 }
470
4fc3d7e5 471 /**
472 * Wrapper that calls get_render_options with the appropriate arguments.
473 *
474 * @return object the render options for this user on this attempt.
475 */
476 public function get_render_options($state) {
477 return quiz_get_renderoptions($this->quiz->review, $state);
478 }
479
b10c38a3 480 /**
481 * Get a quiz_attempt_question_iterator for either a page of the quiz, or a whole quiz.
482 * You must have called load_questions with an appropriate argument first.
483 *
484 * @param mixed $page as for the @see{get_question_ids} method.
485 * @return quiz_attempt_question_iterator the requested iterator.
486 */
36e413e3 487 public function get_question_iterator($page = 'all') {
488 return new quiz_attempt_question_iterator($this, $page);
489 }
490
b10c38a3 491 /**
492 * Return a summary of the current state of a question in this attempt. You must previously
493 * have called load_question_states to load the state data about this question.
494 *
495 * @param integer $questionid question id of a question that belongs to this quiz.
496 * @return string a brief string (that could be used as a CSS class name, for example)
497 * that describes the current state of a question in this attempt. Possible results are:
498 * open|saved|closed|correct|partiallycorrect|incorrect.
499 */
36e413e3 500 public function get_question_status($questionid) {
b10c38a3 501 $this->ensure_state_loaded($questionid);
502 $state = $this->states[$questionid];
503 switch ($state->event) {
504 case QUESTION_EVENTOPEN:
505 return 'open';
506
507 case QUESTION_EVENTSAVE:
508 case QUESTION_EVENTGRADE:
66d07f81 509 return 'answered';
b10c38a3 510
511 case QUESTION_EVENTCLOSEANDGRADE:
512 case QUESTION_EVENTCLOSE:
513 case QUESTION_EVENTMANUALGRADE:
514 $options = quiz_get_renderoptions($this->quiz->review, $this->states[$questionid]);
515 if ($options->scores) {
516 return question_get_feedback_class($state->last_graded->raw_grade /
517 $this->questions[$questionid]->maxgrade);
518 } else {
519 return 'closed';
520 }
521
522 default:
523 $a = new stdClass;
524 $a->event = $state->event;
525 $a->questionid = $questionid;
526 $a->attemptid = $this->attempt->id;
527 throw new moodle_quiz_exception($this, 'errorunexpectedevent', $a);
528 }
36e413e3 529 }
530
531 /**
b10c38a3 532 * Return the grade obtained on a particular question, if the user is permitted to see it.
533 * You must previously have called load_question_states to load the state data about this question.
36e413e3 534 *
b10c38a3 535 * @param integer $questionid question id of a question that belongs to this quiz.
36e413e3 536 * @return string the formatted grade, to the number of decimal places specified by the quiz.
537 */
538 public function get_question_score($questionid) {
539 $this->ensure_state_loaded($questionid);
540 $options = quiz_get_renderoptions($this->quiz->review, $this->states[$questionid]);
541 if ($options->scores) {
542 return round($this->states[$questionid]->last_graded->grade, $this->quiz->decimalpoints);
543 } else {
544 return '';
545 }
546 }
547
548 // URLs related to this attempt ========================================================
b10c38a3 549 /**
550 * @param integer $page if specified, the URL of this particular page of the attempt, otherwise
551 * the URL will go to the first page.
78e7a3dd 552 * @param integer $questionid a question id. If set, will add a fragment to the URL
b10c38a3 553 * to jump to a particuar question on the page.
554 * @return string the URL to continue this attempt.
555 */
78e7a3dd 556 public function attempt_url($questionid = 0, $page = -1) {
36e413e3 557 global $CFG;
78e7a3dd 558 return $CFG->wwwroot . '/mod/quiz/attempt.php?attempt=' . $this->attempt->id . '&' .
559 $this->page_and_question_fragment($questionid, $page);
36e413e3 560 }
561
b10c38a3 562 /**
563 * @return string the URL of this quiz's summary page.
564 */
36e413e3 565 public function summary_url() {
566 global $CFG;
567 return $CFG->wwwroot . '/mod/quiz/summary.php?attempt=' . $this->attempt->id;
568 }
569
b10c38a3 570 /**
571 * @param integer $page if specified, the URL of this particular page of the attempt, otherwise
572 * the URL will go to the first page.
78e7a3dd 573 * @param integer $questionid a question id. If set, will add a fragment to the URL
b10c38a3 574 * to jump to a particuar question on the page.
575 * @param boolean $showall if true, the URL will be to review the entire attempt on one page,
576 * and $page will be ignored.
b9b3aa94 577 * @param $otherattemptid if given, link to another attempt, instead of the one we represent.
b10c38a3 578 * @return string the URL to review this attempt.
579 */
4fc3d7e5 580 public function review_url($questionid = 0, $page = -1, $showall = false, $otherattemptid = null) {
36e413e3 581 global $CFG;
4fc3d7e5 582 if (is_null($otherattemptid)) {
583 $otherattemptid = $this->attempt->id;
584 }
585 return $CFG->wwwroot . '/mod/quiz/review.php?attempt=' . $otherattemptid . '&' .
78e7a3dd 586 $this->page_and_question_fragment($questionid, $page, $showall);
587 }
588
589 // Bits of content =====================================================================
590 public function get_html_head_contributions($page = 'all') {
591 return get_html_head_contributions($this->get_question_ids($page),
592 $this->questions, $this->states);
593 }
594
595 public function print_restart_preview_button() {
596 global $CFG;
597 echo '<div class="controls">';
598 print_single_button($this->start_attempt_url(), array('cmid' => $this->cm->id,
599 'forcenew' => true, 'sesskey' => sesskey()), get_string('startagain', 'quiz'), 'post');
600 echo '</div>';
36e413e3 601 }
602
78e7a3dd 603 public function print_question($id) {
4fc3d7e5 604 if ($this->is_finished()) {
605 $options = $this->get_review_options();
606 } else {
607 $options = $this->get_render_options($this->states[$id]);
608 }
78e7a3dd 609 print_question($this->questions[$id], $this->states[$id], $this->questions[$id]->_number,
610 $this->quiz, $options);
611 }
612
613 public function quiz_send_notification_emails() {
614 quiz_send_notification_emails($this->course, $this->quiz, $this->attempt,
615 $this->context, $this->cm);
616 }
617
36e413e3 618
619 // Private methods =====================================================================
b10c38a3 620 // Check that the state of a particular question is loaded, and if not throw an exception.
36e413e3 621 private function ensure_state_loaded($id) {
622 if (!array_key_exists($id, $this->states)) {
623 throw new moodle_quiz_exception($this, 'statenotloaded', $id);
624 }
625 }
78e7a3dd 626
627 /**
628 * @return string the layout of this quiz. Used by number_questions to
629 * work out which questions are on which pages.
630 */
631 protected function get_layout_string() {
632 return $this->attempt->layout;
633 }
634
635 /**
636 * Enter description here...
637 *
638 * @param unknown_type $questionid the id of a particular question on the page to jump to.
639 * @param integer $page -1 to look up the page number from the questionid, otherwise the page number to use.
640 * @param boolean $showall
641 * @return string bit to add to the end of a URL.
642 */
643 private function page_and_question_fragment($questionid, $page, $showall = false) {
644 if ($page = -1) {
645 if ($questionid) {
646 $page = $this->questions[$questionid]->_page;
647 } else {
648 $page = 0;
649 }
650 }
651 if ($showall) {
652 $page = 0;
653 }
654 $fragment = '';
655 if ($questionid && $questionid != reset($this->pagequestionids[$page])) {
656 $fragment = '#q' . $questionid;
657 }
658 $param = '';
659 if ($showall) {
660 $param = 'showall=1';
661 } else if (/*$page > 1*/ true) {
662 // TODO currently needed by the navigate JS, but clean this up later.
663 $param = 'page=' . $page;
664 }
665 return $param . $fragment;
666 }
36e413e3 667}
668
b10c38a3 669/**
670 * A PHP Iterator for conviniently looping over the questions in a quiz. The keys are the question
671 * numbers (with 'i' for descriptions) and the values are the question objects.
672 */
36e413e3 673class quiz_attempt_question_iterator implements Iterator {
b10c38a3 674 private $attemptobj; // Reference to the quiz_attempt object we provide access to.
675 private $questionids; // Array of the question ids within that attempt we are iterating over.
676
677 /**
678 * Constructor. Normally, you don't want to call this directly. Instead call
679 * quiz_attempt::get_question_iterator
680 *
681 * @param quiz_attempt $attemptobj the quiz_attempt object we will be providing access to.
682 * @param mixed $page as for @see{quiz_attempt::get_question_iterator}.
683 */
36e413e3 684 public function __construct(quiz_attempt $attemptobj, $page = 'all') {
685 $this->attemptobj = $attemptobj;
36e413e3 686 $this->questionids = $attemptobj->get_question_ids($page);
687 }
688
b10c38a3 689 // Implementation of the Iterator interface ============================================
36e413e3 690 public function rewind() {
691 reset($this->questionids);
692 }
693
694 public function current() {
695 $id = current($this->questionids);
696 if ($id) {
697 return $this->attemptobj->get_question($id);
698 } else {
699 return false;
700 }
701 }
702
703 public function key() {
704 $id = current($this->questionids);
705 if ($id) {
78e7a3dd 706 return $this->attemptobj->get_question($id)->_number;
36e413e3 707 } else {
708 return false;
709 }
36e413e3 710 }
711
712 public function next() {
713 $id = next($this->questionids);
714 if ($id) {
715 return $this->attemptobj->get_question($id);
716 } else {
717 return false;
718 }
719 }
720
721 public function valid() {
722 return $this->current() !== false;
723 }
724}
725?>