Merge commit 'catalyst/MOODLE_19_STABLE' into mdl19-linuxchix
[moodle-linuxchix.git] / mod / hotpot / review.php
blob4804979e2b2451d4a021c38a29ef00880e5004ab
1 <?PHP // $Id$
2 // This page prints a review of a particular quiz attempt
3 require_once("../../config.php");
4 require_once("lib.php");
6 $id = optional_param('id', 0, PARAM_INT); // Course Module ID, or
7 $hp = optional_param('hp', 0, PARAM_INT); // hotpot ID
8 $attempt = required_param('attempt', PARAM_INT); // A particular attempt ID for review
10 if ($id) {
11 if (! $cm = get_coursemodule_from_id('hotpot', $id)) {
12 error("Course Module ID was incorrect");
14 if (! $course = get_record("course", "id", $cm->course)) {
15 error("Course is misconfigured");
17 if (! $hotpot = get_record("hotpot", "id", $cm->instance)) {
18 error("Course module is incorrect");
20 } else {
21 if (! $hotpot = get_record("hotpot", "id", $hp)) {
22 error("Course module is incorrect");
24 if (! $course = get_record("course", "id", $hotpot->course)) {
25 error("Course is misconfigured");
27 if (! $cm = get_coursemodule_from_instance("hotpot", $hotpot->id, $course->id)) {
28 error("Course Module ID was incorrect");
31 if (! $attempt = get_record("hotpot_attempts", "id", $attempt)) {
32 error("Attempt ID was incorrect");
35 require_login($course);
37 // check user can access this hotpot activity
38 if (!hotpot_is_visible($cm)) {
39 print_error("activityiscurrentlyhidden");
42 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
43 if (!has_capability('mod/hotpot:viewreport',$context)) {
44 if (!$hotpot->review) {
45 print_error("noreview", "quiz");
47 //if (time() < $hotpot->timeclose) {
48 // print_error("noreviewuntil", "quiz", '', userdate($hotpot->timeclose));
49 //}
50 if ($attempt->userid != $USER->id) {
51 error("This is not your attempt!");
54 add_to_log($course->id, "hotpot", "review", "review.php?id=$cm->id&attempt=$attempt->id", "$hotpot->id", "$cm->id");
55 // Print the page header
56 $strmodulenameplural = get_string("modulenameplural", "hotpot");
57 $strmodulename = get_string("modulename", "hotpot");
58 // print header
59 $title = format_string($course->shortname) . ": $hotpot->name";
60 $heading = $course->fullname;
62 $navigation = build_navigation('', $cm);
63 $button = update_module_button($cm->id, $course->id, $strmodulename);
64 print_header($title, $heading, $navigation, "", "", true, $button, navmenu($course, $cm));
65 print '<div id="overDiv" style="position:absolute; visibility:hidden; z-index:1000;"></div>'; // for overlib
66 print_heading($hotpot->name);
67 hotpot_print_attempt_summary($hotpot, $attempt);
68 hotpot_print_review_buttons($course, $hotpot, $attempt, $context);
69 $action = has_capability('mod/hotpot:viewreport',$context) ? optional_param('action', '', PARAM_ALPHA) : '';
70 if ($action) {
71 $xml = get_field('hotpot_details', 'details', 'attempt', $attempt->id);
72 print '<hr>';
73 switch ($action) {
74 case 'showxmltree':
75 print '<pre id="contents">';
76 $xml_tree = new hotpot_xml_tree($xml, "['hpjsresult']['#']");
77 print_r ($xml_tree->xml_value('fields'));
78 print '</pre>';
79 break;
80 case 'showxmlsource':
81 print htmlspecialchars($xml);
82 break;
83 default:
84 print "Action '$action' not recognized";
86 print '<hr>';
87 } else {
88 hotpot_print_attempt_details($hotpot, $attempt);
90 hotpot_print_review_buttons($course, $hotpot, $attempt, $context);
91 print_footer($course);
92 ///////////////////////////
93 // functions
94 ///////////////////////////
95 function hotpot_print_attempt_summary(&$hotpot, &$attempt) {
96 // start table
97 print_simple_box_start("center", "80%", "#ffffff", 0);
98 print '<table width="100%" border="1" valign="top" align="center" cellpadding="2" cellspacing="2" class="generaltable">'."\n";
99 // add attempt properties
100 $fields = array('attempt', 'score', 'penalties', 'status', 'timetaken', 'timerecorded');
101 foreach ($fields as $field) {
102 switch ($field) {
103 case 'score':
104 $value = hotpot_format_score($attempt);
105 break;
106 case 'status':
107 $value = hotpot_format_status($attempt);
108 break;
109 case 'timerecorded':
110 $value = empty($attempt->timefinish) ? '-' : userdate($attempt->timefinish);
111 break;
112 case 'timetaken':
113 $value = empty($attempt->timefinish) ? '-' : format_time($attempt->timefinish - $attempt->timestart);
114 break;
115 default:
116 $value = isset($attempt->$field) ? $attempt->$field : NULL;
118 if (isset($value)) {
119 switch ($field) {
120 case 'status':
121 case 'timerecorded':
122 $name = get_string('report'.$field, 'hotpot');
123 break;
124 case 'penalties':
125 $name = get_string('penalties', 'hotpot');
126 break;
127 default:
128 $name = get_string($field, 'quiz');
130 print '<tr><th align="right" width="100" class="generaltableheader" scope="row">'.$name.':</th><td class="generaltablecell">'.$value.'</td></tr>';
133 // finish table
134 print '</table>';
135 print_simple_box_end();
137 function hotpot_print_review_buttons(&$course, &$hotpot, &$attempt, $context) {
138 print "\n".'<table border="0" align="center" cellpadding="2" cellspacing="2" class="generaltable">';
139 print "\n<tr>\n".'<td align="center">';
140 print_single_button("report.php?hp=$hotpot->id", NULL, get_string('continue'), 'post');
141 if (has_capability('mod/hotpot:viewreport',$context) && record_exists('hotpot_details', 'attempt', $attempt->id)) {
142 print "</td>\n".'<td align="center">';
143 print_single_button("review.php?hp=$hotpot->id&attempt=$attempt->id&action=showxmlsource", NULL, get_string('showxmlsource', 'hotpot'), 'post');
144 print "</td>\n".'<td align="center">';
145 print_single_button("review.php?hp=$hotpot->id&attempt=$attempt->id&action=showxmltree", NULL, get_string('showxmltree', 'hotpot'), 'post');
146 $colspan = 3;
147 } else {
148 $colspan = 1;
150 print "</td>\n</tr>\n";
151 print '<tr><td colspan="'.$colspan.'">';
152 print_spacer(4, 1, false); // height=4, width=1, no <br />
153 print "</td></tr>\n";
154 print "</table>\n";
156 function hotpot_print_attempt_details(&$hotpot, &$attempt) {
157 // define fields to print
158 $textfields = array('correct', 'ignored', 'wrong');
159 $numfields = array('score', 'weighting', 'hints', 'clues', 'checks');
160 $fields = array_merge($textfields, $numfields);
161 $q = array(); // questions
162 $f = array(); // fields
163 foreach ($fields as $field) {
164 $name = get_string($field, 'hotpot');
165 $f[$field] = array('count'=>0, 'name'=>$name);
167 // get questions and responses for this attempt
168 $questions = get_records_select('hotpot_questions', "hotpot='$hotpot->id'", 'id');
169 $responses = get_records_select('hotpot_responses', "attempt='$attempt->id'", 'id');
170 if ($questions && $responses) {
171 foreach ($responses as $response) {
172 $id = $response->question;
173 foreach ($fields as $field) {
174 if (!isset($f[$field])) {
175 $name = get_string($field, 'hotpot');
176 $f[$field] = array('count'=>0, 'name'=>$name);
178 if (isset($response->$field)) {
179 $f[$field]['count']++;
180 if (!isset($q[$id])) {
181 $name = hotpot_get_question_name($questions[$id]);
182 $q[$id] = array('name'=>$name);
184 $q[$id][$field] = $response->$field;
189 // count the number of columns required in the table
190 $colspan = 0;
191 foreach ($numfields as $field) {
192 if ($f[$field]['count']) {
193 $colspan += 2;
196 $colspan = max(2, $colspan);
197 // start table of questions and responses
198 print_simple_box_start("center", "80%", "#ffffff", 0);
199 print '<table width="100%" border="1" valign="top" align="center" cellpadding="2" cellspacing="2" class="generaltable">'."\n";
200 if (empty($q)) {
201 print '<tr><td align="center" class="generaltablecell"><b>'.get_string("noresponses", "hotpot")."</b></td></tr>\n";
202 } else {
203 // flag to ensure separators are only printed before the 2nd and subsequent questions
204 $printseparator = false;
205 foreach ($q as $i=>$question) {
206 // flag to ensure questions are only printed when there is at least one response
207 $printedquestion = false;
208 // add rows of text fields
209 foreach ($textfields as $field) {
210 if (isset($question[$field])) {
211 $text = hotpot_strings($question[$field]);
212 if (trim($text)) {
213 // print question if necessary
214 if (!$printedquestion) {
215 if ($printseparator) {
216 print '<tr><td colspan="'.$colspan.'"><div class="tabledivider"></div></td></tr>'."\n";
218 $printseparator = true;
219 print '<tr><td colspan="'.$colspan.'" class="generaltablecell"><b>'.$question['name'].'</b></td></tr>'."\n";
220 $printedquestion = true;
222 // print response
223 print '<tr><th align="right" width="100" class="generaltableheader" scope="row">'.$f[$field]['name'].':</th><td colspan="'.($colspan-1).'" class="generaltablecell">'.$text.'</td></tr>'."\n";
227 // add row of numeric fields
228 print '<tr>';
229 foreach ($numfields as $field) {
230 if ($f[$field]['count']) {
231 // print question if necessary
232 if (!$printedquestion) {
233 print '<td colspan="'.$colspan.'" class="generaltablecell"><b>'.$question['name']."</b></td></tr>\n<tr>";
234 $printedquestion = true;
236 // print numeric response
237 $value = isset($question[$field]) ? $question[$field] : '-';
238 print '<th align="right" width="100" class="generaltableheader" scope="row">'.$f[$field]['name'].':</th><td class="generaltablecell">'.$value.'</td>';
241 print "</tr>\n";
242 } // foreach $q
244 // finish table
245 print "</table>\n";
246 print_simple_box_end();