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);
27 $next_url = "$CFG->wwwroot/course/view.php?id=$course->id";
30 // check user can access this hotpot activity
31 if (!hotpot_is_visible($cm)) {
32 print_error("activityiscurrentlyhidden", 'hotpot', $next_url);
35 // update attempt record fields using incoming data
36 $attempt->score
= optional_param('mark', NULL, PARAM_INT
);
37 $attempt->status
= optional_param('status', NULL, PARAM_INT
);
38 $attempt->details
= optional_param('detail', NULL, PARAM_RAW
);
39 $attempt->endtime
= optional_param('endtime', NULL, PARAM_ALPHA
);
40 $attempt->starttime
= optional_param('starttime', NULL, PARAM_ALPHA
);
41 $attempt->timefinish
= $time;
43 // convert times, if necessary
44 if (empty($attempt->starttime
)) {
45 $attempt->starttime
= 0;
47 $attempt->starttime
= strtotime($attempt->starttime
);
49 if (empty($attempt->endtime
)) {
50 $attempt->endtime
= 0;
52 $attempt->endtime
= strtotime($attempt->endtime
);
55 // set clickreportid, (for click reporting)
56 $attempt->clickreportid
= $attempt->id
;
58 $quiztype = optional_param('quiztype', 0, PARAM_INT
);
60 if (empty($attempt->details
)) {
61 hotpot_set_attempt_details($attempt);
62 $javascript_is_off = true;
64 $javascript_is_off = false;
67 if (empty($attempt->status
)) {
68 if (empty($attempt->endtime
)) {
69 $attempt->status
= HOTPOT_STATUS_INPROGRESS
;
71 $attempt->status
= HOTPOT_STATUS_COMPLETED
;
75 // check if this is the second (or subsequent) click
76 if (get_field("hotpot_attempts", "timefinish", "id", $attempt->id
)) {
78 if ($hotpot->clickreporting
==HOTPOT_YES
) {
79 // add attempt record for each form submission
80 // records are linked via the "clickreportid" field
82 // update status in previous records in this group
83 set_field("hotpot_attempts", "status", $attempt->status
, "clickreportid", $attempt->clickreportid
);
85 // add new attempt record
87 $attempt->id
= insert_record("hotpot_attempts", $attempt);
89 if (empty($attempt->id
)) {
90 error("Could not insert attempt record: ".$db->ErrorMsg(), $next_url);
93 // add attempt details record, if necessary
94 if (!empty($attempt->details
)) {
96 $details->attempt
= $attempt->id
;
97 $details->details
= $attempt->details
;
98 if (! insert_record("hotpot_details", $details, false)) {
99 error("Could not insert attempt details record: ".$db->ErrorMsg(), $next_url);
103 // remove previous responses for this attempt, if required
104 // (N.B. this does NOT remove the attempt record, just the responses)
105 delete_records("hotpot_responses", "attempt", $attempt->id
);
109 // remove slashes added by lib/setup.php
110 $attempt->details
= stripslashes($attempt->details
);
112 // add details of this attempt
113 hotpot_add_attempt_details($attempt);
115 // add slashes again, so the details can be added to the database
116 $attempt->details
= addslashes($attempt->details
);
118 // update the attempt record
119 if (! update_record("hotpot_attempts", $attempt)) {
120 error("Could not update attempt record: ".$db->ErrorMsg(), $next_url);
123 // update grades for this user
124 hotpot_update_grades($hotpot, $attempt->userid
);
126 // get previous attempt details record, if any
127 $details_exist = record_exists("hotpot_details", "attempt", $attempt->id
);
129 // delete/update/add the attempt details record
130 if (empty($attempt->details
)) {
131 if ($details_exist) {
132 delete_records("hotpot_details", "attempt", $attempt->id
);
135 if ($details_exist) {
136 set_field("hotpot_details", "details", $attempt->details
, "attempt", $attempt->id
);
139 $details->attempt
= $attempt->id
;
140 $details->details
= $attempt->details
;
141 if (! insert_record("hotpot_details", $details)) {
142 error("Could not insert attempt details record: ".$db->ErrorMsg(), $next_url);
147 if ($attempt->status
==HOTPOT_STATUS_INPROGRESS
) {
148 if ($javascript_is_off) {
149 // regenerate HTML page
150 define('HOTPOT_FIRST_ATTEMPT', false);
151 include ("$CFG->hotpotroot/view.php");
153 // continue without reloading the page
154 header("Status: 204");
155 header("HTTP/1.0 204 No Response");
158 } else { // quiz is finished
160 add_to_log($course->id
, "hotpot", "submit", "review.php?id=$cm->id&attempt=$attempt->id", "$hotpot->id", "$cm->id");
162 if ($hotpot->shownextquiz
==HOTPOT_YES
) {
163 if (is_numeric($next_cm = hotpot_get_next_cm($cm))) {
164 $next_url = "$CFG->wwwroot/mod/hotpot/view.php?id=$next_cm";
168 // redirect to the next quiz or the course page
169 redirect($next_url, get_string('resultssaved', 'hotpot'));
176 function hotpot_get_next_cm(&$cm) {
177 // gets the next module in this section of the course
178 // that is the same type of module as the current module
182 // get a list of $ids of modules in this section
183 if ($ids = get_field('course_sections', 'sequence', 'id', $cm->section
)) {
186 $ids = explode(',', $ids);
187 foreach ($ids as $id) {
188 if ($found && ($cm->module
==get_field('course_modules', 'module', 'id', $id))) {
191 } else if ($cm->id
==$id) {
198 function hotpot_set_attempt_details(&$attempt) {
199 global $CFG, $HOTPOT_QUIZTYPE;
201 // optional_param('showallquestions', 0, PARAM_INT);
203 $attempt->details
= '';
205 $attempt->status
= HOTPOT_STATUS_COMPLETED
;
207 $buttons = array('clues', 'hints', 'checks');
208 $textfields = array('correct', 'wrong', 'ignored');
211 $quiztype = optional_param('quiztype', 0, PARAM_ALPHANUM
);
213 if (is_numeric($quiztype)) {
214 $ok = array_key_exists($quiztype, $HOTPOT_QUIZTYPE);
216 $quiztype = array_search($quiztype, $HOTPOT_QUIZTYPE);
217 $ok = is_numeric($quiztype);
222 // error('Quiz type is missing or invalid');
223 // print_error('error_invalidquiztype', 'hotpot');
225 // script finishes here if quiztype is invalid
229 // special flag to detect jquiz multiselect
230 $is_jquiz_multiselect = false;
232 // set maximum question number
238 $field="q{$q_max}_a0_text";
247 } while ($field && isset($_POST[$field]) && ($q_max = $q_max+
1));
249 // check JQuiz navigation buttons
251 case isset($_POST['ShowAllQuestionsButton']):
252 $_POST['ShowAllQuestions'] = 1;
254 case isset($_POST['ShowOneByOneButton']):
255 $_POST['ShowAllQuestions'] = 0;
257 case isset($_POST['PrevQButton']):
258 $_POST['ThisQuestion']--;
260 case isset($_POST['NextQButton']):
261 $_POST['ThisQuestion']++
;
267 $responsefield="q{$q}";
269 $questiontype = optional_param("{$responsefield}_questiontype", 0, PARAM_INT
);
270 $is_jquiz_multiselect = ($quiztype==HOTPOT_JQUIZ
&& $questiontype==HOTPOT_JQUIZ_MULTISELECT
);
272 if (isset($_POST[$responsefield]) && is_array($_POST[$responsefield])) {
273 $responsevalue = array();
274 foreach ($_POST[$responsefield] as $key=>$value) {
275 $responsevalue[$key] = clean_param($value, PARAM_CLEAN
);
278 $responsevalue = optional_param($responsefield, '');
280 if (is_array($responsevalue)) {
281 // incomplete jquiz multi-select
282 $responsevalues = $responsevalue;
283 $responsevalue = implode('+', $responsevalue);
285 $responsevalues = explode('+', $responsevalue);
288 // initialize $response object
289 $response = new stdClass();
290 $response->correct
= array();
291 $response->wrong
= array();
292 $response->ignored
= array();
293 $response->clues
= 0;
294 $response->hints
= 0;
295 $response->checks
= 0;
296 $response->score
= 0;
297 $response->weighting
= 0;
299 // create another empty object to hold all previous responses (from database)
300 $oldresponse = new stdClass();
301 $vars = get_object_vars($response);
302 foreach($vars as $name=>$value) {
303 $oldresponse->$name = $value;
306 foreach ($buttons as $button) {
307 if (($field = "q{$q}_{$button}_button") && isset($_POST[$field])) {
308 $value = optional_param($field, '', PARAM_RAW
);
309 if (!empty($value)) {
310 $response->$button++
;
315 // loop through possible answers to this question
316 $firstcorrectvalue = '';
319 while (($valuefield="q{$q}_a{$a}_text") && isset($_POST[$valuefield])) {
320 $value = optional_param($valuefield, '', PARAM_RAW
);
322 if (($percentfield="q{$q}_a{$a}_percent") && isset($_POST[$percentfield])) {
323 $percent = optional_param($percentfield, 0, PARAM_INT
);
325 $percents[$value] = $percent;
329 if (($correctfield="q{$q}_a{$a}_correct") && isset($_POST[$correctfield])) {
330 $correct = optional_param($correctfield, 0, PARAM_INT
);
335 if ($correct && empty($firstcorrectvalue)) {
336 $firstcorrectvalue = $value;
339 if ($is_jquiz_multiselect) {
340 $selected = in_array($value, $responsevalues);
342 $response->correct
[] = $value;
343 if (empty($selected)) {
344 $response->wrong
[] = true;
348 $response->wrong
[] = true;
352 // single answer only required
353 if ($responsevalue==$value) {
355 $response->correct
[] = $value;
357 $response->wrong
[] = $value;
360 $response->ignored
[] = $value;
366 // number of answers for this question
369 if ($is_jquiz_multiselect) {
370 if (empty($response->wrong
) && count($responsevalues)==count($response->correct
)) {
371 $response->wrong
= array();
372 $response->correct
= array($responsevalue);
374 $response->correct
= array();
375 $response->wrong
= array($responsevalue);
378 // if response did not match any answer, then this response is wrong
379 if (empty($response->correct
) && empty($response->wrong
)) {
380 $response->wrong
[] = $responsevalue;
384 // if this question has not been answered correctly, quiz is still in progress
385 if (empty($response->correct
)) {
387 if (isset($_POST["q{$q}_ShowAnswers_button"])) {
388 $_POST[$responsefield] = $firstcorrectvalue;
390 $attempt->status
= HOTPOT_STATUS_INPROGRESS
;
392 if (isset($_POST["q{$q}_Hint_button"])) {
393 // a particular hint button in JQuiz shortanswer
394 $_POST['HintButton'] = true;
397 // give a hint, if necessary
398 if (isset($_POST['HintButton']) && $firstcorrectvalue) {
400 // make sure we only come through here once
401 unset($_POST['HintButton']);
403 $correctlen = strlen($firstcorrectvalue);
404 $responselen = strlen($responsevalue);
406 // check how many letters are the same
408 while ($i<$responselen && $i<$correctlen && $responsevalue{$i}==$firstcorrectvalue{$i}) {
412 if ($i<$responselen) {
413 // remove incorrect characters on the end of the response
414 $responsevalue = substr($responsevalue, 0, $i);
416 if ($i<$correctlen) {
417 // append next correct letter
418 $responsevalue .= $firstcorrectvalue{$i};
420 $_POST[$responsefield] = $responsevalue;
424 } // end if not correct
426 // get clue text, if any
427 if (($field="q{$q}_clue") && isset($_POST[$field])) {
428 $response->clue_text
= optional_param($field, '', PARAM_RAW
);
432 $qq = sprintf('%02d', $q); // (a padded, two-digit version of $q)
433 if (($field="q{$q}_name") && isset($_POST[$field])) {
434 $questionname = optional_param($field, '', PARAM_RAW
);
435 $questionname = strip_tags($questionname);
440 // get previous responses to this question (if any)
441 $records = get_records_sql("
445 {$CFG->prefix}hotpot_attempts a,
446 {$CFG->prefix}hotpot_questions q,
447 {$CFG->prefix}hotpot_responses r
449 a.clickreportid = $attempt->clickreportid AND
451 r.question = q.id AND
452 q.name = '$questionname' AND
453 q.hotpot = $attempt->hotpot
459 foreach ($records as $record) {
460 foreach ($buttons as $button) {
461 $oldresponse->$button = max($oldresponse->$button, $record->$button);
463 foreach ($textfields as $field) {
464 if ($record->$field && ($field=='correct' ||
$field=='wrong')) {
465 $values = explode(',', hotpot_strings($record->$field));
466 $oldresponse->$field = array_merge($oldresponse->$field, $values);
472 // remove "correct" and "wrong" values from "ignored" values
473 $response->ignored
= array_diff($response->ignored
,
474 $response->correct
, $response->wrong
, $oldresponse->correct
, $oldresponse->wrong
477 foreach ($buttons as $button) {
478 $response->$button +
= $oldresponse->$button;
481 $value_has_changed = false;
482 foreach ($textfields as $field) {
483 $response->$field = array_merge($oldresponse->$field, $response->$field);
484 $response->$field = array_unique($response->$field);
485 $response->$field = implode(',', $response->$field);
487 if ($field=='correct' ||
$field=='wrong') {
488 $array = $oldresponse->$field;
489 $array = array_unique($array);
490 $oldresponse->$field = implode(',', $array);
491 if ($response->$field<>$oldresponse->$field) {
492 $value_has_changed = true;
496 if ($value_has_changed) {
500 // $response now holds amalgamation of all responses so far to this question
502 // set question score and weighting
503 if ($response->correct
) {
508 $strlen = strlen($response->correct
);
509 $response->score
= 100*($strlen-($response->checks
-1))/$strlen;
510 $attempt->score +
= $response->score
;
519 switch ($questiontype) {
520 case HOTPOT_JQUIZ_MULTICHOICE
:
521 $wrong = explode(',', $response->wrong
);
522 foreach ($wrong as $value) {
523 if (isset($percents[$value])) {
524 $percent = $percents[$value];
529 case HOTPOT_JQUIZ_SHORTANSWER
:
530 $strlen = strlen($response->correct
);
531 $response->score
= 100*($strlen-($response->checks
-1))/$strlen;
533 case HOTPOT_JQUIZ_MULTISELECT
:
534 if (isset($percents[$response->correct
])) {
535 $percent = $percents[$response->correct
];
539 if ($a_max>0 && $response->checks
>0 && $a_max>$response->checks
) {
540 $response->score
= $percent*($a_max-($response->checks
-1))/$a_max;
544 $attempt->score +
= $response->score
;
549 $fieldname = $HOTPOT_QUIZTYPE[$quiztype]."_q{$qq}_name";
550 $attempt->details
.= "<field><fieldname>$fieldname</fieldname><fielddata>$questionname</fielddata></field>";
552 // encode $response fields as XML
553 $vars = get_object_vars($response);
554 foreach($vars as $name=>$value) {
555 if (!empty($value)) {
556 $fieldname = $HOTPOT_QUIZTYPE[$quiztype]."_q{$qq}_{$name}";
557 $attempt->details
.= "<field><fieldname>$fieldname</fieldname><fielddata>$value</fielddata></field>";
562 } // end main loop through $q(uestions)
570 $attempt->score
= floor($attempt->score
/ $q);
583 if ($attempt->details
) {
584 $attempt->details
= '<?xml version="1.0"?><hpjsresult><fields>'.$attempt->details
.'</fields></hpjsresult>';
587 // print "forcing status to in progress ..<br/>\n";
588 // $attempt->status = HOTPOT_STATUS_INPROGRESS;