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.
28 * @copyright 2009 Tim Hunt
29 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
32 require_once(dirname(__FILE__) . '/../../config.php');
33 require_once($CFG->dirroot . '/mod/quiz/locallib.php');
35 // Remember the current time as the time any responses were submitted
36 // (so as to make sure students don't get penalized for slow processing on this page).
39 // Get submitted parameters.
40 $attemptid = required_param('attempt', PARAM_INT);
41 $next = optional_param('next', false, PARAM_BOOL);
42 $thispage = optional_param('thispage', 0, PARAM_INT);
43 $nextpage = optional_param('nextpage', 0, PARAM_INT);
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 $transaction = $DB->start_delegated_transaction();
49 $attemptobj = quiz_attempt::create($attemptid);
58 $nexturl = $attemptobj->summary_url();
60 $nexturl = $attemptobj->attempt_url(null, $page);
61 if ($scrollpos !== '') {
62 $nexturl->param('scrollpos', $scrollpos);
66 // If there is only a very small amount of time left, there is no point trying
67 // to show the student another page of the quiz. Just finish now.
68 $graceperiodmin = null;
69 $accessmanager = $attemptobj->get_access_manager($timenow);
70 $timeleft = $accessmanager->get_time_left($attemptobj->get_attempt(), $timenow);
72 if ($timeleft !== false && $timeleft < QUIZ_MIN_TIME_TO_CONTINUE) {
74 $graceperiodmin = get_config('quiz', 'graceperiodmin');
75 if ($timeleft < -$graceperiodmin) {
81 require_login($attemptobj->get_course(), false, $attemptobj->get_cm());
84 // Check that this attempt belongs to this user.
85 if ($attemptobj->get_userid() != $USER->id) {
86 throw new moodle_quiz_exception($attemptobj->get_quizobj(), 'notyourattempt');
89 // Check capabilities.
90 if (!$attemptobj->is_preview_user()) {
91 $attemptobj->require_capability('mod/quiz:attempt');
94 // If the attempt is already closed, send them to the review page.
95 if ($attemptobj->is_finished()) {
96 throw new moodle_quiz_exception($attemptobj->get_quizobj(),
97 'attemptalreadyclosed', null, $attemptobj->review_url());
100 // If time is running out, trigger the appropriate action.
101 $becomingoverdue = false;
102 $becomingabandoned = false;
104 if ($attemptobj->get_quiz()->overduehandling == 'graceperiod') {#
105 if (is_null($graceperiodmin)) {
106 $graceperiodmin = get_config('quiz', 'graceperiodmin');
108 if ($timeleft < -$attemptobj->get_quiz()->graceperiod - $graceperiodmin) {
109 // Grace period has run out.
110 $finishattempt = true;
111 $becomingabandoned = true;
113 $becomingoverdue = true;
116 $finishattempt = true;
120 // Don't log - we will end with a redirect to a page that is logged.
122 if (!$finishattempt) {
123 // Just process the responses for this page and go to the next page.
126 $attemptobj->process_submitted_actions($timenow, $becomingoverdue);
128 } catch (question_out_of_sequence_exception $e) {
129 print_error('submissionoutofsequencefriendlymessage', 'question',
130 $attemptobj->attempt_url(null, $thispage));
132 } catch (Exception $e) {
133 // This sucks, if we display our own custom error message, there is no way
134 // to display the original stack trace.
136 if (!empty($e->debuginfo)) {
137 $debuginfo = $e->debuginfo;
139 print_error('errorprocessingresponses', 'question',
140 $attemptobj->attempt_url(null, $thispage), $e->getMessage(), $debuginfo);
144 // The student is too late.
145 $attemptobj->process_going_overdue($timenow, true);
148 $transaction->allow_commit();
149 if ($becomingoverdue) {
150 redirect($attemptobj->summary_url());
156 // Otherwise, we have been asked to finish attempt, so do that.
158 // Log the end of this attempt.
159 add_to_log($attemptobj->get_courseid(), 'quiz', 'close attempt',
160 'review.php?attempt=' . $attemptobj->get_attemptid(),
161 $attemptobj->get_quizid(), $attemptobj->get_cmid());
163 // Update the quiz attempt record.
165 if ($becomingabandoned) {
166 $attemptobj->process_abandon($timenow, true);
168 $attemptobj->process_finish($timenow, !$toolate);
171 } catch (question_out_of_sequence_exception $e) {
172 print_error('submissionoutofsequencefriendlymessage', 'question',
173 $attemptobj->attempt_url(null, $thispage));
175 } catch (Exception $e) {
176 // This sucks, if we display our own custom error message, there is no way
177 // to display the original stack trace.
179 if (!empty($e->debuginfo)) {
180 $debuginfo = $e->debuginfo;
182 print_error('errorprocessingresponses', 'question',
183 $attemptobj->attempt_url(null, $thispage), $e->getMessage(), $debuginfo);
186 // Send the user to the review page.
187 $transaction->allow_commit();
188 redirect($attemptobj->review_url());