Merge commit 'catalyst/MOODLE_19_STABLE' into mdl19-linuxchix
[moodle-linuxchix.git] / mod / hotpot / index.php
bloba3a71bca20c3ccb974d3a95cf2b0ccea1a3a37d8
1 <?PHP // $Id$
3 // This page lists all the instances of hotpot in a particular course
5 require_once("../../config.php");
6 require_once("../../course/lib.php");
7 require_once("lib.php");
9 $id = required_param('id', PARAM_INT); // course
10 if (! $course = get_record("course", "id", $id)) {
11 error("Course ID is incorrect");
14 require_login($course->id);
16 $coursecontext = get_context_instance(CONTEXT_COURSE, $id);
17 $sitecontext = get_context_instance(CONTEXT_SYSTEM);
19 add_to_log($course->id, "hotpot", "view all", "index.php?id=$course->id", "");
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 = '';
28 // get message strings for titles
29 $strmodulenameplural = get_string("modulenameplural", "hotpot");
30 $strmodulename = get_string("modulename", "hotpot");
32 // string translation array for single and double quotes
33 $quotes = array("'"=>"\'", '"'=>'&quot;');
35 // Print the header
37 $title = format_string($course->shortname) . ": $strmodulenameplural";
38 $heading = $course->fullname;
40 $navlinks = array();
41 $navlinks[] = array('name' => $strmodulenameplural, 'link' => '', 'type' => 'activity');
42 $navigation = build_navigation($navlinks);
44 print_header($title, $heading, $navigation, "", "", true, "", navmenu($course));
46 $next_url = "$CFG->wwwroot/course/view.php?id=$course->id";
48 // get display section, if any
49 $section = optional_param('section', 0, PARAM_ALPHANUM);
50 if ($section=='all') {
51 // do nothing
52 } else {
53 $section = intval($section);
55 if ($section) {
56 $displaysection = course_set_display($course->id, $section);
57 } else {
58 if (isset($USER->display[$course->id])) {
59 $displaysection = $USER->display[$course->id];
60 } else {
61 $displaysection = 0;
65 // Get all hotpot instances in this course
66 $hotpots = array();
67 if ($hotpot_instances = hotpot_get_all_instances_in_course('hotpot', $course)) {
68 foreach ($hotpot_instances as $hotpot_instance) {
69 if ($displaysection>0 && $hotpot_instance->section>0 && $displaysection<>$hotpot_instance->section) {
70 // do nothing (user is not diplaying this section)
71 } else {
72 $cm = get_coursemodule_from_instance('hotpot', $hotpot_instance->id);
73 if ($cm && hotpot_is_visible($cm)) {
74 $hotpots[$hotpot_instance->id] = $hotpot_instance;
79 if (empty($hotpots)) {
80 notice(get_string('thereareno', 'moodle', $strmodulenameplural), $next_url);
81 exit;
83 $hotpotids = implode(',', array_keys($hotpots));
85 if (has_capability('mod/hotpot:grade', $sitecontext)) {
87 // array of hotpots to be regraded
88 $regrade_hotpots = array();
90 // do we need to regrade any or all of the hotpots?
91 $regrade = optional_param('regrade', 0, PARAM_SEQUENCE);
92 if ($regrade) {
93 // add valid hotpot ids to the regrade array
94 $regrade = explode(',', $regrade);
95 foreach ($regrade as $id) {
96 if (isset($hotpots[$id])) {
97 $regrade_hotpots[$id] = &$hotpots[$id];
100 $regrade = implode(',', array_keys($regrade_hotpots));
102 if ($regrade) {
104 $confirm = optional_param('confirm', 0, PARAM_BOOL);
105 if (!$confirm) {
107 print_simple_box_start("center", "60%", "#FFAAAA", 20, "noticebox");
109 if (count($regrade_hotpots)==1) {
110 print_heading(get_string('regradecheck', 'hotpot', $regrade_hotpots[$regrade]->name));
111 } else {
112 print_heading(get_string('regradecheck', 'hotpot', ''));
113 print '<ul>';
114 foreach ($regrade_hotpots as $hotpot) {
115 print "<li>$hotpot->name</li>";
117 print '</ul>';
119 print ''
120 . '<div align="center"><table border="0"><tr><td>'
121 . '<form target="_parent" method="post" action="'.$ME.'">'
122 . '<input type="hidden" name="id" value="'.$course->id.'" />'
123 . '<input type="hidden" name="regrade" value="'.$regrade.'" />'
124 . '<input type="hidden" name="confirm" value="1" />'
125 . $sesskey
126 . '<input type="submit" value="'.get_string("yes").'" />'
127 . '</form>'
128 . '</td><td> &nbsp; </td><td>'
129 . '<form target="_parent" method="post" action="'.$ME.'">'
130 . '<input type="hidden" name="id" value="'.$course->id.'" />'
131 . $sesskey
132 . '<input type="submit" value="'.get_string("no").'" />'
133 . '</form>'
134 . '</td></tr></table></div>'
137 print_simple_box_end();
138 print_footer($course);
139 exit;
141 } else { // regrade has been confirmed, so proceed
143 // start hotpot counter and timer
144 $hotpotstart = microtime();
145 $hotpotcount = 0;
147 // regrade attempts for these hotpots
148 foreach ($regrade_hotpots as $hotpot) {
149 notify("<b>$hotpot->name</b>");
151 // delete questions and responses for this hotpot
152 if ($records = get_records_select('hotpot_questions', "hotpot=$hotpot->id", '', 'id,hotpot')) {
153 $questionids = implode(',', array_keys($records));
154 hotpot_delete_and_notify('hotpot_questions', "id IN ($questionids)", get_string('question', 'quiz'));
155 hotpot_delete_and_notify('hotpot_responses', "question IN ($questionids)", get_string('answer', 'quiz'));
158 // start attempt counter and timer
159 $attemptstart = microtime();
160 $attemptcount = 0;
162 // regrade attempts, if any, for this hotpot
163 if ($attempts = get_records_select('hotpot_attempts', "hotpot=$hotpot->id")) {
164 foreach ($attempts as $attempt) {
165 $attempt->details = get_field('hotpot_details', 'details', 'attempt', $attempt->id);
166 if ($attempt->details) {
167 hotpot_add_attempt_details($attempt);
168 if (! update_record('hotpot_attempts', $attempt)) {
169 error("Could not update attempt record: ".$db->ErrorMsg(), $next_url);
172 $attemptcount++;
175 if ($attemptcount) {
176 $msg = get_string('added', 'moodle', "$attemptcount x ".get_string('attempts', 'quiz'));
177 if (!empty($CFG->hotpot_showtimes)) {
178 $msg .= ' ('.format_time(sprintf("%0.2f", microtime_diff($attemptstart, microtime()))).')';
180 notify($msg);
182 $hotpotcount++;
183 } // end foreach $hotpots
184 if ($hotpotcount) {
185 $msg = get_string('regrade', 'quiz').": $hotpotcount x ".get_string('modulenameplural', 'hotpot');
186 if (!empty($CFG->hotpot_showtimes)) {
187 $msg .= ' ('.format_time(sprintf("%0.2f", microtime_diff($hotpotstart, microtime()))).')';
189 notify($msg);
191 notify(get_string('regradecomplete', 'quiz'));
192 } // end if $confirm
193 } // end regrade
195 // get duplicate hotpot-name questions
196 // - JMatch LHS is longer than 255 bytes
197 // - JQuiz question text is longer than 255 bytes
198 // - other unidentified situations ?!
200 $regrade_hotpots = array();
201 $concat_field = sql_concat('hotpot', "'_'", 'name');
202 if ($concat_field) {
203 $records = get_records_sql("
204 SELECT $concat_field, COUNT(*), hotpot, name
205 FROM {$CFG->prefix}hotpot_questions
206 WHERE hotpot IN ($hotpotids)
207 GROUP BY hotpot, name
208 HAVING COUNT(*) >1
210 if ($records) {
211 foreach ($records as $record) {
212 $regrade_hotpots[$record->hotpot] = 1;
214 ksort($regrade_hotpots);
215 $regrade_hotpots = array_keys($regrade_hotpots);
220 // start timer
221 $start = microtime();
223 // get total number of attempts, users and details for these hotpots
224 $tables = "{$CFG->prefix}hotpot_attempts a";
225 $fields = "
226 a.hotpot AS hotpot,
227 COUNT(DISTINCT a.clickreportid) AS attemptcount,
228 COUNT(DISTINCT a.userid) AS usercount,
229 MAX(a.score) AS maxscore
231 $select = "a.hotpot IN ($hotpotids)";
232 if (has_capability('mod/hotpot:viewreport', $coursecontext)) {
233 // do nothing (=get all users)
234 } else {
235 // restrict results to this user only
236 $select .= " AND a.userid='$USER->id'";
238 $usejoin = 0;
239 if (has_capability('mod/hotpot:grade', get_context_instance(CONTEXT_SYSTEM)) && $usejoin) {
240 // join attempts table and details table
241 $tables .= ",{$CFG->prefix}hotpot_details d";
242 $fields .= ',COUNT(DISTINCT d.id) AS detailcount';
243 $select .= " AND a.id=d.attempt";
245 // this may take about twice as long as getting the gradecounts separately :-(
246 // so this operation could be done after getting the $totals from the attempts table
248 $totals = get_records_sql("SELECT $fields FROM $tables WHERE $select GROUP BY a.hotpot");
250 if (has_capability('mod/hotpot:grade', get_context_instance(CONTEXT_SYSTEM)) && empty($usejoin)) {
251 foreach ($hotpots as $hotpot) {
252 $totals[$hotpot->id]->detailcount = 0;
253 if ($ids = get_records('hotpot_attempts', 'hotpot', $hotpot->id)) {
254 $ids = join(',', array_keys($ids));
255 $totals[$hotpot->id]->detailcount = count_records_select('hotpot_details', "attempt IN ($ids)");
260 // message strings for main table
261 $strusers = get_string('users');
262 $strupdate = get_string('update');
263 $strregrade = get_string('regrade', 'hotpot');
264 $strneverclosed = get_string('neverclosed', 'hotpot');
265 $strregraderequired = get_string('regraderequired', 'hotpot');
267 // column headings and attributes
268 $table->head = array();
269 $table->align = array();
271 if (!empty($CFG->hotpot_showtimes)) {
272 print '<H3>'.sprintf("%0.3f", microtime_diff($start, microtime())).' secs'."</H3>\n";
275 switch ($course->format) {
276 case 'weeks' :
277 $title = get_string("week");
278 break;
279 case 'topics' :
280 $title = get_string("topic");
281 break;
282 default :
283 $title = '';
284 break;
286 if ($title) {
287 array_push($table->head, $title);
288 array_push($table->align, "center");
290 if (has_capability('moodle/course:manageactivities', $coursecontext)) {
291 array_push($table->head, $strupdate);
292 array_push($table->align, "center");
294 array_push($table->head,
295 get_string("name"),
296 get_string("quizcloses", "quiz"),
297 get_string("bestgrade", "quiz"),
298 get_string("attempts", "quiz")
300 array_push($table->align,
301 "left", "left", "center", "left"
303 if (has_capability('mod/hotpot:grade', $coursecontext)) {
304 array_push($table->head, $strregrade);
305 array_push($table->align, "center");
308 $currentsection = -1;
309 foreach ($hotpots as $hotpot) {
311 $printsection = "";
312 if ($hotpot->section != $currentsection) {
313 if ($hotpot->section) {
314 $printsection = $hotpot->section;
315 if ($course->format=='weeks' || $course->format=='topics') {
316 // Show the zoom boxes
317 if ($displaysection==$hotpot->section) {
318 $strshowall = get_string('showall'.$course->format);
319 $printsection .= '<br /><a href="index.php?id='.$course->id.'&amp;section=all" title="'.$strshowall.'"><img src="'.$CFG->pixpath.'/i/all.gif" style="height:25px; width:16px; border:0px" alt="'.$strshowall.'" /></a><br />';
320 } else {
321 $strshowone = get_string('showonly'.preg_replace('|s$|', '', $course->format, 1), '', $hotpot->section);
322 $printsection .= '<br /><a href="index.php?id='.$course->id.'&amp;section='.$hotpot->section.'" title="'.$strshowone.'"><img src="'.$CFG->pixpath.'/i/one.gif" class="icon" alt="'.$strshowone.'" /></a><br />';
326 if ($currentsection>=0) {
327 $table->data[] = 'hr';
329 $currentsection = $hotpot->section;
332 $class = ($hotpot->visible) ? '' : 'class="dimmed" ';
333 $quizname = '<a '.$class.'href="view.php?id='.$hotpot->coursemodule.'">'.$hotpot->name.'</a>';
334 $quizclose = empty($hotpot->timeclose) ? $strneverclosed : userdate($hotpot->timeclose);
336 // are there any totals for this hotpot?
337 if (empty($totals[$hotpot->id]->attemptcount)) {
338 $report = "&nbsp;";
339 $bestscore = "&nbsp;";
341 } else {
343 $cm = get_coursemodule_from_instance('hotpot', $hotpot->id);
344 // report number of attempts and users
345 $report = get_string("viewallreports","quiz", $totals[$hotpot->id]->attemptcount);
346 if (has_capability('mod/hotpot:viewreport', get_context_instance(CONTEXT_MODULE, $cm->id))) {
347 $report .= " (".$totals[$hotpot->id]->usercount." $strusers)";
349 $report = '<a href="report.php?hp='.$hotpot->id.'">'.$report.'</a>';
351 // get best score
352 if (is_numeric($totals[$hotpot->id]->maxscore)) {
353 $weighting = $hotpot->grade / 100;
354 $precision = hotpot_get_precision($hotpot);
355 $bestscore = round($totals[$hotpot->id]->maxscore * $weighting, $precision)." / $hotpot->grade";
356 } else {
357 $bestscore = "&nbsp;";
361 if (has_capability('mod/hotpot:grade', $sitecontext)) {
362 if (in_array($hotpot->id, $regrade_hotpots)) {
363 $report .= ' <font color="red">'.$strregraderequired.'</font>';
367 $data = array ();
369 if ($course->format=="weeks" || $course->format=="topics") {
370 array_push($data, $printsection);
373 if (has_capability('moodle/course:manageactivities', $coursecontext)) {
374 $updatebutton = ''
375 . '<form '.$CFG->frametarget.' method="get" action="'.$CFG->wwwroot.'/course/mod.php">'
376 . '<input type="hidden" name="update" value="'.$hotpot->coursemodule.'" />'
377 . $sesskey
378 . '<input type="submit" value="'.$strupdate.'" />'
379 . '</form>'
381 array_push($data, $updatebutton);
384 array_push($data, $quizname, $quizclose, $bestscore, $report);
386 if (has_capability('mod/hotpot:grade', $sitecontext)) {
387 if (empty($totals[$hotpot->id]->detailcount)) {
388 // no details records for this hotpot, so disable regrade
389 $regradebutton = '&nbsp;';
390 } else {
391 $strregradecheck = get_string('regradecheck', 'hotpot', strtr($hotpot->name, $quotes));
392 $regradebutton = ''
393 . '<form target="_parent" method="post" action="'.$ME.'" onsubmit="var x=window.confirm('."'$strregradecheck'".');this.confirm.value=x;return x;">'
394 . '<input type="hidden" name="id" value="'.$course->id.'" />'
395 . '<input type="hidden" name="regrade" value="'.$hotpot->id.'" />'
396 . '<input type="hidden" name="confirm" value="" />'
397 . $sesskey
398 . '<input type="submit" value="'.$strregrade.'" />'
399 . '</form>'
402 array_push($data, $regradebutton);
405 $table->data[] = $data;
408 echo "<br />";
410 print_table($table);
412 // Finish the page
413 print_footer($course);