MDL-15452 - Put the OU quiz navigation improvements into the Moodle codebase - quite...
[moodle.git] / mod / quiz / startattempt.php
CommitLineData
78e7a3dd 1<?php // $Id$
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 *
7 * @author Tim Hunt.
8 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
9 * @package quiz
10 */
11
12require_once(dirname(__FILE__) . '/../../config.php');
13require_once($CFG->dirroot . '/mod/quiz/locallib.php');
14
15/// Get submitted parameters.
16$id = required_param('cmid', PARAM_INT); // Course Module ID
17$forcenew = optional_param('forcenew', false, PARAM_BOOL); // Used to force a new preview
18
19if (!$cm = get_coursemodule_from_id('quiz', $id)) {
20 print_error('invalidcoursemodule');
21}
22if (!$course = $DB->get_record('course', array('id' => $cm->course))) {
23 print_error("coursemisconf");
24}
25if (!$quiz = $DB->get_record('quiz', array('id' => $cm->instance))) {
26 print_error('invalidcoursemodule');
27}
28
29$quizobj = new quiz($quiz, $cm, $course);
30
31/// Check login and get contexts.
32require_login($quizobj->get_courseid(), false, $quizobj->get_cm());
33
34/// if no questions have been set up yet redirect to edit.php
35if (!$quizobj->get_question_ids() && $quizobj->has_capability('mod/quiz:manage')) {
36 redirect($quizobj->edit_url());
37}
38
39/// Create an object to manage all the other (non-roles) access rules.
40$accessmanager = $quizobj->get_access_manager(time());
41if ($quizobj->is_preview_user() && $forcenew) {
42 $accessmanager->clear_password_access();
43}
44
45// This page should only respond to post requests, if not, redirect to the view page.
46// However, becuase 'secure' mode opens in a new window, we cannot do enforce this rule for them.
47if (!data_submitted() && !$accessmanager->securewindow_required($quizobj->is_preview_user())) {
48 redirect($quizobj->view_url());
49}
50if (!confirm_sesskey()) {
51 throw new moodle_exception('confirmsesskeybad', 'error', $quizobj->view_url());
52}
53
54/// Check capabilites.
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.
95if ($oldattempts = $DB->get_records_select('quiz_attempts', "quiz = ?
96 AND userid = ? AND preview = 1", array($quiz->id, $USER->id))) {
97 foreach ($oldattempts as $oldattempt) {
98 quiz_delete_attempt($oldattempt, $quiz);
99 }
100}
101
102/// Create the new attempt and initialize the question sessions
103$attempt = quiz_create_attempt($quiz, $attemptnumber, $lastattempt, time(), $quizobj->is_preview_user());
104
105/// Save the attempt in the database.
106if (!$attempt->id = $DB->insert_record('quiz_attempts', $attempt)) {
107 quiz_error($quiz, 'newattemptfail');
108}
109
110/// Log the new attempt.
111if ($attempt->preview) {
112 add_to_log($course->id, 'quiz', 'preview', "attempt.php?id=$cm->id",
113 "$quiz->id", $cm->id);
114} else {
115 add_to_log($course->id, 'quiz', 'attempt', "review.php?attempt=$attempt->id",
116 "$quiz->id", $cm->id);
117}
118
119/// Fully load all the questions in this quiz.
120$quizobj->preload_questions();
121$quizobj->load_questions();
122
123/// Create initial states for all questions in this quiz.
124if (!$states = get_question_states($quizobj->get_questions(), $quizobj->get_quiz(), $attempt, $lastattemptid)) {
125 print_error('cannotrestore', 'quiz');
126}
127
128/// Save all the newly created states.
129foreach ($quizobj->get_questions() as $i => $question) {
130 save_question_session($question, $states[$i]);
131}
132
133/// Redirect to the attempt page.
134redirect($quizobj->attempt_url($attempt->id));
135?>