MDL-49813 quiz: add sections in the right place after page changes
[moodle.git] / mod / quiz / processattempt.php
blob1b5070ed2cb3e331b6d72ba2277d214acae5433f
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(dirname(__FILE__) . '/../../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 $next = optional_param('next', false, PARAM_BOOL);
43 $finishattempt = optional_param('finishattempt', false, PARAM_BOOL);
44 $timeup = optional_param('timeup', 0, PARAM_BOOL); // True if form was submitted by timer.
45 $scrollpos = optional_param('scrollpos', '', PARAM_RAW);
47 $transaction = $DB->start_delegated_transaction();
48 $attemptobj = quiz_attempt::create($attemptid);
50 // Set $nexturl now.
51 if ($next) {
52 $page = $nextpage;
53 } else {
54 $page = $thispage;
56 if ($page == -1) {
57 $nexturl = $attemptobj->summary_url();
58 } else {
59 $nexturl = $attemptobj->attempt_url(null, $page);
60 if ($scrollpos !== '') {
61 $nexturl->param('scrollpos', $scrollpos);
65 // If there is only a very small amount of time left, there is no point trying
66 // to show the student another page of the quiz. Just finish now.
67 $graceperiodmin = null;
68 $accessmanager = $attemptobj->get_access_manager($timenow);
69 $timeclose = $accessmanager->get_end_time($attemptobj->get_attempt());
71 // Don't enforce timeclose for previews
72 if ($attemptobj->is_preview()) {
73 $timeclose = false;
75 $toolate = false;
76 if ($timeclose !== false && $timenow > $timeclose - QUIZ_MIN_TIME_TO_CONTINUE) {
77 $timeup = true;
78 $graceperiodmin = get_config('quiz', 'graceperiodmin');
79 if ($timenow > $timeclose + $graceperiodmin) {
80 $toolate = true;
84 // Check login.
85 require_login($attemptobj->get_course(), false, $attemptobj->get_cm());
86 require_sesskey();
88 // Check that this attempt belongs to this user.
89 if ($attemptobj->get_userid() != $USER->id) {
90 throw new moodle_quiz_exception($attemptobj->get_quizobj(), 'notyourattempt');
93 // Check capabilities.
94 if (!$attemptobj->is_preview_user()) {
95 $attemptobj->require_capability('mod/quiz:attempt');
98 // If the attempt is already closed, send them to the review page.
99 if ($attemptobj->is_finished()) {
100 throw new moodle_quiz_exception($attemptobj->get_quizobj(),
101 'attemptalreadyclosed', null, $attemptobj->review_url());
104 // If time is running out, trigger the appropriate action.
105 $becomingoverdue = false;
106 $becomingabandoned = false;
107 if ($timeup) {
108 if ($attemptobj->get_quiz()->overduehandling == 'graceperiod') {
109 if (is_null($graceperiodmin)) {
110 $graceperiodmin = get_config('quiz', 'graceperiodmin');
112 if ($timenow > $timeclose + $attemptobj->get_quiz()->graceperiod + $graceperiodmin) {
113 // Grace period has run out.
114 $finishattempt = true;
115 $becomingabandoned = true;
116 } else {
117 $becomingoverdue = true;
119 } else {
120 $finishattempt = true;
124 // Don't log - we will end with a redirect to a page that is logged.
126 if (!$finishattempt) {
127 // Just process the responses for this page and go to the next page.
128 if (!$toolate) {
129 try {
130 $attemptobj->process_submitted_actions($timenow, $becomingoverdue);
132 } catch (question_out_of_sequence_exception $e) {
133 print_error('submissionoutofsequencefriendlymessage', 'question',
134 $attemptobj->attempt_url(null, $thispage));
136 } catch (Exception $e) {
137 // This sucks, if we display our own custom error message, there is no way
138 // to display the original stack trace.
139 $debuginfo = '';
140 if (!empty($e->debuginfo)) {
141 $debuginfo = $e->debuginfo;
143 print_error('errorprocessingresponses', 'question',
144 $attemptobj->attempt_url(null, $thispage), $e->getMessage(), $debuginfo);
147 if (!$becomingoverdue) {
148 foreach ($attemptobj->get_slots() as $slot) {
149 if (optional_param('redoslot' . $slot, false, PARAM_BOOL)) {
150 $attemptobj->process_redo_question($slot, $timenow);
155 } else {
156 // The student is too late.
157 $attemptobj->process_going_overdue($timenow, true);
160 $transaction->allow_commit();
161 if ($becomingoverdue) {
162 redirect($attemptobj->summary_url());
163 } else {
164 redirect($nexturl);
168 // Update the quiz attempt record.
169 try {
170 if ($becomingabandoned) {
171 $attemptobj->process_abandon($timenow, true);
172 } else {
173 $attemptobj->process_finish($timenow, !$toolate);
176 } catch (question_out_of_sequence_exception $e) {
177 print_error('submissionoutofsequencefriendlymessage', 'question',
178 $attemptobj->attempt_url(null, $thispage));
180 } catch (Exception $e) {
181 // This sucks, if we display our own custom error message, there is no way
182 // to display the original stack trace.
183 $debuginfo = '';
184 if (!empty($e->debuginfo)) {
185 $debuginfo = $e->debuginfo;
187 print_error('errorprocessingresponses', 'question',
188 $attemptobj->attempt_url(null, $thispage), $e->getMessage(), $debuginfo);
191 // Send the user to the review page.
192 $transaction->allow_commit();
193 redirect($attemptobj->review_url());