adding some strings
[moodle-linuxchix.git] / mod / hotpot / index.php
blob6bec8c52772b073423168d4983ea38e02197cb34
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, SITEID);
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 $hotpots[$hotpot_instance->id] = $hotpot_instance;
76 if (empty($hotpots)) {
77 notice("There are no $strmodulenameplural", $next_url);
78 exit;
80 $hotpotids = implode(',', array_keys($hotpots));
82 if (has_capability('mod/hotpot:grade', $sitecontext)) {
84 // array of hotpots to be regraded
85 $regrade_hotpots = array();
87 // do we need to regrade any or all of the hotpots?
88 $regrade = optional_param('regrade', 0, PARAM_SEQUENCE);
89 if ($regrade) {
90 // add valid hotpot ids to the regrade array
91 $regrade = explode(',', $regrade);
92 foreach ($regrade as $id) {
93 if (isset($hotpots[$id])) {
94 $regrade_hotpots[$id] = &$hotpots[$id];
97 $regrade = implode(',', array_keys($regrade_hotpots));
99 if ($regrade) {
101 $confirm = optional_param('confirm', 0, PARAM_BOOL);
102 if (!$confirm) {
104 print_simple_box_start("center", "60%", "#FFAAAA", 20, "noticebox");
106 if (count($regrade_hotpots)==1) {
107 print_heading(get_string('regradecheck', 'hotpot', $regrade_hotpots[$regrade]->name));
108 } else {
109 print_heading(get_string('regradecheck', 'hotpot', ''));
110 print '<ul>';
111 foreach ($regrade_hotpots as $hotpot) {
112 print "<li>$hotpot->name</li>";
114 print '</ul>';
116 print ''
117 . '<div align="center"><table border="0"><tr><td>'
118 . '<form target="_parent" method="post" action="'.$ME.'">'
119 . '<input type="hidden" name="id" value="'.$course->id.'">'
120 . '<input type="hidden" name="regrade" value="'.$regrade.'" />'
121 . '<input type="hidden" name="confirm" value="1" />'
122 . $sesskey
123 . '<input type="submit" value="'.get_string("yes").'" />'
124 . '</form>'
125 . '</td><td> &nbsp; </td><td>'
126 . '<form target="_parent" method="post" action="'.$ME.'">'
127 . '<input type="hidden" name="id" value="'.$course->id.'">'
128 . $sesskey
129 . '<input type="submit" value="'.get_string("no").'" />'
130 . '</form>'
131 . '</td></tr></table></div>'
134 print_simple_box_end();
135 print_footer($course);
136 exit;
138 } else { // regrade has been confirmed, so proceed
140 // start hotpot counter and timer
141 $hotpotstart = microtime();
142 $hotpotcount = 0;
144 // regrade attempts for these hotpots
145 foreach ($regrade_hotpots as $hotpot) {
146 notify("<b>$hotpot->name</b>");
148 // delete questions and responses for this hotpot
149 if ($records = get_records_select('hotpot_questions', "hotpot=$hotpot->id", '', 'id,hotpot')) {
150 $questionids = implode(',', array_keys($records));
151 hotpot_delete_and_notify('hotpot_questions', "id IN ($questionids)", get_string('question', 'quiz'));
152 hotpot_delete_and_notify('hotpot_responses', "question IN ($questionids)", get_string('answer', 'quiz'));
155 // start attempt counter and timer
156 $attemptstart = microtime();
157 $attemptcount = 0;
159 // regrade attempts, if any, for this hotpot
160 if ($attempts = get_records_select('hotpot_attempts', "hotpot=$hotpot->id")) {
161 foreach ($attempts as $attempt) {
162 $attempt->details = get_field('hotpot_details', 'details', 'attempt', $attempt->id);
163 if ($attempt->details) {
164 hotpot_add_attempt_details($attempt);
165 if (! update_record('hotpot_attempts', $attempt)) {
166 error("Could not update attempt record: ".$db->ErrorMsg(), $next_url);
169 $attemptcount++;
172 if ($attemptcount) {
173 $msg = get_string('added', 'moodle', "$attemptcount x ".get_string('attempts', 'quiz'));
174 if (!empty($CFG->hotpot_showtimes)) {
175 $msg .= ' ('.format_time(sprintf("%0.2f", microtime_diff($attemptstart, microtime()))).')';
177 notify($msg);
179 $hotpotcount++;
180 } // end foreach $hotpots
181 if ($hotpotcount) {
182 $msg = get_string('regrade', 'quiz').": $hotpotcount x ".get_string('modulenameplural', 'hotpot');
183 if (!empty($CFG->hotpot_showtimes)) {
184 $msg .= ' ('.format_time(sprintf("%0.2f", microtime_diff($hotpotstart, microtime()))).')';
186 notify($msg);
188 notify(get_string('regradecomplete', 'quiz'));
189 } // end if $confirm
190 } // end regrade
192 // get duplicate hotpot-name questions
193 // - JMatch LHS is longer than 255 bytes
194 // - JQuiz question text is longer than 255 bytes
195 // - other unidentified situations ?!
197 $regrade_hotpots = array();
198 $concat_field = sql_concat('hotpot', "'_'", 'name');
199 if ($concat_field) {
200 $records = get_records_sql("
201 SELECT $concat_field, COUNT(*), hotpot, name
202 FROM {$CFG->prefix}hotpot_questions
203 WHERE hotpot IN ($hotpotids)
204 GROUP BY hotpot, name
205 HAVING COUNT(*) >1
207 if ($records) {
208 foreach ($records as $record) {
209 $regrade_hotpots[$record->hotpot] = 1;
211 ksort($regrade_hotpots);
212 $regrade_hotpots = array_keys($regrade_hotpots);
217 // start timer
218 $start = microtime();
220 // get total number of attempts, users and details for these hotpots
221 $tables = "{$CFG->prefix}hotpot_attempts a";
222 $fields = "
223 a.hotpot AS hotpot,
224 COUNT(DISTINCT a.clickreportid) AS attemptcount,
225 COUNT(DISTINCT a.userid) AS usercount,
226 MAX(a.score) AS maxscore
228 $select = "a.hotpot IN ($hotpotids)";
229 if (has_capability('mod/hotpot:viewreport', $coursecontext)) {
230 // do nothing (=get all users)
231 } else {
232 // restrict results to this user only
233 $select .= " AND a.userid='$USER->id'";
235 $usejoin = 0;
236 if (has_capability('mod/hotpot:grade', get_context_instance(CONTEXT_SYSTEM, SITEID)) && $usejoin) {
237 // join attempts table and details table
238 $tables .= ",{$CFG->prefix}hotpot_details d";
239 $fields .= ',COUNT(DISTINCT d.id) AS detailcount';
240 $select .= " AND a.id=d.attempt";
242 // this may take about twice as long as getting the gradecounts separately :-(
243 // so this operation could be done after getting the $totals from the attempts table
245 $totals = get_records_sql("SELECT $fields FROM $tables WHERE $select GROUP BY a.hotpot");
247 if (has_capability('mod/hotpot:grade', get_context_instance(CONTEXT_SYSTEM, SITEID)) && empty($usejoin)) {
248 foreach ($hotpots as $hotpot) {
249 $totals[$hotpot->id]->detailcount = 0;
250 if ($ids = get_records('hotpot_attempts', 'hotpot', $hotpot->id)) {
251 $ids = join(',', array_keys($ids));
252 $totals[$hotpot->id]->detailcount = count_records_select('hotpot_details', "attempt IN ($ids)");
257 // message strings for main table
258 $strusers = get_string('users');
259 $strupdate = get_string('update');
260 $strregrade = get_string('regrade', 'hotpot');
261 $strneverclosed = get_string('neverclosed', 'hotpot');
262 $strregraderequired = get_string('regraderequired', 'hotpot');
264 // column headings and attributes
265 $table->head = array();
266 $table->align = array();
268 if (!empty($CFG->hotpot_showtimes)) {
269 print '<H3>'.sprintf("%0.3f", microtime_diff($start, microtime())).' secs'."</H3>\n";
272 switch ($course->format) {
273 case 'weeks' :
274 $title = get_string("week");
275 break;
276 case 'topics' :
277 $title = get_string("topic");
278 break;
279 default :
280 $title = '';
281 break;
283 if ($title) {
284 array_push($table->head, $title);
285 array_push($table->align, "center");
287 if (has_capability('moodle/course:manageactivities', $coursecontext)) {
288 array_push($table->head, $strupdate);
289 array_push($table->align, "center");
291 array_push($table->head,
292 get_string("name"),
293 get_string("quizcloses", "quiz"),
294 get_string("bestgrade", "quiz"),
295 get_string("attempts", "quiz")
297 array_push($table->align,
298 "left", "left", "center", "left"
300 if (has_capability('mod/hotpot:grade', $coursecontext)) {
301 array_push($table->head, $strregrade);
302 array_push($table->align, "center");
305 $currentsection = -1;
306 foreach ($hotpots as $hotpot) {
308 $printsection = "";
309 if ($hotpot->section != $currentsection) {
310 if ($hotpot->section) {
311 $printsection = $hotpot->section;
312 if ($course->format=='weeks' || $course->format=='topics') {
313 // Show the zoom boxes
314 if ($displaysection==$hotpot->section) {
315 $strshowall = get_string('showall'.$course->format);
316 $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 />';
317 } else {
318 $strshowone = get_string('showonly'.preg_replace('|s$|', '', $course->format, 1), '', $hotpot->section);
319 $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 />';
323 if ($currentsection>=0) {
324 $table->data[] = 'hr';
326 $currentsection = $hotpot->section;
329 $class = ($hotpot->visible) ? '' : 'class="dimmed" ';
330 $quizname = '<a '.$class.'href="view.php?id='.$hotpot->coursemodule.'">'.$hotpot->name.'</A>';
331 $quizclose = empty($hotpot->timeclose) ? $strneverclosed : userdate($hotpot->timeclose);
333 // are there any totals for this hotpot?
334 if (empty($totals[$hotpot->id]->attemptcount)) {
335 $report = "&nbsp;";
336 $bestscore = "&nbsp;";
338 } else {
340 $cm = get_coursemodule_from_instance('hotpot', $hotpot->id);
341 // report number of attempts and users
342 $report = get_string("viewallreports","quiz", $totals[$hotpot->id]->attemptcount);
343 if (has_capability('mod/hotpot:viewreport', get_context_instance(CONTEXT_MODULE, $cm->id))) {
344 $report .= " (".$totals[$hotpot->id]->usercount." $strusers)";
346 $report = '<a href="report.php?hp='.$hotpot->id.'">'.$report.'</a>';
348 // get best score
349 if (is_numeric($totals[$hotpot->id]->maxscore)) {
350 $weighting = $hotpot->grade / 100;
351 $precision = hotpot_get_precision($hotpot);
352 $bestscore = round($totals[$hotpot->id]->maxscore * $weighting, $precision)." / $hotpot->grade";
353 } else {
354 $bestscore = "&nbsp;";
358 if (has_capability('mod/hotpot:grade', $sitecontext)) {
359 if (in_array($hotpot->id, $regrade_hotpots)) {
360 $report .= ' <font color="red">'.$strregraderequired.'</font>';
364 $data = array ();
366 if ($course->format=="weeks" || $course->format=="topics") {
367 array_push($data, $printsection);
370 if (has_capability('moodle/course:manageactivities', $coursecontext)) {
371 $updatebutton = ''
372 . '<form '.$CFG->frametarget.'" method="get" action="'.$CFG->wwwroot.'/course/mod.php">'
373 . '<input type="hidden" name="update" value="'.$hotpot->coursemodule.'" />'
374 . $sesskey
375 . '<input type="submit" value="'.$strupdate.'" />'
376 . '</form>'
378 array_push($data, $updatebutton);
381 array_push($data, $quizname, $quizclose, $bestscore, $report);
383 if (has_capability('mod/hotpot:grade', $sitecontext)) {
384 if (empty($totals[$hotpot->id]->detailcount)) {
385 // no details records for this hotpot, so disable regrade
386 $regradebutton = '&nbsp;';
387 } else {
388 $strregradecheck = get_string('regradecheck', 'hotpot', strtr($hotpot->name, $quotes));
389 $regradebutton = ''
390 . '<form target="_parent" method="post" action="'.$ME.'" onsubmit="var x=window.confirm('."'$strregradecheck'".');this.confirm.value=x;return x;">'
391 . '<input type="hidden" name="id" value="'.$course->id.'">'
392 . '<input type="hidden" name="regrade" value="'.$hotpot->id.'" />'
393 . '<input type="hidden" name="confirm" value="" />'
394 . $sesskey
395 . '<input type="submit" value="'.$strregrade.'" />'
396 . '</form>'
399 array_push($data, $regradebutton);
402 $table->data[] = $data;
405 echo "<br />";
407 print_table($table);
409 // Finish the page
410 print_footer($course);