Merged from HEAD
[moodle.git] / mod / hotpot / report.php
blob6778cf1fd4c94d0860127c907d730e8939c39ade
1 <?PHP // $Id$
3 // This script uses installed report plugins to print quiz reports
5 require_once("../../config.php");
6 require_once("lib.php");
8 $id = optional_param("id"); // Course Module ID, or
9 $hp = optional_param("hp"); // hotpot ID
11 if ($id) {
12 if (! $cm = get_record("course_modules", "id", $id)) {
13 error("Course Module ID was incorrect");
15 if (! $course = get_record("course", "id", $cm->course)) {
16 error("Course is misconfigured");
18 if (! $hotpot = get_record("hotpot", "id", $cm->instance)) {
19 error("Course module is incorrect");
22 } else {
23 if (! $hotpot = get_record("hotpot", "id", $hp)) {
24 error("Course module is incorrect");
26 if (! $course = get_record("course", "id", $hotpot->course)) {
27 error("Course is misconfigured");
29 if (! $cm = get_coursemodule_from_instance("hotpot", $hotpot->id, $course->id)) {
30 error("Course Module ID was incorrect");
34 // set homeurl of couse (for error messages)
35 $course_homeurl = "$CFG->wwwroot/course/view.php?id=$course->id";
37 require_login($course->id);
39 // get report mode
40 if (isteacher($course->id)) {
41 $mode = optional_param("mode", "overview", 0);
42 if (is_array($mode)) {
43 $mode = array_keys($mode);
44 $mode = $mode[0];
46 } else {
47 // students have no choice
48 $mode = 'overview';
51 // get report attributes
52 if (isadmin()) {
53 $reportcourse = optional_param("reportcourse", "this");
54 } else {
55 // students and ordinary teachers have no choice
56 $reportcourse = 'this';
58 if (isteacher($course->id)) {
59 $reportusers = optional_param("reportusers", "all");
60 } else {
61 // students have no choice
62 $reportusers = 'this';
64 $reportattempts = optional_param("reportattempts", "all");
66 /// Start the report
68 add_to_log($course->id, "hotpot", "report", "report.php?id=$cm->id", "$hotpot->id", "$cm->id");
70 // print page header. if required
71 if (empty($noheader)) {
72 hotpot_print_report_heading($course, $cm, $hotpot, $mode);
73 if (isteacher($course->id)) {
74 hotpot_print_report_selector($course, $hotpot, $mode, $reportcourse, $reportusers, $reportattempts);
78 // delete selected attempts, if any
79 if (isteacher($course->id)) {
80 $del = optional_param("del", "");
81 hotpot_delete_selected_attempts($hotpot, $del);
84 $hotpot_ids = '';
85 $course_ids = '';
86 switch ($reportcourse) {
87 case 'this':
88 $course_ids = $course->id;
89 $hotpot_ids = $hotpot->id;
90 break;
91 case 'all' :
92 $records = get_records_select_menu('user_teachers', "userid='$USER->id'", 'course', 'id, course');
93 $course_ids = join(',', array_values($records));
95 $records = get_records_select_menu('hotpot', "reference='$hotpot->reference'", 'reference', 'id, reference');
96 $hotpot_ids = join(',', array_keys($records));
97 break;
100 $user_ids = '';
101 $users = array();
102 switch ($reportusers) {
103 case 'all':
104 $admin_ids = get_records_select_menu('user_admins');
105 if (is_array($admin_ids)) {
106 $users = array_merge($users, $admin_ids);
108 $creator_ids = get_records_select_menu('user_coursecreators');
109 if (is_array($creator_ids)) {
110 $users = array_merge($users, $creator_ids);
112 $teacher_ids = get_records_select_menu('user_teachers', "course IN ($course_ids)", 'course', 'id, userid');
113 if (is_array($teacher_ids)) {
114 $users = array_merge($users, $teacher_ids);
116 $guest_id = get_records_select_menu('user', "username='guest'", '', 'id,id');
117 if (is_array($guest_id)) {
118 $users = array_merge($users, $guest_id);
120 case 'students':
121 $student_ids = get_records_select_menu('user_students', "course IN ($course_ids)", 'course', 'id, userid');
122 if (is_array($student_ids)) {
123 $users = array_merge($users, $student_ids);
125 $user_ids = join(',', array_values($users));
126 break;
127 case 'this':
128 $user_ids = $USER->id;
129 break;
132 if (empty($user_ids)) {
133 print_heading(get_string('nousersyet'));
134 exit;
137 // get attempts for these users
138 $fields = 'a.*, u.firstname, u.lastname, u.picture';
139 $table = "{$CFG->prefix}hotpot_attempts AS a, {$CFG->prefix}user AS u";
140 $select = ($mode=='simplestat' || $mode=='fullstat') ? 'a.score IS NOT NULL' : 'a.timefinish>0';
141 $select .= " AND a.hotpot IN ($hotpot_ids) AND a.userid IN ($user_ids) AND a.userid = u.id";
142 $order = "u.lastname, a.attempt";
143 if ($reportattempts=='best') {
144 $fields .= ", MAX(a.score) AS grade";
145 $select .= " GROUP BY a.userid";
147 $attempts = get_records_sql("SELECT $fields FROM $table WHERE $select ORDER BY $order");
149 if (empty($attempts)) {
150 print_heading(get_string('noattempts','quiz'));
151 exit;
154 // get the questions
155 if (!$questions = get_records_select('hotpot_questions', "hotpot='$hotpot->id'")) {
156 $questions = array();
159 // get grades
160 $grades = hotpot_get_grades($hotpot, $user_ids);
162 // get list of attempts by user
163 $users = array();
164 foreach ($attempts as $id=>$attempt) {
166 $userid = $attempt->userid;
168 if (!isset($users[$userid])) {
169 $grade = isset($grades[$userid]) ? $grades[$userid] : '&nbsp;';
170 $users[$userid]->grade = $grade;
171 $users[$userid]->attempts = array();
174 $users[$userid]->attempts[] = &$attempts[$id];
178 /// Open the selected hotpot report and display it
180 $mode = clean_filename($mode);
182 if (! is_readable("report/$mode/report.php")) {
183 error("Report not known (".clean_text($mode).")", $course_homeurl);
186 include("report/default.php"); // Parent class
187 include("report/$mode/report.php");
189 $report = new hotpot_report();
191 if (! $report->display($hotpot, $cm, $course, $users, $attempts, $questions)) {
192 error("Error occurred during pre-processing!", $course_homeurl);
195 if (empty($noheader)) {
196 print_footer($course);
199 //////////////////////////////////////////////
200 /// functions to delete attempts and responses
202 function hotpot_grade_heading($hotpot, $download) {
204 global $HOTPOT_GRADEMETHOD;
205 $grademethod = $HOTPOT_GRADEMETHOD[$hotpot->grademethod];
207 if ($hotpot->grade!=100) {
208 $grademethod = "$hotpot->grade x $grademethod/100";
210 if (empty($download)) {
211 $grademethod = '<FONT size="1">'.$grademethod.'</FONT>';
213 $nl = $download ? "\n" : '<br>';
214 return get_string('grade')."$nl($grademethod)";
216 function hotpot_delete_selected_attempts(&$hotpot, $del) {
218 $select = '';
219 switch ($del) {
220 case 'all' :
221 $select = "hotpot='$hotpot->id'";
222 break;
223 case 'abandoned':
224 $select = "hotpot='$hotpot->id' AND timefinish>0 AND score IS NULL";
225 break;
226 case 'selection':
227 $ids = (array)data_submitted();
228 unset($ids['del']);
229 unset($ids['id']);
230 if (!empty($ids)) {
231 $select = "hotpot='$hotpot->id' AND id IN (".implode(',', $ids).")";
233 break;
236 // delete attempts using $select, if it is set
237 if ($select) {
239 $table = 'hotpot_attempts';
240 if ($attempts = get_records_select($table, $select)) {
242 hotpot_delete_and_notify($table, $select, get_string('attempts', 'quiz'));
244 $table = 'hotpot_responses';
245 $select = 'attempt IN ('.implode(',', array_keys($attempts)).')';
246 hotpot_delete_and_notify($table, $select, get_string('answer', 'quiz'));
252 //////////////////////////////////////////////
253 /// functions to print the report headings and
254 /// report selector menus
256 function hotpot_print_report_heading(&$course, &$cm, &$hotpot, &$mode) {
257 $strmodulenameplural = get_string("modulenameplural", "hotpot");
258 $strmodulename = get_string("modulename", "hotpot");
260 $title = "$course->shortname: $hotpot->name";
261 $heading = "$course->fullname";
263 $navigation = "<a href=index.php?id=$course->id>$strmodulenameplural</a> -> ";
264 $navigation .= "<a href=\"view.php?id=$cm->id\">$hotpot->name</a> -> ";
265 if (isteacher($course->id)) {
266 $navigation .= get_string("report$mode", "quiz");
267 } else {
268 $navigation .= get_string("report", "quiz");
270 if ($course->category) {
271 $navigation = "<a href=\"../../course/view.php?id=$course->id\">$course->shortname</a> -> $navigation";
273 $button = update_module_button($cm->id, $course->id, $strmodulename);
275 print_header($title, $heading, $navigation, "", "", true, $button, navmenu($course, $cm));
277 print_heading($hotpot->name);
279 function hotpot_print_report_selector(&$course, &$hotpot, &$mode, &$reportcourse, &$reportusers, &$reportattempts) {
281 global $CFG;
283 $reports = hotpot_get_report_names('overview,simplestat');
285 print '<form method="post" action="'."$CFG->wwwroot/mod/hotpot/report.php?hp=$hotpot->id".'">';
286 print '<table cellpadding="4" align="center">';
288 $menus = array();
289 if (isadmin()) {
290 $menus['reportcourse'] = array(
291 'this' => get_string('thiscourse', 'hotpot'),
292 'all' => get_string('allmycourses', 'hotpot')
295 $menus['reportusers'] = array(
296 'students' => get_string('students'),
297 'all' => get_string('allparticipants')
299 $menus['reportattempts'] = array(
300 'best' => get_string('bestattempt', 'hotpot'),
301 'all' => get_string('allattempts', 'hotpot')
304 print '<tr><td align="center" colspan="'.count($reports).'">';
305 foreach ($menus as $name => $options) {
306 eval('$value=$'.$name.';');
307 print choose_from_menu($options, $name, $value, "", "", 0, true);
309 if (isteacher($course->id)) {
310 helpbutton('reportselector', get_string('report'), 'hotpot');
312 print '</td></tr>';
314 print '<tr>';
315 foreach ($reports as $name) {
316 print '<td><input type="submit" name="'."mode[$name]".'" value="'.get_string("report$name", "quiz").'"></td>';
318 print '</tr>';
320 print '</table>';
322 print '<hr size="1" noshade="noshade" />';
323 print '</form>'."\n";
325 function hotpot_get_report_names($names='') {
326 // $names : optional list showing required order reports names
328 $reports = array();
330 // convert $names to an array, if necessary (usually is)
331 if (!is_array($names)) {
332 $names = explode(',', $names);
335 $plugins = get_list_of_plugins("mod/hotpot/report");
336 foreach($names as $name) {
337 if (is_numeric($i = array_search($name, $plugins))) {
338 $reports[] = $name;
339 unset($plugins[$i]);
343 // append remaining plugins
344 $reports = array_merge($reports, $plugins);
346 return $reports;
348 function hotpot_get_report_users($course_ids, $reportusers) {
349 $users = array();
351 /// Check to see if groups are being used in this module
352 $currentgroup = false;
353 if ($groupmode = groupmode($course, $cm)) { // Groups are being used
354 $currentgroup = setup_and_print_groups($course, $groupmode, "report.php?id=$cm->id&mode=simplestat");
357 $sort = "u.lastname ASC";
359 switch ($reportusers) {
360 case 'students':
361 if ($currentgroup) {
362 $users = get_group_students($currentgroup, $sort);
363 } else {
364 $users = get_course_students($course->id, $sort);
366 break;
367 case 'all':
368 if ($currentgroup) {
369 $users = get_group_users($currentgroup, $sort);
370 } else {
371 $users = get_course_users($course->id, $sort);
373 break;
376 return $users;