MDL-20636 Quiz view page now works.
[moodle.git] / mod / quiz / startattempt.php
CommitLineData
83192608 1<?php
78e7a3dd 2/**
3 * This page deals with starting a new attempt at a quiz.
4 *
5 * Normally, it will end up redirecting to attempt.php - unless a password form is displayed.
6 *
ccc45155 7 * This code used to be at the top of attempt.php, if you are looking for CVS history.
8 *
78e7a3dd 9 * @author Tim Hunt.
10 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
11 * @package quiz
12 */
13
14require_once(dirname(__FILE__) . '/../../config.php');
15require_once($CFG->dirroot . '/mod/quiz/locallib.php');
16
17/// Get submitted parameters.
18$id = required_param('cmid', PARAM_INT); // Course Module ID
19$forcenew = optional_param('forcenew', false, PARAM_BOOL); // Used to force a new preview
20
21if (!$cm = get_coursemodule_from_id('quiz', $id)) {
22 print_error('invalidcoursemodule');
23}
24if (!$course = $DB->get_record('course', array('id' => $cm->course))) {
25 print_error("coursemisconf");
26}
27if (!$quiz = $DB->get_record('quiz', array('id' => $cm->instance))) {
28 print_error('invalidcoursemodule');
29}
30
990650f9 31$quizobj = quiz::create($quiz->id, $USER->id);
a88ba570
TH
32// This script should only ever be posted to, so set page URL to the view page.
33$PAGE->set_url($quizobj->view_url());
78e7a3dd 34
b1103f2b 35/// Check login and sesskey.
78e7a3dd 36require_login($quizobj->get_courseid(), false, $quizobj->get_cm());
b1103f2b 37if (!confirm_sesskey()) {
38 throw new moodle_exception('confirmsesskeybad', 'error', $quizobj->view_url());
39}
a88ba570 40$PAGE->set_pagelayout('base');
78e7a3dd 41
42/// if no questions have been set up yet redirect to edit.php
43if (!$quizobj->get_question_ids() && $quizobj->has_capability('mod/quiz:manage')) {
44 redirect($quizobj->edit_url());
45}
46
47/// Create an object to manage all the other (non-roles) access rules.
48$accessmanager = $quizobj->get_access_manager(time());
49if ($quizobj->is_preview_user() && $forcenew) {
50 $accessmanager->clear_password_access();
51}
52
78e7a3dd 53
eab8ed9f 54/// Check capabilities.
78e7a3dd 55if (!$quizobj->is_preview_user()) {
56 $quizobj->require_capability('mod/quiz:attempt');
57}
58
59/// Check to see if a new preview was requested.
60if ($quizobj->is_preview_user() && $forcenew) {
61/// To force the creation of a new preview, we set a finish time on the
62/// current attempt (if any). It will then automatically be deleted below
63 $DB->set_field('quiz_attempts', 'timefinish', time(), array('quiz' => $quiz->id, 'userid' => $USER->id));
64}
65
66/// Look for an existing attempt.
67$lastattempt = quiz_get_latest_attempt_by_user($quiz->id, $USER->id);
68
69if ($lastattempt && !$lastattempt->timefinish) {
70/// Continuation of an attempt - check password then redirect.
71 $accessmanager->do_password_check($quizobj->is_preview_user());
72 redirect($quizobj->attempt_url($lastattempt->id));
73}
74
75/// Get number for the next or unfinished attempt
76if ($lastattempt && !$lastattempt->preview && !$quizobj->is_preview_user()) {
77 $lastattemptid = $lastattempt->id;
78 $attemptnumber = $lastattempt->attempt + 1;
79} else {
80 $lastattempt = false;
81 $lastattemptid = false;
82 $attemptnumber = 1;
83}
84
85/// Check access.
86$messages = $accessmanager->prevent_access() +
87 $accessmanager->prevent_new_attempt($attemptnumber - 1, $lastattempt);
88if (!$quizobj->is_preview_user() && $messages) {
89 print_error('attempterror', 'quiz', $quizobj->view_url(),
90 $accessmanager->print_messages($messages, true));
91}
92$accessmanager->do_password_check($quizobj->is_preview_user());
93
94/// Delete any previous preview attempts belonging to this user.
4299df1d 95quiz_delete_previews($quiz, $USER->id);
78e7a3dd 96
97/// Create the new attempt and initialize the question sessions
98$attempt = quiz_create_attempt($quiz, $attemptnumber, $lastattempt, time(), $quizobj->is_preview_user());
99
100/// Save the attempt in the database.
a8f3a651 101$attempt->id = $DB->insert_record('quiz_attempts', $attempt);
78e7a3dd 102
103/// Log the new attempt.
104if ($attempt->preview) {
b1103f2b 105 add_to_log($course->id, 'quiz', 'preview', 'view.php?id=' . $quizobj->get_cmid(),
106 $quizobj->get_quizid(), $quizobj->get_cmid());
78e7a3dd 107} else {
b1103f2b 108 add_to_log($course->id, 'quiz', 'attempt', 'review.php?attempt=' . $attempt->id,
109 $quizobj->get_quizid(), $quizobj->get_cmid());
78e7a3dd 110}
111
112/// Fully load all the questions in this quiz.
113$quizobj->preload_questions();
114$quizobj->load_questions();
115
116/// Create initial states for all questions in this quiz.
6723a039
TH
117if (!$quiz->attemptonlast) {
118 $lastattemptid = false;
119}
78e7a3dd 120if (!$states = get_question_states($quizobj->get_questions(), $quizobj->get_quiz(), $attempt, $lastattemptid)) {
121 print_error('cannotrestore', 'quiz');
122}
123
124/// Save all the newly created states.
125foreach ($quizobj->get_questions() as $i => $question) {
126 save_question_session($question, $states[$i]);
127}
9df209c1 128/// Trigger event
39790bd8 129$eventdata = new stdClass();
204f394e 130$eventdata->component = 'mod_quiz';
9df209c1
DM
131$eventdata->course = $quizobj->get_courseid();
132$eventdata->quiz = $quizobj->get_quizid();
133$eventdata->cm = $quizobj->get_cmid();
134$eventdata->user = $USER;
135$eventdata->attempt = $attempt->id;
136events_trigger('quiz_attempt_started', $eventdata);
78e7a3dd 137
138/// Redirect to the attempt page.
139redirect($quizobj->attempt_url($attempt->id));