blocklib MDL-25654 fix API inconsistency be renaming add_pretend_block to add_fake_block.
[moodle.git] / mod / quiz / attempt.php
CommitLineData
83192608 1<?php
ee1fb969 2/**
2b3b36d0 3 * This page prints a particular instance of quiz
4 *
5 * @author Martin Dougiamas and many others. This has recently been completely
6 * rewritten by Alex Smith, Julian Sedding and Gustav Delius as part of
7 * the Serving Mathematics project
8 * {@link http://maths.york.ac.uk/serving_maths}
9 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
10 * @package quiz
11 */
55c0c75c 12
78e7a3dd 13 require_once(dirname(__FILE__) . '/../../config.php');
c52c62d1 14 require_once($CFG->dirroot . '/mod/quiz/locallib.php');
55c0c75c 15
3b1d5cc4 16/// Look for old-style URLs, such as may be in the logs, and redirect them to startattemtp.php
b1103f2b 17 if ($id = optional_param('id', 0, PARAM_INTEGER)) {
18 redirect($CFG->wwwroot . '/mod/quiz/startattempt.php?cmid=' . $id . '&sesskey=' . sesskey());
19 } else if ($qid = optional_param('q', 0, PARAM_INTEGER)) {
20 if (!$cm = get_coursemodule_from_instance('quiz', $qid)) {
21 print_error('invalidquizid', 'quiz');
22 }
23 redirect($CFG->wwwroot . '/mod/quiz/startattempt.php?cmid=' . $cm->id . '&sesskey=' . sesskey());
24 }
25
c52c62d1 26/// Get submitted parameters.
78e7a3dd 27 $attemptid = required_param('attempt', PARAM_INT);
ee1fb969 28 $page = optional_param('page', 0, PARAM_INT);
ee1fb969 29
fb6dcdab 30 $url = new moodle_url('/mod/quiz/attempt.php', array('attempt' => $attemptid));
55f599f0 31 if ($page !== 0) {
32 $url->param('page', $page);
33 }
34 $PAGE->set_url($url);
35
990650f9 36 $attemptobj = quiz_attempt::create($attemptid);
55c0c75c 37
10440b74 38/// Check login.
56ed242b 39 require_login($attemptobj->get_course(), false, $attemptobj->get_cm());
e63faf2b 40
b9b3aa94 41/// Check that this attempt belongs to this user.
42 if ($attemptobj->get_userid() != $USER->id) {
d4ad9adf 43 if ($attemptobj->has_capability('mod/quiz:viewreports')) {
44 redirect($attemptobj->review_url(0, $page));
45 } else {
46 quiz_error($attemptobj->get_quiz(), 'notyourattempt');
47 }
b9b3aa94 48 }
49
eab8ed9f 50/// Check capabilities and block settings
56ed242b 51 if (!$attemptobj->is_preview_user()) {
b9b3aa94 52 $attemptobj->require_capability('mod/quiz:attempt');
56ed242b
SH
53 if (empty($attemptobj->get_quiz()->showblocks)) {
54 $PAGE->blocks->show_only_fake_blocks();
55 }
c68287a9 56
eb02301a 57 } else {
73eba4be 58 navigation_node::override_active_url($attemptobj->start_attempt_url());
a8a372cc 59 }
c52c62d1 60
9f9eec1e 61/// If the attempt is already closed, send them to the review page.
62 if ($attemptobj->is_finished()) {
63 redirect($attemptobj->review_url(0, $page));
64 }
65
66/// Check the access rules.
67 $accessmanager = $attemptobj->get_access_manager(time());
68 $messages = $accessmanager->prevent_access();
69 if (!$attemptobj->is_preview_user() && $messages) {
70 print_error('attempterror', 'quiz', $quizobj->view_url(),
71 $accessmanager->print_messages($messages, true));
72 }
73 $accessmanager->do_password_check($attemptobj->is_preview_user());
74
0f66aced 75 add_to_log($attemptobj->get_courseid(), 'quiz', 'continue attempt',
44d1cba4 76 'review.php?attempt=' . $attemptobj->get_attemptid(),
77 $attemptobj->get_quizid(), $attemptobj->get_cmid());
34283aa8 78
c52c62d1 79/// Get the list of questions needed by this page.
9f9eec1e 80 $questionids = $attemptobj->get_question_ids($page);
7bbe08a2 81
78e7a3dd 82/// Check.
83 if (empty($questionids)) {
c52c62d1 84 quiz_error($quiz, 'noquestionsfound');
ee1fb969 85 }
55c0c75c 86
78e7a3dd 87/// Load those questions and the associated states.
88 $attemptobj->load_questions($questionids);
89 $attemptobj->load_question_states($questionids);
cee5ff1d 90
ee1fb969 91/// Print the quiz page ////////////////////////////////////////////////////////
56ed242b 92
ff065f96
TH
93 // Initialise the JavaScript.
94 $headtags = $attemptobj->get_html_head_contributions($page);
95 $PAGE->requires->js_init_call('M.mod_quiz.init_attempt_form', null, false, quiz_get_js_module());
96
d4a03c00 97 // Arrange for the navigation to be displayed.
98 $navbc = $attemptobj->get_navigation_panel('quiz_attempt_nav_panel', $page);
99 $firstregion = reset($PAGE->blocks->get_regions());
d9c26e21 100 $PAGE->blocks->add_fake_block($navbc, $firstregion);
d4a03c00 101
50d7b53e 102 // Print the page header
78e7a3dd 103 $title = get_string('attempt', 'quiz', $attemptobj->get_attempt_number());
56ed242b 104 $PAGE->set_heading($attemptobj->get_course()->fullname);
78e7a3dd 105 if ($accessmanager->securewindow_required($attemptobj->is_preview_user())) {
106 $accessmanager->setup_secure_page($attemptobj->get_course()->shortname . ': ' .
f2cb0c3d
TH
107 format_string($attemptobj->get_quiz_name()));
108 } else if ($accessmanager->safebrowser_required($attemptobj->is_preview_user())) {
a09f21d0 109 $PAGE->set_title($attemptobj->get_course()->shortname . ': '.format_string($attemptobj->get_quiz_name()));
110 $PAGE->set_cacheable(false);
111 echo $OUTPUT->header();
50d7b53e 112 } else {
03da0c39 113 $PAGE->set_title(format_string($attemptobj->get_quiz_name()));
03da0c39 114 echo $OUTPUT->header();
50d7b53e 115 }
50d7b53e 116
78e7a3dd 117 if ($attemptobj->is_preview_user()) {
56ed242b
SH
118
119 $quiz = $attemptobj->get_quiz();
50d7b53e 120
c52c62d1 121 /// Heading and tab bar.
90cd54cb 122 echo $OUTPUT->heading(get_string('previewquiz', 'quiz', format_string($quiz->name)));
78e7a3dd 123 $attemptobj->print_restart_preview_button();
c52c62d1 124
125 /// Inform teachers of any restrictions that would apply to students at this point.
126 if ($messages) {
3b1d5cc4 127 echo $OUTPUT->box_start('quizaccessnotices');
90cd54cb 128 echo $OUTPUT->heading(get_string('accessnoticesheader', 'quiz'), 3);
c52c62d1 129 $accessmanager->print_messages($messages);
3b1d5cc4 130 echo $OUTPUT->box_end();
d8d94977 131 }
bb6f1955 132 }
a5e1f35c 133
5df53d00 134 // Start the form
d4ad9adf 135 echo '<form id="responseform" method="post" action="', s($attemptobj->processattempt_url()),
1bd66f39 136 '" enctype="multipart/form-data" accept-charset="utf-8">', "\n";
5328c274 137 echo '<div>';
ee1fb969 138
a5e1f35c 139/// Print all the questions
78e7a3dd 140 foreach ($attemptobj->get_question_ids($page) as $id) {
aafdb447 141 $attemptobj->print_question($id, false, $attemptobj->attempt_url($id, $page));
586b2c82 142 }
ee1fb969 143
78e7a3dd 144/// Print a link to the next page.
3c168fbb 145 echo '<div class="submitbtns">';
78e7a3dd 146 if ($attemptobj->is_last_page($page)) {
9f9eec1e 147 $nextpage = -1;
78e7a3dd 148 } else {
9f9eec1e 149 $nextpage = $page + 1;
ee1fb969 150 }
d18675a8 151 echo '<input type="submit" value="' . get_string('next') . '" />';
d975f343 152 echo "</div>";
ee1fb969 153
9f9eec1e 154 // Some hidden fields to trach what is going on.
155 echo '<input type="hidden" name="attempt" value="' . $attemptobj->get_attemptid() . '" />';
d18675a8 156 echo '<input type="hidden" name="nextpage" id="nextpagehiddeninput" value="' . $nextpage . '" />';
690510bd 157 echo '<input type="hidden" name="timeup" id="timeup" value="0" />';
9f9eec1e 158 echo '<input type="hidden" name="sesskey" value="' . sesskey() . '" />';
fba3ea31 159
c52c62d1 160 // Add a hidden field with questionids. Do this at the end of the form, so
fba3ea31 161 // if you navigate before the form has finished loading, it does not wipe all
162 // the student's answers.
78e7a3dd 163 echo '<input type="hidden" name="questionids" value="' .
164 implode(',', $attemptobj->get_question_ids($page)) . "\" />\n";
fba3ea31 165
5d6a723d 166 // Finish the form
167 echo '</div>';
5d6a723d 168 echo "</form>\n";
169
ee1fb969 170 // Finish the page
78e7a3dd 171 $accessmanager->show_attempt_timer_if_needed($attemptobj->get_attempt(), time());
867847e3 172 echo $OUTPUT->footer();
83192608 173