MDL-61046 lang: Merge English strings from the en_fix language pack
[moodle.git] / mod / quiz / attempt.php
blob96b8e1c0950cbd61dafb6510d4389a8a840a7899
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);
43 $attemptobj = quiz_attempt::create($attemptid);
44 $page = $attemptobj->force_page_number_into_range($page);
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(null, $page));
54 } else {
55 throw new moodle_quiz_exception($attemptobj->get_quizobj(), 'notyourattempt');
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();
66 } else {
67 navigation_node::override_active_url($attemptobj->start_attempt_url());
70 // If the attempt is already closed, send them to the review page.
71 if ($attemptobj->is_finished()) {
72 redirect($attemptobj->review_url(null, $page));
73 } else if ($attemptobj->get_state() == quiz_attempt::OVERDUE) {
74 redirect($attemptobj->summary_url());
77 // Check the access rules.
78 $accessmanager = $attemptobj->get_access_manager(time());
79 $accessmanager->setup_attempt_page($PAGE);
80 $output = $PAGE->get_renderer('mod_quiz');
81 $messages = $accessmanager->prevent_access();
82 if (!$attemptobj->is_preview_user() && $messages) {
83 print_error('attempterror', 'quiz', $attemptobj->view_url(),
84 $output->access_messages($messages));
86 if ($accessmanager->is_preflight_check_required($attemptobj->get_attemptid())) {
87 redirect($attemptobj->start_attempt_url(null, $page));
90 // Set up auto-save if required.
91 $autosaveperiod = get_config('quiz', 'autosaveperiod');
92 if ($autosaveperiod) {
93 $PAGE->requires->yui_module('moodle-mod_quiz-autosave',
94 'M.mod_quiz.autosave.init', array($autosaveperiod));
97 // Log this page view.
98 $attemptobj->fire_attempt_viewed_event();
100 // Get the list of questions needed by this page.
101 $slots = $attemptobj->get_slots($page);
103 // Check.
104 if (empty($slots)) {
105 throw new moodle_quiz_exception($attemptobj->get_quizobj(), 'noquestionsfound');
108 // Update attempt page, redirecting the user if $page is not valid.
109 if (!$attemptobj->set_currentpage($page)) {
110 redirect($attemptobj->start_attempt_url(null, $attemptobj->get_currentpage()));
113 // Initialise the JavaScript.
114 $headtags = $attemptobj->get_html_head_contributions($page);
115 $PAGE->requires->js_init_call('M.mod_quiz.init_attempt_form', null, false, quiz_get_js_module());
117 // Arrange for the navigation to be displayed in the first region on the page.
118 $navbc = $attemptobj->get_navigation_panel($output, 'quiz_attempt_nav_panel', $page);
119 $regions = $PAGE->blocks->get_regions();
120 $PAGE->blocks->add_fake_block($navbc, reset($regions));
122 $title = get_string('attempt', 'quiz', $attemptobj->get_attempt_number());
123 $headtags = $attemptobj->get_html_head_contributions($page);
124 $PAGE->set_title($attemptobj->get_quiz_name());
125 $PAGE->set_heading($attemptobj->get_course()->fullname);
127 if ($attemptobj->is_last_page($page)) {
128 $nextpage = -1;
129 } else {
130 $nextpage = $page + 1;
133 echo $output->attempt_page($attemptobj, $page, $accessmanager, $messages, $slots, $id, $nextpage);