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 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.
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).
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
);
48 $attemptobj = quiz_attempt
::create($attemptid);
53 } else if ($previous && $thispage > 0) {
54 $page = $thispage - 1;
59 $nexturl = $attemptobj->summary_url();
61 $nexturl = $attemptobj->attempt_url(null, $page);
62 if ($scrollpos !== '') {
63 $nexturl->param('scrollpos', $scrollpos);
68 require_login($attemptobj->get_course(), false, $attemptobj->get_cm());
71 // Check that this attempt belongs to this user.
72 if ($attemptobj->get_userid() != $USER->id
) {
73 throw new moodle_quiz_exception($attemptobj->get_quizobj(), 'notyourattempt');
76 // Check capabilities.
77 if (!$attemptobj->is_preview_user()) {
78 $attemptobj->require_capability('mod/quiz:attempt');
81 // If the attempt is already closed, send them to the review page.
82 if ($attemptobj->is_finished()) {
83 throw new moodle_quiz_exception($attemptobj->get_quizobj(),
84 'attemptalreadyclosed', null, $attemptobj->review_url());
87 // Process the attempt, getting the new status for the attempt.
88 $status = $attemptobj->process_attempt($timenow, $finishattempt, $timeup, $thispage);
90 if ($status == quiz_attempt
::OVERDUE
) {
91 redirect($attemptobj->summary_url());
92 } else if ($status == quiz_attempt
::IN_PROGRESS
) {
95 // Attempt abandoned or finished.
96 redirect($attemptobj->review_url());