MDL-28976 quiz was passing 0 to ->attempt_url when it should have been null.
[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 *
ba643847 26 * @package mod
8f37f7fb 27 * @subpackage quiz
ba643847
TH
28 * @copyright 2009 Tim Hunt
29 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
9f9eec1e 30 */
31
32require_once(dirname(__FILE__) . '/../../config.php');
33require_once($CFG->dirroot . '/mod/quiz/locallib.php');
34
404da238
TH
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)
9f9eec1e 37$timenow = time();
38
404da238 39// Get submitted parameters.
9f9eec1e 40$attemptid = required_param('attempt', PARAM_INT);
8f37f7fb
TH
41$next = optional_param('next', false, PARAM_BOOL);
42$thispage = optional_param('thispage', 0, PARAM_INT);
9f9eec1e 43$nextpage = optional_param('nextpage', 0, PARAM_INT);
9f9eec1e 44$finishattempt = optional_param('finishattempt', 0, PARAM_BOOL);
45$timeup = optional_param('timeup', 0, PARAM_BOOL); // True if form was submitted by timer.
8f37f7fb 46$scrollpos = optional_param('scrollpos', '', PARAM_RAW);
9f9eec1e 47
8f37f7fb 48$transaction = $DB->start_delegated_transaction();
990650f9 49$attemptobj = quiz_attempt::create($attemptid);
9f9eec1e 50
404da238 51// Set $nexturl now.
8f37f7fb
TH
52if ($next) {
53 $page = $nextpage;
54} else {
55 $page = $thispage;
56}
57if ($page == -1) {
9f9eec1e 58 $nexturl = $attemptobj->summary_url();
59} else {
45e5fbe1 60 $nexturl = $attemptobj->attempt_url(null, $page);
8f37f7fb 61 if ($scrollpos !== '') {
56e82d99 62 $nexturl->param('scrollpos', $scrollpos);
8f37f7fb 63 }
9f9eec1e 64}
65
404da238 66// We treat automatically closed attempts just like normally closed attempts
9f9eec1e 67if ($timeup) {
68 $finishattempt = 1;
69}
70
404da238 71// Check login.
8f37f7fb 72require_login($attemptobj->get_course(), false, $attemptobj->get_cm());
82165a0c 73require_sesskey();
9f9eec1e 74
404da238 75// Check that this attempt belongs to this user.
9f9eec1e 76if ($attemptobj->get_userid() != $USER->id) {
8f37f7fb 77 throw new moodle_quiz_exception($attemptobj->get_quizobj(), 'notyourattempt');
9f9eec1e 78}
79
404da238 80// Check capabilities.
9f9eec1e 81if (!$attemptobj->is_preview_user()) {
82 $attemptobj->require_capability('mod/quiz:attempt');
83}
84
404da238 85// If the attempt is already closed, send them to the review page.
9f9eec1e 86if ($attemptobj->is_finished()) {
55ca80ed
TH
87 throw new moodle_quiz_exception($attemptobj->get_quizobj(),
88 'attemptalreadyclosed', null, $attemptobj->review_url());
9f9eec1e 89}
90
404da238 91// Don't log - we will end with a redirect to a page that is logged.
9f9eec1e 92
9f9eec1e 93if (!$finishattempt) {
404da238 94 // Just process the responses for this page and go to the next page.
8f37f7fb
TH
95 try {
96 $attemptobj->process_all_actions($timenow);
55ca80ed 97 } catch (question_out_of_sequence_exception $e) {
8f37f7fb 98 print_error('submissionoutofsequencefriendlymessage', 'question',
45e5fbe1 99 $attemptobj->attempt_url(null, $thispage));
8f37f7fb
TH
100 }
101 $transaction->allow_commit();
9f9eec1e 102 redirect($nexturl);
103}
104
404da238 105// Otherwise, we have been asked to finish attempt, so do that.
9f9eec1e 106
404da238 107// Log the end of this attempt.
9f9eec1e 108add_to_log($attemptobj->get_courseid(), 'quiz', 'close attempt',
109 'review.php?attempt=' . $attemptobj->get_attemptid(),
110 $attemptobj->get_quizid(), $attemptobj->get_cmid());
111
404da238 112// Update the quiz attempt record.
8f37f7fb 113$attemptobj->finish_attempt($timenow);
9f9eec1e 114
404da238 115// Send the user to the review page.
8f37f7fb 116$transaction->allow_commit();
9f9eec1e 117redirect($attemptobj->review_url());