Take out debugging & merge branch 'w05_MDL-26198_20_completion' of git://github.com...
[moodle.git] / course / report / completion / index.php
CommitLineData
2be4d090
MD
1<?php
2
3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17
18
19/**
20 * Course completion progress report
21 *
22 * @package moodlecore
23 * @copyright 2009 Catalyst IT Ltd
24 * @author Aaron Barnes <aaronb@catalyst.net.nz>
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 */
27require_once('../../../config.php');
28require_once($CFG->libdir.'/completionlib.php');
29
2be4d090
MD
30/**
31 * Configuration
32 */
33define('COMPLETION_REPORT_PAGE', 25);
34define('COMPLETION_REPORT_COL_TITLES', true);
35
2be4d090
MD
36/**
37 * Setup page, check permissions
38 */
39
40// Get course
e67e026f
SH
41$courseid = required_param('course', PARAM_INT);
42$format = optional_param('format','',PARAM_ALPHA);
43$sort = optional_param('sort','',PARAM_ALPHA);
2be4d090
MD
44$edituser = optional_param('edituser', 0, PARAM_INT);
45
2be4d090 46
e67e026f
SH
47$course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);
48
49$url = new moodle_url('/course/report/completion/index.php', array('course'=>$course->id));
50$PAGE->set_url($url);
cf5a0dbd 51$PAGE->set_pagelayout('report');
e67e026f
SH
52
53$firstnamesort = ($sort == 'firstname');
54$excel = ($format == 'excelcsv');
55$csv = ($format == 'csv' || $excel);
2be4d090 56
24a3b341
AB
57// Paging
58$start = optional_param('start', 0, PARAM_INT);
59$sifirst = optional_param('sifirst', 'all', PARAM_ALPHA);
60$silast = optional_param('silast', 'all', PARAM_ALPHA);
2be4d090
MD
61
62// Whether to show idnumber
63$idnumbers = $CFG->grade_report_showuseridnumber;
64
65// Function for quoting csv cell values
66function csv_quote($value) {
67 global $excel;
68 if($excel) {
69 $tl=textlib_get_instance();
70 return $tl->convert('"'.str_replace('"',"'",$value).'"','UTF-8','UTF-16LE');
71 } else {
72 return '"'.str_replace('"',"'",$value).'"';
73 }
74}
75
76
77// Check permissions
78require_login($course);
79
80$context=get_context_instance(CONTEXT_COURSE, $course->id);
81require_capability('coursereport/completion:view', $context);
82
83// Get group mode
84$group = groups_get_course_group($course, true); // Supposed to verify group
85if($group === 0 && $course->groupmode == SEPARATEGROUPS) {
86 require_capability('moodle/site:accessallgroups',$context);
87}
88
2be4d090
MD
89/**
90 * Load data
91 */
92
93// Get criteria for course
94$completion = new completion_info($course);
95
96if (!$completion->has_criteria()) {
97 print_error('err_nocriteria', 'completion', $CFG->wwwroot.'/course/report.php?id='.$course->id);
98}
99
100// Get criteria and put in correct order
101$criteria = array();
102
103foreach ($completion->get_criteria(COMPLETION_CRITERIA_TYPE_COURSE) as $criterion) {
104 $criteria[] = $criterion;
105}
106
107foreach ($completion->get_criteria(COMPLETION_CRITERIA_TYPE_ACTIVITY) as $criterion) {
108 $criteria[] = $criterion;
109}
110
111foreach ($completion->get_criteria() as $criterion) {
112 if (!in_array($criterion->criteriatype, array(
113 COMPLETION_CRITERIA_TYPE_COURSE, COMPLETION_CRITERIA_TYPE_ACTIVITY))) {
114 $criteria[] = $criterion;
115 }
116}
117
118// Can logged in user mark users as complete?
119// (if the logged in user has a role defined in the role criteria)
120$allow_marking = false;
121$allow_marking_criteria = null;
122
123if (!$csv) {
124 // Get role criteria
125 $rcriteria = $completion->get_criteria(COMPLETION_CRITERIA_TYPE_ROLE);
126
127 if (!empty($rcriteria)) {
128
129 foreach ($rcriteria as $rcriterion) {
130 $users = get_role_users($rcriterion->role, $context, true);
131
132 // If logged in user has this role, allow marking complete
133 if ($users && in_array($USER->id, array_keys($users))) {
134 $allow_marking = true;
135 $allow_marking_criteria = $rcriterion->id;
136 break;
137 }
138 }
139 }
140}
141
2be4d090
MD
142/**
143 * Setup page header
144 */
145if ($csv) {
146 header('Content-Disposition: attachment; filename=progress.'.
147 preg_replace('/[^a-z0-9-]/','_',strtolower($course->shortname)).'.csv');
148 // Unicode byte-order mark for Excel
149 if($excel) {
150 header('Content-Type: text/csv; charset=UTF-16LE');
151 print chr(0xFF).chr(0xFE);
152 $sep="\t".chr(0);
153 $line="\n".chr(0);
154 } else {
155 header('Content-Type: text/csv; charset=UTF-8');
156 $sep=",";
157 $line="\n";
158 }
159
160} else {
161 // Navigation and header
cfc33925 162 $strcompletion = get_string('coursecompletion');
2be4d090
MD
163
164 $PAGE->set_title($strcompletion);
165 $PAGE->set_heading($course->fullname);
24a3b341 166
2be4d090
MD
167 echo $OUTPUT->header();
168
169 $PAGE->requires->yui2_lib(
170 array(
171 'yahoo',
172 'dom',
173 'element',
174 'event',
175 )
176 );
177
178 $PAGE->requires->js('/course/report/completion/textrotate.js');
179
180 // Handle groups (if enabled)
24a3b341 181 groups_print_course_menu($course, $CFG->wwwroot.'/course/report/completion/?course='.$course->id);
2be4d090
MD
182}
183
2be4d090 184
24a3b341
AB
185// Generate where clause
186$where = array();
5642a8e5 187$where_params = array();
24a3b341
AB
188
189if ($sifirst !== 'all') {
5642a8e5
AB
190 $where[] = $DB->sql_like('u.firstname', ':sifirst', false);
191 $where_params['sifirst'] = $sifirst.'%';
24a3b341
AB
192}
193
194if ($silast !== 'all') {
5642a8e5
AB
195 $where[] = $DB->sql_like('u.lastname', ':silast', false);
196 $where_params['silast'] = $silast.'%';
24a3b341
AB
197}
198
199// Get user match count
5642a8e5 200$total = $completion->get_num_tracked_users(implode(' AND ', $where), $where_params, $group);
24a3b341
AB
201
202// Total user count
5642a8e5 203$grandtotal = $completion->get_num_tracked_users('', array(), $group);
24a3b341
AB
204
205// If no users in this course what-so-ever
206if (!$grandtotal) {
207 echo $OUTPUT->container(get_string('err_nousers', 'completion'), 'errorbox errorboxcontent');
208 echo $OUTPUT->footer();
209 exit;
210}
211
212// Get user data
213$progress = array();
214
215if ($total) {
216 $progress = $completion->get_progress_all(
217 implode(' AND ', $where),
5642a8e5 218 $where_params,
24a3b341
AB
219 $group,
220 $firstnamesort ? 'u.firstname ASC' : 'u.lastname ASC',
221 $csv ? 0 : COMPLETION_REPORT_PAGE,
222 $csv ? 0 : $start
223 );
224}
225
226
227// Build link for paging
228$link = $CFG->wwwroot.'/course/report/completion/?course='.$course->id;
229if (strlen($sort)) {
230 $link .= '&amp;sort='.$sort;
231}
232$link .= '&amp;start=';
233
234// Build the the page by Initial bar
235$initials = array('first', 'last');
236$alphabet = explode(',', get_string('alphabet', 'langconfig'));
237
238$pagingbar = '';
239foreach ($initials as $initial) {
240 $var = 'si'.$initial;
241
242 $pagingbar .= ' <div class="initialbar '.$initial.'initial">';
243 $pagingbar .= get_string($initial.'name').':&nbsp;';
244
245 if ($$var == 'all') {
246 $pagingbar .= '<strong>'.get_string('all').'</strong> ';
247 }
248 else {
249 $pagingbar .= '<a href="'.$link.'">'.get_string('all').'</a> ';
250 }
251
252 foreach ($alphabet as $letter) {
253 if ($$var === $letter) {
254 $pagingbar .= '<strong>'.$letter.'</strong> ';
255 }
256 else {
257 $pagingbar .= '<a href="'.$link.'&amp;'.$var.'='.$letter.'">'.$letter.'</a> ';
2be4d090 258 }
2be4d090
MD
259 }
260
24a3b341
AB
261 $pagingbar .= '</div>';
262}
2be4d090 263
24a3b341
AB
264// Do we need a paging bar?
265if($total > COMPLETION_REPORT_PAGE) {
266
267 // Paging bar
268 $pagingbar .= '<div class="paging">';
269 $pagingbar .= get_string('page').': ';
270
271 // Display previous link
272 if ($start > 0) {
273 $pstart = max($start - COMPLETION_REPORT_PAGE, 0);
274 $pagingbar .= '(<a class="previous" href="'.$link.$pstart.'">'.get_string('previous').'</a>)&nbsp;';
2be4d090
MD
275 }
276
24a3b341
AB
277 // Create page links
278 $curstart = 0;
279 $curpage = 0;
280 while ($curstart < $total) {
281 $curpage++;
282
283 if ($curstart == $start) {
284 $pagingbar .= '&nbsp;'.$curpage.'&nbsp;';
285 }
286 else {
287 $pagingbar .= '&nbsp;<a href="'.$link.$curstart.'">'.$curpage.'</a>&nbsp;';
288 }
289
290 $curstart += COMPLETION_REPORT_PAGE;
291 }
292
293 // Display next link
294 $nstart = $start + COMPLETION_REPORT_PAGE;
295 if ($nstart < $total) {
296 $pagingbar .= '&nbsp;(<a class="next" href="'.$link.$nstart.'">'.get_string('next').'</a>)';
297 }
298
299 $pagingbar .= '</div>';
2be4d090
MD
300}
301
302
303/**
304 * Draw table header
305 */
306
307// Start of table
308if(!$csv) {
309 print '<br class="clearer"/>'; // ugh
310
24a3b341
AB
311 $total_header = ($total == $grandtotal) ? $total : "{$total}/{$grandtotal}";
312 echo $OUTPUT->heading(get_string('allparticipants').": {$total_header}", 3);
313
314 print $pagingbar;
315
316 if (!$total) {
317 echo $OUTPUT->heading(get_string('nothingtodisplay'), 2);
318 echo $OUTPUT->footer();
2be4d090
MD
319 exit;
320 }
321
2be4d090
MD
322 print '<table id="completion-progress" class="generaltable flexible boxaligncenter completionreport" style="text-align: left" cellpadding="5" border="1">';
323
324 // Print criteria group names
325 print PHP_EOL.'<tr style="vertical-align: top">';
326 print '<th scope="row" class="rowheader">'.get_string('criteriagroup', 'completion').'</th>';
327
328 $current_group = false;
329 $col_count = 0;
330 for ($i = 0; $i <= count($criteria); $i++) {
331
332 if (isset($criteria[$i])) {
333 $criterion = $criteria[$i];
334
335 if ($current_group && $criterion->criteriatype === $current_group->criteriatype) {
336 ++$col_count;
337 continue;
338 }
339 }
340
341 // Print header cell
342 if ($col_count) {
343 print '<th scope="col" colspan="'.$col_count.'" class="colheader criteriagroup">'.$current_group->get_type_title().'</th>';
344 }
345
346 if (isset($criteria[$i])) {
347 // Move to next criteria type
348 $current_group = $criterion;
349 $col_count = 1;
350 }
351 }
352
353 // Overall course completion status
354 print '<th style="text-align: center;">'.get_string('course').'</th>';
355
356 print '</tr>';
357
358 // Print aggregation methods
359 print PHP_EOL.'<tr style="vertical-align: top">';
360 print '<th scope="row" class="rowheader">'.get_string('aggregationmethod', 'completion').'</th>';
361
362 $current_group = false;
363 $col_count = 0;
364 for ($i = 0; $i <= count($criteria); $i++) {
365
366 if (isset($criteria[$i])) {
367 $criterion = $criteria[$i];
368
369 if ($current_group && $criterion->criteriatype === $current_group->criteriatype) {
370 ++$col_count;
371 continue;
372 }
373 }
374
375 // Print header cell
376 if ($col_count) {
377 $has_agg = array(
378 COMPLETION_CRITERIA_TYPE_COURSE,
379 COMPLETION_CRITERIA_TYPE_ACTIVITY,
380 COMPLETION_CRITERIA_TYPE_ROLE,
381 );
382
383 if (in_array($current_group->criteriatype, $has_agg)) {
384 // Try load a aggregation method
385 $method = $completion->get_aggregation_method($current_group->criteriatype);
386
387 $method = $method == 1 ? 'All' : 'Any';
388
389 } else {
390 $method = '-';
391 }
392
393 print '<th scope="col" colspan="'.$col_count.'" class="colheader aggheader">'.$method.'</th>';
394 }
395
396 if (isset($criteria[$i])) {
397 // Move to next criteria type
398 $current_group = $criterion;
399 $col_count = 1;
400 }
401 }
402
403 // Overall course aggregation method
404 print '<th scope="col" class="colheader aggheader aggcriteriacourse">';
405
406 // Get course aggregation
407 $method = $completion->get_aggregation_method();
408
409 print $method == 1 ? 'All' : 'Any';
410 print '</th>';
411
412 print '</tr>';
413
414
415 // Print criteria titles
416 if (COMPLETION_REPORT_COL_TITLES) {
417
418 print PHP_EOL.'<tr>';
419 print '<th scope="row" class="rowheader">'.get_string('criteria', 'completion').'</th>';
420
421 foreach ($criteria as $criterion) {
422 // Get criteria details
423 $details = $criterion->get_title_detailed();
424 print '<th scope="col" class="colheader criterianame">';
425 print '<span class="completion-criterianame">'.$details.'</span>';
426 print '</th>';
427 }
428
429 // Overall course completion status
430 print '<th scope="col" class="colheader criterianame">';
431
432 print '<span class="completion-criterianame">'.get_string('coursecomplete', 'completion').'</span>';
433
434 print '</th></tr>';
435 }
436
437 // Print user heading and icons
438 print '<tr>';
439
440 // User heading / sort option
441 print '<th scope="col" class="completion-sortchoice" style="clear: both;">';
442 if($firstnamesort) {
443 print
444 get_string('firstname').' / <a href="./?course='.$course->id.'">'.
445 get_string('lastname').'</a>';
446 } else {
447 print '<a href="./?course='.$course->id.'&amp;sort=firstname">'.
448 get_string('firstname').'</a> / '.
449 get_string('lastname');
450 }
451 print '</th>';
452
453
454 // Print user id number column
455 if($idnumbers) {
456 print '<th>'.get_string('idnumber').'</th>';
457 }
458
459 ///
460 /// Print criteria icons
461 ///
462 foreach ($criteria as $criterion) {
463
464 // Generate icon details
465 $icon = '';
466 $iconlink = '';
467 $icontitle = ''; // Required if $iconlink set
468 $iconalt = ''; // Required
469 switch ($criterion->criteriatype) {
470
471 case COMPLETION_CRITERIA_TYPE_ACTIVITY:
472 // Load activity
473 $activity = $criterion->get_mod_instance();
474
475 // Display icon
f8e9c93a 476 $icon = $OUTPUT->pix_url('icon', $criterion->module);
2be4d090
MD
477 $iconlink = $CFG->wwwroot.'/mod/'.$criterion->module.'/view.php?id='.$activity->id;
478 $icontitle = $activity->name;
479 $iconalt = get_string('modulename', $criterion->module);
480 break;
481
482 case COMPLETION_CRITERIA_TYPE_COURSE:
483 // Load course
ffe50258 484 $crs = $DB->get_record('course', array('id' => $criterion->courseinstance));
2be4d090
MD
485
486 // Display icon
487 $iconlink = $CFG->wwwroot.'/course/view.php?id='.$criterion->courseinstance;
488 $icontitle = $crs->fullname;
489 $iconalt = $crs->shortname;
490 break;
491
492 case COMPLETION_CRITERIA_TYPE_ROLE:
493 // Load role
494 $role = $DB->get_record('role', array('id' => $criterion->role));
495
496 // Display icon
497 $iconalt = $role->name;
498 break;
499 }
500
501 // Print icon and cell
502 print '<th class="criteriaicon">';
503
504 // Create icon if not supplied
505 if (!$icon) {
f8e9c93a 506 $icon = $OUTPUT->pix_url('i/'.$COMPLETION_CRITERIA_TYPES[$criterion->criteriatype]);
2be4d090
MD
507 }
508
509 print ($iconlink ? '<a href="'.$iconlink.'" title="'.$icontitle.'">' : '');
510 print '<img src="'.$icon.'" class="icon" alt="'.$iconalt.'" '.(!$iconlink ? 'title="'.$iconalt.'"' : '').' />';
511 print ($iconlink ? '</a>' : '');
512
513 print '</th>';
514 }
515
516 // Overall course completion status
517 print '<th class="criteriaicon">';
f8e9c93a 518 print '<img src="'.$OUTPUT->pix_url('i/course').'" class="icon" alt="Course" title="Course Complete" />'; //TODO: localize
2be4d090
MD
519 print '</th>';
520
521 print '</tr>';
522
523
524} else {
525 // TODO
526 if($idnumbers) {
527 print $sep;
528 }
529}
530
531
532///
533/// Display a row for each user
534///
24a3b341 535foreach ($progress as $user) {
2be4d090
MD
536
537 // User name
538 if($csv) {
539 print csv_quote(fullname($user));
540 if($idnumbers) {
541 print $sep.csv_quote($user->idnumber);
542 }
543 } else {
544 print PHP_EOL.'<tr id="user-'.$user->id.'">';
545
546 print '<th scope="row"><a href="'.$CFG->wwwroot.'/user/view.php?id='.
547 $user->id.'&amp;course='.$course->id.'">'.fullname($user).'</a></th>';
548 if($idnumbers) {
549 print '<td>'.htmlspecialchars($user->idnumber).'</td>';
550 }
551 }
552
553 // Progress for each course completion criteria
554 foreach ($criteria as $criterion) {
555
556 // Handle activity completion differently
557 if ($criterion->criteriatype == COMPLETION_CRITERIA_TYPE_ACTIVITY) {
558
559 // Load activity
560 $mod = $criterion->get_mod_instance();
561 $activity = $DB->get_record('course_modules', array('id' => $criterion->moduleinstance));
562 $activity->name = $mod->name;
563
564
565 // Get progress information and state
566 if(array_key_exists($activity->id,$user->progress)) {
567 $thisprogress=$user->progress[$activity->id];
568 $state=$thisprogress->completionstate;
569 $date=userdate($thisprogress->timemodified);
570 } else {
571 $state=COMPLETION_INCOMPLETE;
572 $date='';
573 }
574
575 $criteria_completion = $completion->get_user_completion($user->id, $criterion);
576
577 // Work out how it corresponds to an icon
578 switch($state) {
579 case COMPLETION_INCOMPLETE : $completiontype='n'; break;
580 case COMPLETION_COMPLETE : $completiontype='y'; break;
581 case COMPLETION_COMPLETE_PASS : $completiontype='pass'; break;
582 case COMPLETION_COMPLETE_FAIL : $completiontype='fail'; break;
583 }
584
585 $completionicon='completion-'.
586 ($activity->completion==COMPLETION_TRACKING_AUTOMATIC ? 'auto' : 'manual').
587 '-'.$completiontype;
588
589 $describe=get_string('completion-alt-auto-'.$completiontype,'completion');
590 $a=new StdClass;
591 $a->state=$describe;
592 $a->date=$date;
593 $a->user=fullname($user);
594 $a->activity=strip_tags($activity->name);
595 $fulldescribe=get_string('progress-title','completion',$a);
596
597 if($csv) {
598 print $sep.csv_quote($describe).$sep.csv_quote($date);
599 } else {
600 print '<td class="completion-progresscell">';
601
602 print '<img src="'.$OUTPUT->pix_url('i/'.$completionicon).
603 '" alt="'.$describe.'" class="icon" title="'.$fulldescribe.'" />';
604
605 print '</td>';
606 }
607
608 continue;
609 }
610
611 // Handle all other criteria
612 $criteria_completion = $completion->get_user_completion($user->id, $criterion);
613 $is_complete = $criteria_completion->is_complete();
614
615 $completiontype = $is_complete ? 'y' : 'n';
616 $completionicon = 'completion-auto-'.$completiontype;
617
618 $describe = get_string('completion-alt-auto-'.$completiontype, 'completion');
619
fbaea88f 620 $a = new stdClass();
2be4d090
MD
621 $a->state = $describe;
622 $a->date = $is_complete ? userdate($criteria_completion->timecompleted) : '';
623 $a->user = fullname($user);
624 $a->activity = strip_tags($criterion->get_title());
625 $fulldescribe = get_string('progress-title', 'completion', $a);
626
627 if ($csv) {
628 print $sep.csv_quote($describe);
629 } else {
630
631 if ($allow_marking_criteria === $criterion->id) {
632 $describe = get_string('completion-alt-auto-'.$completiontype,'completion');
633
634 print '<td class="completion-progresscell">'.
9cedb80c 635 '<a href="'.$CFG->wwwroot.'/course/togglecompletion.php?user='.$user->id.'&amp;course='.$course->id.'&amp;rolec='.$allow_marking_criteria.'&amp;sesskey='.sesskey().'">'.
2be4d090 636 '<img src="'.$OUTPUT->pix_url('i/completion-manual-'.($is_complete ? 'y' : 'n')).
f8e9c93a 637 '" alt="'.$describe.'" class="icon" title="Mark as complete" /></a></td>'; //TODO: localize
2be4d090
MD
638 } else {
639 print '<td class="completion-progresscell">'.
640 '<img src="'.$OUTPUT->pix_url('i/'.$completionicon).
641 '" alt="'.$describe.'" class="icon" title="'.$fulldescribe.'" /></td>';
642 }
643 }
644 }
645
646 // Handle overall course completion
647
648 // Load course completion
649 $params = array(
650 'userid' => $user->id,
651 'course' => $course->id
652 );
653
654 $ccompletion = new completion_completion($params);
655 $completiontype = $ccompletion->is_complete() ? 'y' : 'n';
656
657 $describe = get_string('completion-alt-auto-'.$completiontype, 'completion');
658
659 $a = new StdClass;
660 $a->state = $describe;
661 $a->date = '';
662 $a->user = fullname($user);
663 $a->activity = strip_tags(get_string('coursecomplete', 'completion'));
664 $fulldescribe = get_string('progress-title', 'completion', $a);
665
666 if ($csv) {
667 print $sep.csv_quote($describe);
668 } else {
669
670 print '<td class="completion-progresscell">';
671
672 // Display course completion status icon
673 print '<img src="'.$OUTPUT->pix_url('i/completion-auto-'.$completiontype).
674 '" alt="'.$describe.'" class="icon" title="'.$fulldescribe.'" />';
675
676 print '</td>';
677 }
678
679 if($csv) {
680 print $line;
681 } else {
682 print '</tr>';
683 }
684}
685
686if($csv) {
687 exit;
688}
689print '</table>';
690print $pagingbar;
691
692print '<ul class="progress-actions"><li><a href="index.php?course='.$course->id.
693 '&amp;format=csv">'.get_string('csvdownload','completion').'</a></li>
694 <li><a href="index.php?course='.$course->id.'&amp;format=excelcsv">'.
695 get_string('excelcsvdownload','completion').'</a></li></ul>';
696
697echo $OUTPUT->footer($course);