weekly release 2.2dev
[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}
44if (!$quiz = $DB->get_record('quiz', array('id' => $cm->instance))) {
45 print_error('invalidcoursemodule');
46}
47
990650f9 48$quizobj = quiz::create($quiz->id, $USER->id);
a88ba570
TH
49// This script should only ever be posted to, so set page URL to the view page.
50$PAGE->set_url($quizobj->view_url());
78e7a3dd 51
7c5bd5bf 52// Check login and sesskey.
78e7a3dd 53require_login($quizobj->get_courseid(), false, $quizobj->get_cm());
88f0eb15 54require_sesskey();
a88ba570 55$PAGE->set_pagelayout('base');
78e7a3dd 56
7c5bd5bf 57// if no questions have been set up yet redirect to edit.php
a1eb3a44 58if (!$quizobj->has_questions() && $quizobj->has_capability('mod/quiz:manage')) {
78e7a3dd 59 redirect($quizobj->edit_url());
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) {
65 $accessmanager->clear_password_access();
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(),
78 array('quiz' => $quiz->id, 'userid' => $USER->id));
78e7a3dd 79}
80
7c5bd5bf 81// Look for an existing attempt.
3461dbad
TH
82$attempts = quiz_get_user_attempts($quiz->id, $USER->id, 'all');
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) {
78e7a3dd 87 $accessmanager->do_password_check($quizobj->is_preview_user());
da729916 88 redirect($quizobj->attempt_url($lastattempt->id, $page));
78e7a3dd 89}
90
7c5bd5bf 91// Get number for the next or unfinished attempt
78e7a3dd 92if ($lastattempt && !$lastattempt->preview && !$quizobj->is_preview_user()) {
78e7a3dd 93 $attemptnumber = $lastattempt->attempt + 1;
94} else {
95 $lastattempt = false;
78e7a3dd 96 $attemptnumber = 1;
97}
98
7c5bd5bf 99// Check access.
78e7a3dd 100$messages = $accessmanager->prevent_access() +
3461dbad 101 $accessmanager->prevent_new_attempt(count($attempts), $lastattempt);
78e7a3dd 102if (!$quizobj->is_preview_user() && $messages) {
3461dbad 103 $output = $PAGE->get_renderer('mod_quiz');
78e7a3dd 104 print_error('attempterror', 'quiz', $quizobj->view_url(),
bcd42560 105 $output->print_messages($messages));
78e7a3dd 106}
107$accessmanager->do_password_check($quizobj->is_preview_user());
108
7c5bd5bf 109// Delete any previous preview attempts belonging to this user.
4299df1d 110quiz_delete_previews($quiz, $USER->id);
78e7a3dd 111
7c5bd5bf
TH
112$quba = question_engine::make_questions_usage_by_activity('mod_quiz', $quizobj->get_context());
113$quba->set_preferred_behaviour($quiz->preferredbehaviour);
114
115// Create the new attempt and initialize the question sessions
55ca80ed
TH
116$attempt = quiz_create_attempt($quiz, $attemptnumber, $lastattempt, time(),
117 $quizobj->is_preview_user());
78e7a3dd 118
7c5bd5bf
TH
119if (!($quiz->attemptonlast && $lastattempt)) {
120 // Starting a normal, new, quiz attempt.
121
122 // Fully load all the questions in this quiz.
123 $quizobj->preload_questions();
124 $quizobj->load_questions();
125
126 // Add them all to the $quba.
127 $idstoslots = array();
128 $questionsinuse = array_keys($quizobj->get_questions());
129 foreach ($quizobj->get_questions() as $i => $questiondata) {
130 if ($questiondata->qtype != 'random') {
131 if (!$quiz->shuffleanswers) {
132 $questiondata->options->shuffleanswers = false;
133 }
134 $question = question_bank::make_question($questiondata);
135
136 } else {
137 $question = question_bank::get_qtype('random')->choose_other_question(
138 $questiondata, $questionsinuse, $quiz->shuffleanswers);
139 if (is_null($question)) {
140 throw new moodle_exception('notenoughrandomquestions', 'quiz',
141 $quizobj->view_url(), $questiondata);
142 }
143 }
144
145 $idstoslots[$i] = $quba->add_question($question, $questiondata->maxmark);
146 $questionsinuse[] = $question->id;
147 }
148
c014b989
TH
149 // Start all the questions.
150 if ($attempt->preview) {
151 $variantoffset = rand(1, 100);
152 } else {
153 $variantoffset = $attemptnumber;
154 }
155 $quba->start_all_questions(
156 new question_variant_pseudorandom_no_repeats_strategy($variantoffset),
157 time());
7c5bd5bf
TH
158
159 // Update attempt layout.
160 $newlayout = array();
161 foreach (explode(',', $attempt->layout) as $qid) {
162 if ($qid != 0) {
163 $newlayout[] = $idstoslots[$qid];
164 } else {
165 $newlayout[] = 0;
166 }
167 }
168 $attempt->layout = implode(',', $newlayout);
169
170} else {
171 // Starting a subsequent attempt in each attempt builds on last mode.
172
173 $oldquba = question_engine::load_questions_usage_by_activity($lastattempt->uniqueid);
174
175 $oldnumberstonew = array();
176 foreach ($oldquba->get_attempt_iterator() as $oldslot => $oldqa) {
177 $newslot = $quba->add_question($oldqa->get_question(), $oldqa->get_max_mark());
178
179 $quba->start_question_based_on($newslot, $oldqa);
78e7a3dd 180
7c5bd5bf
TH
181 $oldnumberstonew[$oldslot] = $newslot;
182 }
183
184 // Update attempt layout.
185 $newlayout = array();
186 foreach (explode(',', $lastattempt->layout) as $oldslot) {
187 if ($oldslot != 0) {
188 $newlayout[] = $oldnumberstonew[$oldslot];
189 } else {
190 $newlayout[] = 0;
191 }
192 }
193 $attempt->layout = implode(',', $newlayout);
194}
195
196// Save the attempt in the database.
a1eb3a44 197$transaction = $DB->start_delegated_transaction();
7c5bd5bf
TH
198question_engine::save_questions_usage_by_activity($quba);
199$attempt->uniqueid = $quba->get_id();
88f0eb15 200$attempt->id = $DB->insert_record('quiz_attempts', $attempt);
7c5bd5bf
TH
201
202// Log the new attempt.
78e7a3dd 203if ($attempt->preview) {
b1103f2b 204 add_to_log($course->id, 'quiz', 'preview', 'view.php?id=' . $quizobj->get_cmid(),
205 $quizobj->get_quizid(), $quizobj->get_cmid());
78e7a3dd 206} else {
b1103f2b 207 add_to_log($course->id, 'quiz', 'attempt', 'review.php?attempt=' . $attempt->id,
208 $quizobj->get_quizid(), $quizobj->get_cmid());
78e7a3dd 209}
210
7c5bd5bf 211// Trigger event
39790bd8 212$eventdata = new stdClass();
204f394e 213$eventdata->component = 'mod_quiz';
9df209c1
DM
214$eventdata->course = $quizobj->get_courseid();
215$eventdata->quiz = $quizobj->get_quizid();
216$eventdata->cm = $quizobj->get_cmid();
217$eventdata->user = $USER;
218$eventdata->attempt = $attempt->id;
219events_trigger('quiz_attempt_started', $eventdata);
78e7a3dd 220
a1eb3a44 221$transaction->allow_commit();
7c5bd5bf
TH
222
223// Redirect to the attempt page.
da729916 224redirect($quizobj->attempt_url($attempt->id, $page));