-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathstorage.php
388 lines (323 loc) · 15.3 KB
/
storage.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
<?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/>.
/**
* Store results of an attempt at a HotPot quiz
*
* @package mod-hotpot
* @copyright 2010 Gordon Bateson <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
// get the standard XML parser supplied with Moodle
require_once($CFG->dirroot.'/lib/xmlize.php');
/**
* mod_hotpot_storage
*
* @copyright 2010 Gordon Bateson
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since Moodle 2.0
*/
class mod_hotpot_storage {
/**
* the name of the $_POST fields holding the score and details
* and the xml tag within the details that holds the results
*/
const scorefield = 'mark';
const detailsfield = 'detail';
const xmlresultstag = 'hpjsresult';
/**
* the two fields that will be used to determine the duration of a quiz attempt
* starttime/endtime are recorded by the client (and may not be trustworthy)
* resumestart/resumefinish are recorded by the server (but include transfer time to and from client)
*/
const durationstartfield = 'timestart';
const durationfinishfield = 'timefinish';
// functions to store responses returned from browser
/**
* store
*
* @param xxx $hotpot
*/
static public function store($hotpot) {
global $CFG, $DB, $USER;
if (empty($hotpot->attempt)) {
return; // no attempt record - shouldn't happen !!
}
if ($hotpot->attempt->userid != $USER->id) {
return; // wrong userid - shouldn't happen !!
}
// update quiz attempt fields using incoming data
$hotpot->attempt->score = max(0, optional_param(self::scorefield, 0, PARAM_INT));
$hotpot->attempt->status = max(0, optional_param('status', 0, PARAM_INT));
$hotpot->attempt->redirect = max(0, optional_param('redirect', 0, PARAM_INT));
$hotpot->attempt->details = optional_param(self::detailsfield, '', PARAM_RAW);
// update timemodified for this attempt
$hotpot->attempt->timemodified = $hotpot->time;
// time values, e.g. "2008-09-12 16:18:18 +0900",
// need to be converted to numeric date stamps
$timefields = array('starttime', 'endtime');
foreach ($timefields as $timefield) {
$hotpot->attempt->$timefield = 0; // default
if ($time = optional_param($timefield, '', PARAM_RAW)) {
// make sure the timezone has a "+" sign
// Note: sometimes it gets stripped (by optional_param?)
$time = preg_replace('/(?<= )\d{4}$/', '+$0', trim($time));
// convert $time to numeric date stamp
// PHP4 gives -1 on error, whereas PHP5 give false
$time = strtotime($time);
if ($time && $time>0) {
$hotpot->attempt->$timefield = $time;
}
}
}
unset($timefields, $timefield, $time);
// set finish times
$hotpot->attempt->timefinish = $hotpot->time;
// increment quiz attempt duration
$startfield = self::durationstartfield; // "starttime" or "timestart"
$finishfield = self::durationfinishfield; // "endtime" or "timefinish"
$duration = ($hotpot->attempt->$finishfield - $hotpot->attempt->$startfield);
if (empty($hotpot->attempt->duration)) {
$hotpot->attempt->duration = $duration;
} else if ($duration > 0) {
$hotpot->attempt->duration += $duration;
}
unset($duration, $startfield, $finishfield);
// set clickreportid, (for click reporting)
$hotpot->attempt->clickreportid = $hotpot->attempt->id;
// check if there are any previous results stored for this attempt
// this could happen if ...
// - the quiz has been resumed
// - clickreporting is enabled for this quiz
if ($DB->get_field('hotpot_attempts', 'timefinish', array('id'=>$hotpot->attempt->id))) {
if ($hotpot->clickreporting) { // self::can_clickreporting()
// add quiz attempt record for each form submission
// records are linked via the "clickreportid" field
// update timemodified and status in previous records in this clickreportid group
$DB->set_field('hotpot_attempts', 'timemodified', $hotpot->time, array('clickreportid'=>$hotpot->attempt->clickreportid));
$DB->set_field('hotpot_attempts', 'status', $hotpot->attempt->status, array('clickreportid'=>$hotpot->attempt->clickreportid));
// add new attempt record
unset($hotpot->attempt->id);
if (! $hotpot->attempt->id = $DB->insert_record('hotpot_attempts', $hotpot->attempt)) {
print_error('error_insertrecord', 'hotpot', '', 'hotpot_attempts');
}
} else {
// remove previous responses for this attempt, if required
// (N.B. this does NOT remove the attempt record, just the responses)
$DB->delete_records('hotpot_responses', array('attemptid'=>$hotpot->attempt->id));
}
}
// add details of this quiz attempt, if required
// "hotpot_storedetails" is set by administrator
// Site Admin -> Modules -> Activities -> HotPot
if ($CFG->hotpot_storedetails) {
// delete/update/add the details record
if ($DB->record_exists('hotpot_details', array('attemptid'=>$hotpot->attempt->id))) {
$DB->set_field('hotpot_details', 'details', $hotpot->attempt->details, array('attemptid'=>$hotpot->attempt->id));
} else {
$details = (object)array(
'attemptid' => $hotpot->attempt->id,
'details' => $hotpot->attempt->details
);
if (! $DB->insert_record('hotpot_details', $details, false)) {
print_error('error_insertrecord', 'hotpot', '', 'hotpot_details');
}
unset($details);
}
}
// add details of this attempt
self::store_details($hotpot->attempt);
// update the attempt record
if (! $DB->update_record('hotpot_attempts', $hotpot->attempt)) {
print_error('error_updaterecord', 'hotpot', '', 'hotpot_attempts');
}
// regrade the quiz to take account of the latest quiz attempt score
hotpot_update_grades($hotpot->to_stdclass(), $hotpot->attempt->userid);
}
/**
* pre_xmlize
*
* @param xxx $old_string (passed by reference)
* @return xxx
*/
static public function pre_xmlize(&$old_string) {
$new_string = '';
$str_start = 0;
while (($cdata_start = strpos($old_string, '<![CDATA[', $str_start)) && ($cdata_end = strpos($old_string, ']]>', $cdata_start))) {
$cdata_end += 3;
$new_string .= str_replace('&', '&', substr($old_string, $str_start, $cdata_start-$str_start)).substr($old_string, $cdata_start, $cdata_end-$cdata_start);
$str_start = $cdata_end;
}
$new_string .= str_replace('&', '&', substr($old_string, $str_start));
return $new_string;
}
/**
* store_details
*
* @param xxx $attempt (passed by reference)
*/
static public function store_details($attempt) {
// encode ampersands so that HTML entities are preserved in the XML parser
// N.B. ampersands inside <![CDATA[ ]]> blocks do NOT need to be encoded
// disabled 2008.11.20
// $attempt->details = self::pre_xmlize($attempt->details);
// parse the attempt details as xml
$details = xmlize($attempt->details);
$question_number; // initially unset
$question = false;
$response = false;
$i = 0;
while (isset($details[self::xmlresultstag]['#']['fields']['0']['#']['field'][$i]['#'])) {
// shortcut to field
$field = &$details[self::xmlresultstag]['#']['fields']['0']['#']['field'][$i]['#'];
// extract field name and data
if (isset($field['fieldname'][0]['#']) && is_string($field['fieldname'][0]['#'])) {
$name = $field['fieldname'][0]['#'];
} else {
$name = '';
}
if (isset($field['fielddata'][0]['#']) && is_string($field['fielddata'][0]['#'])) {
$data = $field['fielddata'][0]['#'];
} else {
$data = '';
}
// parse the field name into $matches
// [1] quiz type
// [2] attempt detail name
if (preg_match('/^(\w+?)_(\w+)$/', $name, $matches)) {
$quiztype = strtolower($matches[1]);
$name = strtolower($matches[2]);
// parse the attempt detail $name into $matches
// [1] question number
// [2] question detail name
if (preg_match('/^q(\d+)_(\w+)$/', $name, $matches)) {
$num = $matches[1];
$name = strtolower($matches[2]);
// not needed Moodle 2.0 and later
// $data = addslashes($data);
// adjust JCross question numbers
if (preg_match('/^(across|down)(.*)$/', $name, $matches)) {
$num .= '_'.$matches[1]; // e.g. 01_across, 02_down
$name = $matches[2];
if (substr($name, 0, 1)=='_') {
$name = substr($name, 1); // remove leading '_'
}
}
if (isset($question_number) && $question_number==$num) {
// do nothing - this response is for the same question as the previous response
} else {
// store previous question / response (if any)
self::add_response($attempt, $question, $response);
// initialize question object
$question = new stdClass();
$question->name = '';
$question->text = '';
$question->hotpotid = $attempt->hotpotid;
// initialize response object
$response = new stdClass();
$response->attemptid = $attempt->id;
// update question number
$question_number = $num;
}
// adjust field name and value, and set question type
// (may not be necessary one day)
// hotpot_adjust_response_field($quiztype, $question, $num, $name, $data);
// add $data to the question/response details
switch ($name) {
case 'name':
case 'type':
$question->$name = $data;
break;
case 'text':
$question->$name = hotpot::string_id($data);
break;
case 'correct':
case 'ignored':
case 'wrong':
$response->$name = hotpot::string_ids($data);
break;
case 'score':
case 'weighting':
case 'hints':
case 'clues':
case 'checks':
$response->$name = intval($data);
break;
}
} else { // attempt details
// adjust field name and value
//hotpot_adjust_response_field($quiztype, $question, $num='', $name, $data);
// add $data to the attempt details
if ($name=='penalties') {
$attempt->$name = intval($data);
}
}
}
$i++;
} // end while
// add the final question and response, if any
self::add_response($attempt, $question, $response);
}
/**
* add_response
*
* @param xxx $attempt (passed by reference)
* @param xxx $question (passed by reference)
* @param xxx $response (passed by reference)
*/
static public function add_response(&$attempt, &$question, &$response) {
global $DB;
if (! $question || ! $response || ! isset($question->name)) {
// nothing to add
return;
}
$loopcount = 1;
$questionname = $question->name;
// loop until we are able to add the response record
$looping = true;
while ($looping) {
$question->md5key = md5($question->name);
if (! $question->id = $DB->get_field('hotpot_questions', 'id', array('hotpotid'=>$attempt->hotpotid, 'md5key'=>$question->md5key))) {
// add question record
if (! $question->id = $DB->insert_record('hotpot_questions', $question)) {
print_error('error_insertrecord', 'hotpot', '', 'hotpot_questions');
}
}
if ($DB->record_exists('hotpot_responses', array('attemptid'=>$attempt->id, 'questionid'=>$question->id))) {
// there is already a response to this question for this attempt
// probably because this quiz has two questions with the same text
// e.g. Which one of these answers is correct?
// To workaround this, we create new question names
// e.g. Which one of these answers is correct? (2)
// until we get a question name for which there is no response yet on this attempt
$loopcount++;
$question->name = "$questionname ($loopcount)";
// This method fails to correctly identify questions in
// quizzes which allow questions to be shuffled or omitted.
// As yet, there is no workaround for such cases.
} else {
// no response found to this question in this attempt
// so we can proceed
$response->questionid = $question->id;
// add response record
if(! $response->id = $DB->insert_record('hotpot_responses', $response)) {
print_error('error_insertrecord', 'hotpot', '', 'hotpot_responses');
}
$looping = false;
}
} // end while
}
}