MDL-42181 quiz nav: highight all buttons when all Qs on one page
[moodle.git] / mod / quiz / attemptlib.php
CommitLineData
36e413e3 1<?php
d18675a8 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
36e413e3 17/**
d18675a8 18 * Back-end code for handling data about quizzes and the current user's attempt.
19 *
20 * There are classes for loading all the information about a quiz and attempts,
21 * and for displaying the navigation panel.
22 *
34b7d838
TH
23 * @package mod_quiz
24 * @copyright 2008 onwards Tim Hunt
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
d18675a8 26 */
36e413e3 27
36e413e3 28
a17b297d 29defined('MOODLE_INTERNAL') || die();
a1eb3a44
TH
30
31
36e413e3 32/**
b10c38a3 33 * Class for quiz exceptions. Just saves a couple of arguments on the
34 * constructor for a moodle_exception.
d18675a8 35 *
34b7d838
TH
36 * @copyright 2008 Tim Hunt
37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38 * @since Moodle 2.0
36e413e3 39 */
40class moodle_quiz_exception extends moodle_exception {
25a03faa 41 public function __construct($quizobj, $errorcode, $a = null, $link = '', $debuginfo = null) {
36e413e3 42 if (!$link) {
43 $link = $quizobj->view_url();
44 }
45 parent::__construct($errorcode, 'quiz', $link, $a, $debuginfo);
46 }
47}
48
f7970e3c 49
b10c38a3 50/**
d18675a8 51 * A class encapsulating a quiz and the questions it contains, and making the
52 * information available to scripts like view.php.
53 *
54 * Initially, it only loads a minimal amout of information about each question - loading
55 * extra information only when necessary or when asked. The class tracks which questions
56 * are loaded.
57 *
f7970e3c
TH
58 * @copyright 2008 Tim Hunt
59 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
60 * @since Moodle 2.0
b10c38a3 61 */
36e413e3 62class quiz {
63 // Fields initialised in the constructor.
64 protected $course;
65 protected $cm;
66 protected $quiz;
67 protected $context;
a1eb3a44 68 protected $questionids;
7b6757b0 69
36e413e3 70 // Fields set later if that data is needed.
78e7a3dd 71 protected $questions = null;
36e413e3 72 protected $accessmanager = null;
b10c38a3 73 protected $ispreviewuser = null;
36e413e3 74
9e83f3d1 75 // Constructor =============================================================
b10c38a3 76 /**
77 * Constructor, assuming we already have the necessary data loaded.
78 *
79 * @param object $quiz the row from the quiz table.
80 * @param object $cm the course_module object for this quiz.
81 * @param object $course the row from the course table for the course we belong to.
f7970e3c 82 * @param bool $getcontext intended for testing - stops the constructor getting the context.
b10c38a3 83 */
c7df5006 84 public function __construct($quiz, $cm, $course, $getcontext = true) {
36e413e3 85 $this->quiz = $quiz;
86 $this->cm = $cm;
a18ba12c 87 $this->quiz->cmid = $this->cm->id;
36e413e3 88 $this->course = $course;
739b0711 89 if ($getcontext && !empty($cm->id)) {
c492a78e 90 $this->context = context_module::instance($cm->id);
739b0711 91 }
389cb589
TH
92 $questionids = quiz_questions_in_quiz($this->quiz->questions);
93 if ($questionids) {
94 $this->questionids = explode(',', quiz_questions_in_quiz($this->quiz->questions));
95 } else {
96 $this->questionids = array(); // Which idiot made explode(',', '') = array('')?
97 }
36e413e3 98 }
99
990650f9
TH
100 /**
101 * Static function to create a new quiz object for a specific user.
102 *
f7970e3c
TH
103 * @param int $quizid the the quiz id.
104 * @param int $userid the the userid.
a88ba570 105 * @return quiz the new quiz object
990650f9 106 */
c7df5006 107 public static function create($quizid, $userid) {
990650f9
TH
108 global $DB;
109
dd70d561 110 $quiz = quiz_access_manager::load_quiz_and_settings($quizid);
74df2951 111 $course = $DB->get_record('course', array('id' => $quiz->course), '*', MUST_EXIST);
88f0eb15 112 $cm = get_coursemodule_from_instance('quiz', $quiz->id, $course->id, false, MUST_EXIST);
990650f9 113
9e83f3d1 114 // Update quiz with override information.
990650f9
TH
115 $quiz = quiz_update_effective_access($quiz, $userid);
116
117 return new quiz($quiz, $cm, $course);
118 }
119
34b7d838
TH
120 /**
121 * Create a {@link quiz_attempt} for an attempt at this quiz.
122 * @param object $attemptdata row from the quiz_attempts table.
123 * @return quiz_attempt the new quiz_attempt object.
124 */
125 public function create_attempt_object($attemptdata) {
126 return new quiz_attempt($attemptdata, $this->quiz, $this->cm, $this->course);
127 }
128
9e83f3d1 129 // Functions for loading more data =========================================
78e7a3dd 130
d18675a8 131 /**
132 * Load just basic information about all the questions in this quiz.
133 */
78e7a3dd 134 public function preload_questions() {
135 if (empty($this->questionids)) {
136 throw new moodle_quiz_exception($this, 'noquestions', $this->edit_url());
137 }
138 $this->questions = question_preload_questions($this->questionids,
a1eb3a44 139 'qqi.grade AS maxmark, qqi.id AS instance',
78e7a3dd 140 '{quiz_question_instances} qqi ON qqi.quiz = :quizid AND q.id = qqi.question',
141 array('quizid' => $this->quiz->id));
36e413e3 142 }
143
25a03faa
TH
144 /**
145 * Fully load some or all of the questions for this quiz. You must call
146 * {@link preload_questions()} first.
36e413e3 147 *
78e7a3dd 148 * @param array $questionids question ids of the questions to load. null for all.
36e413e3 149 */
78e7a3dd 150 public function load_questions($questionids = null) {
151 if (is_null($questionids)) {
152 $questionids = $this->questionids;
153 }
154 $questionstoprocess = array();
155 foreach ($questionids as $id) {
a1eb3a44
TH
156 if (array_key_exists($id, $this->questions)) {
157 $questionstoprocess[$id] = $this->questions[$id];
158 }
36e413e3 159 }
a8a8ec51 160 get_question_options($questionstoprocess);
36e413e3 161 }
162
9e83f3d1 163 // Simple getters ==========================================================
f7970e3c 164 /** @return int the course id. */
36e413e3 165 public function get_courseid() {
166 return $this->course->id;
167 }
168
78e7a3dd 169 /** @return object the row of the course table. */
170 public function get_course() {
171 return $this->course;
172 }
173
f7970e3c 174 /** @return int the quiz id. */
36e413e3 175 public function get_quizid() {
176 return $this->quiz->id;
177 }
178
b10c38a3 179 /** @return object the row of the quiz table. */
36e413e3 180 public function get_quiz() {
181 return $this->quiz;
182 }
183
b10c38a3 184 /** @return string the name of this quiz. */
36e413e3 185 public function get_quiz_name() {
186 return $this->quiz->name;
187 }
188
33c8d37b
CF
189 /** @return int the quiz navigation method. */
190 public function get_navigation_method() {
191 return $this->quiz->navmethod;
192 }
193
f7970e3c 194 /** @return int the number of attempts allowed at this quiz (0 = infinite). */
b9b3aa94 195 public function get_num_attempts_allowed() {
196 return $this->quiz->attempts;
197 }
198
f7970e3c 199 /** @return int the course_module id. */
36e413e3 200 public function get_cmid() {
201 return $this->cm->id;
202 }
203
b10c38a3 204 /** @return object the course_module object. */
36e413e3 205 public function get_cm() {
206 return $this->cm;
207 }
208
a1eb3a44
TH
209 /** @return object the module context for this quiz. */
210 public function get_context() {
211 return $this->context;
212 }
213
b10c38a3 214 /**
f7970e3c 215 * @return bool wether the current user is someone who previews the quiz,
b10c38a3 216 * rather than attempting it.
217 */
218 public function is_preview_user() {
219 if (is_null($this->ispreviewuser)) {
220 $this->ispreviewuser = has_capability('mod/quiz:preview', $this->context);
221 }
222 return $this->ispreviewuser;
223 }
224
78e7a3dd 225 /**
a1eb3a44 226 * @return whether any questions have been added to this quiz.
78e7a3dd 227 */
a1eb3a44
TH
228 public function has_questions() {
229 return !empty($this->questionids);
78e7a3dd 230 }
231
b10c38a3 232 /**
f7970e3c 233 * @param int $id the question id.
b10c38a3 234 * @return object the question object with that id.
235 */
36e413e3 236 public function get_question($id) {
36e413e3 237 return $this->questions[$id];
238 }
239
78e7a3dd 240 /**
241 * @param array $questionids question ids of the questions to load. null for all.
242 */
243 public function get_questions($questionids = null) {
244 if (is_null($questionids)) {
245 $questionids = $this->questionids;
246 }
247 $questions = array();
248 foreach ($questionids as $id) {
a1eb3a44
TH
249 if (!array_key_exists($id, $this->questions)) {
250 throw new moodle_exception('cannotstartmissingquestion', 'quiz', $this->view_url());
251 }
78e7a3dd 252 $questions[$id] = $this->questions[$id];
253 $this->ensure_question_loaded($id);
254 }
255 return $questions;
256 }
257
b10c38a3 258 /**
f7970e3c 259 * @param int $timenow the current time as a unix timestamp.
25a03faa
TH
260 * @return quiz_access_manager and instance of the quiz_access_manager class
261 * for this quiz at this time.
b10c38a3 262 */
36e413e3 263 public function get_access_manager($timenow) {
264 if (is_null($this->accessmanager)) {
78e7a3dd 265 $this->accessmanager = new quiz_access_manager($this, $timenow,
25a03faa 266 has_capability('mod/quiz:ignoretimelimits', $this->context, null, false));
36e413e3 267 }
268 return $this->accessmanager;
269 }
270
78e7a3dd 271 /**
272 * Wrapper round the has_capability funciton that automatically passes in the quiz context.
273 */
25a03faa 274 public function has_capability($capability, $userid = null, $doanything = true) {
78e7a3dd 275 return has_capability($capability, $this->context, $userid, $doanything);
276 }
277
278 /**
279 * Wrapper round the require_capability funciton that automatically passes in the quiz context.
280 */
25a03faa 281 public function require_capability($capability, $userid = null, $doanything = true) {
78e7a3dd 282 return require_capability($capability, $this->context, $userid, $doanything);
283 }
284
9e83f3d1 285 // URLs related to this attempt ============================================
b10c38a3 286 /**
287 * @return string the URL of this quiz's view page.
288 */
36e413e3 289 public function view_url() {
290 global $CFG;
291 return $CFG->wwwroot . '/mod/quiz/view.php?id=' . $this->cm->id;
292 }
293
78e7a3dd 294 /**
295 * @return string the URL of this quiz's edit page.
296 */
297 public function edit_url() {
298 global $CFG;
299 return $CFG->wwwroot . '/mod/quiz/edit.php?cmid=' . $this->cm->id;
300 }
301
302 /**
f7970e3c 303 * @param int $attemptid the id of an attempt.
da729916 304 * @param int $page optional page number to go to in the attempt.
78e7a3dd 305 * @return string the URL of that attempt.
306 */
da729916 307 public function attempt_url($attemptid, $page = 0) {
78e7a3dd 308 global $CFG;
da729916
TH
309 $url = $CFG->wwwroot . '/mod/quiz/attempt.php?attempt=' . $attemptid;
310 if ($page) {
311 $url .= '&page=' . $page;
312 }
313 return $url;
78e7a3dd 314 }
315
316 /**
317 * @return string the URL of this quiz's edit page. Needs to be POSTed to with a cmid parameter.
318 */
da729916
TH
319 public function start_attempt_url($page = 0) {
320 $params = array('cmid' => $this->cm->id, 'sesskey' => sesskey());
321 if ($page) {
322 $params['page'] = $page;
323 }
324 return new moodle_url('/mod/quiz/startattempt.php', $params);
78e7a3dd 325 }
326
327 /**
f7970e3c 328 * @param int $attemptid the id of an attempt.
78e7a3dd 329 * @return string the URL of the review of that attempt.
330 */
331 public function review_url($attemptid) {
ea9dbf24 332 return new moodle_url('/mod/quiz/review.php', array('attempt' => $attemptid));
78e7a3dd 333 }
334
34b7d838
TH
335 /**
336 * @param int $attemptid the id of an attempt.
337 * @return string the URL of the review of that attempt.
338 */
339 public function summary_url($attemptid) {
340 return new moodle_url('/mod/quiz/summary.php', array('attempt' => $attemptid));
341 }
342
9e83f3d1 343 // Bits of content =========================================================
36e413e3 344
d755b0f5
TH
345 /**
346 * @param bool $unfinished whether there is currently an unfinished attempt active.
347 * @return string if the quiz policies merit it, return a warning string to
348 * be displayed in a javascript alert on the start attempt button.
349 */
350 public function confirm_start_attempt_message($unfinished) {
351 if ($unfinished) {
352 return '';
353 }
354
355 if ($this->quiz->timelimit && $this->quiz->attempts) {
356 return get_string('confirmstartattempttimelimit', 'quiz', $this->quiz->attempts);
357 } else if ($this->quiz->timelimit) {
358 return get_string('confirmstarttimelimit', 'quiz');
359 } else if ($this->quiz->attempts) {
360 return get_string('confirmstartattemptlimit', 'quiz', $this->quiz->attempts);
361 }
362
363 return '';
364 }
365
366 /**
367 * If $reviewoptions->attempt is false, meaning that students can't review this
368 * attempt at the moment, return an appropriate string explaining why.
369 *
370 * @param int $when One of the mod_quiz_display_options::DURING,
371 * IMMEDIATELY_AFTER, LATER_WHILE_OPEN or AFTER_CLOSE constants.
372 * @param bool $short if true, return a shorter string.
373 * @return string an appropraite message.
374 */
375 public function cannot_review_message($when, $short = false) {
376
377 if ($short) {
378 $langstrsuffix = 'short';
379 $dateformat = get_string('strftimedatetimeshort', 'langconfig');
380 } else {
381 $langstrsuffix = '';
382 $dateformat = '';
383 }
384
385 if ($when == mod_quiz_display_options::DURING ||
386 $when == mod_quiz_display_options::IMMEDIATELY_AFTER) {
387 return '';
388 } else if ($when == mod_quiz_display_options::LATER_WHILE_OPEN && $this->quiz->timeclose &&
389 $this->quiz->reviewattempt & mod_quiz_display_options::AFTER_CLOSE) {
390 return get_string('noreviewuntil' . $langstrsuffix, 'quiz',
391 userdate($this->quiz->timeclose, $dateformat));
392 } else {
393 return get_string('noreview' . $langstrsuffix, 'quiz');
394 }
395 }
396
b10c38a3 397 /**
398 * @param string $title the name of this particular quiz page.
399 * @return array the data that needs to be sent to print_header_simple as the $navigation
400 * parameter.
401 */
36e413e3 402 public function navigation($title) {
2698e9c1 403 global $PAGE;
404 $PAGE->navbar->add($title);
405 return '';
36e413e3 406 }
407
9e83f3d1 408 // Private methods =========================================================
d18675a8 409 /**
25a03faa
TH
410 * Check that the definition of a particular question is loaded, and if not throw an exception.
411 * @param $id a questionid.
d18675a8 412 */
78e7a3dd 413 protected function ensure_question_loaded($id) {
414 if (isset($this->questions[$id]->_partiallyloaded)) {
36e413e3 415 throw new moodle_quiz_exception($this, 'questionnotloaded', $id);
416 }
417 }
418}
419
f7970e3c 420
b10c38a3 421/**
422 * This class extends the quiz class to hold data about the state of a particular attempt,
423 * in addition to the data about the quiz.
d18675a8 424 *
f7970e3c
TH
425 * @copyright 2008 Tim Hunt
426 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
427 * @since Moodle 2.0
b10c38a3 428 */
a1eb3a44 429class quiz_attempt {
2de9be52
TH
430
431 /** @var string to identify the in progress state. */
432 const IN_PROGRESS = 'inprogress';
433 /** @var string to identify the overdue state. */
434 const OVERDUE = 'overdue';
435 /** @var string to identify the finished state. */
436 const FINISHED = 'finished';
437 /** @var string to identify the abandoned state. */
438 const ABANDONED = 'abandoned';
439
9e83f3d1 440 // Basic data.
a1eb3a44 441 protected $quizobj;
36e413e3 442 protected $attempt;
443
32953f37 444 /** @var question_usage_by_activity the question usage for this quiz attempt. */
2de9be52 445 protected $quba;
32953f37
TH
446
447 /** @var array page no => array of slot numbers on the page in order. */
448 protected $pagelayout;
449
450 /** @var array slot => displayed question number for this slot. (E.g. 1, 2, 3 or 'i'.) */
451 protected $questionnumbers;
452
453 /** @var array slot => page number for this slot. */
454 protected $questionpages;
455
456 /** @var mod_quiz_display_options cache for the appropriate review options. */
4fc3d7e5 457 protected $reviewoptions = null;
36e413e3 458
9e83f3d1 459 // Constructor =============================================================
b10c38a3 460 /**
990650f9
TH
461 * Constructor assuming we already have the necessary data loaded.
462 *
463 * @param object $attempt the row of the quiz_attempts table.
464 * @param object $quiz the quiz object for this attempt and user.
465 * @param object $cm the course_module object for this quiz.
466 * @param object $course the row from the course table for the course we belong to.
08502b57
TH
467 * @param bool $loadquestions (optional) if true, the default, load all the details
468 * of the state of each question. Else just set up the basic details of the attempt.
990650f9 469 */
08502b57 470 public function __construct($attempt, $quiz, $cm, $course, $loadquestions = true) {
990650f9 471 $this->attempt = $attempt;
a1eb3a44 472 $this->quizobj = new quiz($quiz, $cm, $course);
08502b57
TH
473
474 if (!$loadquestions) {
475 return;
476 }
477
a1eb3a44
TH
478 $this->quba = question_engine::load_questions_usage_by_activity($this->attempt->uniqueid);
479 $this->determine_layout();
480 $this->number_questions();
990650f9
TH
481 }
482
483 /**
56e82d99
TH
484 * Used by {create()} and {create_from_usage_id()}.
485 * @param array $conditions passed to $DB->get_record('quiz_attempts', $conditions).
b10c38a3 486 */
c7df5006 487 protected static function create_helper($conditions) {
36e413e3 488 global $DB;
990650f9 489
88f0eb15 490 $attempt = $DB->get_record('quiz_attempts', $conditions, '*', MUST_EXIST);
dd70d561 491 $quiz = quiz_access_manager::load_quiz_and_settings($attempt->quiz);
74df2951 492 $course = $DB->get_record('course', array('id' => $quiz->course), '*', MUST_EXIST);
88f0eb15 493 $cm = get_coursemodule_from_instance('quiz', $quiz->id, $course->id, false, MUST_EXIST);
a1eb3a44 494
9e83f3d1 495 // Update quiz with override information.
990650f9
TH
496 $quiz = quiz_update_effective_access($quiz, $attempt->userid);
497
498 return new quiz_attempt($attempt, $quiz, $cm, $course);
36e413e3 499 }
500
56e82d99
TH
501 /**
502 * Static function to create a new quiz_attempt object given an attemptid.
503 *
f7970e3c 504 * @param int $attemptid the attempt id.
56e82d99
TH
505 * @return quiz_attempt the new quiz_attempt object
506 */
c7df5006 507 public static function create($attemptid) {
56e82d99
TH
508 return self::create_helper(array('id' => $attemptid));
509 }
510
511 /**
512 * Static function to create a new quiz_attempt object given a usage id.
513 *
f7970e3c 514 * @param int $usageid the attempt usage id.
56e82d99
TH
515 * @return quiz_attempt the new quiz_attempt object
516 */
c7df5006 517 public static function create_from_usage_id($usageid) {
56e82d99
TH
518 return self::create_helper(array('uniqueid' => $usageid));
519 }
520
b39b5d77
TH
521 /**
522 * @param string $state one of the state constants like IN_PROGRESS.
523 * @return string the human-readable state name.
524 */
525 public static function state_name($state) {
be18f589 526 return quiz_attempt_state_name($state);
b39b5d77
TH
527 }
528
a1eb3a44
TH
529 private function determine_layout() {
530 $this->pagelayout = array();
531
532 // Break up the layout string into pages.
533 $pagelayouts = explode(',0', quiz_clean_layout($this->attempt->layout, true));
534
535 // Strip off any empty last page (normally there is one).
536 if (end($pagelayouts) == '') {
537 array_pop($pagelayouts);
36e413e3 538 }
a1eb3a44
TH
539
540 // File the ids into the arrays.
541 $this->pagelayout = array();
542 foreach ($pagelayouts as $page => $pagelayout) {
543 $pagelayout = trim($pagelayout, ',');
544 if ($pagelayout == '') {
545 continue;
546 }
547 $this->pagelayout[$page] = explode(',', $pagelayout);
78e7a3dd 548 }
a1eb3a44
TH
549 }
550
551 // Number the questions.
552 private function number_questions() {
553 $number = 1;
554 foreach ($this->pagelayout as $page => $slots) {
555 foreach ($slots as $slot) {
556 $question = $this->quba->get_question($slot);
557 if ($question->length > 0) {
32953f37 558 $this->questionnumbers[$slot] = $number;
a1eb3a44
TH
559 $number += $question->length;
560 } else {
32953f37 561 $this->questionnumbers[$slot] = get_string('infoshort', 'quiz');
a1eb3a44 562 }
32953f37 563 $this->questionpages[$slot] = $page;
a1eb3a44 564 }
36e413e3 565 }
81d833ad 566 }
567
ff2ec2cb
TH
568 /**
569 * If the given page number is out of range (before the first page, or after
570 * the last page, chnage it to be within range).
571 * @param int $page the requested page number.
572 * @return int a safe page number to use.
573 */
574 public function force_page_number_into_range($page) {
575 return min(max($page, 0), count($this->pagelayout) - 1);
576 }
577
9e83f3d1 578 // Simple getters ==========================================================
a1eb3a44
TH
579 public function get_quiz() {
580 return $this->quizobj->get_quiz();
581 }
582
583 public function get_quizobj() {
584 return $this->quizobj;
585 }
586
f7970e3c 587 /** @return int the course id. */
a1eb3a44
TH
588 public function get_courseid() {
589 return $this->quizobj->get_courseid();
590 }
591
f7970e3c 592 /** @return int the course id. */
a1eb3a44
TH
593 public function get_course() {
594 return $this->quizobj->get_course();
595 }
596
f7970e3c 597 /** @return int the quiz id. */
a1eb3a44
TH
598 public function get_quizid() {
599 return $this->quizobj->get_quizid();
600 }
601
602 /** @return string the name of this quiz. */
603 public function get_quiz_name() {
604 return $this->quizobj->get_quiz_name();
605 }
606
33c8d37b
CF
607 /** @return int the quiz navigation method. */
608 public function get_navigation_method() {
609 return $this->quizobj->get_navigation_method();
610 }
611
a1eb3a44
TH
612 /** @return object the course_module object. */
613 public function get_cm() {
614 return $this->quizobj->get_cm();
615 }
616
617 /** @return object the course_module object. */
618 public function get_cmid() {
619 return $this->quizobj->get_cmid();
620 }
621
d18675a8 622 /**
f7970e3c 623 * @return bool wether the current user is someone who previews the quiz,
a1eb3a44 624 * rather than attempting it.
d18675a8 625 */
a1eb3a44
TH
626 public function is_preview_user() {
627 return $this->quizobj->is_preview_user();
628 }
629
f7970e3c 630 /** @return int the number of attempts allowed at this quiz (0 = infinite). */
a1eb3a44
TH
631 public function get_num_attempts_allowed() {
632 return $this->quizobj->get_num_attempts_allowed();
633 }
634
f7970e3c 635 /** @return int number fo pages in this quiz. */
a1eb3a44
TH
636 public function get_num_pages() {
637 return count($this->pagelayout);
36e413e3 638 }
639
d18675a8 640 /**
f7970e3c 641 * @param int $timenow the current time as a unix timestamp.
25a03faa
TH
642 * @return quiz_access_manager and instance of the quiz_access_manager class
643 * for this quiz at this time.
d18675a8 644 */
a1eb3a44
TH
645 public function get_access_manager($timenow) {
646 return $this->quizobj->get_access_manager($timenow);
b55797b8 647 }
648
f7970e3c 649 /** @return int the attempt id. */
36e413e3 650 public function get_attemptid() {
651 return $this->attempt->id;
652 }
653
f7970e3c 654 /** @return int the attempt unique id. */
766df8f7 655 public function get_uniqueid() {
656 return $this->attempt->uniqueid;
657 }
658
b10c38a3 659 /** @return object the row from the quiz_attempts table. */
36e413e3 660 public function get_attempt() {
661 return $this->attempt;
662 }
663
f7970e3c 664 /** @return int the number of this attemp (is it this user's first, second, ... attempt). */
78e7a3dd 665 public function get_attempt_number() {
666 return $this->attempt->attempt;
667 }
668
96e5168c 669 /** @return string one of the quiz_attempt::IN_PROGRESS, FINISHED, OVERDUE or ABANDONED constants. */
2de9be52
TH
670 public function get_state() {
671 return $this->attempt->state;
672 }
673
f7970e3c 674 /** @return int the id of the user this attempt belongs to. */
36e413e3 675 public function get_userid() {
676 return $this->attempt->userid;
677 }
c5fd1682 678
5db82949
CF
679 /** @return int the current page of the attempt. */
680 public function get_currentpage() {
681 return $this->attempt->currentpage;
682 }
36e413e3 683
2de9be52
TH
684 public function get_sum_marks() {
685 return $this->attempt->sumgrades;
686 }
687
25a03faa
TH
688 /**
689 * @return bool whether this attempt has been finished (true) or is still
34b7d838
TH
690 * in progress (false). Be warned that this is not just state == self::FINISHED,
691 * it also includes self::ABANDONED.
25a03faa 692 */
36e413e3 693 public function is_finished() {
34b7d838 694 return $this->attempt->state == self::FINISHED || $this->attempt->state == self::ABANDONED;
36e413e3 695 }
696
f7970e3c 697 /** @return bool whether this attempt is a preview attempt. */
4fc3d7e5 698 public function is_preview() {
699 return $this->attempt->preview;
700 }
701
b55797b8 702 /**
703 * Is this a student dealing with their own attempt/teacher previewing,
704 * or someone with 'mod/quiz:viewreports' reviewing someone elses attempt.
7b6757b0 705 *
f7970e3c 706 * @return bool whether this situation should be treated as someone looking at their own
b55797b8 707 * attempt. The distinction normally only matters when an attempt is being reviewed.
708 */
709 public function is_own_attempt() {
710 global $USER;
711 return $this->attempt->userid == $USER->id &&
712 (!$this->is_preview_user() || $this->attempt->preview);
b55797b8 713 }
714
b3782c71
TH
715 /**
716 * @return bool whether this attempt is a preview belonging to the current user.
717 */
718 public function is_own_preview() {
719 global $USER;
720 return $this->attempt->userid == $USER->id &&
721 $this->is_preview_user() && $this->attempt->preview;
722 }
723
8032cd79
TH
724 /**
725 * Is the current user allowed to review this attempt. This applies when
726 * {@link is_own_attempt()} returns false.
727 * @return bool whether the review should be allowed.
728 */
729 public function is_review_allowed() {
730 if (!$this->has_capability('mod/quiz:viewreports')) {
731 return false;
732 }
733
734 $cm = $this->get_cm();
735 if ($this->has_capability('moodle/site:accessallgroups') ||
736 groups_get_activity_groupmode($cm) != SEPARATEGROUPS) {
737 return true;
738 }
739
740 // Check the users have at least one group in common.
741 $teachersgroups = groups_get_activity_allowed_groups($cm);
25a03faa
TH
742 $studentsgroups = groups_get_all_groups(
743 $cm->course, $this->attempt->userid, $cm->groupingid);
8032cd79
TH
744 return $teachersgroups && $studentsgroups &&
745 array_intersect(array_keys($teachersgroups), array_keys($studentsgroups));
746 }
747
474aa125
TH
748 /**
749 * Get extra summary information about this attempt.
750 *
751 * Some behaviours may be able to provide interesting summary information
752 * about the attempt as a whole, and this method provides access to that data.
753 * To see how this works, try setting a quiz to one of the CBM behaviours,
754 * and then look at the extra information displayed at the top of the quiz
755 * review page once you have sumitted an attempt.
756 *
757 * In the return value, the array keys are identifiers of the form
758 * qbehaviour_behaviourname_meaningfullkey. For qbehaviour_deferredcbm_highsummary.
759 * The values are arrays with two items, title and content. Each of these
760 * will be either a string, or a renderable.
761 *
474aa125
TH
762 * @return array as described above.
763 */
764 public function get_additional_summary_data(question_display_options $options) {
765 return $this->quba->get_summary_information($options);
766 }
767
b2607ccc
TH
768 /**
769 * Get the overall feedback corresponding to a particular mark.
770 * @param $grade a particular grade.
771 */
772 public function get_overall_feedback($grade) {
773 return quiz_feedback_for_grade($grade, $this->get_quiz(),
2709ee45 774 $this->quizobj->get_context());
b2607ccc
TH
775 }
776
a1eb3a44
TH
777 /**
778 * Wrapper round the has_capability funciton that automatically passes in the quiz context.
779 */
25a03faa 780 public function has_capability($capability, $userid = null, $doanything = true) {
a1eb3a44
TH
781 return $this->quizobj->has_capability($capability, $userid, $doanything);
782 }
783
784 /**
785 * Wrapper round the require_capability funciton that automatically passes in the quiz context.
786 */
25a03faa 787 public function require_capability($capability, $userid = null, $doanything = true) {
a1eb3a44
TH
788 return $this->quizobj->require_capability($capability, $userid, $doanything);
789 }
790
96c7d771 791 /**
792 * Check the appropriate capability to see whether this user may review their own attempt.
793 * If not, prints an error.
794 */
795 public function check_review_capability() {
796 if (!$this->has_capability('mod/quiz:viewreports')) {
a1eb3a44 797 if ($this->get_attempt_state() == mod_quiz_display_options::IMMEDIATELY_AFTER) {
96c7d771 798 $this->require_capability('mod/quiz:attempt');
799 } else {
800 $this->require_capability('mod/quiz:reviewmyattempts');
801 }
802 }
803 }
804
33c8d37b
CF
805 /**
806 * Checks whether a user may navigate to a particular slot
807 */
808 public function can_navigate_to($slot) {
809 switch ($this->get_navigation_method()) {
810 case QUIZ_NAVMETHOD_FREE:
811 return true;
812 break;
813 case QUIZ_NAVMETHOD_SEQ:
814 return false;
815 break;
816 }
817 return true;
818 }
819
d18675a8 820 /**
f7970e3c 821 * @return int one of the mod_quiz_display_options::DURING,
a1eb3a44 822 * IMMEDIATELY_AFTER, LATER_WHILE_OPEN or AFTER_CLOSE constants.
d18675a8 823 */
a1eb3a44
TH
824 public function get_attempt_state() {
825 return quiz_attempt_state($this->get_quiz(), $this->attempt);
78e7a3dd 826 }
827
b10c38a3 828 /**
a1eb3a44
TH
829 * Wrapper that the correct mod_quiz_display_options for this quiz at the
830 * moment.
b10c38a3 831 *
a1eb3a44 832 * @return question_display_options the render options for this user on this attempt.
b10c38a3 833 */
a1eb3a44
TH
834 public function get_display_options($reviewing) {
835 if ($reviewing) {
836 if (is_null($this->reviewoptions)) {
7ee80cab 837 $this->reviewoptions = quiz_get_review_options($this->get_quiz(),
a1eb3a44
TH
838 $this->attempt, $this->quizobj->get_context());
839 }
840 return $this->reviewoptions;
841
842 } else {
843 $options = mod_quiz_display_options::make_from_quiz($this->get_quiz(),
844 mod_quiz_display_options::DURING);
845 $options->flags = quiz_get_flag_option($this->attempt, $this->quizobj->get_context());
846 return $options;
36e413e3 847 }
36e413e3 848 }
849
da729916
TH
850 /**
851 * Wrapper that the correct mod_quiz_display_options for this quiz at the
852 * moment.
853 *
854 * @param bool $reviewing true for review page, else attempt page.
855 * @param int $slot which question is being displayed.
856 * @param moodle_url $thispageurl to return to after the editing form is
857 * submitted or cancelled. If null, no edit link will be generated.
858 *
859 * @return question_display_options the render options for this user on this
860 * attempt, with extra info to generate an edit link, if applicable.
861 */
862 public function get_display_options_with_edit_link($reviewing, $slot, $thispageurl) {
863 $options = clone($this->get_display_options($reviewing));
864
865 if (!$thispageurl) {
866 return $options;
867 }
868
869 if (!($reviewing || $this->is_preview())) {
870 return $options;
871 }
872
873 $question = $this->quba->get_question($slot);
874 if (!question_has_capability_on($question, 'edit', $question->category)) {
875 return $options;
876 }
877
878 $options->editquestionparams['cmid'] = $this->get_cmid();
879 $options->editquestionparams['returnurl'] = $thispageurl;
880
881 return $options;
882 }
883
4fc3d7e5 884 /**
a1eb3a44 885 * @param int $page page number
f7970e3c 886 * @return bool true if this is the last page of the quiz.
4fc3d7e5 887 */
a1eb3a44
TH
888 public function is_last_page($page) {
889 return $page == count($this->pagelayout) - 1;
4fc3d7e5 890 }
891
b10c38a3 892 /**
a1eb3a44
TH
893 * Return the list of question ids for either a given page of the quiz, or for the
894 * whole quiz.
b10c38a3 895 *
a1eb3a44
TH
896 * @param mixed $page string 'all' or integer page number.
897 * @return array the reqested list of question ids.
b10c38a3 898 */
a1eb3a44 899 public function get_slots($page = 'all') {
a1eb3a44
TH
900 if ($page === 'all') {
901 $numbers = array();
902 foreach ($this->pagelayout as $numbersonpage) {
903 $numbers = array_merge($numbers, $numbersonpage);
904 }
905 return $numbers;
906 } else {
907 return $this->pagelayout[$page];
908 }
36e413e3 909 }
910
b10c38a3 911 /**
a1eb3a44 912 * Get the question_attempt object for a particular question in this attempt.
f7970e3c 913 * @param int $slot the number used to identify this question within this attempt.
a1eb3a44 914 * @return question_attempt
b10c38a3 915 */
a1eb3a44
TH
916 public function get_question_attempt($slot) {
917 return $this->quba->get_question_attempt($slot);
918 }
b10c38a3 919
a1eb3a44
TH
920 /**
921 * Is a particular question in this attempt a real question, or something like a description.
f7970e3c
TH
922 * @param int $slot the number used to identify this question within this attempt.
923 * @return bool whether that question is a real question.
a1eb3a44
TH
924 */
925 public function is_real_question($slot) {
926 return $this->quba->get_question($slot)->length != 0;
36e413e3 927 }
928
62e76c67 929 /**
a1eb3a44 930 * Is a particular question in this attempt a real question, or something like a description.
f7970e3c
TH
931 * @param int $slot the number used to identify this question within this attempt.
932 * @return bool whether that question is a real question.
62e76c67 933 */
a1eb3a44
TH
934 public function is_question_flagged($slot) {
935 return $this->quba->get_question_attempt($slot)->is_flagged();
62e76c67 936 }
937
36e413e3 938 /**
f7970e3c 939 * @param int $slot the number used to identify this question within this attempt.
32953f37
TH
940 * @return string the displayed question number for the question in this slot.
941 * For example '1', '2', '3' or 'i'.
36e413e3 942 */
a1eb3a44 943 public function get_question_number($slot) {
32953f37
TH
944 return $this->questionnumbers[$slot];
945 }
946
947 /**
948 * @param int $slot the number used to identify this question within this attempt.
949 * @return int the page of the quiz this question appears on.
950 */
951 public function get_question_page($slot) {
952 return $this->questionpages[$slot];
a1eb3a44
TH
953 }
954
955 /**
25a03faa
TH
956 * Return the grade obtained on a particular question, if the user is permitted
957 * to see it. You must previously have called load_question_states to load the
958 * state data about this question.
a1eb3a44 959 *
f7970e3c 960 * @param int $slot the number used to identify this question within this attempt.
25a03faa
TH
961 * @return string the formatted grade, to the number of decimal places specified
962 * by the quiz.
a1eb3a44
TH
963 */
964 public function get_question_name($slot) {
965 return $this->quba->get_question($slot)->name;
966 }
967
968 /**
25a03faa
TH
969 * Return the grade obtained on a particular question, if the user is permitted
970 * to see it. You must previously have called load_question_states to load the
971 * state data about this question.
a1eb3a44 972 *
f7970e3c
TH
973 * @param int $slot the number used to identify this question within this attempt.
974 * @param bool $showcorrectness Whether right/partial/wrong states should
a1eb3a44 975 * be distinguised.
25a03faa
TH
976 * @return string the formatted grade, to the number of decimal places specified
977 * by the quiz.
a1eb3a44
TH
978 */
979 public function get_question_status($slot, $showcorrectness) {
980 return $this->quba->get_question_state_string($slot, $showcorrectness);
981 }
982
97cdc1de
TH
983 /**
984 * Return the grade obtained on a particular question, if the user is permitted
985 * to see it. You must previously have called load_question_states to load the
986 * state data about this question.
987 *
988 * @param int $slot the number used to identify this question within this attempt.
989 * @param bool $showcorrectness Whether right/partial/wrong states should
990 * be distinguised.
991 * @return string class name for this state.
992 */
993 public function get_question_state_class($slot, $showcorrectness) {
994 return $this->quba->get_question_state_class($slot, $showcorrectness);
995 }
996
a1eb3a44
TH
997 /**
998 * Return the grade obtained on a particular question.
999 * You must previously have called load_question_states to load the state
1000 * data about this question.
1001 *
f7970e3c 1002 * @param int $slot the number used to identify this question within this attempt.
a1eb3a44
TH
1003 * @return string the formatted grade, to the number of decimal places specified by the quiz.
1004 */
b2607ccc 1005 public function get_question_mark($slot) {
a1eb3a44
TH
1006 return quiz_format_question_grade($this->get_quiz(), $this->quba->get_question_mark($slot));
1007 }
1008
1009 /**
1010 * Get the time of the most recent action performed on a question.
f7970e3c
TH
1011 * @param int $slot the number used to identify this question within this usage.
1012 * @return int timestamp.
a1eb3a44
TH
1013 */
1014 public function get_question_action_time($slot) {
1015 return $this->quba->get_question_action_time($slot);
36e413e3 1016 }
1017
2b2b6458
TH
1018 /**
1019 * Get the time remaining for an in-progress attempt, if the time is short
1020 * enought that it would be worth showing a timer.
1021 * @param int $timenow the time to consider as 'now'.
1022 * @return int|false the number of seconds remaining for this attempt.
1023 * False if there is no limit.
1024 */
8e771aed 1025 public function get_time_left_display($timenow) {
2b2b6458
TH
1026 if ($this->attempt->state != self::IN_PROGRESS) {
1027 return false;
1028 }
8e771aed 1029 return $this->get_access_manager($timenow)->get_time_left_display($this->attempt, $timenow);
2b2b6458 1030 }
2de9be52 1031
8e771aed 1032
2de9be52
TH
1033 /**
1034 * @return int the time when this attempt was submitted. 0 if it has not been
1035 * submitted yet.
1036 */
1037 public function get_submitted_date() {
1038 return $this->attempt->timefinish;
1039 }
1040
1041 /**
1042 * If the attempt is in an applicable state, work out the time by which the
1043 * student should next do something.
1044 * @return int timestamp by which the student needs to do something.
1045 */
9e83f3d1 1046 public function get_due_date() {
2b2b6458
TH
1047 $deadlines = array();
1048 if ($this->quizobj->get_quiz()->timelimit) {
1049 $deadlines[] = $this->attempt->timestart + $this->quizobj->get_quiz()->timelimit;
1050 }
1051 if ($this->quizobj->get_quiz()->timeclose) {
1052 $deadlines[] = $this->quizobj->get_quiz()->timeclose;
1053 }
1054 if ($deadlines) {
1055 $duedate = min($deadlines);
1056 } else {
1057 return false;
1058 }
2de9be52 1059
2b2b6458 1060 switch ($this->attempt->state) {
2de9be52 1061 case self::IN_PROGRESS:
2b2b6458 1062 return $duedate;
2de9be52
TH
1063
1064 case self::OVERDUE:
2b2b6458 1065 return $duedate + $this->quizobj->get_quiz()->graceperiod;
2de9be52
TH
1066
1067 default:
2b2b6458 1068 throw new coding_exception('Unexpected state: ' . $this->attempt->state);
2de9be52
TH
1069 }
1070 }
1071
9e83f3d1 1072 // URLs related to this attempt ============================================
b10c38a3 1073 /**
a1eb3a44
TH
1074 * @return string quiz view url.
1075 */
1076 public function view_url() {
1077 return $this->quizobj->view_url();
1078 }
1079
1080 /**
1081 * @return string the URL of this quiz's edit page. Needs to be POSTed to with a cmid parameter.
1082 */
da729916
TH
1083 public function start_attempt_url($slot = null, $page = -1) {
1084 if ($page == -1 && !is_null($slot)) {
32953f37 1085 $page = $this->get_question_page($slot);
da729916
TH
1086 } else {
1087 $page = 0;
1088 }
1089 return $this->quizobj->start_attempt_url($page);
a1eb3a44
TH
1090 }
1091
1092 /**
f7970e3c
TH
1093 * @param int $slot if speified, the slot number of a specific question to link to.
1094 * @param int $page if specified, a particular page to link to. If not givem deduced
a1eb3a44 1095 * from $slot, or goes to the first page.
f7970e3c 1096 * @param int $questionid a question id. If set, will add a fragment to the URL
b10c38a3 1097 * to jump to a particuar question on the page.
f7970e3c 1098 * @param int $thispage if not -1, the current page. Will cause links to other things on
d18675a8 1099 * this page to be output as only a fragment.
b10c38a3 1100 * @return string the URL to continue this attempt.
1101 */
56e82d99 1102 public function attempt_url($slot = null, $page = -1, $thispage = -1) {
a1eb3a44 1103 return $this->page_and_question_url('attempt', $slot, $page, false, $thispage);
36e413e3 1104 }
1105
b10c38a3 1106 /**
1107 * @return string the URL of this quiz's summary page.
1108 */
36e413e3 1109 public function summary_url() {
a1eb3a44 1110 return new moodle_url('/mod/quiz/summary.php', array('attempt' => $this->attempt->id));
36e413e3 1111 }
1112
9f9eec1e 1113 /**
1114 * @return string the URL of this quiz's summary page.
1115 */
1116 public function processattempt_url() {
a1eb3a44 1117 return new moodle_url('/mod/quiz/processattempt.php');
9f9eec1e 1118 }
1119
b10c38a3 1120 /**
f7970e3c
TH
1121 * @param int $slot indicates which question to link to.
1122 * @param int $page if specified, the URL of this particular page of the attempt, otherwise
a1eb3a44 1123 * the URL will go to the first page. If -1, deduce $page from $slot.
f7970e3c 1124 * @param bool $showall if true, the URL will be to review the entire attempt on one page,
b10c38a3 1125 * and $page will be ignored.
f7970e3c 1126 * @param int $thispage if not -1, the current page. Will cause links to other things on
d18675a8 1127 * this page to be output as only a fragment.
b10c38a3 1128 * @return string the URL to review this attempt.
1129 */
56e82d99 1130 public function review_url($slot = null, $page = -1, $showall = false, $thispage = -1) {
a1eb3a44 1131 return $this->page_and_question_url('review', $slot, $page, $showall, $thispage);
78e7a3dd 1132 }
1133
9e83f3d1 1134 // Bits of content =========================================================
a1eb3a44 1135
d755b0f5
TH
1136 /**
1137 * If $reviewoptions->attempt is false, meaning that students can't review this
1138 * attempt at the moment, return an appropriate string explaining why.
1139 *
1140 * @param bool $short if true, return a shorter string.
1141 * @return string an appropraite message.
1142 */
1143 public function cannot_review_message($short = false) {
1144 return $this->quizobj->cannot_review_message(
1145 $this->get_attempt_state(), $short);
1146 }
1147
d18675a8 1148 /**
ff2ec2cb 1149 * Initialise the JS etc. required all the questions on a page.
d18675a8 1150 * @param mixed $page a page number, or 'all'.
1151 */
a1eb3a44
TH
1152 public function get_html_head_contributions($page = 'all', $showall = false) {
1153 if ($showall) {
1154 $page = 'all';
1155 }
1156 $result = '';
1157 foreach ($this->get_slots($page) as $slot) {
1158 $result .= $this->quba->render_question_head_html($slot);
1159 }
1160 $result .= question_engine::initialise_js();
1161 return $result;
78e7a3dd 1162 }
1163
d18675a8 1164 /**
1165 * Initialise the JS etc. required by one question.
f7970e3c 1166 * @param int $questionid the question id.
d18675a8 1167 */
a1eb3a44
TH
1168 public function get_question_html_head_contributions($slot) {
1169 return $this->quba->render_question_head_html($slot) .
1170 question_engine::initialise_js();
b826bcef 1171 }
1172
d18675a8 1173 /**
3c6185e9
TH
1174 * Print the HTML for the start new preview button, if the current user
1175 * is allowed to see one.
1176 */
1177 public function restart_preview_button() {
1178 global $OUTPUT;
1179 if ($this->is_preview() && $this->is_preview_user()) {
1180 return $OUTPUT->single_button(new moodle_url(
1181 $this->start_attempt_url(), array('forcenew' => true)),
1182 get_string('startnewpreview', 'quiz'));
1183 } else {
1184 return '';
1185 }
36e413e3 1186 }
1187
aafdb447 1188 /**
a1eb3a44
TH
1189 * Generate the HTML that displayes the question in its current state, with
1190 * the appropriate display options.
aafdb447 1191 *
f7970e3c
TH
1192 * @param int $id the id of a question in this quiz attempt.
1193 * @param bool $reviewing is the being printed on an attempt or a review page.
da729916 1194 * @param moodle_url $thispageurl the URL of the page this question is being printed on.
a1eb3a44 1195 * @return string HTML for the question in its current state.
aafdb447 1196 */
da729916 1197 public function render_question($slot, $reviewing, $thispageurl = null) {
a1eb3a44 1198 return $this->quba->render_question($slot,
da729916 1199 $this->get_display_options_with_edit_link($reviewing, $slot, $thispageurl),
32953f37 1200 $this->get_question_number($slot));
a1eb3a44 1201 }
fb6dcdab 1202
a1eb3a44
TH
1203 /**
1204 * Like {@link render_question()} but displays the question at the past step
1205 * indicated by $seq, rather than showing the latest step.
1206 *
f7970e3c
TH
1207 * @param int $id the id of a question in this quiz attempt.
1208 * @param int $seq the seq number of the past state to display.
1209 * @param bool $reviewing is the being printed on an attempt or a review page.
a1eb3a44
TH
1210 * @param string $thispageurl the URL of the page this question is being printed on.
1211 * @return string HTML for the question in its current state.
1212 */
1213 public function render_question_at_step($slot, $seq, $reviewing, $thispageurl = '') {
1214 return $this->quba->render_question_at_step($slot, $seq,
2515be0e 1215 $this->get_display_options_with_edit_link($reviewing, $slot, $thispageurl),
32953f37 1216 $this->get_question_number($slot));
a1eb3a44
TH
1217 }
1218
1219 /**
1220 * Wrapper round print_question from lib/questionlib.php.
1221 *
f7970e3c 1222 * @param int $id the id of a question in this quiz attempt.
a1eb3a44
TH
1223 */
1224 public function render_question_for_commenting($slot) {
1225 $options = $this->get_display_options(true);
1226 $options->hide_all_feedback();
1227 $options->manualcomment = question_display_options::EDITABLE;
25a03faa 1228 return $this->quba->render_question($slot, $options,
32953f37 1229 $this->get_question_number($slot));
78e7a3dd 1230 }
1231
a1eb3a44
TH
1232 /**
1233 * Check wheter access should be allowed to a particular file.
1234 *
f7970e3c
TH
1235 * @param int $id the id of a question in this quiz attempt.
1236 * @param bool $reviewing is the being printed on an attempt or a review page.
a1eb3a44
TH
1237 * @param string $thispageurl the URL of the page this question is being printed on.
1238 * @return string HTML for the question in its current state.
1239 */
56e82d99 1240 public function check_file_access($slot, $reviewing, $contextid, $component,
fe6ce234 1241 $filearea, $args, $forcedownload) {
56e82d99
TH
1242 return $this->quba->check_file_access($slot, $this->get_display_options($reviewing),
1243 $component, $filearea, $args, $forcedownload);
fe6ce234
DC
1244 }
1245
d18675a8 1246 /**
1247 * Get the navigation panel object for this attempt.
1248 *
1249 * @param $panelclass The type of panel, quiz_attempt_nav_panel or quiz_review_nav_panel
1250 * @param $page the current page number.
1251 * @param $showall whether we are showing the whole quiz on one page. (Used by review.php)
1252 * @return quiz_nav_panel_base the requested object.
1253 */
b3782c71
TH
1254 public function get_navigation_panel(mod_quiz_renderer $output,
1255 $panelclass, $page, $showall = false) {
a1eb3a44 1256 $panel = new $panelclass($this, $this->get_display_options(true), $page, $showall);
b3782c71
TH
1257
1258 $bc = new block_contents();
83f93ec4 1259 $bc->attributes['id'] = 'mod_quiz_navblock';
b3782c71
TH
1260 $bc->title = get_string('quiznavigation', 'quiz');
1261 $bc->content = $output->navigation_panel($panel);
1262 return $bc;
3c168fbb 1263 }
36e413e3 1264
d18675a8 1265 /**
83192608 1266 * Given a URL containing attempt={this attempt id}, return an array of variant URLs
b3782c71 1267 * @param moodle_url $url a URL.
d18675a8 1268 * @return string HTML fragment. Comma-separated list of links to the other
1269 * attempts with the attempt number as the link text. The curent attempt is
1270 * included but is not a link.
1271 */
b3782c71 1272 public function links_to_other_attempts(moodle_url $url) {
a1eb3a44 1273 $attempts = quiz_get_user_attempts($this->get_quiz()->id, $this->attempt->userid, 'all');
f88fb62c 1274 if (count($attempts) <= 1) {
1275 return false;
1276 }
b3782c71
TH
1277
1278 $links = new mod_quiz_links_to_other_attempts();
b55797b8 1279 foreach ($attempts as $at) {
1280 if ($at->id == $this->attempt->id) {
b3782c71 1281 $links->links[$at->attempt] = null;
b55797b8 1282 } else {
b3782c71 1283 $links->links[$at->attempt] = new moodle_url($url, array('attempt' => $at->id));
b55797b8 1284 }
1285 }
b3782c71 1286 return $links;
b55797b8 1287 }
1288
a1eb3a44
TH
1289 // Methods for processing ==================================================
1290
34b7d838
TH
1291 /**
1292 * Check this attempt, to see if there are any state transitions that should
8e771aed 1293 * happen automatically. This function will update the attempt checkstatetime.
34b7d838 1294 * @param int $timestamp the timestamp that should be stored as the modifed
a403bce0 1295 * @param bool $studentisonline is the student currently interacting with Moodle?
34b7d838 1296 */
a403bce0 1297 public function handle_if_time_expired($timestamp, $studentisonline) {
34b7d838
TH
1298 global $DB;
1299
8e771aed 1300 $timeclose = $this->get_access_manager($timestamp)->get_end_time($this->attempt);
a403bce0 1301
8e771aed
MP
1302 if ($timeclose === false || $this->is_preview()) {
1303 $this->update_timecheckstate(null);
1304 return; // No time limit
1305 }
1306 if ($timestamp < $timeclose) {
1307 $this->update_timecheckstate($timeclose);
34b7d838
TH
1308 return; // Time has not yet expired.
1309 }
1310
1311 // If the attempt is already overdue, look to see if it should be abandoned ...
9e83f3d1 1312 if ($this->attempt->state == self::OVERDUE) {
8e771aed
MP
1313 $timeoverdue = $timestamp - $timeclose;
1314 $graceperiod = $this->quizobj->get_quiz()->graceperiod;
1315 if ($timeoverdue >= $graceperiod) {
a403bce0 1316 $this->process_abandon($timestamp, $studentisonline);
8e771aed
MP
1317 } else {
1318 // Overdue time has not yet expired
1319 $this->update_timecheckstate($timeclose + $graceperiod);
34b7d838 1320 }
34b7d838
TH
1321 return; // ... and we are done.
1322 }
1323
9e83f3d1 1324 if ($this->attempt->state != self::IN_PROGRESS) {
8e771aed 1325 $this->update_timecheckstate(null);
34b7d838
TH
1326 return; // Attempt is already in a final state.
1327 }
1328
1329 // Otherwise, we were in quiz_attempt::IN_PROGRESS, and time has now expired.
1330 // Transition to the appropriate state.
1331 switch ($this->quizobj->get_quiz()->overduehandling) {
1332 case 'autosubmit':
1333 $this->process_finish($timestamp, false);
1334 return;
1335
1336 case 'graceperiod':
a403bce0 1337 $this->process_going_overdue($timestamp, $studentisonline);
34b7d838
TH
1338 return;
1339
1340 case 'autoabandon':
a403bce0 1341 $this->process_abandon($timestamp, $studentisonline);
34b7d838
TH
1342 return;
1343 }
8e771aed
MP
1344
1345 // This is an overdue attempt with no overdue handling defined, so just abandon.
1346 $this->process_abandon($timestamp, $studentisonline);
1347 return;
34b7d838
TH
1348 }
1349
d18675a8 1350 /**
a1eb3a44
TH
1351 * Process all the actions that were submitted as part of the current request.
1352 *
388f0473
JP
1353 * @param int $timestamp the timestamp that should be stored as the modifed
1354 * time in the database for these actions. If null, will use the current time.
1355 * @param bool $becomingoverdue
1356 * @param array|null $simulatedresponses If not null, then we are testing, and this is an array of simulated data, keys are slot
1357 * nos and values are arrays representing student responses which will be passed to
1358 * question_definition::prepare_simulated_post_data method and then have the
1359 * appropriate prefix added.
1360 */
1361 public function process_submitted_actions($timestamp, $becomingoverdue = false, $simulatedresponses = null) {
8f37f7fb 1362 global $DB;
34b7d838
TH
1363
1364 $transaction = $DB->start_delegated_transaction();
1365
388f0473
JP
1366 if ($simulatedresponses !== null) {
1367 $simulatedpostdata = $this->quba->prepare_simulated_post_data($simulatedresponses);
1368 } else {
1369 $simulatedpostdata = null;
1370 }
1371
1372 $this->quba->process_all_actions($timestamp, $simulatedpostdata);
a1eb3a44
TH
1373 question_engine::save_questions_usage_by_activity($this->quba);
1374
1375 $this->attempt->timemodified = $timestamp;
34b7d838 1376 if ($this->attempt->state == self::FINISHED) {
a1eb3a44
TH
1377 $this->attempt->sumgrades = $this->quba->get_total_mark();
1378 }
34b7d838 1379 if ($becomingoverdue) {
a403bce0
TH
1380 $this->process_going_overdue($timestamp, true);
1381 } else {
1382 $DB->update_record('quiz_attempts', $this->attempt);
34b7d838 1383 }
88f0eb15 1384
9e83f3d1 1385 if (!$this->is_preview() && $this->attempt->state == self::FINISHED) {
a1eb3a44 1386 quiz_save_best_grade($this->get_quiz(), $this->get_userid());
766df8f7 1387 }
34b7d838
TH
1388
1389 $transaction->allow_commit();
766df8f7 1390 }
1391
59570888
TH
1392 /**
1393 * Process all the autosaved data that was part of the current request.
1394 *
1395 * @param int $timestamp the timestamp that should be stored as the modifed
1396 * time in the database for these actions. If null, will use the current time.
1397 */
1398 public function process_auto_save($timestamp) {
1399 global $DB;
1400
1401 $transaction = $DB->start_delegated_transaction();
1402
1403 $this->quba->process_all_autosaves($timestamp);
1404 question_engine::save_questions_usage_by_activity($this->quba);
1405
1406 $transaction->allow_commit();
1407 }
1408
d18675a8 1409 /**
a1eb3a44
TH
1410 * Update the flagged state for all question_attempts in this usage, if their
1411 * flagged state was changed in the request.
d18675a8 1412 */
a1eb3a44 1413 public function save_question_flags() {
4be550c5
TH
1414 global $DB;
1415
34b7d838 1416 $transaction = $DB->start_delegated_transaction();
a1eb3a44
TH
1417 $this->quba->update_question_flags();
1418 question_engine::save_questions_usage_by_activity($this->quba);
34b7d838 1419 $transaction->allow_commit();
a1eb3a44 1420 }
e8f99abc 1421
34b7d838 1422 public function process_finish($timestamp, $processsubmitted) {
a403bce0 1423 global $DB;
34b7d838
TH
1424
1425 $transaction = $DB->start_delegated_transaction();
1426
1427 if ($processsubmitted) {
1428 $this->quba->process_all_actions($timestamp);
1429 }
a1eb3a44 1430 $this->quba->finish_all_questions($timestamp);
e8f99abc 1431
a1eb3a44 1432 question_engine::save_questions_usage_by_activity($this->quba);
e8f99abc 1433
a1eb3a44
TH
1434 $this->attempt->timemodified = $timestamp;
1435 $this->attempt->timefinish = $timestamp;
1436 $this->attempt->sumgrades = $this->quba->get_total_mark();
a403bce0 1437 $this->attempt->state = self::FINISHED;
8e771aed 1438 $this->attempt->timecheckstate = null;
88f0eb15 1439 $DB->update_record('quiz_attempts', $this->attempt);
766df8f7 1440
a1eb3a44 1441 if (!$this->is_preview()) {
704f062b 1442 quiz_save_best_grade($this->get_quiz(), $this->attempt->userid);
cbb8b55c 1443
9e83f3d1 1444 // Trigger event.
d900f1dc 1445 $this->fire_state_transition_event('\mod_quiz\event\attempt_submitted', $timestamp);
cbb8b55c 1446
987c2d49
TH
1447 // Tell any access rules that care that the attempt is over.
1448 $this->get_access_manager($timestamp)->current_attempt_finished();
36e413e3 1449 }
34b7d838
TH
1450
1451 $transaction->allow_commit();
1452 }
1453
8e771aed
MP
1454 /**
1455 * Update this attempt timecheckstate if necessary.
1456 * @param int|null the timecheckstate
1457 */
1458 public function update_timecheckstate($time) {
1459 global $DB;
1460 if ($this->attempt->timecheckstate !== $time) {
1461 $this->attempt->timecheckstate = $time;
1462 $DB->set_field('quiz_attempts', 'timecheckstate', $time, array('id'=>$this->attempt->id));
1463 }
1464 }
1465
34b7d838
TH
1466 /**
1467 * Mark this attempt as now overdue.
1468 * @param int $timestamp the time to deem as now.
a403bce0 1469 * @param bool $studentisonline is the student currently interacting with Moodle?
34b7d838 1470 */
a403bce0
TH
1471 public function process_going_overdue($timestamp, $studentisonline) {
1472 global $DB;
1473
1474 $transaction = $DB->start_delegated_transaction();
1475 $this->attempt->timemodified = $timestamp;
1476 $this->attempt->state = self::OVERDUE;
8e771aed
MP
1477 // If we knew the attempt close time, we could compute when the graceperiod ends.
1478 // Instead we'll just fix it up through cron.
1479 $this->attempt->timecheckstate = $timestamp;
a403bce0
TH
1480 $DB->update_record('quiz_attempts', $this->attempt);
1481
0a8b091d 1482 $this->fire_state_transition_event('\mod_quiz\event\attempt_becameoverdue', $timestamp);
a403bce0
TH
1483
1484 $transaction->allow_commit();
34b7d838
TH
1485 }
1486
1487 /**
1488 * Mark this attempt as abandoned.
1489 * @param int $timestamp the time to deem as now.
a403bce0 1490 * @param bool $studentisonline is the student currently interacting with Moodle?
34b7d838 1491 */
a403bce0
TH
1492 public function process_abandon($timestamp, $studentisonline) {
1493 global $DB;
1494
1495 $transaction = $DB->start_delegated_transaction();
1496 $this->attempt->timemodified = $timestamp;
1497 $this->attempt->state = self::ABANDONED;
8e771aed 1498 $this->attempt->timecheckstate = null;
a403bce0
TH
1499 $DB->update_record('quiz_attempts', $this->attempt);
1500
d900f1dc 1501 $this->fire_state_transition_event('\mod_quiz\event\attempt_abandoned', $timestamp);
a403bce0
TH
1502
1503 $transaction->allow_commit();
1504 }
1505
1506 /**
1507 * Fire a state transition event.
d900f1dc
FM
1508 * the same event information.
1509 * @param string $eventclass the event class name.
a403bce0 1510 * @param int $timestamp the timestamp to include in the event.
d900f1dc 1511 * @return void
a403bce0 1512 */
d900f1dc 1513 protected function fire_state_transition_event($eventclass, $timestamp) {
a403bce0
TH
1514 global $USER;
1515
d900f1dc
FM
1516 $params = array(
1517 'context' => $this->get_quizobj()->get_context(),
1518 'courseid' => $this->get_courseid(),
1519 'objectid' => $this->attempt->id,
1520 'relateduserid' => $this->attempt->userid,
1521 'other' => array(
1522 'submitterid' => CLI_SCRIPT ? null : $USER->id
1523 )
1524 );
1525
1526 $event = $eventclass::create($params);
1527 $event->add_record_snapshot('quiz', $this->get_quiz());
1528 $event->add_record_snapshot('quiz_attempts', $this->get_attempt());
1529 $event->trigger();
36e413e3 1530 }
78e7a3dd 1531
1532 /**
a1eb3a44
TH
1533 * Print the fields of the comment form for questions in this attempt.
1534 * @param $slot which question to output the fields for.
1535 * @param $prefix Prefix to add to all field names.
78e7a3dd 1536 */
a1eb3a44
TH
1537 public function question_print_comment_fields($slot, $prefix) {
1538 // Work out a nice title.
1539 $student = get_record('user', 'id', $this->get_userid());
1540 $a = new object();
1541 $a->fullname = fullname($student, true);
1542 $a->attempt = $this->get_attempt_number();
1543
1544 question_print_comment_fields($this->quba->get_question_attempt($slot),
1545 $prefix, $this->get_display_options(true)->markdp,
1546 get_string('gradingattempt', 'quiz_grading', $a));
78e7a3dd 1547 }
1548
9e83f3d1 1549 // Private methods =========================================================
a1eb3a44 1550
78e7a3dd 1551 /**
d18675a8 1552 * Get a URL for a particular question on a particular page of the quiz.
1553 * Used by {@link attempt_url()} and {@link review_url()}.
78e7a3dd 1554 *
d18675a8 1555 * @param string $script. Used in the URL like /mod/quiz/$script.php
25a03faa
TH
1556 * @param int $slot identifies the specific question on the page to jump to.
1557 * 0 to just use the $page parameter.
1558 * @param int $page -1 to look up the page number from the slot, otherwise
1559 * the page number to go to.
f7970e3c
TH
1560 * @param bool $showall if true, return a URL with showall=1, and not page number
1561 * @param int $thispage the page we are currently on. Links to questions on this
d18675a8 1562 * page will just be a fragment #q123. -1 to disable this.
1563 * @return The requested URL.
78e7a3dd 1564 */
a1eb3a44 1565 protected function page_and_question_url($script, $slot, $page, $showall, $thispage) {
9e83f3d1 1566 // Fix up $page.
3c168fbb 1567 if ($page == -1) {
56e82d99 1568 if (!is_null($slot) && !$showall) {
32953f37 1569 $page = $this->get_question_page($slot);
78e7a3dd 1570 } else {
1571 $page = 0;
1572 }
1573 }
a1eb3a44 1574
78e7a3dd 1575 if ($showall) {
1576 $page = 0;
1577 }
d18675a8 1578
fb6dcdab 1579 // Add a fragment to scroll down to the question.
a1eb3a44 1580 $fragment = '';
56e82d99 1581 if (!is_null($slot)) {
a1eb3a44 1582 if ($slot == reset($this->pagelayout[$page])) {
d4a03c00 1583 // First question on page, go to top.
a1eb3a44 1584 $fragment = '#';
d4a03c00 1585 } else {
a1eb3a44 1586 $fragment = '#q' . $slot;
d4a03c00 1587 }
78e7a3dd 1588 }
d18675a8 1589
a1eb3a44
TH
1590 // Work out the correct start to the URL.
1591 if ($thispage == $page) {
1592 return new moodle_url($fragment);
36e413e3 1593
36e413e3 1594 } else {
a1eb3a44
TH
1595 $url = new moodle_url('/mod/quiz/' . $script . '.php' . $fragment,
1596 array('attempt' => $this->attempt->id));
1597 if ($showall) {
1598 $url->param('showall', 1);
1599 } else if ($page > 0) {
1600 $url->param('page', $page);
1601 }
1602 return $url;
36e413e3 1603 }
1604 }
36e413e3 1605}
3c168fbb 1606
f7970e3c 1607
b3782c71
TH
1608/**
1609 * Represents a single link in the navigation panel.
1610 *
1611 * @copyright 2011 The Open University
1612 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1613 * @since Moodle 2.1
1614 */
1615class quiz_nav_question_button implements renderable {
1616 public $id;
1617 public $number;
1618 public $stateclass;
1619 public $statestring;
1620 public $currentpage;
1621 public $flagged;
1622 public $url;
1623}
1624
1625
d18675a8 1626/**
1627 * Represents the navigation panel, and builds a {@link block_contents} to allow
1628 * it to be output.
1629 *
f7970e3c
TH
1630 * @copyright 2008 Tim Hunt
1631 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1632 * @since Moodle 2.0
d18675a8 1633 */
3c168fbb 1634abstract class quiz_nav_panel_base {
38c9edd0 1635 /** @var quiz_attempt */
3c168fbb 1636 protected $attemptobj;
38c9edd0 1637 /** @var question_display_options */
3c168fbb 1638 protected $options;
38c9edd0 1639 /** @var integer */
3c168fbb 1640 protected $page;
38c9edd0 1641 /** @var boolean */
d18675a8 1642 protected $showall;
3c168fbb 1643
a1eb3a44
TH
1644 public function __construct(quiz_attempt $attemptobj,
1645 question_display_options $options, $page, $showall) {
38c9edd0
TH
1646 $this->attemptobj = $attemptobj;
1647 $this->options = $options;
1648 $this->page = $page;
1649 $this->showall = $showall;
3c168fbb 1650 }
1651
b3782c71
TH
1652 public function get_question_buttons() {
1653 $buttons = array();
a1eb3a44
TH
1654 foreach ($this->attemptobj->get_slots() as $slot) {
1655 $qa = $this->attemptobj->get_question_attempt($slot);
1656 $showcorrectness = $this->options->correctness && $qa->has_marks();
a1eb3a44 1657
b3782c71
TH
1658 $button = new quiz_nav_question_button();
1659 $button->id = 'quiznavbutton' . $slot;
32953f37 1660 $button->number = $this->attemptobj->get_question_number($slot);
97cdc1de 1661 $button->stateclass = $qa->get_state_class($showcorrectness);
33c8d37b 1662 $button->navmethod = $this->attemptobj->get_navigation_method();
b3782c71
TH
1663 if (!$showcorrectness && $button->stateclass == 'notanswered') {
1664 $button->stateclass = 'complete';
1665 }
79a46626 1666 $button->statestring = $this->get_state_string($qa, $showcorrectness);
0328341f 1667 $button->currentpage = $this->showall || $this->attemptobj->get_question_page($slot) == $this->page;
b3782c71
TH
1668 $button->flagged = $qa->is_flagged();
1669 $button->url = $this->get_question_url($slot);
1670 $buttons[] = $button;
a1eb3a44
TH
1671 }
1672
b3782c71 1673 return $buttons;
c752264f 1674 }
1675
79a46626 1676 protected function get_state_string(question_attempt $qa, $showcorrectness) {
0eafc988 1677 if ($qa->get_question()->length > 0) {
79a46626
TH
1678 return $qa->get_state_string($showcorrectness);
1679 }
1680
1681 // Special case handling for 'information' items.
1682 if ($qa->get_state() == question_state::$todo) {
1683 return get_string('notyetviewed', 'quiz');
1684 } else {
1685 return get_string('viewed', 'quiz');
1686 }
1687 }
1688
b3782c71 1689 public function render_before_button_bits(mod_quiz_renderer $output) {
d18675a8 1690 return '';
1691 }
3c168fbb 1692
0eb253f7
TH
1693 abstract public function render_end_bits(mod_quiz_renderer $output);
1694
1695 protected function render_restart_preview_link($output) {
b3782c71
TH
1696 if (!$this->attemptobj->is_own_preview()) {
1697 return '';
1698 }
1699 return $output->restart_preview_button(new moodle_url(
1700 $this->attemptobj->start_attempt_url(), array('forcenew' => true)));
1701 }
3c168fbb 1702
c7df5006 1703 protected abstract function get_question_url($slot);
d18675a8 1704
b3782c71
TH
1705 public function user_picture() {
1706 global $DB;
1ecd3c30 1707 if ($this->attemptobj->get_quiz()->showuserpicture == QUIZ_SHOWIMAGE_NONE) {
e28bbd89
TH
1708 return null;
1709 }
a733c4b9 1710 $user = $DB->get_record('user', array('id' => $this->attemptobj->get_userid()));
b3782c71
TH
1711 $userpicture = new user_picture($user);
1712 $userpicture->courseid = $this->attemptobj->get_courseid();
1ecd3c30
JA
1713 if ($this->attemptobj->get_quiz()->showuserpicture == QUIZ_SHOWIMAGE_LARGE) {
1714 $userpicture->size = true;
1715 }
b3782c71 1716 return $userpicture;
3c168fbb 1717 }
0328341f
K
1718
1719 /**
1720 * Return 'allquestionsononepage' as CSS class name when $showall is set,
1721 * otherwise, return 'multipages' as CSS class name.
1722 * @return string, CSS class name
1723 */
1724 public function get_button_container_class() {
1725 // Quiz navigation is set on 'Show all questions on one page'.
1726 if ($this->showall) {
1727 return 'allquestionsononepage';
1728 }
1729 // Quiz navigation is set on 'Show one page at a time'.
1730 return 'multipages';
1731 }
3c168fbb 1732}
1733
f7970e3c 1734
d18675a8 1735/**
1736 * Specialisation of {@link quiz_nav_panel_base} for the attempt quiz page.
1737 *
f7970e3c
TH
1738 * @copyright 2008 Tim Hunt
1739 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1740 * @since Moodle 2.0
d18675a8 1741 */
3c168fbb 1742class quiz_attempt_nav_panel extends quiz_nav_panel_base {
b3782c71 1743 public function get_question_url($slot) {
33c8d37b
CF
1744 if ($this->attemptobj->can_navigate_to($slot)) {
1745 return $this->attemptobj->attempt_url($slot, -1, $this->page);
1746 } else {
1747 return null;
1748 }
3c168fbb 1749 }
1750
b3782c71
TH
1751 public function render_before_button_bits(mod_quiz_renderer $output) {
1752 return html_writer::tag('div', get_string('navnojswarning', 'quiz'),
1753 array('id' => 'quiznojswarning'));
3c168fbb 1754 }
1755
b3782c71
TH
1756 public function render_end_bits(mod_quiz_renderer $output) {
1757 return html_writer::link($this->attemptobj->summary_url(),
c9272e87 1758 get_string('endtest', 'quiz'), array('class' => 'endtestlink')) .
2b2b6458 1759 $output->countdown_timer($this->attemptobj, time()) .
0eb253f7 1760 $this->render_restart_preview_link($output);
3c168fbb 1761 }
1762}
1763
f7970e3c 1764
d18675a8 1765/**
1766 * Specialisation of {@link quiz_nav_panel_base} for the review quiz page.
1767 *
f7970e3c
TH
1768 * @copyright 2008 Tim Hunt
1769 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1770 * @since Moodle 2.0
d18675a8 1771 */
3c168fbb 1772class quiz_review_nav_panel extends quiz_nav_panel_base {
b3782c71 1773 public function get_question_url($slot) {
a1eb3a44 1774 return $this->attemptobj->review_url($slot, -1, $this->showall, $this->page);
3c168fbb 1775 }
1776
b3782c71 1777 public function render_end_bits(mod_quiz_renderer $output) {
d18675a8 1778 $html = '';
1779 if ($this->attemptobj->get_num_pages() > 1) {
1780 if ($this->showall) {
b3782c71
TH
1781 $html .= html_writer::link($this->attemptobj->review_url(null, 0, false),
1782 get_string('showeachpage', 'quiz'));
d18675a8 1783 } else {
b3782c71
TH
1784 $html .= html_writer::link($this->attemptobj->review_url(null, 0, true),
1785 get_string('showall', 'quiz'));
d18675a8 1786 }
1787 }
d869de66 1788 $html .= $output->finish_review_link($this->attemptobj);
0eb253f7 1789 $html .= $this->render_restart_preview_link($output);
3c168fbb 1790 return $html;
1791 }
1792}