MDL-7861 still some icons left ;grr previous commid had wrong MDL id
[moodle.git] / mod / hotpot / index.php
CommitLineData
29781718 1<?PHP // $Id$
2
3// This page lists all the instances of hotpot in a particular course
4
dabfd0ed 5 require_once("../../config.php");
6 require_once("../../course/lib.php");
7 require_once("lib.php");
8
fb414b62 9 $id = required_param('id', PARAM_INT); // course
dabfd0ed 10 if (! $course = get_record("course", "id", $id)) {
11 error("Course ID is incorrect");
12 }
13
14 require_login($course->id);
15
fb414b62 16 $coursecontext = get_context_instance(CONTEXT_COURSE, $id);
17 $sitecontext = get_context_instance(CONTEXT_SYSTEM, SITEID);
18
dabfd0ed 19 add_to_log($course->id, "hotpot", "view all", "index.php?id=$course->id", "");
20
21 // Moodle 1.4+ requires sesskey to be passed in forms
22 if (isset($USER->sesskey)) {
23 $sesskey = '<input type="hidden" name="sesskey" value="'.$USER->sesskey.'" />';
24 } else {
25 $sesskey = '';
26 }
27
28 // get message strings for titles
29 $strmodulenameplural = get_string("modulenameplural", "hotpot");
30 $strmodulename = get_string("modulename", "hotpot");
31
32 // string translation array for single and double quotes
33 $quotes = array("'"=>"\'", '"'=>'&quot;');
34
35 // Print the header
36
37 $title = "$course->shortname: $strmodulenameplural";
38 $heading = "$course->fullname";
39 $navigation = "$strmodulenameplural";
1936c10e 40 if ($course->id != SITEID) {
dabfd0ed 41 $navigation = "<a href=\"../../course/view.php?id=$course->id\">$course->shortname</a> -> $navigation";
42 }
43 print_header($title, $heading, $navigation, "", "", true, "", navmenu($course));
44
45 $next_url = "$CFG->wwwroot/course/view.php?id=$course->id";
46
47 // get display section, if any
fb414b62 48 $section = optional_param('section', 0, PARAM_INT);
dabfd0ed 49 if ($section) {
50 $displaysection = course_set_display($course->id, $section);
51 } else {
52 if (isset($USER->display[$course->id])) {
53 $displaysection = $USER->display[$course->id];
54 } else {
55 $displaysection = 0;
56 }
57 }
58
fb414b62 59 // Get all hotpot instances in this course
60 $hotpots = array();
61 if ($hotpot_instances = hotpot_get_all_instances_in_course('hotpot', $course)) {
62 foreach ($hotpot_instances as $hotpot_instance) {
63 if ($displaysection>0 && $hotpot_instance->section>0 && $displaysection<>$hotpot_instance->section) {
64 // do nothing (user is not diplaying this section)
65 } else {
66 $hotpots[$hotpot_instance->id] = $hotpot_instance;
67 }
dabfd0ed 68 }
69 }
dabfd0ed 70 if (empty($hotpots)) {
71 notice("There are no $strmodulenameplural", $next_url);
72 exit;
73 }
fb414b62 74 $hotpotids = implode(',', array_keys($hotpots));
dabfd0ed 75
fb414b62 76 if (has_capability('mod/hotpot:grade', $sitecontext)) {
dabfd0ed 77
fb414b62 78 // array of hotpots to be regraded
79 $regrade_hotpots = array();
dabfd0ed 80
fb414b62 81 // do we need to regrade any or all of the hotpots?
82 $regrade = optional_param('regrade', 0, PARAM_SEQUENCE);
83 if ($regrade) {
84 // add valid hotpot ids to the regrade array
85 $regrade = explode(',', $regrade);
86 foreach ($regrade as $id) {
87 if (isset($hotpots[$id])) {
88 $regrade_hotpots[$id] = &$hotpots[$id];
dabfd0ed 89 }
90 }
fb414b62 91 $regrade = implode(',', array_keys($regrade_hotpots));
dabfd0ed 92 }
fb414b62 93 if ($regrade) {
dabfd0ed 94
fb414b62 95 $confirm = optional_param('confirm', 0, PARAM_BOOL);
96 if (!$confirm) {
dabfd0ed 97
98 print_simple_box_start("center", "60%", "#FFAAAA", 20, "noticebox");
fb414b62 99
100 if (count($regrade_hotpots)==1) {
101 print_heading(get_string('regradecheck', 'hotpot', $regrade_hotpots[$regrade]->name));
102 } else {
103 print_heading(get_string('regradecheck', 'hotpot', ''));
104 print '<ul>';
105 foreach ($regrade_hotpots as $hotpot) {
106 print "<li>$hotpot->name</li>";
107 }
108 print '</ul>';
109 }
dabfd0ed 110 print ''
fb414b62 111 . '<div align="center"><table border="0"><tr><td>'
dabfd0ed 112 . '<form target="_parent" method="post" action="'.$ME.'">'
113 . '<input type="hidden" name="id" value="'.$course->id.'">'
114 . '<input type="hidden" name="regrade" value="'.$regrade.'" />'
115 . '<input type="hidden" name="confirm" value="1" />'
116 . $sesskey
117 . '<input type="submit" value="'.get_string("yes").'" />'
118 . '</form>'
119 . '</td><td> &nbsp; </td><td>'
120 . '<form target="_parent" method="post" action="'.$ME.'">'
121 . '<input type="hidden" name="id" value="'.$course->id.'">'
122 . $sesskey
123 . '<input type="submit" value="'.get_string("no").'" />'
124 . '</form>'
fb414b62 125 . '</td></tr></table></div>'
dabfd0ed 126 ;
fb414b62 127
dabfd0ed 128 print_simple_box_end();
129 print_footer($course);
130 exit;
131
132 } else { // regrade has been confirmed, so proceed
133
fb414b62 134 // start hotpot counter and timer
135 $hotpotstart = microtime();
136 $hotpotcount = 0;
dabfd0ed 137
fb414b62 138 // regrade attempts for these hotpots
139 foreach ($regrade_hotpots as $hotpot) {
140 notify("<b>$hotpot->name</b>");
dabfd0ed 141
fb414b62 142 // delete questions and responses for this hotpot
143 if ($records = get_records_select('hotpot_questions', "hotpot=$hotpot->id", '', 'id,hotpot')) {
144 $questionids = implode(',', array_keys($records));
145 hotpot_delete_and_notify('hotpot_questions', "id IN ($questionids)", get_string('question', 'quiz'));
146 hotpot_delete_and_notify('hotpot_responses', "question IN ($questionids)", get_string('answer', 'quiz'));
147 }
dabfd0ed 148
fb414b62 149 // start attempt counter and timer
150 $attemptstart = microtime();
151 $attemptcount = 0;
152
153 // regrade attempts, if any, for this hotpot
154 if ($attempts = get_records_select('hotpot_attempts', "hotpot=$hotpot->id")) {
155 foreach ($attempts as $attempt) {
156 $attempt->details = get_field('hotpot_details', 'details', 'attempt', $attempt->id);
157 if ($attempt->details) {
158 hotpot_add_attempt_details($attempt);
159 if (! update_record('hotpot_attempts', $attempt)) {
160 error("Could not update attempt record: ".$db->ErrorMsg(), $next_url);
161 }
dabfd0ed 162 }
fb414b62 163 $attemptcount++;
dabfd0ed 164 }
dabfd0ed 165 }
fb414b62 166 if ($attemptcount) {
167 $msg = get_string('added', 'moodle', "$attemptcount x ".get_string('attempts', 'quiz'));
168 if (!empty($CFG->hotpot_showtimes)) {
169 $msg .= ' ('.format_time(sprintf("%0.2f", microtime_diff($attemptstart, microtime()))).')';
170 }
171 notify($msg);
dabfd0ed 172 }
fb414b62 173 $hotpotcount++;
174 } // end foreach $hotpots
175 if ($hotpotcount) {
176 $msg = get_string('regrade', 'quiz').": $hotpotcount x ".get_string('modulenameplural', 'hotpot');
dabfd0ed 177 if (!empty($CFG->hotpot_showtimes)) {
fb414b62 178 $msg .= ' ('.format_time(sprintf("%0.2f", microtime_diff($hotpotstart, microtime()))).')';
dabfd0ed 179 }
180 notify($msg);
181 }
fb414b62 182 notify(get_string('regradecomplete', 'quiz'));
183 } // end if $confirm
dabfd0ed 184 } // end regrade
185
dabfd0ed 186 // get duplicate hotpot-name questions
187 // - JMatch LHS is longer than 255 bytes
188 // - JQuiz question text is longer than 255 bytes
fb414b62 189 // - other unidentified situations ?!
dabfd0ed 190
fb414b62 191 $regrade_hotpots = array();
192 $concat_field = sql_concat('hotpot', "'_'", 'name');
193 if ($concat_field) {
194 $records = get_records_sql("
195 SELECT $concat_field, COUNT(*), hotpot, name
dabfd0ed 196 FROM {$CFG->prefix}hotpot_questions
197 WHERE hotpot IN ($hotpotids)
198 GROUP BY hotpot, name
199 HAVING COUNT(*) >1
200 ");
fb414b62 201 if ($records) {
202 foreach ($records as $record) {
203 $regrade_hotpots[$record->hotpot] = 1;
204 }
205 ksort($regrade_hotpots);
206 $regrade_hotpots = array_keys($regrade_hotpots);
dabfd0ed 207 }
dabfd0ed 208 }
209 }
210
211 // start timer
212 $start = microtime();
213
214 // get total number of attempts, users and details for these hotpots
f274fe07 215 $tables = "{$CFG->prefix}hotpot_attempts a";
dabfd0ed 216 $fields = "
217 a.hotpot AS hotpot,
218 COUNT(DISTINCT a.clickreportid) AS attemptcount,
219 COUNT(DISTINCT a.userid) AS usercount,
220 MAX(a.score) AS maxscore
221 ";
222 $select = "a.hotpot IN ($hotpotids)";
81e956b9 223 if (has_capability('mod/hotpot:viewreport', $coursecontext)) {
dabfd0ed 224 // do nothing (=get all users)
225 } else {
226 // restrict results to this user only
227 $select .= " AND a.userid='$USER->id'";
228 }
42cd7a10 229 $usejoin = 0;
9ce384ba 230 if (has_capability('mod/hotpot:grade', get_context_instance(CONTEXT_SYSTEM, SITEID)) && $usejoin) {
dabfd0ed 231 // join attempts table and details table
f274fe07 232 $tables .= ",{$CFG->prefix}hotpot_details d";
dabfd0ed 233 $fields .= ',COUNT(DISTINCT d.id) AS detailcount';
234 $select .= " AND a.id=d.attempt";
235
236 // this may take about twice as long as getting the gradecounts separately :-(
237 // so this operation could be done after getting the $totals from the attempts table
238 }
239 $totals = get_records_sql("SELECT $fields FROM $tables WHERE $select GROUP BY a.hotpot");
fb414b62 240
eb87ac71 241 if (has_capability('mod/hotpot:grade', get_context_instance(CONTEXT_SYSTEM, SITEID)) && empty($usejoin)) {
dabfd0ed 242 foreach ($hotpots as $hotpot) {
243 $totals[$hotpot->id]->detailcount = 0;
244 if ($ids = get_records('hotpot_attempts', 'hotpot', $hotpot->id)) {
245 $ids = join(',', array_keys($ids));
246 $totals[$hotpot->id]->detailcount = count_records_select('hotpot_details', "attempt IN ($ids)");
247 }
248 }
249 }
250
251 // message strings for main table
252 $strusers = get_string('users');
253 $strupdate = get_string('update');
254 $strregrade = get_string('regrade', 'hotpot');
255 $strneverclosed = get_string('neverclosed', 'hotpot');
256 $strregraderequired = get_string('regraderequired', 'hotpot');
257
258 // column headings and attributes
259 $table->head = array();
260 $table->align = array();
261
262 if (!empty($CFG->hotpot_showtimes)) {
263 print '<H3>'.sprintf("%0.3f", microtime_diff($start, microtime())).' secs'."</H3>\n";
264 }
265
266 switch ($course->format) {
267 case 'weeks' :
268 $title = get_string("week");
269 break;
270 case 'topics' :
271 $title = get_string("topic");
272 break;
273 default :
274 $title = '';
275 break;
276 }
277 if ($title) {
278 array_push($table->head, $title);
279 array_push($table->align, "center");
280 }
81e956b9 281 if (has_capability('moodle/course:manageactivities', $coursecontext)) {
dabfd0ed 282 array_push($table->head, $strupdate);
283 array_push($table->align, "center");
284 }
285 array_push($table->head,
286 get_string("name"),
287 get_string("quizcloses", "quiz"),
288 get_string("bestgrade", "quiz"),
289 get_string("attempts", "quiz")
290 );
291 array_push($table->align,
292 "left", "left", "center", "left"
293 );
81e956b9 294 if (has_capability('mod/hotpot:grade', $coursecontext)) {
dabfd0ed 295 array_push($table->head, $strregrade);
296 array_push($table->align, "center");
297 }
298
299 $currentsection = -1;
300 foreach ($hotpots as $hotpot) {
301
302 $printsection = "";
303 if ($hotpot->section != $currentsection) {
304 if ($hotpot->section) {
305 $printsection = $hotpot->section;
306 if ($course->format=='weeks' || $course->format=='topics') {
307 // Show the zoom boxes
308 if ($displaysection==$hotpot->section) {
309 $strshowall = get_string('showall'.$course->format);
ff8889d8 310 $printsection .= '<br /><a href="index.php?id='.$course->id.'&section=all" title="'.$strshowall.'"><img src="'.$CFG->pixpath.'/i/all.gif" style="height:25px; width:16px; border:0px" alt="'.$strshowall.'"></a><br />';
dabfd0ed 311 } else {
312 $strshowone = get_string('showonly'.preg_replace('|s$|', '', $course->format, 1), '', $hotpot->section);
0d905d9f 313 $printsection .= '<br /><a href="index.php?id='.$course->id.'&section='.$hotpot->section.'" title="'.$strshowone.'"><img src="'.$CFG->pixpath.'/i/one.gif" class="icon" alt="'.$strshowone.'"></a><br />';
dabfd0ed 314 }
315 }
316 }
317 if ($currentsection>=0) {
318 $table->data[] = 'hr';
319 }
320 $currentsection = $hotpot->section;
321 }
322
323 $class = ($hotpot->visible) ? '' : 'class="dimmed" ';
324 $quizname = '<a '.$class.'href="view.php?id='.$hotpot->coursemodule.'">'.$hotpot->name.'</A>';
325 $quizclose = empty($hotpot->timeclose) ? $strneverclosed : userdate($hotpot->timeclose);
326
327 // are there any totals for this hotpot?
328 if (empty($totals[$hotpot->id]->attemptcount)) {
329 $report = "&nbsp;";
330 $bestscore = "&nbsp;";
331
332 } else {
81e956b9 333
334 $cm = get_coursemodule_from_instance('hotpot', $hotpot->id);
dabfd0ed 335 // report number of attempts and users
336 $report = get_string("viewallreports","quiz", $totals[$hotpot->id]->attemptcount);
81e956b9 337 if (has_capability('mod/hotpot:viewreport', get_context_instance(CONTEXT_MODULE, $cm->id))) {
dabfd0ed 338 $report .= " (".$totals[$hotpot->id]->usercount." $strusers)";
339 }
340 $report = '<a href="report.php?hp='.$hotpot->id.'">'.$report.'</a>';
341
342 // get best score
343 if (is_numeric($totals[$hotpot->id]->maxscore)) {
cc478206 344 $weighting = $hotpot->grade / 100;
345 $precision = hotpot_get_precision($hotpot);
346 $bestscore = round($totals[$hotpot->id]->maxscore * $weighting, $precision)." / $hotpot->grade";
dabfd0ed 347 } else {
348 $bestscore = "&nbsp;";
349 }
350 }
351
fb414b62 352 if (has_capability('mod/hotpot:grade', $sitecontext)) {
353 if (in_array($hotpot->id, $regrade_hotpots)) {
dabfd0ed 354 $report .= ' <font color="red">'.$strregraderequired.'</font>';
355 }
356 }
357
358 $data = array ();
359
360 if ($course->format=="weeks" || $course->format=="topics") {
361 array_push($data, $printsection);
362 }
363
81e956b9 364 if (has_capability('moodle/course:manageactivities', $coursecontext)) {
dabfd0ed 365 $updatebutton = ''
fa738731 366 . '<form '.$CFG->frametarget.'" method="get" action="'.$CFG->wwwroot.'/course/mod.php">'
dabfd0ed 367 . '<input type="hidden" name="update" value="'.$hotpot->coursemodule.'" />'
368 . $sesskey
369 . '<input type="submit" value="'.$strupdate.'" />'
370 . '</form>'
371 ;
372 array_push($data, $updatebutton);
373 }
374
375 array_push($data, $quizname, $quizclose, $bestscore, $report);
376
fb414b62 377 if (has_capability('mod/hotpot:grade', $sitecontext)) {
dabfd0ed 378 if (empty($totals[$hotpot->id]->detailcount)) {
379 // no details records for this hotpot, so disable regrade
380 $regradebutton = '&nbsp;';
381 } else {
382 $strregradecheck = get_string('regradecheck', 'hotpot', strtr($hotpot->name, $quotes));
383 $regradebutton = ''
384 . '<form target="_parent" method="post" action="'.$ME.'" onsubmit="var x=window.confirm('."'$strregradecheck'".');this.confirm.value=x;return x;">'
385 . '<input type="hidden" name="id" value="'.$course->id.'">'
386 . '<input type="hidden" name="regrade" value="'.$hotpot->id.'" />'
387 . '<input type="hidden" name="confirm" value="" />'
388 . $sesskey
389 . '<input type="submit" value="'.$strregrade.'" />'
390 . '</form>'
391 ;
392 }
393 array_push($data, $regradebutton);
394 }
395
396 $table->data[] = $data;
397 }
398
399 echo "<br />";
400
401 print_table($table);
402
403 // Finish the page
404 print_footer($course);
29781718 405?>