Moodle release 3.10.4
[moodle.git] / mod / quiz / processattempt.php
blob50dba84d6ee7c4d6a5e31a0fa4f0a11bfda56872
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 page deals with processing responses during an attempt at a quiz.
20 * People will normally arrive here from a form submission on attempt.php or
21 * summary.php, and once the responses are processed, they will be redirected to
22 * attempt.php or summary.php.
24 * This code used to be near the top of attempt.php, if you are looking for CVS history.
26 * @package mod_quiz
27 * @copyright 2009 Tim Hunt
28 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
31 require_once(__DIR__ . '/../../config.php');
32 require_once($CFG->dirroot . '/mod/quiz/locallib.php');
34 // Remember the current time as the time any responses were submitted
35 // (so as to make sure students don't get penalized for slow processing on this page).
36 $timenow = time();
38 // Get submitted parameters.
39 $attemptid = required_param('attempt', PARAM_INT);
40 $thispage = optional_param('thispage', 0, PARAM_INT);
41 $nextpage = optional_param('nextpage', 0, PARAM_INT);
42 $previous = optional_param('previous', false, PARAM_BOOL);
43 $next = optional_param('next', false, PARAM_BOOL);
44 $finishattempt = optional_param('finishattempt', false, PARAM_BOOL);
45 $timeup = optional_param('timeup', 0, PARAM_BOOL); // True if form was submitted by timer.
46 $scrollpos = optional_param('scrollpos', '', PARAM_RAW);
47 $cmid = optional_param('cmid', null, PARAM_INT);
49 $attemptobj = quiz_create_attempt_handling_errors($attemptid, $cmid);
51 // Set $nexturl now.
52 if ($next) {
53 $page = $nextpage;
54 } else if ($previous && $thispage > 0) {
55 $page = $thispage - 1;
56 } else {
57 $page = $thispage;
59 if ($page == -1) {
60 $nexturl = $attemptobj->summary_url();
61 } else {
62 $nexturl = $attemptobj->attempt_url(null, $page);
63 if ($scrollpos !== '') {
64 $nexturl->param('scrollpos', $scrollpos);
68 // Check login.
69 require_login($attemptobj->get_course(), false, $attemptobj->get_cm());
70 require_sesskey();
72 // Check that this attempt belongs to this user.
73 if ($attemptobj->get_userid() != $USER->id) {
74 throw new moodle_quiz_exception($attemptobj->get_quizobj(), 'notyourattempt');
77 // Check capabilities.
78 if (!$attemptobj->is_preview_user()) {
79 $attemptobj->require_capability('mod/quiz:attempt');
82 // If the attempt is already closed, send them to the review page.
83 if ($attemptobj->is_finished()) {
84 throw new moodle_quiz_exception($attemptobj->get_quizobj(),
85 'attemptalreadyclosed', null, $attemptobj->review_url());
88 // Process the attempt, getting the new status for the attempt.
89 $status = $attemptobj->process_attempt($timenow, $finishattempt, $timeup, $thispage);
91 if ($status == quiz_attempt::OVERDUE) {
92 redirect($attemptobj->summary_url());
93 } else if ($status == quiz_attempt::IN_PROGRESS) {
94 redirect($nexturl);
95 } else {
96 // Attempt abandoned or finished.
97 redirect($attemptobj->review_url());