2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
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.
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 throw new \
moodle_exception('invalidcoursemodule');
40 if (!$course = $DB->get_record('course', array('id' => $cm->course
))) {
41 throw new \
moodle_exception("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());
47 // During quiz attempts, the browser back/forwards buttons should force a reload.
48 $PAGE->set_cacheable(false);
50 // Check login and sesskey.
51 require_login($quizobj->get_course(), false, $quizobj->get_cm());
53 $PAGE->set_heading($quizobj->get_course()->fullname
);
55 // If no questions have been set up yet redirect to edit.php or display an error.
56 if (!$quizobj->has_questions()) {
57 if ($quizobj->has_capability('mod/quiz:manage')) {
58 redirect($quizobj->edit_url());
60 throw new \
moodle_exception('cannotstartnoquestions', 'quiz', $quizobj->view_url());
64 // Create an object to manage all the other (non-roles) access rules.
66 $accessmanager = $quizobj->get_access_manager($timenow);
68 // Validate permissions for creating a new attempt and start a new preview attempt if required.
69 list($currentattemptid, $attemptnumber, $lastattempt, $messages, $page) =
70 quiz_validate_new_attempt($quizobj, $accessmanager, $forcenew, $page, true);
73 if (!$quizobj->is_preview_user() && $messages) {
74 $output = $PAGE->get_renderer('mod_quiz');
75 throw new \
moodle_exception('attempterror', 'quiz', $quizobj->view_url(),
76 $output->access_messages($messages));
79 if ($accessmanager->is_preflight_check_required($currentattemptid)) {
80 // Need to do some checks before allowing the user to continue.
81 $mform = $accessmanager->get_preflight_check_form(
82 $quizobj->start_attempt_url($page), $currentattemptid);
84 if ($mform->is_cancelled()) {
85 $accessmanager->back_to_view_page($PAGE->get_renderer('mod_quiz'));
87 } else if (!$mform->get_data()) {
89 // Form not submitted successfully, re-display it and stop.
90 $PAGE->set_url($quizobj->start_attempt_url($page));
91 $PAGE->set_title($quizobj->get_quiz_name());
92 $accessmanager->setup_attempt_page($PAGE);
93 $output = $PAGE->get_renderer('mod_quiz');
94 if (empty($quizobj->get_quiz()->showblocks
)) {
95 $PAGE->blocks
->show_only_fake_blocks();
98 echo $output->start_attempt_page($quizobj, $mform);
102 // Pre-flight check passed.
103 $accessmanager->notify_preflight_check_passed($currentattemptid);
105 if ($currentattemptid) {
106 if ($lastattempt->state
== quiz_attempt
::OVERDUE
) {
107 redirect($quizobj->summary_url($lastattempt->id
));
109 redirect($quizobj->attempt_url($currentattemptid, $page));
113 $attempt = quiz_prepare_and_start_new_attempt($quizobj, $attemptnumber, $lastattempt);
115 // Redirect to the attempt page.
116 redirect($quizobj->attempt_url($attempt->id
, $page));