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
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");
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
);
40 if (isteacher($course->id
)) {
41 $mode = optional_param("mode", "overview", 0);
42 if (is_array($mode)) {
43 $mode = array_keys($mode);
47 // students have no choice
51 // get report attributes
53 $reportcourse = optional_param("reportcourse", "this");
55 // students and ordinary teachers have no choice
56 $reportcourse = 'this';
58 if (isteacher($course->id
)) {
59 $reportusers = optional_param("reportusers", "all");
61 // students have no choice
62 $reportusers = 'this';
64 $reportattempts = optional_param("reportattempts", "all");
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);
86 switch ($reportcourse) {
88 $course_ids = $course->id
;
89 $hotpot_ids = $hotpot->id
;
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));
102 switch ($reportusers) {
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);
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));
128 $user_ids = $USER->id
;
132 if (empty($user_ids)) {
133 print_heading(get_string('nousersyet'));
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'));
155 if (!$questions = get_records_select('hotpot_questions', "hotpot='$hotpot->id'")) {
156 $questions = array();
160 $grades = hotpot_get_grades($hotpot, $user_ids);
162 // get list of attempts by user
164 foreach ($attempts as $id=>$attempt) {
166 $userid = $attempt->userid
;
168 if (!isset($users[$userid])) {
169 $grade = isset($grades[$userid]) ?
$grades[$userid] : ' ';
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) {
221 $select = "hotpot='$hotpot->id'";
224 $select = "hotpot='$hotpot->id' AND timefinish>0 AND score IS NULL";
227 $ids = (array)data_submitted();
231 $select = "hotpot='$hotpot->id' AND id IN (".implode(',', $ids).")";
236 // delete attempts using $select, if it is set
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");
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) {
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">';
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');
315 foreach ($reports as $name) {
316 print '<td><input type="submit" name="'."mode[$name]".'" value="'.get_string("report$name", "quiz").'"></td>';
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
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))) {
343 // append remaining plugins
344 $reports = array_merge($reports, $plugins);
348 function hotpot_get_report_users($course_ids, $reportusers) {
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) {
362 $users = get_group_students($currentgroup, $sort);
364 $users = get_course_students($course->id
, $sort);
369 $users = get_group_users($currentgroup, $sort);
371 $users = get_course_users($course->id
, $sort);