forked from middlebury/moodle-mod_adaptivequiz
-
Notifications
You must be signed in to change notification settings - Fork 5
/
attempt.php
277 lines (232 loc) · 11.3 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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
<?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/>.
/**
* Adaptive quiz attempt script
*
* This module was created as a collaborative effort between Middlebury College
* and Remote Learner.
*
* @package mod_adaptivequiz
* @copyright 2013 onwards Remote-Learner {@link http://www.remote-learner.ca/}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once(dirname(__FILE__).'/../../config.php');
require_once($CFG->dirroot.'/mod/adaptivequiz/locallib.php');
require_once($CFG->dirroot.'/mod/adaptivequiz/adaptiveattempt.class.php');
require_once($CFG->dirroot.'/mod/adaptivequiz/fetchquestion.class.php');
require_once($CFG->dirroot.'/mod/adaptivequiz/catalgo.class.php');
require_once($CFG->dirroot.'/tag/lib.php');
$id = required_param('cmid', PARAM_INT); // Course module id.
$uniqueid = optional_param('uniqueid', 0, PARAM_INT); // uniqueid of the attempt.
$difflevel = optional_param('dl', 0, PARAM_INT); // difficulty level of question.
if (!$cm = get_coursemodule_from_id('adaptivequiz', $id)) {
print_error('invalidcoursemodule');
}
if (!$course = $DB->get_record('course', array('id' => $cm->course))) {
print_error("coursemisconf");
}
global $USER, $DB, $SESSION;
require_login($course, true, $cm);
$context = context_module::instance($cm->id);
$passwordattempt = false;
try {
$adaptivequiz = $DB->get_record('adaptivequiz', array('id' => $cm->instance), '*', MUST_EXIST);
} catch (dml_exception $e) {
$url = new moodle_url('/mod/adaptivequiz/attempt.php', array('cmid' => $id));
$debuginfo = '';
if (!empty($e->debuginfo)) {
$debuginfo = $e->debuginfo;
}
print_error('invalidmodule', 'error', $url, $e->getMessage(), $debuginfo);
}
// Setup page global for standard viewing.
$viewurl = new moodle_url('/mod/adaptivequiz/view.php', array('id' => $cm->id));
$PAGE->set_url('/mod/adaptivequiz/view.php', array('id' => $cm->id));
$PAGE->set_title(format_string($adaptivequiz->name));
$PAGE->set_context($context);
// Check if the user has the attempt capability.
require_capability('mod/adaptivequiz:attempt', $context);
// Check if the user has any previous attempts at this activity.
$count = adaptivequiz_count_user_previous_attempts($adaptivequiz->id, $USER->id);
if (!adaptivequiz_allowed_attempt($adaptivequiz->attempts, $count)) {
print_error('noattemptsallowed', 'adaptivequiz');
}
// Create an instance of the module renderer class.
$output = $PAGE->get_renderer('mod_adaptivequiz');
// Setup password required form.
$mform = $output->display_password_form($cm->id);
// Check if a password is required.
if (!empty($adaptivequiz->password)) {
// Check if the user has alredy entered in their password.
$condition = adaptivequiz_user_entered_password($adaptivequiz->id);
if (empty($condition) && $mform->is_cancelled()) {
// Return user to landing page.
redirect($viewurl);
} else if (empty($condition) && $data = $mform->get_data()) {
$SESSION->passwordcheckedadpq = array();
if (0 == strcmp($data->quizpassword, $adaptivequiz->password)) {
$SESSION->passwordcheckedadpq[$adaptivequiz->id] = true;
} else {
$SESSION->passwordcheckedadpq[$adaptivequiz->id] = false;
$passwordattempt = true;
}
}
}
// Create an instance of the adaptiveattempt class.
$adaptiveattempt = new adaptiveattempt($adaptivequiz, $USER->id);
$algo = new stdClass();
$nextdiff = null;
$standarderror = 0.0;
$message = '';
// If uniqueid is not empty the process respones.
if (!empty($uniqueid) && confirm_sesskey()) {
// Check if the uniqueid belongs to the same attempt record the user is currently using.
$attemptrec = $adaptiveattempt->get_attempt();
if (!adaptivequiz_uniqueid_part_of_attempt($uniqueid, $cm->instance, $USER->id)) {
print_error('uniquenotpartofattempt', 'adaptivequiz');
}
// Process student's responses.
try {
// Set a time stamp for the actions below.
$time = time();
// Load the user's current usage from the DB.
$quba = question_engine::load_questions_usage_by_activity((int) $uniqueid);
// Update the actions done to the question.
$quba->process_all_actions($time);
// Finish the grade attempt at the question.
$quba->finish_all_questions($time);
// Save the data about the usage to the DB.
question_engine::save_questions_usage_by_activity($quba);
if (!empty($difflevel)) {
// Check if the minimum number of attempts have been reached.
$minattemptreached = adaptivequiz_min_attempts_reached($uniqueid, $cm->instance, $USER->id);
// Create an instance of the CAT algo class.
$algo = new catalgo($quba, (int) $attemptrec->id, $minattemptreached, (int) $difflevel);
// Calculate the next difficulty level.
$nextdiff = $algo->perform_calculation_steps();
// Increment difficulty level for attempt.
$everythingokay = false;
$difflogit = $algo->get_levellogit();
$standarderror = $algo->get_standarderror();
$measure = $algo->get_measure();
$everythingokay = adaptivequiz_update_attempt_data($uniqueid, $cm->instance, $USER->id, $difflogit, $standarderror,
$measure);
// Something went wrong with updating the attempt. Print an error.
if (!$everythingokay) {
$url = new moodle_url('/mod/adaptivequiz/attempt.php', array('cmid' => $id));
print_error('unableupdatediffsum', 'adaptivequiz', $url);
}
// Check whether the status property is empty.
$message = $algo->get_status();
if (!empty($message)) {
$standarderror = $algo->get_standarderror();
// Set the attempt to complete, update the standard error and attempt message, then redirect the user to the
// attempt finished page.
adaptivequiz_complete_attempt($uniqueid, $cm->instance, $USER->id, $standarderror, $message);
$param = array('cmid' => $cm->id, 'id' => $cm->instance, 'uattid' => $uniqueid);
$url = new moodle_url('/mod/adaptivequiz/attemptfinished.php', $param);
redirect($url);
}
// Lastly decrement the sum of questions for the attempted difficulty level.
$fetchquestion = new fetchquestion($quba, $difflevel, $adaptivequiz->lowestlevel, $adaptivequiz->highestlevel);
$tagquestcount = $fetchquestion->get_tagquestsum();
$tagquestcount = $fetchquestion->decrement_question_sum_from_difficulty($tagquestcount, $difflevel);
$fetchquestion->set_tagquestsum($tagquestcount);
// Force the class to deconstruct the object and save the updated mapping to the session global.
unset($fetchquestion);
}
} catch (question_out_of_sequence_exception $e) {
$url = new moodle_url('/mod/adaptivequiz/attempt.php', array('cmid' => $id));
print_error('submissionoutofsequencefriendlymessage', 'question', $url);
} catch (Exception $e) {
$url = new moodle_url('/mod/adaptivequiz/attempt.php', array('cmid' => $id));
$debuginfo = '';
if (!empty($e->debuginfo)) {
$debuginfo = $e->debuginfo;
}
print_error('errorprocessingresponses', 'question', $url, $e->getMessage(), $debuginfo);
}
}
$adaptivequiz->context = $context;
$adaptivequiz->cm = $cm;
// If value is null then set the difficulty level to the starting level for the attempt.
if (!is_null($nextdiff)) {
$adaptiveattempt->set_level((int) $nextdiff);
} else {
$adaptiveattempt->set_level((int) $adaptivequiz->startinglevel);
}
// If we have a previous difficulty level, pass that off to the attempt so that it
// can modify the next-question search process based on this level.
if (isset($difflevel) && !is_null($difflevel)) {
$adaptiveattempt->set_last_difficulty_level($difflevel);
}
$attemptstatus = $adaptiveattempt->start_attempt();
// Check if attempt status is set to ready.
if (empty($attemptstatus)) {
// Retrieve the most recent status message for the attempt.
$message = $adaptiveattempt->get_status();
// Set the attempt to complete, update the standard error and attempt message, then redirect the user to the attempt-finished
// page.
if ($algo instanceof catalgo) {
$standarderror = $algo->get_standarderror();
}
adaptivequiz_complete_attempt($uniqueid, $cm->instance, $USER->id, $standarderror, $message);
// Redirect the user to the attemptfeedback page.
$param = array('cmid' => $cm->id, 'id' => $cm->instance, 'uattid' => $uniqueid);
$url = new moodle_url('/mod/adaptivequiz/attemptfinished.php', $param);
redirect($url);
}
// Retrieve the question slot id.
$slot = $adaptiveattempt->get_question_slot_number();
// Retrieve the question_usage_by_activity object.
$quba = $adaptiveattempt->get_quba();
// If $nextdiff is null then this is either a new attempt or a continuation of an previous attempt. Calculate the current
// difficulty level the attempt should be at.
if (is_null($nextdiff)) {
// Calculate the current difficulty level.
$adaptivequiz->lowestlevel = (int) $adaptivequiz->lowestlevel;
$adaptivequiz->highestlevel = (int) $adaptivequiz->highestlevel;
$adaptivequiz->startinglevel = (int) $adaptivequiz->startinglevel;
// Create an instance of the catalgo class, however constructor arguments are not important.
$algo = new catalgo($quba, 1, false, 1);
$level = $algo->get_current_diff_level($quba, $adaptivequiz->startinglevel, $adaptivequiz);
} else {
// Retrieve the currently set difficulty level.
$level = $adaptiveattempt->get_level();
}
$headtags = $output->init_metadata($quba, $slot);
$PAGE->requires->js_init_call('M.mod_adaptivequiz.init_attempt_form', array($viewurl->out(), $adaptivequiz->browsersecurity),
false, $output->adaptivequiz_get_js_module());
// Init secure window if enabled.
if (!empty($adaptivequiz->browsersecurity)) {
$PAGE->blocks->show_only_fake_blocks();
$output->init_browser_security();
} else {
$PAGE->set_heading(format_string($course->fullname));
}
// Check if the user entered a password.
$condition = adaptivequiz_user_entered_password($adaptivequiz->id);
if (!empty($adaptivequiz->password) && empty($condition)) {
echo $output->print_header();
if ($passwordattempt) {
$mform->set_data(array('message' => get_string('wrongpassword', 'adaptivequiz')));
}
$mform->display();
echo $output->print_footer();
} else {
// Render the question to the page.
echo $output->print_question($id, $quba, $slot, $level);
}