Permalink
Browse files

attempt.php now includes view.php for "mobile" outputformat; added ch…

…ecks, hints and score for "mobile" outputformat
  • Loading branch information...
1 parent ae11b65 commit 6cadcaf0773ee1b298f3de8c8631b3cd866a0f70 gbateson committed Oct 5, 2005
Showing with 138 additions and 82 deletions.
  1. +93 −50 mod/hotpot/attempt.php
  2. +1 −0 mod/hotpot/lib.php
  3. +44 −32 mod/hotpot/view.php
View
@@ -45,8 +45,11 @@
// set clickreportid, (for click reporting)
$attempt->clickreportid = $attempt->id;
- if (empty($attempt->details)) { // "mobile" output format
+ if (empty($attempt->details)) {
hotpot_set_attempt_details($attempt);
+ $javascript_is_off = true;
+ } else {
+ $javascript_is_off = false;
}
if (empty($attempt->status)) {
@@ -127,9 +130,15 @@
}
if ($attempt->status==HOTPOT_STATUS_INPROGRESS) {
- // continue the quiz
- header("Status: 204");
- header("HTTP/1.0 204 No Response");
+ if ($javascript_is_off) {
+ // regenerate HTML page
+ define('HOTPOT_FIRST_ATTEMPT', false);
+ include ("$CFG->hotpotroot/view.php");
+ } else {
+ // continue without reloading the page
+ header("Status: 204");
+ header("HTTP/1.0 204 No Response");
+ }
} else { // quiz is finished
@@ -174,6 +183,7 @@ function hotpot_get_next_cm(&$cm) {
function hotpot_set_attempt_details(&$attempt) {
$attempt->details = '';
+ $attempt->score = 0;
$attempt->status = HOTPOT_STATUS_COMPLETED;
$buttons = array('clues', 'hints', 'checks');
@@ -183,7 +193,7 @@ function hotpot_set_attempt_details(&$attempt) {
$q = 0;
while (($responsefield="q{$q}") && isset($_POST[$responsefield])) {
- $responsevalue = optional_param($responsefield, '', PARAM_ALPHA);
+ $responsevalue = optional_param($responsefield, '');
// initialize $response object
$response = NULL;
@@ -206,10 +216,15 @@ function hotpot_set_attempt_details(&$attempt) {
}
// loop through possible answers to this question
+ $firstcorrectvalue = '';
$i = 0;
while (($correctfield="q{$q}_correct_{$i}") && isset($_POST[$correctfield])) {
$correctvalue = optional_param($correctfield, '', PARAM_RAW);
+ if (empty($firstcorrectvalue)) {
+ $firstcorrectvalue = $correctvalue;
+ }
+
if ($responsevalue==$correctvalue) {
$response->correct = $responsevalue;
} else {
@@ -222,8 +237,34 @@ function hotpot_set_attempt_details(&$attempt) {
if (empty($response->correct)) {
$response->wrong = $responsevalue;
$attempt->status = HOTPOT_STATUS_INPROGRESS;
+
+ // give a hint, if necessary
+ if (isset($_POST['HintButton']) && $firstcorrectvalue) {
+
+ // make sure we only come through here once
+ unset($_POST['HintButton']);
+
+ $correctlen = strlen($firstcorrectvalue);
+ $responselen = strlen($responsevalue);
+
+ $i = 0;
+ while ($i<$responselen && $i<$correctlen && $responsevalue{$i}==$firstcorrectvalue{$i}) {
+ $i++;
+ }
+
+ if ($i<$responselen) {
+ // remove incorrect characters on the end of the response
+ $responsevalue = substr($responsevalue, 0, $i);
+ }
+ if ($i<$correctlen) {
+ // append next correct letter
+ $responsevalue .= $firstcorrectvalue{$i};
+ }
+ $_POST[$responsefield] = $responsevalue;
+ $response->hints++;
+ }
}
-
+
// convert "ignored" array to string
$response->ignored = implode(',', $response->ignored);
@@ -232,6 +273,15 @@ function hotpot_set_attempt_details(&$attempt) {
$response->clue_text = optional_param($field, '', PARAM_RAW);
}
+ $oldresponse = NULL;
+ foreach ($buttons as $button) {
+ $oldresponse->$button = 0;
+ }
+ foreach ($textfields as $field) {
+ $oldresponse->$field = array();
+ $response->$field = empty($response->$field) ? array() : explode(',', $response->$field);
+ }
+
// get question name
$qq = sprintf('%02d', $q); // (a padded, two-digit version of $q)
if (($field="q{$q}_name") && isset($_POST[$field])) {
@@ -248,43 +298,53 @@ function hotpot_set_attempt_details(&$attempt) {
$records = false;
}
if ($records) {
-
- $max = array();
- foreach ($buttons as $button) {
- $max[$button] = 0;
- }
- foreach ($textfields as $field) {
- $response->$field = empty($response->$field) ? array() : explode(',', $response->$field);
- }
-
foreach ($records as $record) {
foreach ($buttons as $button) {
- $max[$button] = max($max[$button], $record->$button);
+ $oldresponse->$button = max($oldresponse->$button, $record->$button);
}
foreach ($textfields as $field) {
if ($record->$field) {
$values = explode(',', hotpot_strings($record->$field));
- $response->$field = array_merge($response->$field, $values);
+ $oldresponse->$field = array_merge($oldresponse->$field, $values);
}
}
}
-
- // remove "correct" and "wrong" values from "ignored" values
- $response->ignored = array_diff($response->ignored, $response->correct, $response->wrong);
-
- foreach ($buttons as $button) {
- $response->$button += $max[$button];
- }
- foreach ($textfields as $field) {
- $response->$field = array_unique($response->$field);
- $response->$field = implode(',', $response->$field);
+ }
+ // remove "correct" and "wrong" values from "ignored" values
+ $response->ignored = array_diff(
+ $response->ignored,
+ $response->correct, $response->wrong, $oldresponse->correct, $oldresponse->wrong
+ );
+ foreach ($buttons as $button) {
+ $response->$button += $oldresponse->$button;
+ }
+ $value_has_changed = false;
+ foreach ($textfields as $field) {
+ $response->$field = array_merge($response->$field, $oldresponse->$field);
+ $response->$field = array_unique($response->$field);
+ $response->$field = implode(',', $response->$field);
+
+ $oldresponse->$field = array_unique($oldresponse->$field);
+ $oldresponse->$field = implode(',', $oldresponse->$field);
+
+ if ($field=='correct' || $field=='wrong') {
+ if ($response->$field<>$oldresponse->$field) {
+ $value_has_changed = true;
+ }
}
-
- } // end if $records
+ }
+ if ($value_has_changed) {
+ $response->checks++;
+ }
// $response now holds amalgamation of all responses so far to this question
// set score and weighting
+ if ($response->correct) {
+ $strlen = strlen($response->correct);
+ $response->score = 100*($strlen-($response->checks-1))/$strlen;
+ $attempt->score += $response->score;
+ }
// encode $response fields as XML
$vars = get_object_vars($response);
@@ -297,6 +357,10 @@ function hotpot_set_attempt_details(&$attempt) {
$q++;
}
+ if ($q>0) {
+ $attempt->score = floor($attempt->score / $q);
+ }
+
if ($attempt->details) {
$attempt->details = '<?xml version="1.0"?><hpjsresult><fields>'.$attempt->details.'</fields></hpjsresult>';
}
@@ -306,25 +370,4 @@ function hotpot_set_attempt_details(&$attempt) {
$attempt->status = HOTPOT_STATUS_INPROGRESS;
}
}
-function hotpot_XXX() {
- $values = explode(',', $value);
- foreach ($values as $value) {
-
- $i_max =strlen($value);
- for ($i=0; $i<$i_max; $i++) {
-
- $char = $value{$i};
- $ord = ord($char);
- if ($ord==43 || $ord==44 || $ord>128) { // comma, plus-sign or multibyte char
- $fielddata .= '&#x'.sprintf('%04X', $char).';';
- } else {
- $fielddata .= $char;
- }
- }
- }
-
- if (preg_match('#[<>]#', $fielddata)) {
- $fielddata = '<![CDATA[' + $fielddata + ']]>';
- }
-}
?>
View
@@ -1134,6 +1134,7 @@ function hotpot_add_attempt($hotpotid) {
break;
}
+ // set all previous "in progress" attempts at this quiz to "abandoned"
$db->Execute("
UPDATE
{$CFG->prefix}hotpot_attempts as a
View
@@ -2,40 +2,48 @@
/// This page prints a hotpot quiz
- require_once("../../config.php");
- require_once("lib.php");
-
- $id = optional_param("id"); // Course Module ID, or
- $hp = optional_param("hp"); // hotpot ID
-
- if ($id) {
- if (! $cm = get_record("course_modules", "id", $id)) {
- error("Course Module ID was incorrect");
- }
- if (! $course = get_record("course", "id", $cm->course)) {
- error("Course is misconfigured");
- }
- if (! $hotpot = get_record("hotpot", "id", $cm->instance)) {
- error("Course module is incorrect");
- }
+ if (defined('HOTPOT_FIRST_ATTEMPT') && HOTPOT_FIRST_ATTEMPT==false) {
+ // this script is being included (by attempt.php)
} else {
- if (! $hotpot = get_record("hotpot", "id", $hp)) {
- error("Course module is incorrect");
- }
- if (! $course = get_record("course", "id", $hotpot->course)) {
- error("Course is misconfigured");
- }
- if (! $cm = get_coursemodule_from_instance("hotpot", $hotpot->id, $course->id)) {
- error("Course Module ID was incorrect");
+ // this script is being called directly from the browser
+ define('HOTPOT_FIRST_ATTEMPT', true);
+
+ require_once("../../config.php");
+ require_once("lib.php");
+
+ $id = optional_param("id"); // Course Module ID, or
+ $hp = optional_param("hp"); // hotpot ID
+
+ if ($id) {
+ if (! $cm = get_record("course_modules", "id", $id)) {
+ error("Course Module ID was incorrect");
+ }
+ if (! $course = get_record("course", "id", $cm->course)) {
+ error("Course is misconfigured");
+ }
+ if (! $hotpot = get_record("hotpot", "id", $cm->instance)) {
+ error("Course module is incorrect");
+ }
+
+ } else {
+ if (! $hotpot = get_record("hotpot", "id", $hp)) {
+ error("Course module is incorrect");
+ }
+ if (! $course = get_record("course", "id", $hotpot->course)) {
+ error("Course is misconfigured");
+ }
+ if (! $cm = get_coursemodule_from_instance("hotpot", $hotpot->id, $course->id)) {
+ error("Course Module ID was incorrect");
+ }
}
+
+ require_login($course->id);
}
// set nextpage (for error messages)
$nextpage = "$CFG->wwwroot/course/view.php?id=$course->id";
- require_login($course->id);
-
// header strings
$title = strip_tags($course->shortname.': '.$hotpot->name);
$heading = "$course->fullname";
@@ -52,7 +60,7 @@
$time = time();
$hppassword = optional_param('hppassword');
- if (!isteacher($course->id)) {
+ if (HOTPOT_FIRST_ATTEMPT && !isteacher($course->id)) {
// check this quiz is available to this student
// error message, if quiz is unavailable
@@ -131,6 +139,7 @@
$available_msg = get_string("quizavailable", "quiz", userdate($hotpot->timeclose))."<br />\n";
}
+
// open and parse the source file
if(!$hp = new hotpot_xml_quiz($hotpot)) {
error("Quiz is unavailable at the moment");
@@ -165,11 +174,14 @@
}
if ($get_html) {
- add_to_log($course->id, "hotpot", "view", "view.php?id=$cm->id", "$hotpot->id", "$cm->id");
-
- $attemptid = hotpot_add_attempt($hotpot->id);
- if (! is_numeric($attemptid)) {
- error('Could not insert attempt record: '.$db->ErrorMsg);
+
+ if (HOTPOT_FIRST_ATTEMPT) {
+ add_to_log($course->id, "hotpot", "view", "view.php?id=$cm->id", "$hotpot->id", "$cm->id");
+
+ $attemptid = hotpot_add_attempt($hotpot->id);
+ if (! is_numeric($attemptid)) {
+ error('Could not insert attempt record: '.$db->ErrorMsg);
+ }
}
$hp->adjust_media_urls();

0 comments on commit 6cadcaf

Please sign in to comment.