MDL-58546 libraries: Patch ADOdb for PHP 7.2 compat
[moodle.git] / mod / quiz / startattempt.php
blob7d2398ebce6d2a228d43dcac7a921de094dd4938
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 deals with starting a new attempt at a quiz.
20 * Normally, it will end up redirecting to attempt.php - unless a password form is displayed.
22 * This code used to be at the top of attempt.php, if you are looking for CVS history.
24 * @package mod_quiz
25 * @copyright 2009 The Open University
26 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
29 require_once(__DIR__ . '/../../config.php');
30 require_once($CFG->dirroot . '/mod/quiz/locallib.php');
32 // Get submitted parameters.
33 $id = required_param('cmid', PARAM_INT); // Course module id
34 $forcenew = optional_param('forcenew', false, PARAM_BOOL); // Used to force a new preview
35 $page = optional_param('page', -1, PARAM_INT); // Page to jump to in the attempt.
37 if (!$cm = get_coursemodule_from_id('quiz', $id)) {
38 print_error('invalidcoursemodule');
40 if (!$course = $DB->get_record('course', array('id' => $cm->course))) {
41 print_error("coursemisconf");
44 $quizobj = quiz::create($cm->instance, $USER->id);
45 // This script should only ever be posted to, so set page URL to the view page.
46 $PAGE->set_url($quizobj->view_url());
48 // Check login and sesskey.
49 require_login($quizobj->get_course(), false, $quizobj->get_cm());
50 require_sesskey();
51 $PAGE->set_heading($quizobj->get_course()->fullname);
53 // If no questions have been set up yet redirect to edit.php or display an error.
54 if (!$quizobj->has_questions()) {
55 if ($quizobj->has_capability('mod/quiz:manage')) {
56 redirect($quizobj->edit_url());
57 } else {
58 print_error('cannotstartnoquestions', 'quiz', $quizobj->view_url());
62 // Create an object to manage all the other (non-roles) access rules.
63 $timenow = time();
64 $accessmanager = $quizobj->get_access_manager($timenow);
66 // Validate permissions for creating a new attempt and start a new preview attempt if required.
67 list($currentattemptid, $attemptnumber, $lastattempt, $messages, $page) =
68 quiz_validate_new_attempt($quizobj, $accessmanager, $forcenew, $page, true);
70 // Check access.
71 if (!$quizobj->is_preview_user() && $messages) {
72 $output = $PAGE->get_renderer('mod_quiz');
73 print_error('attempterror', 'quiz', $quizobj->view_url(),
74 $output->access_messages($messages));
77 if ($accessmanager->is_preflight_check_required($currentattemptid)) {
78 // Need to do some checks before allowing the user to continue.
79 $mform = $accessmanager->get_preflight_check_form(
80 $quizobj->start_attempt_url($page), $currentattemptid);
82 if ($mform->is_cancelled()) {
83 $accessmanager->back_to_view_page($PAGE->get_renderer('mod_quiz'));
85 } else if (!$mform->get_data()) {
87 // Form not submitted successfully, re-display it and stop.
88 $PAGE->set_url($quizobj->start_attempt_url($page));
89 $PAGE->set_title($quizobj->get_quiz_name());
90 $accessmanager->setup_attempt_page($PAGE);
91 $output = $PAGE->get_renderer('mod_quiz');
92 if (empty($quizobj->get_quiz()->showblocks)) {
93 $PAGE->blocks->show_only_fake_blocks();
96 echo $output->start_attempt_page($quizobj, $mform);
97 die();
100 // Pre-flight check passed.
101 $accessmanager->notify_preflight_check_passed($currentattemptid);
103 if ($currentattemptid) {
104 if ($lastattempt->state == quiz_attempt::OVERDUE) {
105 redirect($quizobj->summary_url($lastattempt->id));
106 } else {
107 redirect($quizobj->attempt_url($currentattemptid, $page));
111 $attempt = quiz_prepare_and_start_new_attempt($quizobj, $attemptnumber, $lastattempt);
113 // Redirect to the attempt page.
114 redirect($quizobj->attempt_url($attempt->id, $page));