-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathattempt.php
240 lines (207 loc) · 9.53 KB
/
attempt.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Fault-tolerant quiz mode, replacement attempt.php page.
*
* @package quizaccess_offlinemode
* @copyright 2014 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once(__DIR__ . '/../../../../config.php');
require_once($CFG->dirroot . '/mod/quiz/locallib.php');
// Get submitted parameters.
$attemptid = required_param('attempt', PARAM_INT);
$page = optional_param('page', null, PARAM_INT);
// Create the attempt object.
$attemptobj = quiz_attempt::create($attemptid);
// Fix the page number if necessary.
if ($page === null) {
$page = $attemptobj->get_attempt()->currentpage;
}
if ($attemptobj->get_navigation_method() == QUIZ_NAVMETHOD_SEQ && $page < $attemptobj->get_currentpage()) {
$page = $attemptobj->get_currentpage();
}
// Initialise $PAGE.
$pageurl = $attemptobj->attempt_url(null, $page);
$PAGE->set_url(quizaccess_offlinemode::ATTEMPT_URL, $pageurl->params());
// Check login.
require_login($attemptobj->get_course(), false, $attemptobj->get_cm());
// Check that this attempt belongs to this user.
if ($attemptobj->get_userid() != $USER->id) {
if ($attemptobj->has_capability('mod/quiz:viewreports')) {
redirect($attemptobj->review_url(null, $page));
} else {
throw new moodle_quiz_exception($attemptobj->get_quizobj(), 'notyourattempt');
}
}
// Check capabilities and block settings.
if (!$attemptobj->is_preview_user()) {
$attemptobj->require_capability('mod/quiz:attempt');
if (empty($attemptobj->get_quiz()->showblocks)) {
$PAGE->blocks->show_only_fake_blocks();
}
} else {
navigation_node::override_active_url($attemptobj->start_attempt_url());
}
// If the attempt is already closed, send them to the review page.
if ($attemptobj->is_finished()) {
redirect($attemptobj->review_url(null, $page));
} else if ($attemptobj->get_state() == quiz_attempt::OVERDUE) {
redirect($attemptobj->summary_url());
}
// Check the access rules.
$accessmanager = $attemptobj->get_access_manager(time());
$accessmanager->setup_attempt_page($PAGE);
// Complete masquerading as the mod-quiz-attempt page. Must be done after setup_attempt_page.
$PAGE->set_pagetype('mod-quiz-attempt');
// Get the renderer.
$output = $PAGE->get_renderer('mod_quiz');
$messages = $accessmanager->prevent_access();
if (!$attemptobj->is_preview_user() && $messages) {
print_error('attempterror', 'quiz', $attemptobj->view_url(),
$output->access_messages($messages));
}
if ($accessmanager->is_preflight_check_required($attemptobj->get_attemptid())) {
redirect($attemptobj->start_attempt_url(null, $page));
}
// Initialise the JavaScript.
question_engine::initialise_js();
$PAGE->requires->js_module(quiz_get_js_module());
$autosaveperiod = get_config('quiz', 'autosaveperiod');
if (!$autosaveperiod) {
// Offline mode only works with autosave, so if it is off for normal quizzes,
// use a sensible default.
$autosaveperiod = 60;
}
$firstquestionouterdivid = $attemptobj->get_question_attempt(1)->get_outer_question_div_unique_id();
$PAGE->requires->yui_module('moodle-quizaccess_offlinemode-autosave',
'M.quizaccess_offlinemode.autosave.init', array($autosaveperiod, '#' . substr($firstquestionouterdivid, 0, -1)));
$PAGE->requires->yui_module('moodle-quizaccess_offlinemode-navigation',
'M.quizaccess_offlinemode.navigation.init', array($page));
if (!empty($USER->idnumber)) {
$user = '-i' . $USER->idnumber;
} else {
$user = '-u' . $USER->id;
}
$emergencysavefilename = clean_filename(format_string($attemptobj->get_quiz_name()) .
$user . '-a' . $attemptid . '-d197001010000.attemptdata');
$PAGE->requires->yui_module('moodle-quizaccess_offlinemode-download',
'M.quizaccess_offlinemode.download.init',
array($emergencysavefilename, get_config('quizaccess_offlinemode', 'publickey')));
$PAGE->requires->strings_for_js(array('answerchanged', 'savetheresponses', 'submitting',
'submitfailed', 'submitfailedmessage', 'submitfaileddownloadmessage',
'lastsaved', 'lastsavedtotheserver', 'lastsavedtothiscomputer',
'savingdots', 'savingtryagaindots', 'savefailed', 'logindialogueheader',
'changesmadereallygoaway'), 'quizaccess_offlinemode');
$PAGE->requires->strings_for_js(array('submitallandfinish', 'confirmclose'), 'quiz');
$PAGE->requires->string_for_js('flagged', 'question');
$PAGE->requires->string_for_js('confirmation', 'admin');
// Log this page view.
$params = array(
'objectid' => $attemptid,
'relateduserid' => $attemptobj->get_userid(),
'courseid' => $attemptobj->get_courseid(),
'context' => context_module::instance($attemptobj->get_cmid()),
'other' => array(
'quizid' => $attemptobj->get_quizid(),
'page' => $attemptobj->get_currentpage()
)
);
$event = \mod_quiz\event\attempt_viewed::create($params);
$event->add_record_snapshot('quiz_attempts', $attemptobj->get_attempt());
$event->trigger();
// Arrange for the navigation to be displayed in the first region on the page.
// Passing page number -2 here is a hack. (Conventionally it would be -1 to mean
// that all pages are being shown, but then that causes the Finish attempt link to
// not appear. -2 seems to work.
$navbc = $attemptobj->get_navigation_panel($output, 'quiz_attempt_nav_panel', -2);
$regions = $PAGE->blocks->get_regions();
$PAGE->blocks->add_fake_block($navbc, reset($regions));
// Initialise $PAGE some more.
$title = get_string('attempt', 'quiz', $attemptobj->get_attempt_number());
$PAGE->set_title($attemptobj->get_quiz_name());
$PAGE->set_heading($attemptobj->get_course()->fullname);
// A few final things.
if ($attemptobj->is_last_page($page)) {
$nextpage = -1;
} else {
$nextpage = $page + 1;
}
// Display the page.
// From mod_quiz_renderer::attempt_form.
$form = '';
// Start the form.
$form .= html_writer::start_tag('form',
array('action' => $attemptobj->processattempt_url(), 'method' => 'post',
'enctype' => 'multipart/form-data', 'accept-charset' => 'utf-8',
'id' => 'responseform'));
$form .= html_writer::start_tag('div');
// Print all the questions on every page.
$numpages = $attemptobj->get_num_pages();
for ($i = 0; $i < $numpages; $i++) {
$form .= html_writer::start_div('quiz-loading-hide',
array('id' => 'quizaccess_offlinemode-attempt_page-' . $i));
foreach ($attemptobj->get_slots($i) as $slot) {
$form .= $attemptobj->render_question($slot, false, $output,
$attemptobj->attempt_url($slot, $page));
}
$form .= html_writer::end_div('');
}
$form .= html_writer::start_tag('div', array('class' => 'submitbtns'));
$form .= html_writer::empty_tag('input', array('type' => 'submit', 'name' => 'next',
'value' => get_string('next')));
$form .= html_writer::end_tag('div');
// Some hidden fields to trach what is going on.
$form .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'attempt',
'value' => $attemptobj->get_attemptid()));
$form .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'thispage',
'value' => $page, 'id' => 'followingpage'));
$form .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'nextpage',
'value' => $nextpage));
$form .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'timeup',
'value' => '0', 'id' => 'timeup'));
$form .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'sesskey',
'value' => sesskey()));
$form .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'scrollpos',
'value' => '', 'id' => 'scrollpos'));
$form .= html_writer::empty_tag('input', array('type' => 'hidden',
'value' => $USER->id, 'id' => 'quiz-userid'));
// Add a hidden field with questionids. Do this at the end of the form, so
// if you navigate before the form has finished loading, it does not wipe all
// the student's answers.
$form .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'slots',
'value' => implode(',', $attemptobj->get_slots())));
// Summary page. Code from mod_quiz_renderer::summary_page.
$summary = '';
$summary .= html_writer::start_div('', array('id' => 'quizaccess_offlinemode-attempt_page--1'));
$summary .= $output->heading(format_string($attemptobj->get_quiz_name()));
$summary .= $output->heading(get_string('summaryofattempt', 'quiz'), 3);
$summary .= $output->summary_table($attemptobj, $attemptobj->get_display_options(false));
$controls = $output->summary_page_controls($attemptobj);
$controls = preg_replace('~<div id="quiz-timer".*?</div>~', '', $controls);
$summary .= $controls;
$summary .= html_writer::end_div();
// Finish the form.
$form .= html_writer::end_tag('div');
$form .= html_writer::end_tag('form');
// From mod_quiz_renderer::attempt_page.
$html = '';
$html .= $output->header();
$html .= $output->quiz_notices($messages);
$html .= $form;
$html .= $summary;
$html .= $output->footer();
echo $html;