forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreport.php
640 lines (534 loc) · 22.2 KB
/
report.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
<?PHP // $Id$
// This script uses installed report plugins to print quiz reports
require_once("../../config.php");
require_once("lib.php");
$id = optional_param('id', 0, PARAM_INT); // Course Module ID, or
$hp = optional_param('hp', 0, PARAM_INT); // hotpot ID
if ($id) {
if (! $cm = get_coursemodule_from_id('hotpot', $id)) {
print_error("Course Module ID was incorrect");
}
if (! $course = get_record("course", "id", $cm->course)) {
print_error("Course is misconfigured");
}
if (! $hotpot = get_record("hotpot", "id", $cm->instance)) {
print_error("Course module is incorrect");
}
} else {
if (! $hotpot = get_record("hotpot", "id", $hp)) {
print_error("Course module is incorrect");
}
if (! $course = get_record("course", "id", $hotpot->course)) {
print_error("Course is misconfigured");
}
if (! $cm = get_coursemodule_from_instance("hotpot", $hotpot->id, $course->id)) {
print_error("Course Module ID was incorrect");
}
}
// get the roles context for this course
$sitecontext = get_context_instance(CONTEXT_SYSTEM);
$modulecontext = get_context_instance(CONTEXT_MODULE, $cm->id);
// set homeurl of couse (for error messages)
$course_homeurl = "$CFG->wwwroot/course/view.php?id=$course->id";
require_login($course, true, $cm);
// get report mode
if (has_capability('mod/hotpot:viewreport',$modulecontext)) {
$mode = optional_param('mode', 'overview', PARAM_ALPHA);
} else {
// ordinary students have no choice
$mode = 'overview';
}
// assemble array of form data
$formdata = array(
'mode' => $mode,
'reportusers' => has_capability('mod/hotpot:viewreport',$modulecontext) ? optional_param('reportusers', get_user_preferences('hotpot_reportusers', 'allusers'), PARAM_ALPHANUM) : 'this',
'reportattempts' => optional_param('reportattempts', get_user_preferences('hotpot_reportattempts', 'all'), PARAM_ALPHA),
'reportformat' => optional_param('reportformat', 'htm', PARAM_ALPHA),
'reportshowlegend' => optional_param('reportshowlegend', get_user_preferences('hotpot_reportshowlegend', '0'), PARAM_INT),
'reportencoding' => optional_param('reportencoding', get_user_preferences('hotpot_reportencoding', ''), PARAM_ALPHANUM),
'reportwrapdata' => optional_param('reportwrapdata', get_user_preferences('hotpot_reportwrapdata', '1'), PARAM_INT),
);
foreach ($formdata as $name=>$value) {
set_user_preference("hotpot_$name", $value);
}
/// Start the report
add_to_log($course->id, "hotpot", "report", "report.php?id=$cm->id&mode=$mode", "$hotpot->id", "$cm->id");
// print page header. if required
if ($formdata['reportformat']=='htm') {
hotpot_print_report_heading($course, $cm, $hotpot, $mode);
if (has_capability('mod/hotpot:viewreport',$modulecontext)) {
hotpot_print_report_selector($course, $hotpot, $formdata);
}
}
// delete selected attempts, if any
if (has_capability('mod/hotpot:deleteattempt',$modulecontext)) {
$del = optional_param('del', '', PARAM_ALPHA);
hotpot_delete_selected_attempts($hotpot, $del);
}
// check for groups
if (preg_match('/^group(\d*)$/', $formdata['reportusers'], $matches)) {
$formdata['reportusers'] = 'group';
$formdata['reportgroupid'] = 0;
// validate groupid
if ($groups = groups_get_all_groups($course->id)) {
if (isset($groups[$matches[1]])) {
$formdata['reportgroupid'] = $matches[1];
}
}
}
$user_ids = '';
$users = array();
switch ($formdata['reportusers']) {
case 'allusers':
// anyone who has ever attempted this hotpot
if ($records = get_records_select('hotpot_attempts', "hotpot=$hotpot->id", '', 'id,userid')) {
foreach ($records as $record) {
$users[$record->userid] = 0; // "0" means user is NOT currently allowed to attempt this HotPot
}
unset($records);
}
break;
case 'group':
// group members
if ($members = groups_get_members($formdata['reportgroupid'])) {
foreach ($members as $memberid=>$unused) {
$users[$memberid] = 1; // "1" signifies currently recognized participant
}
}
break;
case 'allparticipants':
// anyone currently allowed to attempt this HotPot
if ($records = get_users_by_capability($modulecontext, 'mod/hotpot:attempt', 'u.id,u.id', 'u.id')) {
foreach ($records as $record) {
$users[$record->id] = 1; // "1" means user is allowed to do this HotPot
}
unset($records);
}
break;
case 'existingstudents':
// anyone currently allowed to attempt this HotPot who is not a teacher
$teachers = get_users_by_capability($modulecontext, 'mod/hotpot:viewreport', 'u.id,u.id', 'u.id');
if ($records = get_users_by_capability($modulecontext, 'mod/hotpot:attempt', 'u.id,u.id', 'u.id')) {
foreach ($records as $record) {
if (empty($teachers[$record->id])) {
$users[$record->id] = 1;
}
}
}
break;
case 'this': // current user only
$user_ids = $USER->id;
break;
default: // specific user selected by teacher
if (is_numeric($formdata['reportusers'])) {
$user_ids = $formdata['reportusers'];
}
}
if (empty($user_ids) && count($users)) {
ksort($users);
$user_ids = join(',', array_keys($users));
}
if (empty($user_ids)) {
print_heading(get_string('nousersyet'));
exit;
}
// database table and selection conditions
$table = "{$CFG->prefix}hotpot_attempts a";
$select = "a.hotpot=$hotpot->id AND a.userid IN ($user_ids)";
if ($mode!='overview') {
$select .= ' AND a.status<>'.HOTPOT_STATUS_INPROGRESS;
}
// confine attempts if necessary
switch ($formdata['reportattempts']) {
case 'best':
$function = 'MAX';
$fieldnames = array('score', 'id', 'clickreportid');
$defaultvalue = 0;
break;
case 'first':
$function = 'MIN';
$fieldnames = array('timefinish', 'id', 'clickreportid');
$default_value = time();
break;
case 'last':
$function = 'MAX';
$fieldnames = array('timefinish', 'id', 'clickreportid');
$defaultvalue = time();
break;
default: // 'all' and any others
$function = '';
$fieldnames = array();
$defaultvalue = '';
break;
}
if (empty($function) || empty($fieldnames)) {
// do nothing (i.e. get ALL attempts)
} else {
$groupby = 'userid';
$records = hotpot_get_records_groupby($function, $fieldnames, $table, $select, $groupby);
$select = '';
if ($records) {
$ids = array();
foreach ($records as $record) {
$ids[] = $record->clickreportid;
}
if (count($ids)) {
$select = "a.clickreportid IN (".join(',', $ids).")";
}
}
}
// pick out last attempt in each clickreport series
if ($select) {
$cr_attempts = hotpot_get_records_groupby('MAX', array('timefinish', 'id'), $table, $select, 'clickreportid');
} else {
$cr_attempts = array();
}
$fields = 'a.*, u.firstname, u.lastname, u.picture';
if ($mode=='click') {
$fields .= ', u.idnumber';
} else {
// overview, simple and detailed reports
// get last attempt record in clickreport series
$ids = array();
foreach ($cr_attempts as $cr_attempt) {
$ids[] = $cr_attempt->id;
}
if (empty($ids)) {
$select = "";
} else {
$ids = array_unique($ids);
sort($ids);
$select = "a.id IN (".join(',', $ids).")";
}
}
$attempts = array();
if ($select) {
// add user information to SQL query
$select .= ' AND a.userid = u.id';
$table .= ", {$CFG->prefix}user u";
$order = "u.lastname, a.attempt, a.timefinish";
// get the attempts (at last!)
$attempts = get_records_sql("SELECT $fields FROM $table WHERE $select ORDER BY $order");
}
// stop now if no attempts were found
if (empty($attempts)) {
print_heading(get_string('noattemptstoshow','quiz'));
exit;
}
// get the questions
if (!$questions = get_records_select('hotpot_questions', "hotpot='$hotpot->id'")) {
$questions = array();
}
// get grades
$grades = hotpot_get_grades($hotpot, $user_ids);
// get list of attempts by user and set reference to last attempt in clickreport series
$users = array();
foreach ($attempts as $id=>$attempt) {
$userid = $attempt->userid;
if (!isset($users[$userid])) {
$users[$userid]->grade = isset($grades[$userid]) ? $grades[$userid] : ' ';
$users[$userid]->attempts = array();
}
$users[$userid]->attempts[] = &$attempts[$id];
if ($mode=='click') {
// shortcut to clickreportid (=the id of the FIRST attempt in this clickreport series)
$clickreportid = $attempt->clickreportid;
if (isset($cr_attempts[$clickreportid])) {
// store id and finish time of LAST attempt in this clickreport series
$attempts[$id]->cr_lastclick = $cr_attempts[$clickreportid]->id;
$attempts[$id]->cr_timefinish = $cr_attempts[$clickreportid]->timefinish;
}
}
}
if ($mode!='overview') {
// initialise details of responses to questions in these attempts
foreach ($attempts as $a=>$attempt) {
$attempts[$a]->responses = array();
}
foreach ($questions as $q=>$question) {
$questions[$q]->attempts = array();
}
// get reponses to these attempts
$attempt_ids = join(',',array_keys($attempts));
if (!$responses = get_records_sql("SELECT * FROM {$CFG->prefix}hotpot_responses WHERE attempt IN ($attempt_ids)")) {
$responses = array();
}
// ids of questions used in these responses
$questionids = array();
foreach ($responses as $response) {
// shortcuts to the attempt and question ids
$a = $response->attempt;
$q = $response->question;
// check the attempt and question objects exist
// (if they don't exist, something is very wrong!)
if (isset($attempts[$a]) || isset($questions[$q])) {
// add the response for this attempt
$attempts[$a]->responses[$q] = $response;
// add a reference from the question to the attempt which includes this question
$questions[$q]->attempts[] = &$attempts[$a];
// flag this id as being used
$questionids[$q] = true;
}
}
// remove unused questions
$questionids = array_keys($questionids);
foreach ($questions as $id=>$question) {
if (!in_array($id, $questionids)) {
unset($questions[$id]);
}
}
}
/// Open the selected hotpot report and display it
if (! is_readable("report/$mode/report.php")) {
print_error("Report not known (".clean_text($mode).")", '', $course_homeurl);
}
include("report/default.php"); // Parent class
include("report/$mode/report.php");
$report = new hotpot_report();
if (! $report->display($hotpot, $cm, $course, $users, $attempts, $questions, $formdata)) {
print_error("Error occurred during report processing!", '', $course_homeurl);
}
if ($formdata['reportformat']=='htm') {
print_footer($course);
}
//////////////////////////////////////////////
/// functions to delete attempts and responses
function hotpot_grade_heading($hotpot, $formdata) {
global $HOTPOT_GRADEMETHOD;
$grademethod = $HOTPOT_GRADEMETHOD[$hotpot->grademethod];
if ($hotpot->grade!=100) {
$grademethod = "$hotpot->grade x $grademethod/100";
}
if ($formdata['reportformat']=='htm') {
$grademethod = '<font size="1">'.$grademethod.'</font>';
}
$nl = $formdata['reportformat']=='htm' ? '<br />' : "\n";
return get_string('grade')."$nl($grademethod)";
}
function hotpot_delete_selected_attempts(&$hotpot, $del) {
$select = '';
switch ($del) {
case 'all' :
$select = "hotpot='$hotpot->id'";
break;
case 'abandoned':
$select = "hotpot='$hotpot->id' AND status=".HOTPOT_STATUS_ABANDONED;
break;
case 'selection':
$ids = (array)data_submitted();
unset($ids['del']);
unset($ids['id']);
if (!empty($ids)) {
$select = "hotpot='$hotpot->id' AND clickreportid IN (".implode(',', $ids).")";
}
break;
}
// delete attempts using $select, if it is set
if ($select) {
$table = 'hotpot_attempts';
if ($attempts = get_records_select($table, $select)) {
hotpot_delete_and_notify($table, $select, get_string('attempts', 'quiz'));
$select = 'attempt IN ('.implode(',', array_keys($attempts)).')';
hotpot_delete_and_notify('hotpot_details', $select, get_string('rawdetails', 'hotpot'));
hotpot_delete_and_notify('hotpot_responses', $select, get_string('answer', 'quiz'));
// update grades for all users for this hotpot
hotpot_update_grades($hotpot);
}
}
}
//////////////////////////////////////////////
/// functions to print the report headings and
/// report selector menus
function hotpot_print_report_heading(&$course, &$cm, &$hotpot, &$mode) {
$strmodulenameplural = get_string("modulenameplural", "hotpot");
$strmodulename = get_string("modulename", "hotpot");
$title = format_string($course->shortname) . ": $hotpot->name";
$heading = $course->fullname;
$modulecontext = get_context_instance(CONTEXT_MODULE, $cm->id);
if (has_capability('mod/hotpot:viewreport',$modulecontext)) {
if ($mode=='overview' || $mode=='simplestat' || $mode=='fullstat') {
$module = "quiz";
} else {
$module = "hotpot";
}
$navigation = build_navigation(get_string("report$mode", $module), $cm);
} else {
$navigation = build_navigation(get_string("report", "quiz"), $cm);
}
$button = update_module_button($cm->id, $course->id, $strmodulename);
print_header($title, $heading, $navigation, "", "", true, $button, navmenu($course, $cm));
$course_context = get_context_instance(CONTEXT_COURSE, $course->id);
if (has_capability('gradereport/grader:view', $course_context) && has_capability('moodle/grade:viewall', $course_context)) {
echo '<div class="allcoursegrades"><a href="' . $CFG->wwwroot . '/grade/report/grader/index.php?id=' . $course->id . '">'
. get_string('seeallcoursegrades', 'grades') . '</a></div>';
}
print_heading($hotpot->name);
}
function hotpot_print_report_selector(&$course, &$hotpot, &$formdata) {
global $CFG;
$reports = hotpot_get_report_names('overview,simplestat,fullstat');
print '<form method="post" action="'."$CFG->wwwroot/mod/hotpot/report.php?hp=$hotpot->id".'">';
print '<table cellpadding="2" align="center">';
$menus = array();
$menus['mode'] = array();
foreach ($reports as $name) {
if ($name=='overview' || $name=='simplestat' || $name=='fullstat') {
$module = "quiz"; // standard reports
} else if ($name=='click' && empty($hotpot->clickreporting)) {
$module = ""; // clickreporting is disabled
} else {
$module = "hotpot"; // custom reports
}
if ($module) {
$menus['mode'][$name] = get_string("report$name", $module);
}
}
$menus['reportusers'] = array(
'allusers' => get_string('allusers', 'hotpot'),
'allparticipants' => get_string('allparticipants')
);
// groups
if ($groups = groups_get_all_groups($course->id)) {
foreach ($groups as $gid => $group) {
$menus['reportusers']["group$gid"] = get_string('group').': '.format_string($group->name);
}
}
// get users who have ever atetmpted this HotPot
$users = get_records_sql("
SELECT
u.id, u.firstname, u.lastname
FROM
{$CFG->prefix}user u,
{$CFG->prefix}hotpot_attempts ha
WHERE
u.id = ha.userid AND ha.hotpot=$hotpot->id
ORDER BY
u.lastname
");
// get context
$cm = get_coursemodule_from_instance('hotpot', $hotpot->id);
$modulecontext = get_context_instance(CONTEXT_MODULE, $cm->id);
// get teachers enrolled students
$teachers = get_users_by_capability($modulecontext, 'mod/hotpot:viewreport', 'u.id,u.firstname,u.lastname', 'u.lastname');
$students = get_users_by_capability($modulecontext, 'mod/hotpot:attempt', 'u.id,u.firstname,u.lastname', 'u.lastname');
// current students
if (!empty($students)) {
$firsttime = true;
foreach ($students as $user) {
if (isset($users[$user->id])) {
if ($firsttime) {
$firsttime = false; // so we only do this once
$menus['reportusers']['existingstudents'] = get_string('existingstudents');
$menus['reportusers'][] = '------';
}
$menus['reportusers']["$user->id"] = fullname($user);
unset($users[$user->id]);
}
}
}
// others (former students, teachers, admins, course creators)
if (!empty($users)) {
$firsttime = true;
foreach ($users as $user) {
if ($firsttime) {
$firsttime = false; // so we only do this once
$menus['reportusers'][] = '======';
}
$menus['reportusers']["$user->id"] = fullname($user);
}
}
$menus['reportattempts'] = array(
'all' => get_string('attemptsall', 'hotpot'),
'best' => get_string('attemptsbest', 'hotpot'),
'first' => get_string('attemptsfirst', 'hotpot'),
'last' => get_string('attemptslast', 'hotpot')
);
print '<tr><td>';
helpbutton('reportcontent', get_string('reportcontent', 'hotpot'), 'hotpot');
print '</td><th align="right" scope="col">'.get_string('reportcontent', 'hotpot').':</th><td colspan="7">';
foreach ($menus as $name => $options) {
$value = $formdata[$name];
print choose_from_menu($options, $name, $value, "", "", 0, true);
};
print '<input type="submit" value="'.get_string('reportbutton', 'hotpot').'" /></td></tr>';
$menus = array();
$menus['reportformat'] = array();
$menus['reportformat']['htm'] = get_string('reportformathtml', 'hotpot');
if (file_exists("$CFG->libdir/excel") || file_exists("$CFG->libdir/excellib.class.php")) {
$menus['reportformat']['xls'] = get_string('reportformatexcel', 'hotpot');
}
$menus['reportformat']['txt'] = get_string('reportformattext', 'hotpot');
if (trim($CFG->hotpot_excelencodings)) {
$menus['reportencoding'] = array(get_string('none')=>'');
$encodings = explode(',', $CFG->hotpot_excelencodings);
foreach ($encodings as $encoding) {
$encoding = trim($encoding);
if ($encoding) {
$menus['reportencoding'][$encoding] = $encoding;
}
}
}
$menus['reportwrapdata'] = array(
'1' => get_string('yes'),
'0' => get_string('no'),
);
$menus['reportshowlegend'] = array(
'1' => get_string('yes'),
'0' => get_string('no'),
);
print '<tr><td>';
helpbutton('reportformat', get_string('reportformat', 'hotpot'), 'hotpot');
print '</td>';
foreach ($menus as $name => $options) {
$value = $formdata[$name];
print '<th align="right" scope="col">'.get_string($name, 'hotpot').':</th><td>'.choose_from_menu($options, $name, $value, "", "", 0, true).'</td>';
}
print '</tr>';
print '</table>';
print '<hr size="1" noshade="noshade" />';
print '</form>'."\n";
}
function hotpot_get_report_names($names='') {
// $names : optional list showing required order reports names
$reports = array();
// convert $names to an array, if necessary (usually is)
if (!is_array($names)) {
$names = explode(',', $names);
}
$plugins = get_list_of_plugins('mod/hotpot/report');
foreach($names as $name) {
if (is_numeric($i = array_search($name, $plugins))) {
$reports[] = $name;
unset($plugins[$i]);
}
}
// append remaining plugins
$reports = array_merge($reports, $plugins);
return $reports;
}
function hotpot_get_records_groupby($function, $fieldnames, $table, $select, $groupby) {
// $function is an SQL aggregate function (MAX or MIN)
$fields = sql_concat_join("'_'", $fieldnames);
$fields = "$groupby, $function($fields) AS joinedvalues";
if ($fields) {
$records = get_records_sql("SELECT $fields FROM $table WHERE $select GROUP BY $groupby");
}
if (empty($fields) || empty($records)) {
$records = array();
}
$fieldcount = count($fieldnames);
foreach ($records as $id=>$record) {
if (empty($record->joinedvalues)) {
unset($records[$id]);
} else {
$values = explode('_', $record->joinedvalues);
for ($i=0; $i<$fieldcount; $i++) {
$fieldname = $fieldnames[$i];
$records[$id]->$fieldname = $values[$i];
}
}
unset($record->joinedvalues);
}
return $records;
}
?>