MDL-20636 Remove © from all PHPdoc comments, and tidy them up a bit.
[moodle.git] / mod / quiz / startattempt.php
CommitLineData
83192608 1<?php
7c5bd5bf
TH
2
3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17
78e7a3dd 18/**
7c5bd5bf 19 * This script deals with starting a new attempt at a quiz.
78e7a3dd 20 *
21 * Normally, it will end up redirecting to attempt.php - unless a password form is displayed.
22 *
ccc45155 23 * This code used to be at the top of attempt.php, if you are looking for CVS history.
24 *
7c5bd5bf
TH
25 * @package mod
26 * @subpackage quiz
27 * @copyright 2009 The Open University
28 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
78e7a3dd 29 */
30
31require_once(dirname(__FILE__) . '/../../config.php');
32require_once($CFG->dirroot . '/mod/quiz/locallib.php');
33
7c5bd5bf 34// Get submitted parameters.
78e7a3dd 35$id = required_param('cmid', PARAM_INT); // Course Module ID
36$forcenew = optional_param('forcenew', false, PARAM_BOOL); // Used to force a new preview
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());
b1103f2b 54if (!confirm_sesskey()) {
55 throw new moodle_exception('confirmsesskeybad', 'error', $quizobj->view_url());
56}
a88ba570 57$PAGE->set_pagelayout('base');
78e7a3dd 58
7c5bd5bf 59// if no questions have been set up yet redirect to edit.php
a1eb3a44 60if (!$quizobj->has_questions() && $quizobj->has_capability('mod/quiz:manage')) {
78e7a3dd 61 redirect($quizobj->edit_url());
62}
63
7c5bd5bf 64// Create an object to manage all the other (non-roles) access rules.
78e7a3dd 65$accessmanager = $quizobj->get_access_manager(time());
66if ($quizobj->is_preview_user() && $forcenew) {
67 $accessmanager->clear_password_access();
68}
69
7c5bd5bf 70// Check capabilities.
78e7a3dd 71if (!$quizobj->is_preview_user()) {
72 $quizobj->require_capability('mod/quiz:attempt');
73}
74
7c5bd5bf 75// Check to see if a new preview was requested.
78e7a3dd 76if ($quizobj->is_preview_user() && $forcenew) {
7c5bd5bf
TH
77// To force the creation of a new preview, we set a finish time on the
78// current attempt (if any). It will then automatically be deleted below
78e7a3dd 79 $DB->set_field('quiz_attempts', 'timefinish', time(), array('quiz' => $quiz->id, 'userid' => $USER->id));
80}
81
7c5bd5bf 82// Look for an existing attempt.
78e7a3dd 83$lastattempt = quiz_get_latest_attempt_by_user($quiz->id, $USER->id);
84
85if ($lastattempt && !$lastattempt->timefinish) {
7c5bd5bf 86// Continuation of an attempt - check password then redirect.
78e7a3dd 87 $accessmanager->do_password_check($quizobj->is_preview_user());
88 redirect($quizobj->attempt_url($lastattempt->id));
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() +
101 $accessmanager->prevent_new_attempt($attemptnumber - 1, $lastattempt);
102if (!$quizobj->is_preview_user() && $messages) {
103 print_error('attempterror', 'quiz', $quizobj->view_url(),
104 $accessmanager->print_messages($messages, true));
105}
106$accessmanager->do_password_check($quizobj->is_preview_user());
107
7c5bd5bf 108// Delete any previous preview attempts belonging to this user.
4299df1d 109quiz_delete_previews($quiz, $USER->id);
78e7a3dd 110
7c5bd5bf
TH
111$quba = question_engine::make_questions_usage_by_activity('mod_quiz', $quizobj->get_context());
112$quba->set_preferred_behaviour($quiz->preferredbehaviour);
113
114// Create the new attempt and initialize the question sessions
78e7a3dd 115$attempt = quiz_create_attempt($quiz, $attemptnumber, $lastattempt, time(), $quizobj->is_preview_user());
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
147 // Start all the quetsions.
148 $quba->start_all_questions();
149
150 // Update attempt layout.
151 $newlayout = array();
152 foreach (explode(',', $attempt->layout) as $qid) {
153 if ($qid != 0) {
154 $newlayout[] = $idstoslots[$qid];
155 } else {
156 $newlayout[] = 0;
157 }
158 }
159 $attempt->layout = implode(',', $newlayout);
160
161} else {
162 // Starting a subsequent attempt in each attempt builds on last mode.
163
164 $oldquba = question_engine::load_questions_usage_by_activity($lastattempt->uniqueid);
165
166 $oldnumberstonew = array();
167 foreach ($oldquba->get_attempt_iterator() as $oldslot => $oldqa) {
168 $newslot = $quba->add_question($oldqa->get_question(), $oldqa->get_max_mark());
169
170 $quba->start_question_based_on($newslot, $oldqa);
78e7a3dd 171
7c5bd5bf
TH
172 $oldnumberstonew[$oldslot] = $newslot;
173 }
174
175 // Update attempt layout.
176 $newlayout = array();
177 foreach (explode(',', $lastattempt->layout) as $oldslot) {
178 if ($oldslot != 0) {
179 $newlayout[] = $oldnumberstonew[$oldslot];
180 } else {
181 $newlayout[] = 0;
182 }
183 }
184 $attempt->layout = implode(',', $newlayout);
185}
186
187// Save the attempt in the database.
a1eb3a44 188$transaction = $DB->start_delegated_transaction();
7c5bd5bf
TH
189question_engine::save_questions_usage_by_activity($quba);
190$attempt->uniqueid = $quba->get_id();
a1eb3a44 191if (!$attempt->id = $DB->insert_record('quiz_attempts', $attempt)) {
7c5bd5bf
TH
192 throw new moodle_quiz_exception($quizobj, 'newattemptfail');
193}
194
195// Log the new attempt.
78e7a3dd 196if ($attempt->preview) {
b1103f2b 197 add_to_log($course->id, 'quiz', 'preview', 'view.php?id=' . $quizobj->get_cmid(),
198 $quizobj->get_quizid(), $quizobj->get_cmid());
78e7a3dd 199} else {
b1103f2b 200 add_to_log($course->id, 'quiz', 'attempt', 'review.php?attempt=' . $attempt->id,
201 $quizobj->get_quizid(), $quizobj->get_cmid());
78e7a3dd 202}
203
7c5bd5bf 204// Trigger event
39790bd8 205$eventdata = new stdClass();
204f394e 206$eventdata->component = 'mod_quiz';
9df209c1
DM
207$eventdata->course = $quizobj->get_courseid();
208$eventdata->quiz = $quizobj->get_quizid();
209$eventdata->cm = $quizobj->get_cmid();
210$eventdata->user = $USER;
211$eventdata->attempt = $attempt->id;
212events_trigger('quiz_attempt_started', $eventdata);
78e7a3dd 213
a1eb3a44 214$transaction->allow_commit();
7c5bd5bf
TH
215
216// Redirect to the attempt page.
78e7a3dd 217redirect($quizobj->attempt_url($attempt->id));