MDL-40063 mod_quiz: removed 'close attempt' add_to_log call
[moodle.git] / mod / quiz / processattempt.php
CommitLineData
83192608 1<?php
8f37f7fb
TH
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.
13//
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/>.
16
9f9eec1e 17/**
18 * This page deals with processing responses during an attempt at a quiz.
19 *
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.
23 *
24 * This code used to be near the top of attempt.php, if you are looking for CVS history.
25 *
01773a6d
SH
26 * @package mod_quiz
27 * @copyright 2009 Tim Hunt
28 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
9f9eec1e 29 */
30
31require_once(dirname(__FILE__) . '/../../config.php');
32require_once($CFG->dirroot . '/mod/quiz/locallib.php');
33
404da238 34// Remember the current time as the time any responses were submitted
9e83f3d1 35// (so as to make sure students don't get penalized for slow processing on this page).
9f9eec1e 36$timenow = time();
37
404da238 38// Get submitted parameters.
ff2ec2cb 39$attemptid = required_param('attempt', PARAM_INT);
a8b8f472
DP
40$thispage = optional_param('thispage', 0, PARAM_INT);
41$nextpage = optional_param('nextpage', 0, PARAM_INT);
ff2ec2cb 42$next = optional_param('next', false, PARAM_BOOL);
34b7d838 43$finishattempt = optional_param('finishattempt', false, PARAM_BOOL);
ff2ec2cb
TH
44$timeup = optional_param('timeup', 0, PARAM_BOOL); // True if form was submitted by timer.
45$scrollpos = optional_param('scrollpos', '', PARAM_RAW);
9f9eec1e 46
8f37f7fb 47$transaction = $DB->start_delegated_transaction();
990650f9 48$attemptobj = quiz_attempt::create($attemptid);
9f9eec1e 49
404da238 50// Set $nexturl now.
8f37f7fb
TH
51if ($next) {
52 $page = $nextpage;
53} else {
54 $page = $thispage;
55}
56if ($page == -1) {
9f9eec1e 57 $nexturl = $attemptobj->summary_url();
58} else {
45e5fbe1 59 $nexturl = $attemptobj->attempt_url(null, $page);
8f37f7fb 60 if ($scrollpos !== '') {
56e82d99 61 $nexturl->param('scrollpos', $scrollpos);
8f37f7fb 62 }
9f9eec1e 63}
64
3e77b60f 65// If there is only a very small amount of time left, there is no point trying
34b7d838 66// to show the student another page of the quiz. Just finish now.
a6866c7e 67$graceperiodmin = null;
34b7d838 68$accessmanager = $attemptobj->get_access_manager($timenow);
8e771aed
MP
69$timeclose = $accessmanager->get_end_time($attemptobj->get_attempt());
70
71// Don't enforce timeclose for previews
72if ($attemptobj->is_preview()) {
73 $timeclose = false;
74}
3e77b60f 75$toolate = false;
8e771aed 76if ($timeclose !== false && $timenow > $timeclose - QUIZ_MIN_TIME_TO_CONTINUE) {
34b7d838 77 $timeup = true;
a6866c7e 78 $graceperiodmin = get_config('quiz', 'graceperiodmin');
8e771aed 79 if ($timenow > $timeclose + $graceperiodmin) {
3e77b60f
TH
80 $toolate = true;
81 }
9f9eec1e 82}
83
404da238 84// Check login.
8f37f7fb 85require_login($attemptobj->get_course(), false, $attemptobj->get_cm());
82165a0c 86require_sesskey();
9f9eec1e 87
404da238 88// Check that this attempt belongs to this user.
9f9eec1e 89if ($attemptobj->get_userid() != $USER->id) {
8f37f7fb 90 throw new moodle_quiz_exception($attemptobj->get_quizobj(), 'notyourattempt');
9f9eec1e 91}
92
404da238 93// Check capabilities.
9f9eec1e 94if (!$attemptobj->is_preview_user()) {
95 $attemptobj->require_capability('mod/quiz:attempt');
96}
97
404da238 98// If the attempt is already closed, send them to the review page.
9f9eec1e 99if ($attemptobj->is_finished()) {
55ca80ed
TH
100 throw new moodle_quiz_exception($attemptobj->get_quizobj(),
101 'attemptalreadyclosed', null, $attemptobj->review_url());
9f9eec1e 102}
103
34b7d838
TH
104// If time is running out, trigger the appropriate action.
105$becomingoverdue = false;
a6866c7e 106$becomingabandoned = false;
34b7d838 107if ($timeup) {
e13388c1 108 if ($attemptobj->get_quiz()->overduehandling == 'graceperiod') {
a6866c7e
TH
109 if (is_null($graceperiodmin)) {
110 $graceperiodmin = get_config('quiz', 'graceperiodmin');
111 }
8e771aed 112 if ($timenow > $timeclose + $attemptobj->get_quiz()->graceperiod + $graceperiodmin) {
a6866c7e
TH
113 // Grace period has run out.
114 $finishattempt = true;
115 $becomingabandoned = true;
116 } else {
117 $becomingoverdue = true;
118 }
34b7d838
TH
119 } else {
120 $finishattempt = true;
121 }
122}
123
404da238 124// Don't log - we will end with a redirect to a page that is logged.
9f9eec1e 125
9f9eec1e 126if (!$finishattempt) {
404da238 127 // Just process the responses for this page and go to the next page.
3e77b60f
TH
128 if (!$toolate) {
129 try {
130 $attemptobj->process_submitted_actions($timenow, $becomingoverdue);
131
132 } catch (question_out_of_sequence_exception $e) {
133 print_error('submissionoutofsequencefriendlymessage', 'question',
134 $attemptobj->attempt_url(null, $thispage));
135
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;
142 }
143 print_error('errorprocessingresponses', 'question',
144 $attemptobj->attempt_url(null, $thispage), $e->getMessage(), $debuginfo);
bed49557 145 }
3e77b60f
TH
146
147 } else {
148 // The student is too late.
149 $attemptobj->process_going_overdue($timenow, true);
8f37f7fb 150 }
bed49557 151
8f37f7fb 152 $transaction->allow_commit();
34b7d838
TH
153 if ($becomingoverdue) {
154 redirect($attemptobj->summary_url());
155 } else {
156 redirect($nexturl);
157 }
9f9eec1e 158}
159
404da238 160// Update the quiz attempt record.
bed49557 161try {
a6866c7e
TH
162 if ($becomingabandoned) {
163 $attemptobj->process_abandon($timenow, true);
164 } else {
165 $attemptobj->process_finish($timenow, !$toolate);
166 }
bed49557
TH
167
168} catch (question_out_of_sequence_exception $e) {
169 print_error('submissionoutofsequencefriendlymessage', 'question',
170 $attemptobj->attempt_url(null, $thispage));
171
172} catch (Exception $e) {
173 // This sucks, if we display our own custom error message, there is no way
174 // to display the original stack trace.
175 $debuginfo = '';
176 if (!empty($e->debuginfo)) {
177 $debuginfo = $e->debuginfo;
178 }
179 print_error('errorprocessingresponses', 'question',
180 $attemptobj->attempt_url(null, $thispage), $e->getMessage(), $debuginfo);
181}
9f9eec1e 182
404da238 183// Send the user to the review page.
8f37f7fb 184$transaction->allow_commit();
9f9eec1e 185redirect($attemptobj->review_url());