MDL-28976 quiz was passing 0 to ->attempt_url when it should have been null.
[moodle.git] / mod / quiz / attempt.php
CommitLineData
83192608 1<?php
7c5bd5bf
TH
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
ee1fb969 17/**
7c5bd5bf 18 * This script displays a particular page of a quiz attempt that is in progress.
2b3b36d0 19 *
ba643847 20 * @package mod
7c5bd5bf 21 * @subpackage quiz
ba643847
TH
22 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
2b3b36d0 24 */
55c0c75c 25
7c5bd5bf
TH
26require_once(dirname(__FILE__) . '/../../config.php');
27require_once($CFG->dirroot . '/mod/quiz/locallib.php');
ee1fb969 28
7c5bd5bf
TH
29// Look for old-style URLs, such as may be in the logs, and redirect them to startattemtp.php
30if ($id = optional_param('id', 0, PARAM_INTEGER)) {
31 redirect($CFG->wwwroot . '/mod/quiz/startattempt.php?cmid=' . $id . '&sesskey=' . sesskey());
32} else if ($qid = optional_param('q', 0, PARAM_INTEGER)) {
33 if (!$cm = get_coursemodule_from_instance('quiz', $qid)) {
34 print_error('invalidquizid', 'quiz');
55f599f0 35 }
55ca80ed
TH
36 redirect(new moodle_url('/mod/quiz/startattempt.php',
37 array('cmid' => $cm->id, 'sesskey' => sesskey())));
7c5bd5bf 38}
55f599f0 39
7c5bd5bf
TH
40// Get submitted parameters.
41$attemptid = required_param('attempt', PARAM_INT);
42$page = optional_param('page', 0, PARAM_INT);
55c0c75c 43
7c5bd5bf 44$attemptobj = quiz_attempt::create($attemptid);
45e5fbe1 45$PAGE->set_url($attemptobj->attempt_url(null, $page));
e63faf2b 46
7c5bd5bf
TH
47// Check login.
48require_login($attemptobj->get_course(), false, $attemptobj->get_cm());
c68287a9 49
7c5bd5bf
TH
50// Check that this attempt belongs to this user.
51if ($attemptobj->get_userid() != $USER->id) {
52 if ($attemptobj->has_capability('mod/quiz:viewreports')) {
9f9eec1e 53 redirect($attemptobj->review_url(0, $page));
50d7b53e 54 } else {
7c5bd5bf 55 throw new moodle_quiz_exception($attemptobj->get_quizobj(), 'notyourattempt');
bb6f1955 56 }
7c5bd5bf 57}
a5e1f35c 58
7c5bd5bf
TH
59// Check capabilities and block settings
60if (!$attemptobj->is_preview_user()) {
61 $attemptobj->require_capability('mod/quiz:attempt');
62 if (empty($attemptobj->get_quiz()->showblocks)) {
63 $PAGE->blocks->show_only_fake_blocks();
586b2c82 64 }
ee1fb969 65
7c5bd5bf
TH
66} else {
67 navigation_node::override_active_url($attemptobj->start_attempt_url());
68}
69
70// If the attempt is already closed, send them to the review page.
71if ($attemptobj->is_finished()) {
72 redirect($attemptobj->review_url(0, $page));
73}
74
75// Check the access rules.
76$accessmanager = $attemptobj->get_access_manager(time());
77$messages = $accessmanager->prevent_access();
bcd42560 78$output = $PAGE->get_renderer('mod_quiz');
7c5bd5bf 79if (!$attemptobj->is_preview_user() && $messages) {
63be47d9 80 print_error('attempterror', 'quiz', $attemptobj->view_url(),
13040e14 81 $output->access_messages($messages));
7c5bd5bf
TH
82}
83$accessmanager->do_password_check($attemptobj->is_preview_user());
84
85add_to_log($attemptobj->get_courseid(), 'quiz', 'continue attempt',
86 'review.php?attempt=' . $attemptobj->get_attemptid(),
87 $attemptobj->get_quizid(), $attemptobj->get_cmid());
88
89// Get the list of questions needed by this page.
a1eb3a44 90$slots = $attemptobj->get_slots($page);
7c5bd5bf
TH
91
92// Check.
93if (empty($slots)) {
94 throw new moodle_quiz_exception($attemptobj->get_quizobj(), 'noquestionsfound');
95}
96
97// Initialise the JavaScript.
98$headtags = $attemptobj->get_html_head_contributions($page);
99$PAGE->requires->js_init_call('M.mod_quiz.init_attempt_form', null, false, quiz_get_js_module());
100
101// Arrange for the navigation to be displayed.
8d47043f 102$navbc = $attemptobj->get_navigation_panel($output, 'quiz_attempt_nav_panel', $page);
7c5bd5bf
TH
103$firstregion = reset($PAGE->blocks->get_regions());
104$PAGE->blocks->add_fake_block($navbc, $firstregion);
105
9f335e76
DL
106$title = get_string('attempt', 'quiz', $attemptobj->get_attempt_number());
107$headtags = $attemptobj->get_html_head_contributions($page);
108$PAGE->set_heading($attemptobj->get_course()->fullname);
109if ($accessmanager->securewindow_required($attemptobj->is_preview_user())) {
110 $accessmanager->setup_secure_page($attemptobj->get_course()->shortname . ': ' .
111 format_string($attemptobj->get_quiz_name()));
112
113} else if ($accessmanager->safebrowser_required($attemptobj->is_preview_user())) {
114 $PAGE->set_title($attemptobj->get_course()->shortname . ': ' .
115 format_string($attemptobj->get_quiz_name()));
116 $PAGE->set_cacheable(false);
117 echo $OUTPUT->header();
118
119} else {
120 $PAGE->set_title(format_string($attemptobj->get_quiz_name()));
121 echo $OUTPUT->header();
122}
123
124if ($attemptobj->is_last_page($page)) {
125 $nextpage = -1;
126} else {
127 $nextpage = $page + 1;
128}
129
130echo $output->attempt_page($attemptobj, $page, $accessmanager, $messages, $slots, $id, $nextpage);
7c5bd5bf 131
9f335e76
DL
132$accessmanager->show_attempt_timer_if_needed($attemptobj->get_attempt(), time());
133echo $OUTPUT->footer();