MDL-57600 course: fix typo on courselib test
[moodle.git] / mod / quiz / attempt.php
blobf1c26ab3e81280472725e40f610eeda8dd637a17
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.
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.
20 * @package mod_quiz
21 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 require_once(__DIR__ . '/../../config.php');
26 require_once($CFG->dirroot . '/mod/quiz/locallib.php');
28 // Look for old-style URLs, such as may be in the logs, and redirect them to startattemtp.php.
29 if ($id = optional_param('id', 0, PARAM_INT)) {
30 redirect($CFG->wwwroot . '/mod/quiz/startattempt.php?cmid=' . $id . '&sesskey=' . sesskey());
31 } else if ($qid = optional_param('q', 0, PARAM_INT)) {
32 if (!$cm = get_coursemodule_from_instance('quiz', $qid)) {
33 print_error('invalidquizid', 'quiz');
35 redirect(new moodle_url('/mod/quiz/startattempt.php',
36 array('cmid' => $cm->id, 'sesskey' => sesskey())));
39 // Get submitted parameters.
40 $attemptid = required_param('attempt', PARAM_INT);
41 $page = optional_param('page', 0, PARAM_INT);
42 $cmid = optional_param('cmid', null, PARAM_INT);
44 $attemptobj = quiz_create_attempt_handling_errors($attemptid, $cmid);
45 $page = $attemptobj->force_page_number_into_range($page);
46 $PAGE->set_url($attemptobj->attempt_url(null, $page));
48 // Check login.
49 require_login($attemptobj->get_course(), false, $attemptobj->get_cm());
51 // Check that this attempt belongs to this user.
52 if ($attemptobj->get_userid() != $USER->id) {
53 if ($attemptobj->has_capability('mod/quiz:viewreports')) {
54 redirect($attemptobj->review_url(null, $page));
55 } else {
56 throw new moodle_quiz_exception($attemptobj->get_quizobj(), 'notyourattempt');
60 // Check capabilities and block settings.
61 if (!$attemptobj->is_preview_user()) {
62 $attemptobj->require_capability('mod/quiz:attempt');
63 if (empty($attemptobj->get_quiz()->showblocks)) {
64 $PAGE->blocks->show_only_fake_blocks();
67 } else {
68 navigation_node::override_active_url($attemptobj->start_attempt_url());
71 // If the attempt is already closed, send them to the review page.
72 if ($attemptobj->is_finished()) {
73 redirect($attemptobj->review_url(null, $page));
74 } else if ($attemptobj->get_state() == quiz_attempt::OVERDUE) {
75 redirect($attemptobj->summary_url());
78 // Check the access rules.
79 $accessmanager = $attemptobj->get_access_manager(time());
80 $accessmanager->setup_attempt_page($PAGE);
81 $output = $PAGE->get_renderer('mod_quiz');
82 $messages = $accessmanager->prevent_access();
83 if (!$attemptobj->is_preview_user() && $messages) {
84 print_error('attempterror', 'quiz', $attemptobj->view_url(),
85 $output->access_messages($messages));
87 if ($accessmanager->is_preflight_check_required($attemptobj->get_attemptid())) {
88 redirect($attemptobj->start_attempt_url(null, $page));
91 // Set up auto-save if required.
92 $autosaveperiod = get_config('quiz', 'autosaveperiod');
93 if ($autosaveperiod) {
94 $PAGE->requires->yui_module('moodle-mod_quiz-autosave',
95 'M.mod_quiz.autosave.init', array($autosaveperiod));
98 // Log this page view.
99 $attemptobj->fire_attempt_viewed_event();
101 // Get the list of questions needed by this page.
102 $slots = $attemptobj->get_slots($page);
104 // Check.
105 if (empty($slots)) {
106 throw new moodle_quiz_exception($attemptobj->get_quizobj(), 'noquestionsfound');
109 // Update attempt page, redirecting the user if $page is not valid.
110 if (!$attemptobj->set_currentpage($page)) {
111 redirect($attemptobj->start_attempt_url(null, $attemptobj->get_currentpage()));
114 // Initialise the JavaScript.
115 $headtags = $attemptobj->get_html_head_contributions($page);
116 $PAGE->requires->js_init_call('M.mod_quiz.init_attempt_form', null, false, quiz_get_js_module());
118 // Arrange for the navigation to be displayed in the first region on the page.
119 $navbc = $attemptobj->get_navigation_panel($output, 'quiz_attempt_nav_panel', $page);
120 $regions = $PAGE->blocks->get_regions();
121 $PAGE->blocks->add_fake_block($navbc, reset($regions));
123 $title = get_string('attempt', 'quiz', $attemptobj->get_attempt_number());
124 $headtags = $attemptobj->get_html_head_contributions($page);
125 $PAGE->set_title($attemptobj->get_quiz_name());
126 $PAGE->set_heading($attemptobj->get_course()->fullname);
128 if ($attemptobj->is_last_page($page)) {
129 $nextpage = -1;
130 } else {
131 $nextpage = $page + 1;
134 echo $output->attempt_page($attemptobj, $page, $accessmanager, $messages, $slots, $id, $nextpage);