Merge branch 'MDL-81073' of https://github.com/paulholden/moodle
[moodle.git] / mod / quiz / attempt.php
bloba8109226c4b7194599d990018547b93f6fa5ce49
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 use mod_quiz\output\navigation_panel_attempt;
26 use mod_quiz\output\renderer;
27 use mod_quiz\quiz_attempt;
29 require_once(__DIR__ . '/../../config.php');
30 require_once($CFG->dirroot . '/mod/quiz/locallib.php');
32 // Look for old-style URLs, such as may be in the logs, and redirect them to startattemtp.php.
33 if ($id = optional_param('id', 0, PARAM_INT)) {
34 redirect($CFG->wwwroot . '/mod/quiz/startattempt.php?cmid=' . $id . '&sesskey=' . sesskey());
35 } else if ($qid = optional_param('q', 0, PARAM_INT)) {
36 if (!$cm = get_coursemodule_from_instance('quiz', $qid)) {
37 throw new \moodle_exception('invalidquizid', 'quiz');
39 redirect(new moodle_url('/mod/quiz/startattempt.php',
40 ['cmid' => $cm->id, 'sesskey' => sesskey()]));
43 // Get submitted parameters.
44 $attemptid = required_param('attempt', PARAM_INT);
45 $page = optional_param('page', 0, PARAM_INT);
46 $cmid = optional_param('cmid', null, PARAM_INT);
48 $attemptobj = quiz_create_attempt_handling_errors($attemptid, $cmid);
49 $page = $attemptobj->force_page_number_into_range($page);
50 $PAGE->set_url($attemptobj->attempt_url(null, $page));
51 // During quiz attempts, the browser back/forwards buttons should force a reload.
52 $PAGE->set_cacheable(false);
54 $PAGE->set_secondary_active_tab("modulepage");
56 // Check login.
57 require_login($attemptobj->get_course(), false, $attemptobj->get_cm());
59 // Check that this attempt belongs to this user.
60 if ($attemptobj->get_userid() != $USER->id) {
61 if ($attemptobj->has_capability('mod/quiz:viewreports')) {
62 redirect($attemptobj->review_url(null, $page));
63 } else {
64 throw new moodle_exception('notyourattempt', 'quiz', $attemptobj->view_url());
68 // Check capabilities and block settings.
69 if (!$attemptobj->is_preview_user()) {
70 $attemptobj->require_capability('mod/quiz:attempt');
71 if (empty($attemptobj->get_quiz()->showblocks)) {
72 $PAGE->blocks->show_only_fake_blocks();
75 } else {
76 navigation_node::override_active_url($attemptobj->start_attempt_url());
79 // If the attempt is already closed, send them to the review page.
80 if ($attemptobj->is_finished()) {
81 redirect($attemptobj->review_url(null, $page));
82 } else if ($attemptobj->get_state() == quiz_attempt::OVERDUE) {
83 redirect($attemptobj->summary_url());
86 // Check the access rules.
87 $accessmanager = $attemptobj->get_access_manager(time());
88 $accessmanager->setup_attempt_page($PAGE);
89 /** @var renderer $output */
90 $output = $PAGE->get_renderer('mod_quiz');
91 $messages = $accessmanager->prevent_access();
92 if (!$attemptobj->is_preview_user() && $messages) {
93 throw new \moodle_exception('attempterror', 'quiz', $attemptobj->view_url(),
94 $output->access_messages($messages));
96 if ($accessmanager->is_preflight_check_required($attemptobj->get_attemptid())) {
97 redirect($attemptobj->start_attempt_url(null, $page));
100 // Set up auto-save if required.
101 $autosaveperiod = get_config('quiz', 'autosaveperiod');
102 if ($autosaveperiod) {
103 $PAGE->requires->string_for_js('strftimedatetimeshortaccurate', 'langconfig');
104 $PAGE->requires->string_for_js('lastautosave', 'quiz');
105 $PAGE->requires->yui_module('moodle-mod_quiz-autosave',
106 'M.mod_quiz.autosave.init', [$autosaveperiod]);
109 // Log this page view.
110 $attemptobj->fire_attempt_viewed_event();
112 // Get the list of questions needed by this page.
113 $slots = $attemptobj->get_slots($page);
115 // Check.
116 if (empty($slots)) {
117 throw new moodle_exception('noquestionsfound', 'quiz', $attemptobj->view_url());
120 // Update attempt page, redirecting the user if $page is not valid.
121 if (!$attemptobj->set_currentpage($page)) {
122 redirect($attemptobj->start_attempt_url(null, $attemptobj->get_currentpage()));
125 if ($attemptobj->is_own_preview()) {
126 $attemptobj->update_questions_to_new_version_if_changed();
129 // Initialise the JavaScript.
130 $headtags = $attemptobj->get_html_head_contributions($page);
131 $PAGE->requires->js_init_call('M.mod_quiz.init_attempt_form', null, false, quiz_get_js_module());
132 \core\session\manager::keepalive(); // Try to prevent sessions expiring during quiz attempts.
134 // Arrange for the navigation to be displayed in the first region on the page.
135 $navbc = $attemptobj->get_navigation_panel($output, navigation_panel_attempt::class, $page);
136 $regions = $PAGE->blocks->get_regions();
137 $PAGE->blocks->add_fake_block($navbc, reset($regions));
139 $headtags = $attemptobj->get_html_head_contributions($page);
140 $PAGE->set_title($attemptobj->attempt_page_title($page));
141 $PAGE->add_body_class('limitedwidth');
142 $PAGE->set_heading($attemptobj->get_course()->fullname);
143 $PAGE->activityheader->disable();
144 if ($attemptobj->is_last_page($page)) {
145 $nextpage = -1;
146 } else {
147 $nextpage = $page + 1;
150 echo $output->attempt_page($attemptobj, $page, $accessmanager, $messages, $slots, $id, $nextpage);