Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

improved detection of $quiztype for "mobile" output format

  • Loading branch information...
commit b24a1f7fe5c9880bada14591770ab826db8b02fd 1 parent bbeff3c
authored

Showing 1 changed file with 419 additions and 403 deletions. Show diff stats Hide diff stats

  1. 822  mod/hotpot/attempt.php
822  mod/hotpot/attempt.php
... ...
@@ -1,404 +1,420 @@
1  
-<?php 
2  
-	require_once("../../config.php");
3  
-	require_once("lib.php");
4  
-
5  
-	$attemptid = required_param("attemptid");
6  
-
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());
10  
-	}
11  
-	if ($attempt->userid != $USER->id) {
12  
-		error("User ID is incorrect");
13  
-	}
14  
-	if (! $hotpot = get_record("hotpot", "id", $attempt->hotpot)) {
15  
-		error("Hot Potatoes ID is incorrect (attempt id = $attempt->id)");
16  
-	}
17  
-	if (! $course = get_record("course", "id", $hotpot->course)) {
18  
-		error("Course ID is incorrect (hotpot id = $hotpot->id)");
19  
-	}
20  
-	if (! $cm = get_coursemodule_from_instance("hotpot", $hotpot->id, $course->id)) {
21  
-		error("Course Module ID is incorrect");
22  
-	}
23  
-
24  
-	// make sure this user is enrolled in this course
25  
-	require_login($course->id);
26  
-
27  
-	$next_url = "$CFG->wwwroot/course/view.php?id=$course->id";
28  
-	$time = time();
29  
-
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;
37  
-
38  
-	if ($attempt->endtime) {
39  
-		 $attempt->endtime = strtotime($attempt->endtime);
40  
-	}
41  
-	if ($attempt->starttime) {
42  
-		 $attempt->starttime = strtotime($attempt->starttime);
43  
-	}
44  
-
45  
-	// set clickreportid, (for click reporting)
46  
-	$attempt->clickreportid = $attempt->id;
47  
-
48  
-	if (empty($attempt->details)) {
49  
-		hotpot_set_attempt_details($attempt);
50  
-		$javascript_is_off = true;
51  
-	} else {
52  
-		$javascript_is_off = false;
53  
-	}
54  
-
55  
-	if (empty($attempt->status)) {
56  
-		if (empty($attempt->endtime)) {
57  
-			$attempt->status = HOTPOT_STATUS_INPROGRESS;
58  
-		} else {
59  
-			$attempt->status = HOTPOT_STATUS_COMPLETED;
60  
-		}
61  
-	}
62  
-
63  
-	// check if this is the second (or subsequent) click
64  
-	if (get_field("hotpot_attempts", "timefinish", "id", $attempt->id)) {
65  
-
66  
-		if ($hotpot->clickreporting==HOTPOT_YES) {
67  
-			// add attempt record for each form submission
68  
-			// records are linked via the "clickreportid" field
69  
-
70  
-			// update status in previous records in this group
71  
-			set_field("hotpot_attempts", "status", $attempt->status, "clickreportid", $attempt->clickreportid);
72  
-
73  
-			// add new attempt record
74  
-			unset ($attempt->id);
75  
-			$attempt->id = insert_record("hotpot_attempts", $attempt);
76  
-
77  
-			if (empty($attempt->id)) {
78  
-				error("Could not insert attempt record: ".$db->ErrorMsg(), $next_url);
79  
-			}
80  
-
81  
-			// add attempt details record, if necessary
82  
-			if (!empty($attempt->details)) {
83  
-				unset($details);
84  
-				$details->attempt = $attempt->id;
85  
-				$details->details = $attempt->details;
86  
-				if (! insert_record("hotpot_details", $details, false)) {
87  
-					error("Could not insert attempt details record: ".$db->ErrorMsg(), $next_url);
88  
-				}
89  
-			}
90  
-		} else {
91  
-			// remove previous responses for this attempt, if required
92  
-			// (N.B. this does NOT remove the attempt record, just the responses)
93  
-			delete_records("hotpot_responses", "attempt", $attempt->id);
94  
-		}
95  
-	}
96  
-
97  
-	// remove slashes added by lib/setup.php
98  
-	$attempt->details = stripslashes($attempt->details);
99  
-
100  
-	// add details of this attempt
101  
-	hotpot_add_attempt_details($attempt);
102  
-
103  
-	// add slashes again, so the details can be added to the database
104  
-	$attempt->details = addslashes($attempt->details);
105  
-
106  
-	// update the attempt record
107  
-	if (! update_record("hotpot_attempts", $attempt)) {
108  
-		error("Could not update attempt record: ".$db->ErrorMsg(), $next_url);
109  
-	}
110  
-
111  
-	// get previous attempt details record, if any
112  
-	$details_exist = record_exists("hotpot_details", "attempt", $attempt->id);
113  
-
114  
-	// delete/update/add the attempt details record
115  
-	if (empty($attempt->details)) {
116  
-		if ($details_exist) {
117  
-			delete_records("hotpot_details", "attempt", $attempt->id);
118  
-		}
119  
-	} else {
120  
-		if ($details_exist) {
121  
-			set_field("hotpot_details", "details", $attempt->details, "attempt", $attempt->id);
122  
-		} else {
123  
-			unset($details);
124  
-			$details->attempt = $attempt->id;
125  
-			$details->details = $attempt->details;
126  
-			if (! insert_record("hotpot_details", $details)) {
127  
-				error("Could not insert attempt details record: ".$db->ErrorMsg(), $next_url);
128  
-			}
129  
-		}
130  
-	}
131  
-
132  
-	if ($attempt->status==HOTPOT_STATUS_INPROGRESS) {
133  
-		if ($javascript_is_off) {
134  
-			// regenerate HTML page
135  
-			define('HOTPOT_FIRST_ATTEMPT', false);
136  
-			include ("$CFG->hotpotroot/view.php");
137  
-		} else {
138  
-			// continue without reloading the page
139  
-			header("Status: 204");
140  
-			header("HTTP/1.0 204 No Response");
141  
-		}
142  
-
143  
-	} else { // quiz is finished
144  
-
145  
-		add_to_log($course->id, "hotpot", "submit", "review.php?id=$cm->id&attempt=$attempt->id", "$hotpot->id", "$cm->id");
146  
-
147  
-		if ($hotpot->shownextquiz==HOTPOT_YES) {
148  
-			if (is_numeric($next_cm = hotpot_get_next_cm($cm))) {
149  
-				$next_url = "$CFG->wwwroot/mod/hotpot/view.php?id=$next_cm";
150  
-			}
151  
-		}
152  
-
153  
-		// redirect to the next quiz or the course page 
154  
-		redirect($next_url, get_string('resultssaved', 'hotpot'));
155  
-	}
156  
-
157  
-// =================
158  
-//	functions
159  
-// =================
160  
-
161  
-function hotpot_get_next_cm(&$cm) {
162  
-	// gets the next module in this section of the course
163  
-	// that is the same type of module as the current module
164  
-
165  
-	$next_mod = false;
166  
-
167  
-	// get a list of $ids of modules in this section
168  
-	if ($ids = get_field('course_sections', 'sequence', 'id', $cm->section)) {
169  
-
170  
-		$found = false;
171  
-		$ids = explode(',', $ids);
172  
-		foreach ($ids as $id) {
173  
-			if ($found && ($cm->module==get_field('course_modules', 'module', 'id', $id))) {
174  
-				$next_mod = $id;
175  
-				break;
176  
-			} else if ($cm->id==$id) {
177  
-				$found = true;
178  
-			}
179  
-		}
180  
-	}
181  
-	return $next_mod;
182  
-}
183  
-function hotpot_set_attempt_details(&$attempt) {
184  
-	global $HOTPOT_QUIZTYPE;
185  
-
186  
-	$attempt->details = '';
187  
-	$attempt->score = 0;
188  
-	$attempt->status = HOTPOT_STATUS_COMPLETED;
189  
-
190  
-	$buttons = array('clues', 'hints', 'checks');
191  
-	$textfields = array('correct', 'wrong', 'ignored');
192  
-
193  
-	$quiztype = HOTPOT_JCLOZE; // optional_param('quiztype', HOTPOT_JCLOZE, PARAM_INT);
194  
-
195  
-	$q = 0;
196  
-	while (($responsefield="q{$q}") && isset($_POST[$responsefield])) {
197  
-		$responsevalue = optional_param($responsefield, '');
198  
-
199  
-		// initialize $response object
200  
-		$response = NULL;
201  
-		$response->correct = array();
202  
-		$response->wrong   = array();
203  
-		$response->ignored = array();
204  
-		$response->clues  = 0;
205  
-		$response->hints  = 0;
206  
-		$response->checks = 0;
207  
-		$response->score  = 0;
208  
-		$response->weighting  = 0;
209  
-		
210  
-		// create another empty object to hold all previous responses (from database)
211  
-		$oldresponse = NULL;
212  
-		$vars = get_object_vars($response);
213  
-		foreach($vars as $name=>$value) {
214  
-			$oldresponse->$name = $value;
215  
-		}
216  
-
217  
-		foreach ($buttons as $button) {
218  
-			if (($field = "q{$q}_{$button}_button") && isset($_POST[$field])) {
219  
-				$value = optional_param($field, '', PARAM_RAW);
220  
-				if (!empty($value)) {
221  
-					$response->$button++;
222  
-				}
223  
-			}
224  
-		}
225  
-
226  
-		// loop through possible answers to this question
227  
-		$firstcorrectvalue = '';
228  
-		$a = 0;
229  
-		while (($correctfield="q{$q}_correct_{$a}") && isset($_POST[$correctfield])) {
230  
-			$correctvalue = optional_param($correctfield, '', PARAM_RAW);
231  
-
232  
-			if (empty($firstcorrectvalue)) {
233  
-				$firstcorrectvalue = $correctvalue;
234  
-			}
235  
-
236  
-			if ($responsevalue==$correctvalue) {
237  
-				$response->correct[] = $responsevalue;
238  
-			} else {
239  
-				$response->ignored[] = $correctvalue;
240  
-			}
241  
-			$a++;
242  
-		}
243  
-
244  
-		// if no correct answer was found, then this answer is wrong
245  
-		if (empty($response->correct)) {
246  
-			$response->wrong[] = $responsevalue;
247  
-			$attempt->status = HOTPOT_STATUS_INPROGRESS;
248  
-
249  
-			// give a hint, if necessary
250  
-			if (isset($_POST['HintButton']) && $firstcorrectvalue) {
251  
-
252  
-				// make sure we only come through here once
253  
-				unset($_POST['HintButton']);
254  
-
255  
-				$correctlen = strlen($firstcorrectvalue);
256  
-				$responselen = strlen($responsevalue);
257  
-
258  
-				// check how many letters are the same
259  
-				$i = 0;
260  
-				while ($i<$responselen && $i<$correctlen && $responsevalue{$i}==$firstcorrectvalue{$i}) {
261  
-					$i++;
262  
-				}
263  
-
264  
-				if ($i<$responselen) {
265  
-					// remove incorrect characters on the end of the response
266  
-					$responsevalue = substr($responsevalue, 0, $i);
267  
-				}
268  
-				if ($i<$correctlen) {
269  
-					// append next correct letter
270  
-					$responsevalue .= $firstcorrectvalue{$i};
271  
-				}
272  
-				$_POST[$responsefield] = $responsevalue;
273  
-				$response->hints++;
274  
-			} // end if hint
275  
-		} // end if not correct
276  
-
277  
-		// get clue text, if any
278  
-		if (($field="q{$q}_clue") && isset($_POST[$field])) {
279  
-			$response->clue_text = optional_param($field, '', PARAM_RAW);
280  
-		}
281  
-
282  
-		// get question name
283  
-		$qq = sprintf('%02d', $q); // (a padded, two-digit version of $q)
284  
-		if (($field="q{$q}_name") && isset($_POST[$field])) {
285  
-			$questionname = optional_param($field, '', PARAM_RAW);
286  
-		} else {
287  
-			$questionname = $qq;
288  
-		}
289  
-
290  
-		// get previous responses to this question (if any)
291  
-		$question = get_record('hotpot_questions', 'name', $questionname, 'hotpot', $attempt->hotpot);
292  
-		if ($question) {
293  
-			$records = get_records_select('hotpot_responses', "attempt=$attempt->clickreportid AND question=$question->id");
294  
-		} else {
295  
-			$records = false;
296  
-		}
297  
-		if ($records) {
298  
-			foreach ($records as $record) {
299  
-				foreach ($buttons as $button) {
300  
-					$oldresponse->$button = max($oldresponse->$button, $record->$button);
301  
-				}
302  
-				foreach ($textfields as $field) {
303  
-					if ($record->$field && ($field=='correct' || $field=='wrong')) {
304  
-						$values = explode(',', hotpot_strings($record->$field));
305  
-						$oldresponse->$field = array_merge($oldresponse->$field, $values);
306  
-					}
307  
-				}
308  
-			}
309  
-		}
310  
-
311  
-		// remove "correct" and "wrong" values from "ignored" values
312  
-		$response->ignored = array_diff($response->ignored, 
313  
-			$response->correct, $response->wrong, $oldresponse->correct, $oldresponse->wrong
314  
-		);
315  
-
316  
-		foreach ($buttons as $button) {
317  
-			$response->$button += $oldresponse->$button;
318  
-		}
319  
-
320  
-		$value_has_changed = false;
321  
-		foreach ($textfields as $field) {
322  
-
323  
-			$response->$field = array_merge($response->$field, $oldresponse->$field);
324  
-			$response->$field = array_unique($response->$field);
325  
-			$response->$field  = implode(',', $response->$field);
326  
-
327  
-			if ($field=='correct' || $field=='wrong') {
328  
-				$array = $oldresponse->$field;
329  
-				$array = array_unique($array);
330  
-				$oldresponse->$field  = implode(',', $array);		
331  
-				if ($response->$field<>$oldresponse->$field) {
332  
-					$value_has_changed = true;
333  
-				}
334  
-			}
335  
-		}
336  
-		if ($value_has_changed) {
337  
-			$response->checks++;
338  
-		}
339  
-
340  
-		// $response now holds amalgamation of all responses so far to this question
341  
-		
342  
-		// set question score and weighting
343  
-		if ($response->correct) {
344  
-			switch ($quiztype) {
345  
-				case HOTPOT_JCB:
346  
-					break;
347  
-				case HOTPOT_JCLOZE:
348  
-					$strlen = strlen($response->correct);
349  
-					$response->score = 100*($strlen-($response->checks-1))/$strlen;
350  
-					$attempt->score += $response->score;
351  
-					break;
352  
-				case HOTPOT_JCROSS:
353  
-					break;
354  
-				case HOTPOT_JMATCH:
355  
-					break;
356  
-				case HOTPOT_JMIX:
357  
-					break;
358  
-				case HOTPOT_JQUIZ:
359  
-					break;
360  
-			}
361  
-		}
362  
-
363  
-		// encode $response fields as XML
364  
-		$vars = get_object_vars($response);
365  
-		foreach($vars as $name=>$value) {
366  
-			if (!empty($value)) {
367  
-				$fieldname = $HOTPOT_QUIZTYPE[$quiztype]."_q{$qq}_{$name}";
368  
-				$attempt->details .= "<field><fieldname>$fieldname</fieldname><fielddata>$value</fielddata></field>";
369  
-			}
370  
-		}
371  
-
372  
-		$q++;
373  
-	} // end main loop through $q(uestions)
374  
-
375  
-	// set attempt score
376  
-	if ($q>0) {
377  
-		switch ($quiztype) {
378  
-			case HOTPOT_JCB:
379  
-				break;
380  
-			case HOTPOT_JCLOZE:
381  
-				$attempt->score = floor($attempt->score / $q);
382  
-				break;
383  
-			case HOTPOT_JCROSS:
384  
-				break;
385  
-			case HOTPOT_JMATCH:
386  
-				break;
387  
-			case HOTPOT_JMIX:
388  
-				break;
389  
-			case HOTPOT_JQUIZ:
390  
-				break;
391  
-		}
392  
-	}
393  
-
394  
-	if ($attempt->details) {
395  
-		$attempt->details = '<?xml version="1.0"?><hpjsresult><fields>'.$attempt->details.'</fields></hpjsresult>';
396  
-	}
397  
-
398  
-	// check there are no unattempted questions
399  
-	if (($field="I_{$q}_correct_0") && isset($_POST[$field])) {
400  
-		$attempt->status = HOTPOT_STATUS_INPROGRESS;
401  
-	}	
402  
-}
403  
-
  1
+<?php 
  2
+	require_once("../../config.php");
  3
+	require_once("lib.php");
  4
+
  5
+	$attemptid = required_param("attemptid");
  6
+
  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());
  10
+	}
  11
+	if ($attempt->userid != $USER->id) {
  12
+		error("User ID is incorrect");
  13
+	}
  14
+	if (! $hotpot = get_record("hotpot", "id", $attempt->hotpot)) {
  15
+		error("Hot Potatoes ID is incorrect (attempt id = $attempt->id)");
  16
+	}
  17
+	if (! $course = get_record("course", "id", $hotpot->course)) {
  18
+		error("Course ID is incorrect (hotpot id = $hotpot->id)");
  19
+	}
  20
+	if (! $cm = get_coursemodule_from_instance("hotpot", $hotpot->id, $course->id)) {
  21
+		error("Course Module ID is incorrect");
  22
+	}
  23
+
  24
+	// make sure this user is enrolled in this course
  25
+	require_login($course->id);
  26
+
  27
+	$next_url = "$CFG->wwwroot/course/view.php?id=$course->id";
  28
+	$time = time();
  29
+
  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;
  37
+
  38
+	if ($attempt->endtime) {
  39
+		 $attempt->endtime = strtotime($attempt->endtime);
  40
+	}
  41
+	if ($attempt->starttime) {
  42
+		 $attempt->starttime = strtotime($attempt->starttime);
  43
+	}
  44
+
  45
+	// set clickreportid, (for click reporting)
  46
+	$attempt->clickreportid = $attempt->id;
  47
+
  48
+	if (empty($attempt->details)) {
  49
+		hotpot_set_attempt_details($attempt);
  50
+		$javascript_is_off = true;
  51
+	} else {
  52
+		$javascript_is_off = false;
  53
+	}
  54
+
  55
+	if (empty($attempt->status)) {
  56
+		if (empty($attempt->endtime)) {
  57
+			$attempt->status = HOTPOT_STATUS_INPROGRESS;
  58
+		} else {
  59
+			$attempt->status = HOTPOT_STATUS_COMPLETED;
  60
+		}
  61
+	}
  62
+
  63
+	// check if this is the second (or subsequent) click
  64
+	if (get_field("hotpot_attempts", "timefinish", "id", $attempt->id)) {
  65
+
  66
+		if ($hotpot->clickreporting==HOTPOT_YES) {
  67
+			// add attempt record for each form submission
  68
+			// records are linked via the "clickreportid" field
  69
+
  70
+			// update status in previous records in this group
  71
+			set_field("hotpot_attempts", "status", $attempt->status, "clickreportid", $attempt->clickreportid);
  72
+
  73
+			// add new attempt record
  74
+			unset ($attempt->id);
  75
+			$attempt->id = insert_record("hotpot_attempts", $attempt);
  76
+
  77
+			if (empty($attempt->id)) {
  78
+				error("Could not insert attempt record: ".$db->ErrorMsg(), $next_url);
  79
+			}
  80
+
  81
+			// add attempt details record, if necessary
  82
+			if (!empty($attempt->details)) {
  83
+				unset($details);
  84
+				$details->attempt = $attempt->id;
  85
+				$details->details = $attempt->details;
  86
+				if (! insert_record("hotpot_details", $details, false)) {
  87
+					error("Could not insert attempt details record: ".$db->ErrorMsg(), $next_url);
  88
+				}
  89
+			}
  90
+		} else {
  91
+			// remove previous responses for this attempt, if required
  92
+			// (N.B. this does NOT remove the attempt record, just the responses)
  93
+			delete_records("hotpot_responses", "attempt", $attempt->id);
  94
+		}
  95
+	}
  96
+
  97
+	// remove slashes added by lib/setup.php
  98
+	$attempt->details = stripslashes($attempt->details);
  99
+
  100
+	// add details of this attempt
  101
+	hotpot_add_attempt_details($attempt);
  102
+
  103
+	// add slashes again, so the details can be added to the database
  104
+	$attempt->details = addslashes($attempt->details);
  105
+
  106
+	// update the attempt record
  107
+	if (! update_record("hotpot_attempts", $attempt)) {
  108
+		error("Could not update attempt record: ".$db->ErrorMsg(), $next_url);
  109
+	}
  110
+
  111
+	// get previous attempt details record, if any
  112
+	$details_exist = record_exists("hotpot_details", "attempt", $attempt->id);
  113
+
  114
+	// delete/update/add the attempt details record
  115
+	if (empty($attempt->details)) {
  116
+		if ($details_exist) {
  117
+			delete_records("hotpot_details", "attempt", $attempt->id);
  118
+		}
  119
+	} else {
  120
+		if ($details_exist) {
  121
+			set_field("hotpot_details", "details", $attempt->details, "attempt", $attempt->id);
  122
+		} else {
  123
+			unset($details);
  124
+			$details->attempt = $attempt->id;
  125
+			$details->details = $attempt->details;
  126
+			if (! insert_record("hotpot_details", $details)) {
  127
+				error("Could not insert attempt details record: ".$db->ErrorMsg(), $next_url);
  128
+			}
  129
+		}
  130
+	}
  131
+
  132
+	if ($attempt->status==HOTPOT_STATUS_INPROGRESS) {
  133
+		if ($javascript_is_off) {
  134
+			// regenerate HTML page
  135
+			define('HOTPOT_FIRST_ATTEMPT', false);
  136
+			include ("$CFG->hotpotroot/view.php");
  137
+		} else {
  138
+			// continue without reloading the page
  139
+			header("Status: 204");
  140
+			header("HTTP/1.0 204 No Response");
  141
+		}
  142
+
  143
+	} else { // quiz is finished
  144
+
  145
+		add_to_log($course->id, "hotpot", "submit", "review.php?id=$cm->id&attempt=$attempt->id", "$hotpot->id", "$cm->id");
  146
+
  147
+		if ($hotpot->shownextquiz==HOTPOT_YES) {
  148
+			if (is_numeric($next_cm = hotpot_get_next_cm($cm))) {
  149
+				$next_url = "$CFG->wwwroot/mod/hotpot/view.php?id=$next_cm";
  150
+			}
  151
+		}
  152
+
  153
+		// redirect to the next quiz or the course page 
  154
+		redirect($next_url, get_string('resultssaved', 'hotpot'));
  155
+	}
  156
+
  157
+// =================
  158
+//	functions
  159
+// =================
  160
+
  161
+function hotpot_get_next_cm(&$cm) {
  162
+	// gets the next module in this section of the course
  163
+	// that is the same type of module as the current module
  164
+
  165
+	$next_mod = false;
  166
+
  167
+	// get a list of $ids of modules in this section
  168
+	if ($ids = get_field('course_sections', 'sequence', 'id', $cm->section)) {
  169
+
  170
+		$found = false;
  171
+		$ids = explode(',', $ids);
  172
+		foreach ($ids as $id) {
  173
+			if ($found && ($cm->module==get_field('course_modules', 'module', 'id', $id))) {
  174
+				$next_mod = $id;
  175
+				break;
  176
+			} else if ($cm->id==$id) {
  177
+				$found = true;
  178
+			}
  179
+		}
  180
+	}
  181
+	return $next_mod;
  182
+}
  183
+function hotpot_set_attempt_details(&$attempt) {
  184
+	global $HOTPOT_QUIZTYPE;
  185
+
  186
+	$attempt->details = '';
  187
+	$attempt->score = 0;
  188
+	$attempt->status = HOTPOT_STATUS_COMPLETED;
  189
+
  190
+	$buttons = array('clues', 'hints', 'checks');
  191
+	$textfields = array('correct', 'wrong', 'ignored');
  192
+
  193
+	$ok = false;
  194
+	$quiztype = optional_param('quiztype', 0, PARAM_ALPHANUM);
  195
+	if ($quiztype) {
  196
+		if (is_numeric($quiztype)) {
  197
+			$ok = array_key_exists($quiztype, $HOTPOT_QUIZTYPE);
  198
+		} else {
  199
+			$quiztype = array_search($quiztype, $HOTPOT_QUIZTYPE);
  200
+			$ok = is_numeric($quiztype);
  201
+		}
  202
+	}
  203
+	if (!$ok) {
  204
+		error('Quiz type is missing or invalid');
  205
+		// error(get_string('error_invalidquiztype', 'hotpot'));
  206
+		//
  207
+		// script finishes here if quiztype is invalid
  208
+		//
  209
+	}
  210
+
  211
+	$q = 0;
  212
+	while (($responsefield="q{$q}") && isset($_POST[$responsefield])) {
  213
+		$responsevalue = optional_param($responsefield, '');
  214
+
  215
+		// initialize $response object
  216
+		$response = NULL;
  217
+		$response->correct = array();
  218
+		$response->wrong   = array();
  219
+		$response->ignored = array();
  220
+		$response->clues  = 0;
  221
+		$response->hints  = 0;
  222
+		$response->checks = 0;
  223
+		$response->score  = 0;
  224
+		$response->weighting  = 0;
  225
+		
  226
+		// create another empty object to hold all previous responses (from database)
  227
+		$oldresponse = NULL;
  228
+		$vars = get_object_vars($response);
  229
+		foreach($vars as $name=>$value) {
  230
+			$oldresponse->$name = $value;
  231
+		}
  232
+
  233
+		foreach ($buttons as $button) {
  234
+			if (($field = "q{$q}_{$button}_button") && isset($_POST[$field])) {
  235
+				$value = optional_param($field, '', PARAM_RAW);
  236
+				if (!empty($value)) {
  237
+					$response->$button++;
  238
+				}
  239
+			}
  240
+		}
  241
+
  242
+		// loop through possible answers to this question
  243
+		$firstcorrectvalue = '';
  244
+		$a = 0;
  245
+		while (($correctfield="q{$q}_correct_{$a}") && isset($_POST[$correctfield])) {
  246
+			$correctvalue = optional_param($correctfield, '', PARAM_RAW);
  247
+
  248
+			if (empty($firstcorrectvalue)) {
  249
+				$firstcorrectvalue = $correctvalue;
  250
+			}
  251
+
  252
+			if ($responsevalue==$correctvalue) {
  253
+				$response->correct[] = $responsevalue;
  254
+			} else {
  255
+				$response->ignored[] = $correctvalue;
  256
+			}
  257
+			$a++;
  258
+		}
  259
+
  260
+		// if no correct answer was found, then this answer is wrong
  261
+		if (empty($response->correct)) {
  262
+			$response->wrong[] = $responsevalue;
  263
+			$attempt->status = HOTPOT_STATUS_INPROGRESS;
  264
+
  265
+			// give a hint, if necessary
  266
+			if (isset($_POST['HintButton']) && $firstcorrectvalue) {
  267
+
  268
+				// make sure we only come through here once
  269
+				unset($_POST['HintButton']);
  270
+
  271
+				$correctlen = strlen($firstcorrectvalue);
  272
+				$responselen = strlen($responsevalue);
  273
+
  274
+				// check how many letters are the same
  275
+				$i = 0;
  276
+				while ($i<$responselen && $i<$correctlen && $responsevalue{$i}==$firstcorrectvalue{$i}) {
  277
+					$i++;
  278
+				}
  279
+
  280
+				if ($i<$responselen) {
  281
+					// remove incorrect characters on the end of the response
  282
+					$responsevalue = substr($responsevalue, 0, $i);
  283
+				}
  284
+				if ($i<$correctlen) {
  285
+					// append next correct letter
  286
+					$responsevalue .= $firstcorrectvalue{$i};
  287
+				}
  288
+				$_POST[$responsefield] = $responsevalue;
  289
+				$response->hints++;
  290
+			} // end if hint
  291
+		} // end if not correct
  292
+
  293
+		// get clue text, if any
  294
+		if (($field="q{$q}_clue") && isset($_POST[$field])) {
  295
+			$response->clue_text = optional_param($field, '', PARAM_RAW);
  296
+		}
  297
+
  298
+		// get question name
  299
+		$qq = sprintf('%02d', $q); // (a padded, two-digit version of $q)
  300
+		if (($field="q{$q}_name") && isset($_POST[$field])) {
  301
+			$questionname = optional_param($field, '', PARAM_RAW);
  302
+		} else {
  303
+			$questionname = $qq;
  304
+		}
  305
+
  306
+		// get previous responses to this question (if any)
  307
+		$question = get_record('hotpot_questions', 'name', $questionname, 'hotpot', $attempt->hotpot);
  308
+		if ($question) {
  309
+			$records = get_records_select('hotpot_responses', "attempt=$attempt->clickreportid AND question=$question->id");
  310
+		} else {
  311
+			$records = false;
  312
+		}
  313
+		if ($records) {
  314
+			foreach ($records as $record) {
  315
+				foreach ($buttons as $button) {
  316
+					$oldresponse->$button = max($oldresponse->$button, $record->$button);
  317
+				}
  318
+				foreach ($textfields as $field) {
  319
+					if ($record->$field && ($field=='correct' || $field=='wrong')) {
  320
+						$values = explode(',', hotpot_strings($record->$field));
  321
+						$oldresponse->$field = array_merge($oldresponse->$field, $values);
  322
+					}
  323
+				}
  324
+			}
  325
+		}
  326
+
  327
+		// remove "correct" and "wrong" values from "ignored" values
  328
+		$response->ignored = array_diff($response->ignored, 
  329
+			$response->correct, $response->wrong, $oldresponse->correct, $oldresponse->wrong
  330
+		);
  331
+
  332
+		foreach ($buttons as $button) {
  333
+			$response->$button += $oldresponse->$button;
  334
+		}
  335
+
  336
+		$value_has_changed = false;
  337
+		foreach ($textfields as $field) {
  338
+
  339
+			$response->$field = array_merge($response->$field, $oldresponse->$field);
  340
+			$response->$field = array_unique($response->$field);
  341
+			$response->$field  = implode(',', $response->$field);
  342
+
  343
+			if ($field=='correct' || $field=='wrong') {
  344
+				$array = $oldresponse->$field;
  345
+				$array = array_unique($array);
  346
+				$oldresponse->$field  = implode(',', $array);		
  347
+				if ($response->$field<>$oldresponse->$field) {
  348
+					$value_has_changed = true;
  349
+				}
  350
+			}
  351
+		}
  352
+		if ($value_has_changed) {
  353
+			$response->checks++;
  354
+		}
  355
+
  356
+		// $response now holds amalgamation of all responses so far to this question
  357
+		
  358
+		// set question score and weighting
  359
+		if ($response->correct) {
  360
+			switch ($quiztype) {
  361
+				case HOTPOT_JCB:
  362
+					break;
  363
+				case HOTPOT_JCLOZE:
  364
+					$strlen = strlen($response->correct);
  365
+					$response->score = 100*($strlen-($response->checks-1))/$strlen;
  366
+					$attempt->score += $response->score;
  367
+					break;
  368
+				case HOTPOT_JCROSS:
  369
+					break;
  370
+				case HOTPOT_JMATCH:
  371
+					break;
  372
+				case HOTPOT_JMIX:
  373
+					break;
  374
+				case HOTPOT_JQUIZ:
  375
+					break;
  376
+			}
  377
+		}
  378
+
  379
+		// encode $response fields as XML
  380
+		$vars = get_object_vars($response);
  381
+		foreach($vars as $name=>$value) {
  382
+			if (!empty($value)) {
  383
+				$fieldname = $HOTPOT_QUIZTYPE[$quiztype]."_q{$qq}_{$name}";
  384
+				$attempt->details .= "<field><fieldname>$fieldname</fieldname><fielddata>$value</fielddata></field>";
  385
+			}
  386
+		}
  387
+
  388
+		$q++;
  389
+	} // end main loop through $q(uestions)
  390
+
  391
+	// set attempt score
  392
+	if ($q>0) {
  393
+		switch ($quiztype) {
  394
+			case HOTPOT_JCB:
  395
+				break;
  396
+			case HOTPOT_JCLOZE:
  397
+				$attempt->score = floor($attempt->score / $q);
  398
+				break;
  399
+			case HOTPOT_JCROSS:
  400
+				break;
  401
+			case HOTPOT_JMATCH:
  402
+				break;
  403
+			case HOTPOT_JMIX:
  404
+				break;
  405
+			case HOTPOT_JQUIZ:
  406
+				break;
  407
+		}
  408
+	}
  409
+
  410
+	if ($attempt->details) {
  411
+		$attempt->details = '<?xml version="1.0"?><hpjsresult><fields>'.$attempt->details.'</fields></hpjsresult>';
  412
+	}
  413
+
  414
+	// check there are no unattempted questions
  415
+	if (($field="I_{$q}_correct_0") && isset($_POST[$field])) {
  416
+		$attempt->status = HOTPOT_STATUS_INPROGRESS;
  417
+	}	
  418
+}
  419
+
404 420
 ?>

0 notes on commit b24a1f7

Please sign in to comment.
Something went wrong with that request. Please try again.