MDL-3030 quiz overdue handling: show the attempt state in the reports.
[moodle.git] / mod / quiz / startattempt.php
CommitLineData
83192608 1<?php
7c5bd5bf
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
78e7a3dd 17/**
7c5bd5bf 18 * This script deals with starting a new attempt at a quiz.
78e7a3dd 19 *
20 * Normally, it will end up redirecting to attempt.php - unless a password form is displayed.
21 *
ccc45155 22 * This code used to be at the top of attempt.php, if you are looking for CVS history.
23 *
ba643847 24 * @package mod
7c5bd5bf 25 * @subpackage quiz
ba643847
TH
26 * @copyright 2009 The Open University
27 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
78e7a3dd 28 */
29
30require_once(dirname(__FILE__) . '/../../config.php');
31require_once($CFG->dirroot . '/mod/quiz/locallib.php');
32
7c5bd5bf 33// Get submitted parameters.
da729916 34$id = required_param('cmid', PARAM_INT); // Course module id
78e7a3dd 35$forcenew = optional_param('forcenew', false, PARAM_BOOL); // Used to force a new preview
5db82949 36$page = optional_param('page', -1, PARAM_INT); // Page to jump to in the attempt.
78e7a3dd 37
38if (!$cm = get_coursemodule_from_id('quiz', $id)) {
39 print_error('invalidcoursemodule');
40}
41if (!$course = $DB->get_record('course', array('id' => $cm->course))) {
42 print_error("coursemisconf");
43}
78e7a3dd 44
dd70d561 45$quizobj = quiz::create($cm->instance, $USER->id);
a88ba570
TH
46// This script should only ever be posted to, so set page URL to the view page.
47$PAGE->set_url($quizobj->view_url());
78e7a3dd 48
7c5bd5bf 49// Check login and sesskey.
cdbea7ee 50require_login($quizobj->get_course(), false, $quizobj->get_cm());
88f0eb15 51require_sesskey();
78e7a3dd 52
18dff757
TH
53// if no questions have been set up yet redirect to edit.php or display an error.
54if (!$quizobj->has_questions()) {
55 if ($quizobj->has_capability('mod/quiz:manage')) {
56 redirect($quizobj->edit_url());
57 } else {
58 print_error('cannotstartnoquestions', 'quiz', $quizobj->view_url());
59 }
78e7a3dd 60}
61
7c5bd5bf 62// Create an object to manage all the other (non-roles) access rules.
78e7a3dd 63$accessmanager = $quizobj->get_access_manager(time());
64if ($quizobj->is_preview_user() && $forcenew) {
987c2d49 65 $accessmanager->current_attempt_finished();
78e7a3dd 66}
67
7c5bd5bf 68// Check capabilities.
78e7a3dd 69if (!$quizobj->is_preview_user()) {
70 $quizobj->require_capability('mod/quiz:attempt');
71}
72
7c5bd5bf 73// Check to see if a new preview was requested.
78e7a3dd 74if ($quizobj->is_preview_user() && $forcenew) {
55ca80ed
TH
75 // To force the creation of a new preview, we set a finish time on the
76 // current attempt (if any). It will then automatically be deleted below
77 $DB->set_field('quiz_attempts', 'timefinish', time(),
dd70d561 78 array('quiz' => $quizobj->get_quizid(), 'userid' => $USER->id));
78e7a3dd 79}
80
7c5bd5bf 81// Look for an existing attempt.
987c2d49 82$attempts = quiz_get_user_attempts($quizobj->get_quizid(), $USER->id, 'all', true);
3461dbad 83$lastattempt = end($attempts);
78e7a3dd 84
3461dbad 85// If an in-progress attempt exists, check password then redirect to it.
78e7a3dd 86if ($lastattempt && !$lastattempt->timefinish) {
987c2d49
TH
87 $currentattemptid = $lastattempt->id;
88 $messages = $accessmanager->prevent_access();
c5fd1682 89
5db82949
CF
90 if ($page == -1) {
91 $page = $lastattempt->currentpage;
92 }
78e7a3dd 93
78e7a3dd 94} else {
987c2d49
TH
95 // Get number for the next or unfinished attempt
96 if ($lastattempt && !$lastattempt->preview && !$quizobj->is_preview_user()) {
97 $attemptnumber = $lastattempt->attempt + 1;
98 } else {
99 $lastattempt = false;
100 $attemptnumber = 1;
101 }
102 $currentattemptid = null;
103
104 $messages = $accessmanager->prevent_access() +
105 $accessmanager->prevent_new_attempt(count($attempts), $lastattempt);
5db82949
CF
106
107 if ($page == -1) {
c5fd1682 108 $page = 0;
5db82949 109 }
78e7a3dd 110}
111
7c5bd5bf 112// Check access.
987c2d49 113$output = $PAGE->get_renderer('mod_quiz');
78e7a3dd 114if (!$quizobj->is_preview_user() && $messages) {
115 print_error('attempterror', 'quiz', $quizobj->view_url(),
7238cd2e 116 $output->access_messages($messages));
78e7a3dd 117}
d755b0f5 118
987c2d49
TH
119if ($accessmanager->is_preflight_check_required($currentattemptid)) {
120 // Need to do some checks before allowing the user to continue.
121 $mform = $accessmanager->get_preflight_check_form(
122 $quizobj->start_attempt_url($page), $currentattemptid);
123
124 if ($mform->is_cancelled()) {
125 $accessmanager->back_to_view_page($output);
126
127 } else if (!$mform->get_data()) {
128
129 // Form not submitted successfully, re-display it and stop.
130 $PAGE->set_url($quizobj->start_attempt_url($page));
131 $PAGE->set_title(format_string($quizobj->get_quiz_name()));
132 $accessmanager->setup_attempt_page($PAGE);
133 if (empty($quizobj->get_quiz()->showblocks)) {
134 $PAGE->blocks->show_only_fake_blocks();
135 }
136
137 echo $output->start_attempt_page($quizobj, $mform);
138 die();
139 }
140
141 // Pre-flight check passed.
142 $accessmanager->notify_preflight_check_passed($currentattemptid);
143}
144if ($currentattemptid) {
145 redirect($quizobj->attempt_url($currentattemptid, $page));
146}
78e7a3dd 147
7c5bd5bf 148// Delete any previous preview attempts belonging to this user.
dd70d561 149quiz_delete_previews($quizobj->get_quiz(), $USER->id);
78e7a3dd 150
7c5bd5bf 151$quba = question_engine::make_questions_usage_by_activity('mod_quiz', $quizobj->get_context());
dd70d561 152$quba->set_preferred_behaviour($quizobj->get_quiz()->preferredbehaviour);
7c5bd5bf
TH
153
154// Create the new attempt and initialize the question sessions
dd70d561 155$attempt = quiz_create_attempt($quizobj->get_quiz(), $attemptnumber, $lastattempt, time(),
55ca80ed 156 $quizobj->is_preview_user());
78e7a3dd 157
dd70d561 158if (!($quizobj->get_quiz()->attemptonlast && $lastattempt)) {
7c5bd5bf
TH
159 // Starting a normal, new, quiz attempt.
160
161 // Fully load all the questions in this quiz.
162 $quizobj->preload_questions();
163 $quizobj->load_questions();
164
165 // Add them all to the $quba.
166 $idstoslots = array();
167 $questionsinuse = array_keys($quizobj->get_questions());
168 foreach ($quizobj->get_questions() as $i => $questiondata) {
169 if ($questiondata->qtype != 'random') {
dd70d561 170 if (!$quizobj->get_quiz()->shuffleanswers) {
7c5bd5bf
TH
171 $questiondata->options->shuffleanswers = false;
172 }
173 $question = question_bank::make_question($questiondata);
174
175 } else {
176 $question = question_bank::get_qtype('random')->choose_other_question(
dd70d561 177 $questiondata, $questionsinuse, $quizobj->get_quiz()->shuffleanswers);
7c5bd5bf
TH
178 if (is_null($question)) {
179 throw new moodle_exception('notenoughrandomquestions', 'quiz',
180 $quizobj->view_url(), $questiondata);
181 }
182 }
183
184 $idstoslots[$i] = $quba->add_question($question, $questiondata->maxmark);
185 $questionsinuse[] = $question->id;
186 }
187
c014b989
TH
188 // Start all the questions.
189 if ($attempt->preview) {
190 $variantoffset = rand(1, 100);
191 } else {
192 $variantoffset = $attemptnumber;
193 }
194 $quba->start_all_questions(
195 new question_variant_pseudorandom_no_repeats_strategy($variantoffset),
196 time());
7c5bd5bf
TH
197
198 // Update attempt layout.
199 $newlayout = array();
200 foreach (explode(',', $attempt->layout) as $qid) {
201 if ($qid != 0) {
202 $newlayout[] = $idstoslots[$qid];
203 } else {
204 $newlayout[] = 0;
205 }
206 }
207 $attempt->layout = implode(',', $newlayout);
208
209} else {
210 // Starting a subsequent attempt in each attempt builds on last mode.
211
212 $oldquba = question_engine::load_questions_usage_by_activity($lastattempt->uniqueid);
213
214 $oldnumberstonew = array();
215 foreach ($oldquba->get_attempt_iterator() as $oldslot => $oldqa) {
216 $newslot = $quba->add_question($oldqa->get_question(), $oldqa->get_max_mark());
217
218 $quba->start_question_based_on($newslot, $oldqa);
78e7a3dd 219
7c5bd5bf
TH
220 $oldnumberstonew[$oldslot] = $newslot;
221 }
222
223 // Update attempt layout.
224 $newlayout = array();
225 foreach (explode(',', $lastattempt->layout) as $oldslot) {
226 if ($oldslot != 0) {
227 $newlayout[] = $oldnumberstonew[$oldslot];
228 } else {
229 $newlayout[] = 0;
230 }
231 }
232 $attempt->layout = implode(',', $newlayout);
233}
234
235// Save the attempt in the database.
a1eb3a44 236$transaction = $DB->start_delegated_transaction();
7c5bd5bf
TH
237question_engine::save_questions_usage_by_activity($quba);
238$attempt->uniqueid = $quba->get_id();
88f0eb15 239$attempt->id = $DB->insert_record('quiz_attempts', $attempt);
7c5bd5bf
TH
240
241// Log the new attempt.
78e7a3dd 242if ($attempt->preview) {
b1103f2b 243 add_to_log($course->id, 'quiz', 'preview', 'view.php?id=' . $quizobj->get_cmid(),
244 $quizobj->get_quizid(), $quizobj->get_cmid());
78e7a3dd 245} else {
b1103f2b 246 add_to_log($course->id, 'quiz', 'attempt', 'review.php?attempt=' . $attempt->id,
247 $quizobj->get_quizid(), $quizobj->get_cmid());
78e7a3dd 248}
249
7c5bd5bf 250// Trigger event
39790bd8 251$eventdata = new stdClass();
cbb8b55c
TH
252$eventdata->component = 'mod_quiz';
253$eventdata->attemptid = $attempt->id;
254$eventdata->timestart = $attempt->timestart;
255$eventdata->userid = $attempt->userid;
256$eventdata->quizid = $quizobj->get_quizid();
257$eventdata->cmid = $quizobj->get_cmid();
258$eventdata->courseid = $quizobj->get_courseid();
9df209c1 259events_trigger('quiz_attempt_started', $eventdata);
78e7a3dd 260
a1eb3a44 261$transaction->allow_commit();
7c5bd5bf
TH
262
263// Redirect to the attempt page.
da729916 264redirect($quizobj->attempt_url($attempt->id, $page));