MDL-28976 quiz was passing 0 to ->attempt_url when it should have been null.
[moodle.git] / mod / quiz / attempt.php
1 <?php
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/>.
17 /**
18  * This script displays a particular page of a quiz attempt that is in progress.
19  *
20  * @package    mod
21  * @subpackage quiz
22  * @copyright  1999 onwards Martin Dougiamas  {@link http://moodle.com}
23  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24  */
26 require_once(dirname(__FILE__) . '/../../config.php');
27 require_once($CFG->dirroot . '/mod/quiz/locallib.php');
29 // Look for old-style URLs, such as may be in the logs, and redirect them to startattemtp.php
30 if ($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');
35     }
36     redirect(new moodle_url('/mod/quiz/startattempt.php',
37             array('cmid' => $cm->id, 'sesskey' => sesskey())));
38 }
40 // Get submitted parameters.
41 $attemptid = required_param('attempt', PARAM_INT);
42 $page = optional_param('page', 0, PARAM_INT);
44 $attemptobj = quiz_attempt::create($attemptid);
45 $PAGE->set_url($attemptobj->attempt_url(null, $page));
47 // Check login.
48 require_login($attemptobj->get_course(), false, $attemptobj->get_cm());
50 // Check that this attempt belongs to this user.
51 if ($attemptobj->get_userid() != $USER->id) {
52     if ($attemptobj->has_capability('mod/quiz:viewreports')) {
53         redirect($attemptobj->review_url(0, $page));
54     } else {
55         throw new moodle_quiz_exception($attemptobj->get_quizobj(), 'notyourattempt');
56     }
57 }
59 // Check capabilities and block settings
60 if (!$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();
64     }
66 } else {
67     navigation_node::override_active_url($attemptobj->start_attempt_url());
68 }
70 // If the attempt is already closed, send them to the review page.
71 if ($attemptobj->is_finished()) {
72     redirect($attemptobj->review_url(0, $page));
73 }
75 // Check the access rules.
76 $accessmanager = $attemptobj->get_access_manager(time());
77 $messages = $accessmanager->prevent_access();
78 $output = $PAGE->get_renderer('mod_quiz');
79 if (!$attemptobj->is_preview_user() && $messages) {
80     print_error('attempterror', 'quiz', $attemptobj->view_url(),
81             $output->access_messages($messages));
82 }
83 $accessmanager->do_password_check($attemptobj->is_preview_user());
85 add_to_log($attemptobj->get_courseid(), 'quiz', 'continue attempt',
86         'review.php?attempt=' . $attemptobj->get_attemptid(),
87         $attemptobj->get_quizid(), $attemptobj->get_cmid());
89 // Get the list of questions needed by this page.
90 $slots = $attemptobj->get_slots($page);
92 // Check.
93 if (empty($slots)) {
94     throw new moodle_quiz_exception($attemptobj->get_quizobj(), 'noquestionsfound');
95 }
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());
101 // Arrange for the navigation to be displayed.
102 $navbc = $attemptobj->get_navigation_panel($output, 'quiz_attempt_nav_panel', $page);
103 $firstregion = reset($PAGE->blocks->get_regions());
104 $PAGE->blocks->add_fake_block($navbc, $firstregion);
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);
109 if ($accessmanager->securewindow_required($attemptobj->is_preview_user())) {
110     $accessmanager->setup_secure_page($attemptobj->get_course()->shortname . ': ' .
111             format_string($attemptobj->get_quiz_name()));
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();
119 } else {
120     $PAGE->set_title(format_string($attemptobj->get_quiz_name()));
121     echo $OUTPUT->header();
124 if ($attemptobj->is_last_page($page)) {
125     $nextpage = -1;
126 } else {
127     $nextpage = $page + 1;
130 echo $output->attempt_page($attemptobj, $page, $accessmanager, $messages, $slots, $id, $nextpage);
132 $accessmanager->show_attempt_timer_if_needed($attemptobj->get_attempt(), time());
133 echo $OUTPUT->footer();