2 require_once("../../config.php");
3 require_once("lib.php");
5 $attemptid = required_param('attemptid', PARAM_INT
);
7 // get attempt, hotpot, course and course_module records
8 if (! $attempt = get_record("hotpot_attempts", "id", $attemptid)) {
9 error("Hot Potatoes attempt record $attemptid could not be accessed: ".$db->ErrorMsg());
11 if ($attempt->userid
!= $USER->id
) {
12 error("User ID is incorrect");
14 if (! $hotpot = get_record("hotpot", "id", $attempt->hotpot
)) {
15 error("Hot Potatoes ID is incorrect (attempt id = $attempt->id)");
17 if (! $course = get_record("course", "id", $hotpot->course
)) {
18 error("Course ID is incorrect (hotpot id = $hotpot->id)");
20 if (! $cm = get_coursemodule_from_instance("hotpot", $hotpot->id
, $course->id
)) {
21 error("Course Module ID is incorrect");
24 // make sure this user is enrolled in this course
25 require_login($course->id
);
27 $next_url = "$CFG->wwwroot/course/view.php?id=$course->id";
30 // update attempt record fields using incoming data
31 $attempt->score
= optional_param('mark', NULL, PARAM_INT
);
32 $attempt->status
= optional_param('status', NULL, PARAM_INT
);
33 $attempt->details
= optional_param('detail', NULL, PARAM_RAW
);
34 $attempt->endtime
= optional_param('endtime', NULL, PARAM_ALPHA
);
35 $attempt->starttime
= optional_param('starttime', NULL, PARAM_ALPHA
);
36 $attempt->timefinish
= $time;
38 // convert times, if necessary
39 if (empty($attempt->starttime
)) {
40 $attempt->starttime
= 0;
42 $attempt->starttime
= strtotime($attempt->starttime
);
44 if (empty($attempt->endtime
)) {
45 $attempt->endtime
= 0;
47 $attempt->endtime
= strtotime($attempt->endtime
);
50 // set clickreportid, (for click reporting)
51 $attempt->clickreportid
= $attempt->id
;
53 $quiztype = optional_param('quiztype', 0, PARAM_INT
);
55 if (empty($attempt->details
)) {
56 hotpot_set_attempt_details($attempt);
57 $javascript_is_off = true;
59 $javascript_is_off = false;
62 if (empty($attempt->status
)) {
63 if (empty($attempt->endtime
)) {
64 $attempt->status
= HOTPOT_STATUS_INPROGRESS
;
66 $attempt->status
= HOTPOT_STATUS_COMPLETED
;
70 // check if this is the second (or subsequent) click
71 if (get_field("hotpot_attempts", "timefinish", "id", $attempt->id
)) {
73 if ($hotpot->clickreporting
==HOTPOT_YES
) {
74 // add attempt record for each form submission
75 // records are linked via the "clickreportid" field
77 // update status in previous records in this group
78 set_field("hotpot_attempts", "status", $attempt->status
, "clickreportid", $attempt->clickreportid
);
80 // add new attempt record
82 $attempt->id
= insert_record("hotpot_attempts", $attempt);
84 if (empty($attempt->id
)) {
85 error("Could not insert attempt record: ".$db->ErrorMsg(), $next_url);
88 // add attempt details record, if necessary
89 if (!empty($attempt->details
)) {
91 $details->attempt
= $attempt->id
;
92 $details->details
= $attempt->details
;
93 if (! insert_record("hotpot_details", $details, false)) {
94 error("Could not insert attempt details record: ".$db->ErrorMsg(), $next_url);
98 // remove previous responses for this attempt, if required
99 // (N.B. this does NOT remove the attempt record, just the responses)
100 delete_records("hotpot_responses", "attempt", $attempt->id
);
104 // remove slashes added by lib/setup.php
105 $attempt->details
= stripslashes($attempt->details
);
107 // add details of this attempt
108 hotpot_add_attempt_details($attempt);
110 // add slashes again, so the details can be added to the database
111 $attempt->details
= addslashes($attempt->details
);
113 // update the attempt record
114 if (! update_record("hotpot_attempts", $attempt)) {
115 error("Could not update attempt record: ".$db->ErrorMsg(), $next_url);
118 // get previous attempt details record, if any
119 $details_exist = record_exists("hotpot_details", "attempt", $attempt->id
);
121 // delete/update/add the attempt details record
122 if (empty($attempt->details
)) {
123 if ($details_exist) {
124 delete_records("hotpot_details", "attempt", $attempt->id
);
127 if ($details_exist) {
128 set_field("hotpot_details", "details", $attempt->details
, "attempt", $attempt->id
);
131 $details->attempt
= $attempt->id
;
132 $details->details
= $attempt->details
;
133 if (! insert_record("hotpot_details", $details)) {
134 error("Could not insert attempt details record: ".$db->ErrorMsg(), $next_url);
139 if ($attempt->status
==HOTPOT_STATUS_INPROGRESS
) {
140 if ($javascript_is_off) {
141 // regenerate HTML page
142 define('HOTPOT_FIRST_ATTEMPT', false);
143 include ("$CFG->hotpotroot/view.php");
145 // continue without reloading the page
146 header("Status: 204");
147 header("HTTP/1.0 204 No Response");
150 } else { // quiz is finished
152 add_to_log($course->id
, "hotpot", "submit", "review.php?id=$cm->id&attempt=$attempt->id", "$hotpot->id", "$cm->id");
154 if ($hotpot->shownextquiz
==HOTPOT_YES
) {
155 if (is_numeric($next_cm = hotpot_get_next_cm($cm))) {
156 $next_url = "$CFG->wwwroot/mod/hotpot/view.php?id=$next_cm";
160 // redirect to the next quiz or the course page
161 redirect($next_url, get_string('resultssaved', 'hotpot'));
168 function hotpot_get_next_cm(&$cm) {
169 // gets the next module in this section of the course
170 // that is the same type of module as the current module
174 // get a list of $ids of modules in this section
175 if ($ids = get_field('course_sections', 'sequence', 'id', $cm->section
)) {
178 $ids = explode(',', $ids);
179 foreach ($ids as $id) {
180 if ($found && ($cm->module
==get_field('course_modules', 'module', 'id', $id))) {
183 } else if ($cm->id
==$id) {
190 function hotpot_set_attempt_details(&$attempt) {
191 global $CFG, $HOTPOT_QUIZTYPE;
193 // optional_param('showallquestions', 0, PARAM_INT);
195 $attempt->details
= '';
197 $attempt->status
= HOTPOT_STATUS_COMPLETED
;
199 $buttons = array('clues', 'hints', 'checks');
200 $textfields = array('correct', 'wrong', 'ignored');
203 $quiztype = optional_param('quiztype', 0, PARAM_ALPHANUM
);
205 if (is_numeric($quiztype)) {
206 $ok = array_key_exists($quiztype, $HOTPOT_QUIZTYPE);
208 $quiztype = array_search($quiztype, $HOTPOT_QUIZTYPE);
209 $ok = is_numeric($quiztype);
214 // error('Quiz type is missing or invalid');
215 // error(get_string('error_invalidquiztype', 'hotpot'));
217 // script finishes here if quiztype is invalid
221 // special flag to detect jquiz multiselect
222 $is_jquiz_multiselect = false;
224 // set maximum question number
230 $field="q{$q_max}_a0_text";
239 } while ($field && isset($_POST[$field]) && ($q_max = $q_max+
1));
241 // check JQuiz navigation buttons
243 case isset($_POST['ShowAllQuestionsButton']):
244 $_POST['ShowAllQuestions'] = 1;
246 case isset($_POST['ShowOneByOneButton']):
247 $_POST['ShowAllQuestions'] = 0;
249 case isset($_POST['PrevQButton']):
250 $_POST['ThisQuestion']--;
252 case isset($_POST['NextQButton']):
253 $_POST['ThisQuestion']++
;
259 $responsefield="q{$q}";
261 $questiontype = optional_param("{$responsefield}_questiontype", 0, PARAM_INT
);
262 $is_jquiz_multiselect = ($quiztype==HOTPOT_JQUIZ
&& $questiontype==HOTPOT_JQUIZ_MULTISELECT
);
264 if (isset($_POST[$responsefield]) && is_array($_POST[$responsefield])) {
265 $responsevalue = array();
266 foreach ($_POST[$responsefield] as $key=>$value) {
267 $responsevalue[$key] = clean_param($value, PARAM_CLEAN
);
270 $responsevalue = optional_param($responsefield, '');
272 if (is_array($responsevalue)) {
273 // incomplete jquiz multi-select
274 $responsevalues = $responsevalue;
275 $responsevalue = implode('+', $responsevalue);
277 $responsevalues = explode('+', $responsevalue);
280 // initialize $response object
281 $response = new stdClass();
282 $response->correct
= array();
283 $response->wrong
= array();
284 $response->ignored
= array();
285 $response->clues
= 0;
286 $response->hints
= 0;
287 $response->checks
= 0;
288 $response->score
= 0;
289 $response->weighting
= 0;
291 // create another empty object to hold all previous responses (from database)
292 $oldresponse = new stdClass();
293 $vars = get_object_vars($response);
294 foreach($vars as $name=>$value) {
295 $oldresponse->$name = $value;
298 foreach ($buttons as $button) {
299 if (($field = "q{$q}_{$button}_button") && isset($_POST[$field])) {
300 $value = optional_param($field, '', PARAM_RAW
);
301 if (!empty($value)) {
302 $response->$button++
;
307 // loop through possible answers to this question
308 $firstcorrectvalue = '';
311 while (($valuefield="q{$q}_a{$a}_text") && isset($_POST[$valuefield])) {
312 $value = optional_param($valuefield, '', PARAM_RAW
);
314 if (($percentfield="q{$q}_a{$a}_percent") && isset($_POST[$percentfield])) {
315 $percent = optional_param($percentfield, 0, PARAM_INT
);
317 $percents[$value] = $percent;
321 if (($correctfield="q{$q}_a{$a}_correct") && isset($_POST[$correctfield])) {
322 $correct = optional_param($correctfield, 0, PARAM_INT
);
327 if ($correct && empty($firstcorrectvalue)) {
328 $firstcorrectvalue = $value;
331 if ($is_jquiz_multiselect) {
332 $selected = in_array($value, $responsevalues);
334 $response->correct
[] = $value;
335 if (empty($selected)) {
336 $response->wrong
[] = true;
340 $response->wrong
[] = true;
344 // single answer only required
345 if ($responsevalue==$value) {
347 $response->correct
[] = $value;
349 $response->wrong
[] = $value;
352 $response->ignored
[] = $value;
358 // number of answers for this question
361 if ($is_jquiz_multiselect) {
362 if (empty($response->wrong
) && count($responsevalues)==count($response->correct
)) {
363 $response->wrong
= array();
364 $response->correct
= array($responsevalue);
366 $response->correct
= array();
367 $response->wrong
= array($responsevalue);
370 // if response did not match any answer, then this response is wrong
371 if (empty($response->correct
) && empty($response->wrong
)) {
372 $response->wrong
[] = $responsevalue;
376 // if this question has not been answered correctly, quiz is still in progress
377 if (empty($response->correct
)) {
379 if (isset($_POST["q{$q}_ShowAnswers_button"])) {
380 $_POST[$responsefield] = $firstcorrectvalue;
382 $attempt->status
= HOTPOT_STATUS_INPROGRESS
;
384 if (isset($_POST["q{$q}_Hint_button"])) {
385 // a particular hint button in JQuiz shortanswer
386 $_POST['HintButton'] = true;
389 // give a hint, if necessary
390 if (isset($_POST['HintButton']) && $firstcorrectvalue) {
392 // make sure we only come through here once
393 unset($_POST['HintButton']);
395 $correctlen = strlen($firstcorrectvalue);
396 $responselen = strlen($responsevalue);
398 // check how many letters are the same
400 while ($i<$responselen && $i<$correctlen && $responsevalue{$i}==$firstcorrectvalue{$i}) {
404 if ($i<$responselen) {
405 // remove incorrect characters on the end of the response
406 $responsevalue = substr($responsevalue, 0, $i);
408 if ($i<$correctlen) {
409 // append next correct letter
410 $responsevalue .= $firstcorrectvalue{$i};
412 $_POST[$responsefield] = $responsevalue;
416 } // end if not correct
418 // get clue text, if any
419 if (($field="q{$q}_clue") && isset($_POST[$field])) {
420 $response->clue_text
= optional_param($field, '', PARAM_RAW
);
424 $qq = sprintf('%02d', $q); // (a padded, two-digit version of $q)
425 if (($field="q{$q}_name") && isset($_POST[$field])) {
426 $questionname = optional_param($field, '', PARAM_RAW
);
427 $questionname = strip_tags($questionname);
432 // get previous responses to this question (if any)
433 $records = get_records_sql("
437 {$CFG->prefix}hotpot_attempts a,
438 {$CFG->prefix}hotpot_questions q,
439 {$CFG->prefix}hotpot_responses r
441 a.clickreportid = $attempt->clickreportid AND
443 r.question = q.id AND
444 q.name = '$questionname' AND
445 q.hotpot = $attempt->hotpot
451 foreach ($records as $record) {
452 foreach ($buttons as $button) {
453 $oldresponse->$button = max($oldresponse->$button, $record->$button);
455 foreach ($textfields as $field) {
456 if ($record->$field && ($field=='correct' ||
$field=='wrong')) {
457 $values = explode(',', hotpot_strings($record->$field));
458 $oldresponse->$field = array_merge($oldresponse->$field, $values);
464 // remove "correct" and "wrong" values from "ignored" values
465 $response->ignored
= array_diff($response->ignored
,
466 $response->correct
, $response->wrong
, $oldresponse->correct
, $oldresponse->wrong
469 foreach ($buttons as $button) {
470 $response->$button +
= $oldresponse->$button;
473 $value_has_changed = false;
474 foreach ($textfields as $field) {
475 $response->$field = array_merge($oldresponse->$field, $response->$field);
476 $response->$field = array_unique($response->$field);
477 $response->$field = implode(',', $response->$field);
479 if ($field=='correct' ||
$field=='wrong') {
480 $array = $oldresponse->$field;
481 $array = array_unique($array);
482 $oldresponse->$field = implode(',', $array);
483 if ($response->$field<>$oldresponse->$field) {
484 $value_has_changed = true;
488 if ($value_has_changed) {
492 // $response now holds amalgamation of all responses so far to this question
494 // set question score and weighting
495 if ($response->correct
) {
500 $strlen = strlen($response->correct
);
501 $response->score
= 100*($strlen-($response->checks
-1))/$strlen;
502 $attempt->score +
= $response->score
;
511 switch ($questiontype) {
512 case HOTPOT_JQUIZ_MULTICHOICE
:
513 $wrong = explode(',', $response->wrong
);
514 foreach ($wrong as $value) {
515 if (isset($percents[$value])) {
516 $percent = $percents[$value];
521 case HOTPOT_JQUIZ_SHORTANSWER
:
522 $strlen = strlen($response->correct
);
523 $response->score
= 100*($strlen-($response->checks
-1))/$strlen;
525 case HOTPOT_JQUIZ_MULTISELECT
:
526 if (isset($percents[$response->correct
])) {
527 $percent = $percents[$response->correct
];
531 if ($a_max>0 && $response->checks
>0 && $a_max>$response->checks
) {
532 $response->score
= $percent*($a_max-($response->checks
-1))/$a_max;
536 $attempt->score +
= $response->score
;
541 $fieldname = $HOTPOT_QUIZTYPE[$quiztype]."_q{$qq}_name";
542 $attempt->details
.= "<field><fieldname>$fieldname</fieldname><fielddata>$questionname</fielddata></field>";
544 // encode $response fields as XML
545 $vars = get_object_vars($response);
546 foreach($vars as $name=>$value) {
547 if (!empty($value)) {
548 $fieldname = $HOTPOT_QUIZTYPE[$quiztype]."_q{$qq}_{$name}";
549 $attempt->details
.= "<field><fieldname>$fieldname</fieldname><fielddata>$value</fielddata></field>";
554 } // end main loop through $q(uestions)
562 $attempt->score
= floor($attempt->score
/ $q);
575 if ($attempt->details
) {
576 $attempt->details
= '<?xml version="1.0"?><hpjsresult><fields>'.$attempt->details
.'</fields></hpjsresult>';
579 // print "forcing status to in progress ..<br/>\n";
580 // $attempt->status = HOTPOT_STATUS_INPROGRESS;