MDL-20636 tinyMCE boxes for each sub-question are enormous. #355
[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 *
ba643847 25 * @package mod
7c5bd5bf 26 * @subpackage quiz
ba643847
TH
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());
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) {
7c5bd5bf
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
78e7a3dd 77 $DB->set_field('quiz_attempts', 'timefinish', time(), array('quiz' => $quiz->id, 'userid' => $USER->id));
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) {
7c5bd5bf 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() +
99 $accessmanager->prevent_new_attempt($attemptnumber - 1, $lastattempt);
100if (!$quizobj->is_preview_user() && $messages) {
101 print_error('attempterror', 'quiz', $quizobj->view_url(),
102 $accessmanager->print_messages($messages, true));
103}
104$accessmanager->do_password_check($quizobj->is_preview_user());
105
7c5bd5bf 106// Delete any previous preview attempts belonging to this user.
4299df1d 107quiz_delete_previews($quiz, $USER->id);
78e7a3dd 108
7c5bd5bf
TH
109$quba = question_engine::make_questions_usage_by_activity('mod_quiz', $quizobj->get_context());
110$quba->set_preferred_behaviour($quiz->preferredbehaviour);
111
112// Create the new attempt and initialize the question sessions
78e7a3dd 113$attempt = quiz_create_attempt($quiz, $attemptnumber, $lastattempt, time(), $quizobj->is_preview_user());
114
7c5bd5bf
TH
115if (!($quiz->attemptonlast && $lastattempt)) {
116 // Starting a normal, new, quiz attempt.
117
118 // Fully load all the questions in this quiz.
119 $quizobj->preload_questions();
120 $quizobj->load_questions();
121
122 // Add them all to the $quba.
123 $idstoslots = array();
124 $questionsinuse = array_keys($quizobj->get_questions());
125 foreach ($quizobj->get_questions() as $i => $questiondata) {
126 if ($questiondata->qtype != 'random') {
127 if (!$quiz->shuffleanswers) {
128 $questiondata->options->shuffleanswers = false;
129 }
130 $question = question_bank::make_question($questiondata);
131
132 } else {
133 $question = question_bank::get_qtype('random')->choose_other_question(
134 $questiondata, $questionsinuse, $quiz->shuffleanswers);
135 if (is_null($question)) {
136 throw new moodle_exception('notenoughrandomquestions', 'quiz',
137 $quizobj->view_url(), $questiondata);
138 }
139 }
140
141 $idstoslots[$i] = $quba->add_question($question, $questiondata->maxmark);
142 $questionsinuse[] = $question->id;
143 }
144
145 // Start all the quetsions.
146 $quba->start_all_questions();
147
148 // Update attempt layout.
149 $newlayout = array();
150 foreach (explode(',', $attempt->layout) as $qid) {
151 if ($qid != 0) {
152 $newlayout[] = $idstoslots[$qid];
153 } else {
154 $newlayout[] = 0;
155 }
156 }
157 $attempt->layout = implode(',', $newlayout);
158
159} else {
160 // Starting a subsequent attempt in each attempt builds on last mode.
161
162 $oldquba = question_engine::load_questions_usage_by_activity($lastattempt->uniqueid);
163
164 $oldnumberstonew = array();
165 foreach ($oldquba->get_attempt_iterator() as $oldslot => $oldqa) {
166 $newslot = $quba->add_question($oldqa->get_question(), $oldqa->get_max_mark());
167
168 $quba->start_question_based_on($newslot, $oldqa);
78e7a3dd 169
7c5bd5bf
TH
170 $oldnumberstonew[$oldslot] = $newslot;
171 }
172
173 // Update attempt layout.
174 $newlayout = array();
175 foreach (explode(',', $lastattempt->layout) as $oldslot) {
176 if ($oldslot != 0) {
177 $newlayout[] = $oldnumberstonew[$oldslot];
178 } else {
179 $newlayout[] = 0;
180 }
181 }
182 $attempt->layout = implode(',', $newlayout);
183}
184
185// Save the attempt in the database.
a1eb3a44 186$transaction = $DB->start_delegated_transaction();
7c5bd5bf
TH
187question_engine::save_questions_usage_by_activity($quba);
188$attempt->uniqueid = $quba->get_id();
88f0eb15 189$attempt->id = $DB->insert_record('quiz_attempts', $attempt);
7c5bd5bf
TH
190
191// Log the new attempt.
78e7a3dd 192if ($attempt->preview) {
b1103f2b 193 add_to_log($course->id, 'quiz', 'preview', 'view.php?id=' . $quizobj->get_cmid(),
194 $quizobj->get_quizid(), $quizobj->get_cmid());
78e7a3dd 195} else {
b1103f2b 196 add_to_log($course->id, 'quiz', 'attempt', 'review.php?attempt=' . $attempt->id,
197 $quizobj->get_quizid(), $quizobj->get_cmid());
78e7a3dd 198}
199
7c5bd5bf 200// Trigger event
39790bd8 201$eventdata = new stdClass();
204f394e 202$eventdata->component = 'mod_quiz';
9df209c1
DM
203$eventdata->course = $quizobj->get_courseid();
204$eventdata->quiz = $quizobj->get_quizid();
205$eventdata->cm = $quizobj->get_cmid();
206$eventdata->user = $USER;
207$eventdata->attempt = $attempt->id;
208events_trigger('quiz_attempt_started', $eventdata);
78e7a3dd 209
a1eb3a44 210$transaction->allow_commit();
7c5bd5bf
TH
211
212// Redirect to the attempt page.
78e7a3dd 213redirect($quizobj->attempt_url($attempt->id));