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/>.
16
17/**
18 * This script displays a particular page of a quiz attempt that is in progress.
19 *
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
23 */
24
25require_once(__DIR__ . '/../../config.php');
26require_once($CFG->dirroot . '/mod/quiz/locallib.php');
27
28// Look for old-style URLs, such as may be in the logs, and redirect them to startattemtp.php.
29if ($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');
34    }
35    redirect(new moodle_url('/mod/quiz/startattempt.php',
36            array('cmid' => $cm->id, 'sesskey' => sesskey())));
37}
38
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);
43
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));
47// During quiz attempts, the browser back/forwards buttons should force a reload.
48$PAGE->set_cacheable(false);
49
50// Check login.
51require_login($attemptobj->get_course(), false, $attemptobj->get_cm());
52
53// Check that this attempt belongs to this user.
54if ($attemptobj->get_userid() != $USER->id) {
55    if ($attemptobj->has_capability('mod/quiz:viewreports')) {
56        redirect($attemptobj->review_url(null, $page));
57    } else {
58        throw new moodle_quiz_exception($attemptobj->get_quizobj(), 'notyourattempt');
59    }
60}
61
62// Check capabilities and block settings.
63if (!$attemptobj->is_preview_user()) {
64    $attemptobj->require_capability('mod/quiz:attempt');
65    if (empty($attemptobj->get_quiz()->showblocks)) {
66        $PAGE->blocks->show_only_fake_blocks();
67    }
68
69} else {
70    navigation_node::override_active_url($attemptobj->start_attempt_url());
71}
72
73// If the attempt is already closed, send them to the review page.
74if ($attemptobj->is_finished()) {
75    redirect($attemptobj->review_url(null, $page));
76} else if ($attemptobj->get_state() == quiz_attempt::OVERDUE) {
77    redirect($attemptobj->summary_url());
78}
79
80// Check the access rules.
81$accessmanager = $attemptobj->get_access_manager(time());
82$accessmanager->setup_attempt_page($PAGE);
83$output = $PAGE->get_renderer('mod_quiz');
84$messages = $accessmanager->prevent_access();
85if (!$attemptobj->is_preview_user() && $messages) {
86    print_error('attempterror', 'quiz', $attemptobj->view_url(),
87            $output->access_messages($messages));
88}
89if ($accessmanager->is_preflight_check_required($attemptobj->get_attemptid())) {
90    redirect($attemptobj->start_attempt_url(null, $page));
91}
92
93// Set up auto-save if required.
94$autosaveperiod = get_config('quiz', 'autosaveperiod');
95if ($autosaveperiod) {
96    $PAGE->requires->yui_module('moodle-mod_quiz-autosave',
97            'M.mod_quiz.autosave.init', array($autosaveperiod));
98}
99
100// Log this page view.
101$attemptobj->fire_attempt_viewed_event();
102
103// Get the list of questions needed by this page.
104$slots = $attemptobj->get_slots($page);
105
106// Check.
107if (empty($slots)) {
108    throw new moodle_quiz_exception($attemptobj->get_quizobj(), 'noquestionsfound');
109}
110
111// Update attempt page, redirecting the user if $page is not valid.
112if (!$attemptobj->set_currentpage($page)) {
113    redirect($attemptobj->start_attempt_url(null, $attemptobj->get_currentpage()));
114}
115
116// Initialise the JavaScript.
117$headtags = $attemptobj->get_html_head_contributions($page);
118$PAGE->requires->js_init_call('M.mod_quiz.init_attempt_form', null, false, quiz_get_js_module());
119\core\session\manager::keepalive(); // Try to prevent sessions expiring during quiz attempts.
120
121// Arrange for the navigation to be displayed in the first region on the page.
122$navbc = $attemptobj->get_navigation_panel($output, 'quiz_attempt_nav_panel', $page);
123$regions = $PAGE->blocks->get_regions();
124$PAGE->blocks->add_fake_block($navbc, reset($regions));
125
126$headtags = $attemptobj->get_html_head_contributions($page);
127$PAGE->set_title($attemptobj->attempt_page_title($page));
128$PAGE->set_heading($attemptobj->get_course()->fullname);
129
130if ($attemptobj->is_last_page($page)) {
131    $nextpage = -1;
132} else {
133    $nextpage = $page + 1;
134}
135
136echo $output->attempt_page($attemptobj, $page, $accessmanager, $messages, $slots, $id, $nextpage);
137