MDL-29627 quiz old popup int column => new browsersecurity column.
[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
da729916 36$page = optional_param('page', 0, 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.
78e7a3dd 50require_login($quizobj->get_courseid(), false, $quizobj->get_cm());
88f0eb15 51require_sesskey();
a88ba570 52$PAGE->set_pagelayout('base');
78e7a3dd 53
7c5bd5bf 54// if no questions have been set up yet redirect to edit.php
a1eb3a44 55if (!$quizobj->has_questions() && $quizobj->has_capability('mod/quiz:manage')) {
78e7a3dd 56 redirect($quizobj->edit_url());
57}
58
7c5bd5bf 59// Create an object to manage all the other (non-roles) access rules.
78e7a3dd 60$accessmanager = $quizobj->get_access_manager(time());
61if ($quizobj->is_preview_user() && $forcenew) {
62 $accessmanager->clear_password_access();
63}
64
7c5bd5bf 65// Check capabilities.
78e7a3dd 66if (!$quizobj->is_preview_user()) {
67 $quizobj->require_capability('mod/quiz:attempt');
68}
69
7c5bd5bf 70// Check to see if a new preview was requested.
78e7a3dd 71if ($quizobj->is_preview_user() && $forcenew) {
55ca80ed
TH
72 // To force the creation of a new preview, we set a finish time on the
73 // current attempt (if any). It will then automatically be deleted below
74 $DB->set_field('quiz_attempts', 'timefinish', time(),
dd70d561 75 array('quiz' => $quizobj->get_quizid(), 'userid' => $USER->id));
78e7a3dd 76}
77
7c5bd5bf 78// Look for an existing attempt.
dd70d561 79$attempts = quiz_get_user_attempts($quizobj->get_quizid(), $USER->id, 'all');
3461dbad 80$lastattempt = end($attempts);
78e7a3dd 81
3461dbad 82// If an in-progress attempt exists, check password then redirect to it.
78e7a3dd 83if ($lastattempt && !$lastattempt->timefinish) {
78e7a3dd 84 $accessmanager->do_password_check($quizobj->is_preview_user());
da729916 85 redirect($quizobj->attempt_url($lastattempt->id, $page));
78e7a3dd 86}
87
7c5bd5bf 88// Get number for the next or unfinished attempt
78e7a3dd 89if ($lastattempt && !$lastattempt->preview && !$quizobj->is_preview_user()) {
78e7a3dd 90 $attemptnumber = $lastattempt->attempt + 1;
91} else {
92 $lastattempt = false;
78e7a3dd 93 $attemptnumber = 1;
94}
95
7c5bd5bf 96// Check access.
78e7a3dd 97$messages = $accessmanager->prevent_access() +
3461dbad 98 $accessmanager->prevent_new_attempt(count($attempts), $lastattempt);
78e7a3dd 99if (!$quizobj->is_preview_user() && $messages) {
3461dbad 100 $output = $PAGE->get_renderer('mod_quiz');
78e7a3dd 101 print_error('attempterror', 'quiz', $quizobj->view_url(),
bcd42560 102 $output->print_messages($messages));
78e7a3dd 103}
d755b0f5 104
78e7a3dd 105$accessmanager->do_password_check($quizobj->is_preview_user());
106
7c5bd5bf 107// Delete any previous preview attempts belonging to this user.
dd70d561 108quiz_delete_previews($quizobj->get_quiz(), $USER->id);
78e7a3dd 109
7c5bd5bf 110$quba = question_engine::make_questions_usage_by_activity('mod_quiz', $quizobj->get_context());
dd70d561 111$quba->set_preferred_behaviour($quizobj->get_quiz()->preferredbehaviour);
7c5bd5bf
TH
112
113// Create the new attempt and initialize the question sessions
dd70d561 114$attempt = quiz_create_attempt($quizobj->get_quiz(), $attemptnumber, $lastattempt, time(),
55ca80ed 115 $quizobj->is_preview_user());
78e7a3dd 116
dd70d561 117if (!($quizobj->get_quiz()->attemptonlast && $lastattempt)) {
7c5bd5bf
TH
118 // Starting a normal, new, quiz attempt.
119
120 // Fully load all the questions in this quiz.
121 $quizobj->preload_questions();
122 $quizobj->load_questions();
123
124 // Add them all to the $quba.
125 $idstoslots = array();
126 $questionsinuse = array_keys($quizobj->get_questions());
127 foreach ($quizobj->get_questions() as $i => $questiondata) {
128 if ($questiondata->qtype != 'random') {
dd70d561 129 if (!$quizobj->get_quiz()->shuffleanswers) {
7c5bd5bf
TH
130 $questiondata->options->shuffleanswers = false;
131 }
132 $question = question_bank::make_question($questiondata);
133
134 } else {
135 $question = question_bank::get_qtype('random')->choose_other_question(
dd70d561 136 $questiondata, $questionsinuse, $quizobj->get_quiz()->shuffleanswers);
7c5bd5bf
TH
137 if (is_null($question)) {
138 throw new moodle_exception('notenoughrandomquestions', 'quiz',
139 $quizobj->view_url(), $questiondata);
140 }
141 }
142
143 $idstoslots[$i] = $quba->add_question($question, $questiondata->maxmark);
144 $questionsinuse[] = $question->id;
145 }
146
c014b989
TH
147 // Start all the questions.
148 if ($attempt->preview) {
149 $variantoffset = rand(1, 100);
150 } else {
151 $variantoffset = $attemptnumber;
152 }
153 $quba->start_all_questions(
154 new question_variant_pseudorandom_no_repeats_strategy($variantoffset),
155 time());
7c5bd5bf
TH
156
157 // Update attempt layout.
158 $newlayout = array();
159 foreach (explode(',', $attempt->layout) as $qid) {
160 if ($qid != 0) {
161 $newlayout[] = $idstoslots[$qid];
162 } else {
163 $newlayout[] = 0;
164 }
165 }
166 $attempt->layout = implode(',', $newlayout);
167
168} else {
169 // Starting a subsequent attempt in each attempt builds on last mode.
170
171 $oldquba = question_engine::load_questions_usage_by_activity($lastattempt->uniqueid);
172
173 $oldnumberstonew = array();
174 foreach ($oldquba->get_attempt_iterator() as $oldslot => $oldqa) {
175 $newslot = $quba->add_question($oldqa->get_question(), $oldqa->get_max_mark());
176
177 $quba->start_question_based_on($newslot, $oldqa);
78e7a3dd 178
7c5bd5bf
TH
179 $oldnumberstonew[$oldslot] = $newslot;
180 }
181
182 // Update attempt layout.
183 $newlayout = array();
184 foreach (explode(',', $lastattempt->layout) as $oldslot) {
185 if ($oldslot != 0) {
186 $newlayout[] = $oldnumberstonew[$oldslot];
187 } else {
188 $newlayout[] = 0;
189 }
190 }
191 $attempt->layout = implode(',', $newlayout);
192}
193
194// Save the attempt in the database.
a1eb3a44 195$transaction = $DB->start_delegated_transaction();
7c5bd5bf
TH
196question_engine::save_questions_usage_by_activity($quba);
197$attempt->uniqueid = $quba->get_id();
88f0eb15 198$attempt->id = $DB->insert_record('quiz_attempts', $attempt);
7c5bd5bf
TH
199
200// Log the new attempt.
78e7a3dd 201if ($attempt->preview) {
b1103f2b 202 add_to_log($course->id, 'quiz', 'preview', 'view.php?id=' . $quizobj->get_cmid(),
203 $quizobj->get_quizid(), $quizobj->get_cmid());
78e7a3dd 204} else {
b1103f2b 205 add_to_log($course->id, 'quiz', 'attempt', 'review.php?attempt=' . $attempt->id,
206 $quizobj->get_quizid(), $quizobj->get_cmid());
78e7a3dd 207}
208
7c5bd5bf 209// Trigger event
39790bd8 210$eventdata = new stdClass();
cbb8b55c
TH
211$eventdata->component = 'mod_quiz';
212$eventdata->attemptid = $attempt->id;
213$eventdata->timestart = $attempt->timestart;
214$eventdata->userid = $attempt->userid;
215$eventdata->quizid = $quizobj->get_quizid();
216$eventdata->cmid = $quizobj->get_cmid();
217$eventdata->courseid = $quizobj->get_courseid();
9df209c1 218events_trigger('quiz_attempt_started', $eventdata);
78e7a3dd 219
a1eb3a44 220$transaction->allow_commit();
7c5bd5bf
TH
221
222// Redirect to the attempt page.
da729916 223redirect($quizobj->attempt_url($attempt->id, $page));