3 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
20 * Course completion progress report
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
27 require_once('../../../config.php');
28 require_once($CFG->libdir.'/completionlib.php');
33 define('COMPLETION_REPORT_PAGE', 25);
34 define('COMPLETION_REPORT_COL_TITLES', true);
37 * Setup page, check permissions
41 $courseid = required_param('course', PARAM_INT);
42 $format = optional_param('format','',PARAM_ALPHA);
43 $sort = optional_param('sort','',PARAM_ALPHA);
44 $edituser = optional_param('edituser', 0, PARAM_INT);
47 $course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);
49 $url = new moodle_url('/course/report/completion/index.php', array('course'=>$course->id));
51 $PAGE->set_pagelayout('standard');
53 $firstnamesort = ($sort == 'firstname');
54 $excel = ($format == 'excelcsv');
55 $csv = ($format == 'csv' || $excel);
58 $start = optional_param('start', 0, PARAM_INT);
59 $sifirst = optional_param('sifirst', 'all', PARAM_ALPHA);
60 $silast = optional_param('silast', 'all', PARAM_ALPHA);
62 // Whether to show idnumber
63 $idnumbers = $CFG->grade_report_showuseridnumber;
65 // Function for quoting csv cell values
66 function csv_quote($value) {
69 $tl=textlib_get_instance();
70 return $tl->convert('"'.str_replace('"',"'",$value).'"','UTF-8','UTF-16LE');
72 return '"'.str_replace('"',"'",$value).'"';
78 require_login($course);
80 $context=get_context_instance(CONTEXT_COURSE, $course->id);
81 require_capability('coursereport/completion:view', $context);
84 $group = groups_get_course_group($course, true); // Supposed to verify group
85 if($group === 0 && $course->groupmode == SEPARATEGROUPS) {
86 require_capability('moodle/site:accessallgroups',$context);
93 // Get criteria for course
94 $completion = new completion_info($course);
96 if (!$completion->has_criteria()) {
97 print_error('err_nocriteria', 'completion', $CFG->wwwroot.'/course/report.php?id='.$course->id);
100 // Get criteria and put in correct order
103 foreach ($completion->get_criteria(COMPLETION_CRITERIA_TYPE_COURSE) as $criterion) {
104 $criteria[] = $criterion;
107 foreach ($completion->get_criteria(COMPLETION_CRITERIA_TYPE_ACTIVITY) as $criterion) {
108 $criteria[] = $criterion;
111 foreach ($completion->get_criteria() as $criterion) {
112 if (!in_array($criterion->criteriatype, array(
113 COMPLETION_CRITERIA_TYPE_COURSE, COMPLETION_CRITERIA_TYPE_ACTIVITY))) {
114 $criteria[] = $criterion;
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;
125 $rcriteria = $completion->get_criteria(COMPLETION_CRITERIA_TYPE_ROLE);
127 if (!empty($rcriteria)) {
129 foreach ($rcriteria as $rcriterion) {
130 $users = get_role_users($rcriterion->role, $context, true);
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;
146 header('Content-Disposition: attachment; filename=progress.'.
147 preg_replace('/[^a-z0-9-]/','_',strtolower($course->shortname)).'.csv');
148 // Unicode byte-order mark for Excel
150 header('Content-Type: text/csv; charset=UTF-16LE');
151 print chr(0xFF).chr(0xFE);
155 header('Content-Type: text/csv; charset=UTF-8');
161 // Navigation and header
162 $strcompletion = get_string('completionreport','completion');
164 $PAGE->set_title($strcompletion);
165 $PAGE->set_heading($course->fullname);
167 echo $OUTPUT->header();
169 $PAGE->requires->yui2_lib(
178 $PAGE->requires->js('/course/report/completion/textrotate.js');
180 // Handle groups (if enabled)
181 groups_print_course_menu($course, $CFG->wwwroot.'/course/report/completion/?course='.$course->id);
185 // Generate where clause
187 $ilike = $DB->sql_ilike();
189 if ($sifirst !== 'all') {
190 $where[] = "u.firstname $ilike '$sifirst%'";
193 if ($silast !== 'all') {
194 $where[] = "u.lastname $ilike '$silast%'";
197 // Get user match count
198 $total = $completion->get_num_tracked_users(implode(' AND ', $where), $group);
201 $grandtotal = $completion->get_num_tracked_users('', $group);
203 // If no users in this course what-so-ever
205 echo $OUTPUT->container(get_string('err_nousers', 'completion'), 'errorbox errorboxcontent');
206 echo $OUTPUT->footer();
214 $progress = $completion->get_progress_all(
215 implode(' AND ', $where),
217 $firstnamesort ? 'u.firstname ASC' : 'u.lastname ASC',
218 $csv ? 0 : COMPLETION_REPORT_PAGE,
224 // Build link for paging
225 $link = $CFG->wwwroot.'/course/report/completion/?course='.$course->id;
227 $link .= '&sort='.$sort;
229 $link .= '&start=';
231 // Build the the page by Initial bar
232 $initials = array('first', 'last');
233 $alphabet = explode(',', get_string('alphabet', 'langconfig'));
236 foreach ($initials as $initial) {
237 $var = 'si'.$initial;
239 $pagingbar .= ' <div class="initialbar '.$initial.'initial">';
240 $pagingbar .= get_string($initial.'name').': ';
242 if ($$var == 'all') {
243 $pagingbar .= '<strong>'.get_string('all').'</strong> ';
246 $pagingbar .= '<a href="'.$link.'">'.get_string('all').'</a> ';
249 foreach ($alphabet as $letter) {
250 if ($$var === $letter) {
251 $pagingbar .= '<strong>'.$letter.'</strong> ';
254 $pagingbar .= '<a href="'.$link.'&'.$var.'='.$letter.'">'.$letter.'</a> ';
258 $pagingbar .= '</div>';
261 // Do we need a paging bar?
262 if($total > COMPLETION_REPORT_PAGE) {
265 $pagingbar .= '<div class="paging">';
266 $pagingbar .= get_string('page').': ';
268 // Display previous link
270 $pstart = max($start - COMPLETION_REPORT_PAGE, 0);
271 $pagingbar .= '(<a class="previous" href="'.$link.$pstart.'">'.get_string('previous').'</a>) ';
277 while ($curstart < $total) {
280 if ($curstart == $start) {
281 $pagingbar .= ' '.$curpage.' ';
284 $pagingbar .= ' <a href="'.$link.$curstart.'">'.$curpage.'</a> ';
287 $curstart += COMPLETION_REPORT_PAGE;
291 $nstart = $start + COMPLETION_REPORT_PAGE;
292 if ($nstart < $total) {
293 $pagingbar .= ' (<a class="next" href="'.$link.$nstart.'">'.get_string('next').'</a>)';
296 $pagingbar .= '</div>';
306 print '<br class="clearer"/>'; // ugh
308 $total_header = ($total == $grandtotal) ? $total : "{$total}/{$grandtotal}";
309 echo $OUTPUT->heading(get_string('allparticipants').": {$total_header}", 3);
314 echo $OUTPUT->heading(get_string('nothingtodisplay'), 2);
315 echo $OUTPUT->footer();
319 print '<table id="completion-progress" class="generaltable flexible boxaligncenter completionreport" style="text-align: left" cellpadding="5" border="1">';
321 // Print criteria group names
322 print PHP_EOL.'<tr style="vertical-align: top">';
323 print '<th scope="row" class="rowheader">'.get_string('criteriagroup', 'completion').'</th>';
325 $current_group = false;
327 for ($i = 0; $i <= count($criteria); $i++) {
329 if (isset($criteria[$i])) {
330 $criterion = $criteria[$i];
332 if ($current_group && $criterion->criteriatype === $current_group->criteriatype) {
340 print '<th scope="col" colspan="'.$col_count.'" class="colheader criteriagroup">'.$current_group->get_type_title().'</th>';
343 if (isset($criteria[$i])) {
344 // Move to next criteria type
345 $current_group = $criterion;
350 // Overall course completion status
351 print '<th style="text-align: center;">'.get_string('course').'</th>';
355 // Print aggregation methods
356 print PHP_EOL.'<tr style="vertical-align: top">';
357 print '<th scope="row" class="rowheader">'.get_string('aggregationmethod', 'completion').'</th>';
359 $current_group = false;
361 for ($i = 0; $i <= count($criteria); $i++) {
363 if (isset($criteria[$i])) {
364 $criterion = $criteria[$i];
366 if ($current_group && $criterion->criteriatype === $current_group->criteriatype) {
375 COMPLETION_CRITERIA_TYPE_COURSE,
376 COMPLETION_CRITERIA_TYPE_ACTIVITY,
377 COMPLETION_CRITERIA_TYPE_ROLE,
380 if (in_array($current_group->criteriatype, $has_agg)) {
381 // Try load a aggregation method
382 $method = $completion->get_aggregation_method($current_group->criteriatype);
384 $method = $method == 1 ? 'All' : 'Any';
390 print '<th scope="col" colspan="'.$col_count.'" class="colheader aggheader">'.$method.'</th>';
393 if (isset($criteria[$i])) {
394 // Move to next criteria type
395 $current_group = $criterion;
400 // Overall course aggregation method
401 print '<th scope="col" class="colheader aggheader aggcriteriacourse">';
403 // Get course aggregation
404 $method = $completion->get_aggregation_method();
406 print $method == 1 ? 'All' : 'Any';
412 // Print criteria titles
413 if (COMPLETION_REPORT_COL_TITLES) {
415 print PHP_EOL.'<tr>';
416 print '<th scope="row" class="rowheader">'.get_string('criteria', 'completion').'</th>';
418 foreach ($criteria as $criterion) {
419 // Get criteria details
420 $details = $criterion->get_title_detailed();
421 print '<th scope="col" class="colheader criterianame">';
422 print '<span class="completion-criterianame">'.$details.'</span>';
426 // Overall course completion status
427 print '<th scope="col" class="colheader criterianame">';
429 print '<span class="completion-criterianame">'.get_string('coursecomplete', 'completion').'</span>';
434 // Print user heading and icons
437 // User heading / sort option
438 print '<th scope="col" class="completion-sortchoice" style="clear: both;">';
441 get_string('firstname').' / <a href="./?course='.$course->id.'">'.
442 get_string('lastname').'</a>';
444 print '<a href="./?course='.$course->id.'&sort=firstname">'.
445 get_string('firstname').'</a> / '.
446 get_string('lastname');
451 // Print user id number column
453 print '<th>'.get_string('idnumber').'</th>';
457 /// Print criteria icons
459 foreach ($criteria as $criterion) {
461 // Generate icon details
464 $icontitle = ''; // Required if $iconlink set
465 $iconalt = ''; // Required
466 switch ($criterion->criteriatype) {
468 case COMPLETION_CRITERIA_TYPE_ACTIVITY:
470 $activity = $criterion->get_mod_instance();
473 $icon = $OUTPUT->pix_url('icon', $criterion->module);
474 $iconlink = $CFG->wwwroot.'/mod/'.$criterion->module.'/view.php?id='.$activity->id;
475 $icontitle = $activity->name;
476 $iconalt = get_string('modulename', $criterion->module);
479 case COMPLETION_CRITERIA_TYPE_COURSE:
481 $crs = $DB->get_record('course', array('id' => $criterion->courseinstance));
484 $iconlink = $CFG->wwwroot.'/course/view.php?id='.$criterion->courseinstance;
485 $icontitle = $crs->fullname;
486 $iconalt = $crs->shortname;
489 case COMPLETION_CRITERIA_TYPE_ROLE:
491 $role = $DB->get_record('role', array('id' => $criterion->role));
494 $iconalt = $role->name;
498 // Print icon and cell
499 print '<th class="criteriaicon">';
501 // Create icon if not supplied
503 $icon = $OUTPUT->pix_url('i/'.$COMPLETION_CRITERIA_TYPES[$criterion->criteriatype]);
506 print ($iconlink ? '<a href="'.$iconlink.'" title="'.$icontitle.'">' : '');
507 print '<img src="'.$icon.'" class="icon" alt="'.$iconalt.'" '.(!$iconlink ? 'title="'.$iconalt.'"' : '').' />';
508 print ($iconlink ? '</a>' : '');
513 // Overall course completion status
514 print '<th class="criteriaicon">';
515 print '<img src="'.$OUTPUT->pix_url('i/course').'" class="icon" alt="Course" title="Course Complete" />'; //TODO: localize
530 /// Display a row for each user
532 foreach ($progress as $user) {
536 print csv_quote(fullname($user));
538 print $sep.csv_quote($user->idnumber);
541 print PHP_EOL.'<tr id="user-'.$user->id.'">';
543 print '<th scope="row"><a href="'.$CFG->wwwroot.'/user/view.php?id='.
544 $user->id.'&course='.$course->id.'">'.fullname($user).'</a></th>';
546 print '<td>'.htmlspecialchars($user->idnumber).'</td>';
550 // Progress for each course completion criteria
551 foreach ($criteria as $criterion) {
553 // Handle activity completion differently
554 if ($criterion->criteriatype == COMPLETION_CRITERIA_TYPE_ACTIVITY) {
557 $mod = $criterion->get_mod_instance();
558 $activity = $DB->get_record('course_modules', array('id' => $criterion->moduleinstance));
559 $activity->name = $mod->name;
562 // Get progress information and state
563 if(array_key_exists($activity->id,$user->progress)) {
564 $thisprogress=$user->progress[$activity->id];
565 $state=$thisprogress->completionstate;
566 $date=userdate($thisprogress->timemodified);
568 $state=COMPLETION_INCOMPLETE;
572 $criteria_completion = $completion->get_user_completion($user->id, $criterion);
574 // Work out how it corresponds to an icon
576 case COMPLETION_INCOMPLETE : $completiontype='n'; break;
577 case COMPLETION_COMPLETE : $completiontype='y'; break;
578 case COMPLETION_COMPLETE_PASS : $completiontype='pass'; break;
579 case COMPLETION_COMPLETE_FAIL : $completiontype='fail'; break;
582 $completionicon='completion-'.
583 ($activity->completion==COMPLETION_TRACKING_AUTOMATIC ? 'auto' : 'manual').
586 $describe=get_string('completion-alt-auto-'.$completiontype,'completion');
590 $a->user=fullname($user);
591 $a->activity=strip_tags($activity->name);
592 $fulldescribe=get_string('progress-title','completion',$a);
595 print $sep.csv_quote($describe).$sep.csv_quote($date);
597 print '<td class="completion-progresscell">';
599 print '<img src="'.$OUTPUT->pix_url('i/'.$completionicon).
600 '" alt="'.$describe.'" class="icon" title="'.$fulldescribe.'" />';
608 // Handle all other criteria
609 $criteria_completion = $completion->get_user_completion($user->id, $criterion);
610 $is_complete = $criteria_completion->is_complete();
612 $completiontype = $is_complete ? 'y' : 'n';
613 $completionicon = 'completion-auto-'.$completiontype;
615 $describe = get_string('completion-alt-auto-'.$completiontype, 'completion');
618 $a->state = $describe;
619 $a->date = $is_complete ? userdate($criteria_completion->timecompleted) : '';
620 $a->user = fullname($user);
621 $a->activity = strip_tags($criterion->get_title());
622 $fulldescribe = get_string('progress-title', 'completion', $a);
625 print $sep.csv_quote($describe);
628 if ($allow_marking_criteria === $criterion->id) {
629 $describe = get_string('completion-alt-auto-'.$completiontype,'completion');
631 print '<td class="completion-progresscell">'.
632 '<a href="'.$CFG->wwwroot.'/course/togglecompletion.php?user='.$user->id.'&course='.$course->id.'&rolec='.$allow_marking_criteria.'">'.
633 '<img src="'.$OUTPUT->pix_url('i/completion-manual-'.($is_complete ? 'y' : 'n')).
634 '" alt="'.$describe.'" class="icon" title="Mark as complete" /></a></td>'; //TODO: localize
636 print '<td class="completion-progresscell">'.
637 '<img src="'.$OUTPUT->pix_url('i/'.$completionicon).
638 '" alt="'.$describe.'" class="icon" title="'.$fulldescribe.'" /></td>';
643 // Handle overall course completion
645 // Load course completion
647 'userid' => $user->id,
648 'course' => $course->id
651 $ccompletion = new completion_completion($params);
652 $completiontype = $ccompletion->is_complete() ? 'y' : 'n';
654 $describe = get_string('completion-alt-auto-'.$completiontype, 'completion');
657 $a->state = $describe;
659 $a->user = fullname($user);
660 $a->activity = strip_tags(get_string('coursecomplete', 'completion'));
661 $fulldescribe = get_string('progress-title', 'completion', $a);
664 print $sep.csv_quote($describe);
667 print '<td class="completion-progresscell">';
669 // Display course completion status icon
670 print '<img src="'.$OUTPUT->pix_url('i/completion-auto-'.$completiontype).
671 '" alt="'.$describe.'" class="icon" title="'.$fulldescribe.'" />';
689 print '<ul class="progress-actions"><li><a href="index.php?course='.$course->id.
690 '&format=csv">'.get_string('csvdownload','completion').'</a></li>
691 <li><a href="index.php?course='.$course->id.'&format=excelcsv">'.
692 get_string('excelcsvdownload','completion').'</a></li></ul>';
694 echo $OUTPUT->footer($course);