on-demand release 2.1beta
[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.
78e7a3dd 34$id = required_param('cmid', PARAM_INT); // Course Module ID
35$forcenew = optional_param('forcenew', false, PARAM_BOOL); // Used to force a new preview
36
37if (!$cm = get_coursemodule_from_id('quiz', $id)) {
38 print_error('invalidcoursemodule');
39}
40if (!$course = $DB->get_record('course', array('id' => $cm->course))) {
41 print_error("coursemisconf");
42}
43if (!$quiz = $DB->get_record('quiz', array('id' => $cm->instance))) {
44 print_error('invalidcoursemodule');
45}
46
990650f9 47$quizobj = quiz::create($quiz->id, $USER->id);
a88ba570
TH
48// This script should only ever be posted to, so set page URL to the view page.
49$PAGE->set_url($quizobj->view_url());
78e7a3dd 50
7c5bd5bf 51// Check login and sesskey.
78e7a3dd 52require_login($quizobj->get_courseid(), false, $quizobj->get_cm());
88f0eb15 53require_sesskey();
a88ba570 54$PAGE->set_pagelayout('base');
78e7a3dd 55
7c5bd5bf 56// if no questions have been set up yet redirect to edit.php
a1eb3a44 57if (!$quizobj->has_questions() && $quizobj->has_capability('mod/quiz:manage')) {
78e7a3dd 58 redirect($quizobj->edit_url());
59}
60
7c5bd5bf 61// Create an object to manage all the other (non-roles) access rules.
78e7a3dd 62$accessmanager = $quizobj->get_access_manager(time());
63if ($quizobj->is_preview_user() && $forcenew) {
64 $accessmanager->clear_password_access();
65}
66
7c5bd5bf 67// Check capabilities.
78e7a3dd 68if (!$quizobj->is_preview_user()) {
69 $quizobj->require_capability('mod/quiz:attempt');
70}
71
7c5bd5bf 72// Check to see if a new preview was requested.
78e7a3dd 73if ($quizobj->is_preview_user() && $forcenew) {
55ca80ed
TH
74 // To force the creation of a new preview, we set a finish time on the
75 // current attempt (if any). It will then automatically be deleted below
76 $DB->set_field('quiz_attempts', 'timefinish', time(),
77 array('quiz' => $quiz->id, 'userid' => $USER->id));
78e7a3dd 78}
79
7c5bd5bf 80// Look for an existing attempt.
78e7a3dd 81$lastattempt = quiz_get_latest_attempt_by_user($quiz->id, $USER->id);
82
83if ($lastattempt && !$lastattempt->timefinish) {
55ca80ed 84 // Continuation of an attempt - check password then redirect.
78e7a3dd 85 $accessmanager->do_password_check($quizobj->is_preview_user());
86 redirect($quizobj->attempt_url($lastattempt->id));
87}
88
7c5bd5bf 89// Get number for the next or unfinished attempt
78e7a3dd 90if ($lastattempt && !$lastattempt->preview && !$quizobj->is_preview_user()) {
78e7a3dd 91 $attemptnumber = $lastattempt->attempt + 1;
92} else {
93 $lastattempt = false;
78e7a3dd 94 $attemptnumber = 1;
95}
96
7c5bd5bf 97// Check access.
78e7a3dd 98$messages = $accessmanager->prevent_access() +
bcd42560
DL
99$accessmanager->prevent_new_attempt($attemptnumber - 1, $lastattempt);
100$output = $PAGE->get_renderer('mod_quiz');
78e7a3dd 101if (!$quizobj->is_preview_user() && $messages) {
102 print_error('attempterror', 'quiz', $quizobj->view_url(),
bcd42560 103 $output->print_messages($messages));
78e7a3dd 104}
105$accessmanager->do_password_check($quizobj->is_preview_user());
106
7c5bd5bf 107// Delete any previous preview attempts belonging to this user.
4299df1d 108quiz_delete_previews($quiz, $USER->id);
78e7a3dd 109
7c5bd5bf
TH
110$quba = question_engine::make_questions_usage_by_activity('mod_quiz', $quizobj->get_context());
111$quba->set_preferred_behaviour($quiz->preferredbehaviour);
112
113// Create the new attempt and initialize the question sessions
55ca80ed
TH
114$attempt = quiz_create_attempt($quiz, $attemptnumber, $lastattempt, time(),
115 $quizobj->is_preview_user());
78e7a3dd 116
7c5bd5bf
TH
117if (!($quiz->attemptonlast && $lastattempt)) {
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') {
129 if (!$quiz->shuffleanswers) {
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(
136 $questiondata, $questionsinuse, $quiz->shuffleanswers);
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();
204f394e 211$eventdata->component = 'mod_quiz';
9df209c1
DM
212$eventdata->course = $quizobj->get_courseid();
213$eventdata->quiz = $quizobj->get_quizid();
214$eventdata->cm = $quizobj->get_cmid();
215$eventdata->user = $USER;
216$eventdata->attempt = $attempt->id;
217events_trigger('quiz_attempt_started', $eventdata);
78e7a3dd 218
a1eb3a44 219$transaction->allow_commit();
7c5bd5bf
TH
220
221// Redirect to the attempt page.
78e7a3dd 222redirect($quizobj->attempt_url($attempt->id));