2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 * Course completion progress report
21 * @subpackage completion
22 * @copyright 2009 Catalyst IT Ltd
23 * @author Aaron Barnes <aaronb@catalyst.net.nz>
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 require('../../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);
48 $context = context_course::instance($course->id);
50 $url = new moodle_url('/report/completion/index.php', array('course'=>$course->id));
52 $PAGE->set_pagelayout('report');
54 $firstnamesort = ($sort == 'firstname');
55 $excel = ($format == 'excelcsv');
56 $csv = ($format == 'csv' || $excel);
59 $start = optional_param('start', 0, PARAM_INT);
60 $sifirst = optional_param('sifirst', 'all', PARAM_ALPHA);
61 $silast = optional_param('silast', 'all', PARAM_ALPHA);
63 // Whether to show extra user identity information
64 $extrafields = get_extra_user_fields($context);
65 $leftcols = 1 + count($extrafields);
67 // Function for quoting csv cell values
68 function csv_quote($value) {
71 $tl = textlib_get_instance();
72 return $tl->convert('"'.str_replace('"',"'",$value).'"','UTF-8','UTF-16LE');
74 return '"'.str_replace('"',"'",$value).'"';
80 require_login($course);
82 require_capability('report/completion:view', $context);
85 $group = groups_get_course_group($course, true); // Supposed to verify group
86 if ($group === 0 && $course->groupmode == SEPARATEGROUPS) {
87 require_capability('moodle/site:accessallgroups',$context);
94 // Get criteria for course
95 $completion = new completion_info($course);
97 if (!$completion->has_criteria()) {
98 print_error('err_nocriteria', 'completion', $CFG->wwwroot.'/course/report.php?id='.$course->id);
101 // Get criteria and put in correct order
104 foreach ($completion->get_criteria(COMPLETION_CRITERIA_TYPE_COURSE) as $criterion) {
105 $criteria[] = $criterion;
108 foreach ($completion->get_criteria(COMPLETION_CRITERIA_TYPE_ACTIVITY) as $criterion) {
109 $criteria[] = $criterion;
112 foreach ($completion->get_criteria() as $criterion) {
113 if (!in_array($criterion->criteriatype, array(
114 COMPLETION_CRITERIA_TYPE_COURSE, COMPLETION_CRITERIA_TYPE_ACTIVITY))) {
115 $criteria[] = $criterion;
119 // Can logged in user mark users as complete?
120 // (if the logged in user has a role defined in the role criteria)
121 $allow_marking = false;
122 $allow_marking_criteria = null;
126 $rcriteria = $completion->get_criteria(COMPLETION_CRITERIA_TYPE_ROLE);
128 if (!empty($rcriteria)) {
130 foreach ($rcriteria as $rcriterion) {
131 $users = get_role_users($rcriterion->role, $context, true);
133 // If logged in user has this role, allow marking complete
134 if ($users && in_array($USER->id, array_keys($users))) {
135 $allow_marking = true;
136 $allow_marking_criteria = $rcriterion->id;
147 $shortname = format_string($course->shortname, true, array('context' => $context));
148 $textlib = textlib_get_instance();
149 header('Content-Disposition: attachment; filename=progress.'.
150 preg_replace('/[^a-z0-9-]/','_',$textlib->strtolower(strip_tags($shortname))).'.csv');
151 // Unicode byte-order mark for Excel
153 header('Content-Type: text/csv; charset=UTF-16LE');
154 print chr(0xFF).chr(0xFE);
158 header('Content-Type: text/csv; charset=UTF-8');
164 // Navigation and header
165 $strcompletion = get_string('coursecompletion');
167 $PAGE->set_title($strcompletion);
168 $PAGE->set_heading($course->fullname);
170 echo $OUTPUT->header();
172 $PAGE->requires->yui2_lib(
181 $PAGE->requires->js('/report/completion/textrotate.js');
183 // Handle groups (if enabled)
184 groups_print_course_menu($course, $CFG->wwwroot.'/report/completion/?course='.$course->id);
188 // Generate where clause
190 $where_params = array();
192 if ($sifirst !== 'all') {
193 $where[] = $DB->sql_like('u.firstname', ':sifirst', false);
194 $where_params['sifirst'] = $sifirst.'%';
197 if ($silast !== 'all') {
198 $where[] = $DB->sql_like('u.lastname', ':silast', false);
199 $where_params['silast'] = $silast.'%';
202 // Get user match count
203 $total = $completion->get_num_tracked_users(implode(' AND ', $where), $where_params, $group);
206 $grandtotal = $completion->get_num_tracked_users('', array(), $group);
208 // If no users in this course what-so-ever
210 echo $OUTPUT->container(get_string('err_nousers', 'completion'), 'errorbox errorboxcontent');
211 echo $OUTPUT->footer();
219 $progress = $completion->get_progress_all(
220 implode(' AND ', $where),
223 $firstnamesort ? 'u.firstname ASC' : 'u.lastname ASC',
224 $csv ? 0 : COMPLETION_REPORT_PAGE,
231 // Build link for paging
232 $link = $CFG->wwwroot.'/report/completion/?course='.$course->id;
234 $link .= '&sort='.$sort;
236 $link .= '&start=';
238 // Build the the page by Initial bar
239 $initials = array('first', 'last');
240 $alphabet = explode(',', get_string('alphabet', 'langconfig'));
243 foreach ($initials as $initial) {
244 $var = 'si'.$initial;
246 $othervar = $initial == 'first' ? 'silast' : 'sifirst';
247 $othervar = $$othervar != 'all' ? "&{$othervar}={$$othervar}" : '';
249 $pagingbar .= ' <div class="initialbar '.$initial.'initial">';
250 $pagingbar .= get_string($initial.'name').': ';
252 if ($$var == 'all') {
253 $pagingbar .= '<strong>'.get_string('all').'</strong> ';
256 $pagingbar .= "<a href=\"{$link}{$othervar}\">".get_string('all').'</a> ';
259 foreach ($alphabet as $letter) {
260 if ($$var === $letter) {
261 $pagingbar .= '<strong>'.$letter.'</strong> ';
264 $pagingbar .= "<a href=\"$link&$var={$letter}{$othervar}\">$letter</a> ";
268 $pagingbar .= '</div>';
271 // Do we need a paging bar?
272 if ($total > COMPLETION_REPORT_PAGE) {
275 $pagingbar .= '<div class="paging">';
276 $pagingbar .= get_string('page').': ';
278 $sistrings = array();
279 if ($sifirst != 'all') {
280 $sistrings[] = "sifirst={$sifirst}";
282 if ($silast != 'all') {
283 $sistrings[] = "silast={$silast}";
285 $sistring = !empty($sistrings) ? '&'.implode('&', $sistrings) : '';
287 // Display previous link
289 $pstart = max($start - COMPLETION_REPORT_PAGE, 0);
290 $pagingbar .= "(<a class=\"previous\" href=\"{$link}{$pstart}{$sistring}\">".get_string('previous').'</a>) ';
296 while ($curstart < $total) {
299 if ($curstart == $start) {
300 $pagingbar .= ' '.$curpage.' ';
303 $pagingbar .= " <a href=\"{$link}{$curstart}{$sistring}\">$curpage</a> ";
306 $curstart += COMPLETION_REPORT_PAGE;
310 $nstart = $start + COMPLETION_REPORT_PAGE;
311 if ($nstart < $total) {
312 $pagingbar .= " (<a class=\"next\" href=\"{$link}{$nstart}{$sistring}\">".get_string('next').'</a>)';
315 $pagingbar .= '</div>';
325 print '<br class="clearer"/>'; // ugh
327 $total_header = ($total == $grandtotal) ? $total : "{$total}/{$grandtotal}";
328 echo $OUTPUT->heading(get_string('allparticipants').": {$total_header}", 3);
333 echo $OUTPUT->heading(get_string('nothingtodisplay'), 2);
334 echo $OUTPUT->footer();
338 print '<table id="completion-progress" class="generaltable flexible boxaligncenter completionreport" style="text-align: left" cellpadding="5" border="1">';
340 // Print criteria group names
341 print PHP_EOL.'<tr style="vertical-align: top">';
342 echo '<th scope="row" class="rowheader" colspan="' . $leftcols . '">' .
343 get_string('criteriagroup', 'completion') . '</th>';
345 $current_group = false;
347 for ($i = 0; $i <= count($criteria); $i++) {
349 if (isset($criteria[$i])) {
350 $criterion = $criteria[$i];
352 if ($current_group && $criterion->criteriatype === $current_group->criteriatype) {
360 print '<th scope="col" colspan="'.$col_count.'" class="colheader criteriagroup">'.$current_group->get_type_title().'</th>';
363 if (isset($criteria[$i])) {
364 // Move to next criteria type
365 $current_group = $criterion;
370 // Overall course completion status
371 print '<th style="text-align: center;">'.get_string('course').'</th>';
375 // Print aggregation methods
376 print PHP_EOL.'<tr style="vertical-align: top">';
377 echo '<th scope="row" class="rowheader" colspan="' . $leftcols . '">' .
378 get_string('aggregationmethod', 'completion').'</th>';
380 $current_group = false;
382 for ($i = 0; $i <= count($criteria); $i++) {
384 if (isset($criteria[$i])) {
385 $criterion = $criteria[$i];
387 if ($current_group && $criterion->criteriatype === $current_group->criteriatype) {
396 COMPLETION_CRITERIA_TYPE_COURSE,
397 COMPLETION_CRITERIA_TYPE_ACTIVITY,
398 COMPLETION_CRITERIA_TYPE_ROLE,
401 if (in_array($current_group->criteriatype, $has_agg)) {
402 // Try load a aggregation method
403 $method = $completion->get_aggregation_method($current_group->criteriatype);
405 $method = $method == 1 ? get_string('all') : get_string('any');
411 print '<th scope="col" colspan="'.$col_count.'" class="colheader aggheader">'.$method.'</th>';
414 if (isset($criteria[$i])) {
415 // Move to next criteria type
416 $current_group = $criterion;
421 // Overall course aggregation method
422 print '<th scope="col" class="colheader aggheader aggcriteriacourse">';
424 // Get course aggregation
425 $method = $completion->get_aggregation_method();
427 print $method == 1 ? get_string('all') : get_string('any');
433 // Print criteria titles
434 if (COMPLETION_REPORT_COL_TITLES) {
436 print PHP_EOL.'<tr>';
437 echo '<th scope="row" class="rowheader" colspan="' . $leftcols . '">' .
438 get_string('criteria', 'completion') . '</th>';
440 foreach ($criteria as $criterion) {
441 // Get criteria details
442 $details = $criterion->get_title_detailed();
443 print '<th scope="col" class="colheader criterianame">';
444 print '<span class="completion-criterianame">'.$details.'</span>';
448 // Overall course completion status
449 print '<th scope="col" class="colheader criterianame">';
451 print '<span class="completion-criterianame">'.get_string('coursecomplete', 'completion').'</span>';
456 // Print user heading and icons
459 // User heading / sort option
460 print '<th scope="col" class="completion-sortchoice" style="clear: both;">';
462 $sistring = "&silast={$silast}&sifirst={$sifirst}";
464 if ($firstnamesort) {
466 get_string('firstname')." / <a href=\"./?course={$course->id}{$sistring}\">".
467 get_string('lastname').'</a>';
469 print "<a href=\"./?course={$course->id}&sort=firstname{$sistring}\">".
470 get_string('firstname').'</a> / '.
471 get_string('lastname');
476 // Print user identity columns
477 foreach ($extrafields as $field) {
478 echo '<th scope="col" class="completion-identifyfield">' .
479 get_user_field_name($field) . '</th>';
483 /// Print criteria icons
485 foreach ($criteria as $criterion) {
487 // Generate icon details
490 $icontitle = ''; // Required if $iconlink set
491 $iconalt = ''; // Required
492 switch ($criterion->criteriatype) {
494 case COMPLETION_CRITERIA_TYPE_ACTIVITY:
496 $activity = $criterion->get_mod_instance();
499 $icon = $OUTPUT->pix_url('icon', $criterion->module);
500 $iconlink = $CFG->wwwroot.'/mod/'.$criterion->module.'/view.php?id='.$criterion->moduleinstance;
501 $icontitle = $activity->name;
502 $iconalt = get_string('modulename', $criterion->module);
505 case COMPLETION_CRITERIA_TYPE_COURSE:
507 $crs = $DB->get_record('course', array('id' => $criterion->courseinstance));
510 $iconlink = $CFG->wwwroot.'/course/view.php?id='.$criterion->courseinstance;
511 $icontitle = format_string($crs->fullname, true, array('context' => get_context_instance(CONTEXT_COURSE, $crs->id, MUST_EXIST)));
512 $iconalt = format_string($crs->shortname, true, array('context' => get_context_instance(CONTEXT_COURSE, $crs->id)));
515 case COMPLETION_CRITERIA_TYPE_ROLE:
517 $role = $DB->get_record('role', array('id' => $criterion->role));
520 $iconalt = $role->name;
524 // Print icon and cell
525 print '<th class="criteriaicon">';
527 // Create icon if not supplied
529 $icon = $OUTPUT->pix_url('i/'.$COMPLETION_CRITERIA_TYPES[$criterion->criteriatype]);
532 print ($iconlink ? '<a href="'.$iconlink.'" title="'.$icontitle.'">' : '');
533 print '<img src="'.$icon.'" class="icon" alt="'.$iconalt.'" '.(!$iconlink ? 'title="'.$iconalt.'"' : '').' />';
534 print ($iconlink ? '</a>' : '');
539 // Overall course completion status
540 print '<th class="criteriaicon">';
541 print '<img src="'.$OUTPUT->pix_url('i/course').'" class="icon" alt="'.get_string('course').'" title="'.get_string('coursecomplete', 'completion').'" />';
548 // The CSV file does not contain any headers
553 /// Display a row for each user
555 foreach ($progress as $user) {
559 print csv_quote(fullname($user));
560 foreach ($extrafields as $field) {
561 echo $sep . csv_quote($user->{$field});
564 print PHP_EOL.'<tr id="user-'.$user->id.'">';
566 print '<th scope="row"><a href="'.$CFG->wwwroot.'/user/view.php?id='.
567 $user->id.'&course='.$course->id.'">'.fullname($user).'</a></th>';
568 foreach ($extrafields as $field) {
569 echo '<td>' . s($user->{$field}) . '</td>';
573 // Progress for each course completion criteria
574 foreach ($criteria as $criterion) {
576 // Handle activity completion differently
577 if ($criterion->criteriatype == COMPLETION_CRITERIA_TYPE_ACTIVITY) {
580 $mod = $criterion->get_mod_instance();
581 $activity = $DB->get_record('course_modules', array('id' => $criterion->moduleinstance));
582 $activity->name = $mod->name;
585 // Get progress information and state
586 if (array_key_exists($activity->id,$user->progress)) {
587 $thisprogress=$user->progress[$activity->id];
588 $state=$thisprogress->completionstate;
589 $date=userdate($thisprogress->timemodified);
591 $state=COMPLETION_INCOMPLETE;
595 $criteria_completion = $completion->get_user_completion($user->id, $criterion);
597 // Work out how it corresponds to an icon
599 case COMPLETION_INCOMPLETE : $completiontype='n'; break;
600 case COMPLETION_COMPLETE : $completiontype='y'; break;
601 case COMPLETION_COMPLETE_PASS : $completiontype='pass'; break;
602 case COMPLETION_COMPLETE_FAIL : $completiontype='fail'; break;
605 $completionicon='completion-'.
606 ($activity->completion==COMPLETION_TRACKING_AUTOMATIC ? 'auto' : 'manual').
609 $describe=get_string('completion-alt-auto-'.$completiontype,'completion');
613 $a->user=fullname($user);
614 $a->activity=strip_tags($activity->name);
615 $fulldescribe=get_string('progress-title','completion',$a);
618 print $sep.csv_quote($describe).$sep.csv_quote($date);
620 print '<td class="completion-progresscell">';
622 print '<img src="'.$OUTPUT->pix_url('i/'.$completionicon).
623 '" alt="'.$describe.'" class="icon" title="'.$fulldescribe.'" />';
631 // Handle all other criteria
632 $criteria_completion = $completion->get_user_completion($user->id, $criterion);
633 $is_complete = $criteria_completion->is_complete();
635 $completiontype = $is_complete ? 'y' : 'n';
636 $completionicon = 'completion-auto-'.$completiontype;
638 $describe = get_string('completion-alt-auto-'.$completiontype, 'completion');
641 $a->state = $describe;
642 $a->date = $is_complete ? userdate($criteria_completion->timecompleted) : '';
643 $a->user = fullname($user);
644 $a->activity = strip_tags($criterion->get_title());
645 $fulldescribe = get_string('progress-title', 'completion', $a);
648 print $sep.csv_quote($describe);
651 if ($allow_marking_criteria === $criterion->id) {
652 $describe = get_string('completion-alt-auto-'.$completiontype,'completion');
654 print '<td class="completion-progresscell">'.
655 '<a href="'.$CFG->wwwroot.'/course/togglecompletion.php?user='.$user->id.'&course='.$course->id.'&rolec='.$allow_marking_criteria.'&sesskey='.sesskey().'">'.
656 '<img src="'.$OUTPUT->pix_url('i/completion-manual-'.($is_complete ? 'y' : 'n')).
657 '" alt="'.$describe.'" class="icon" title="'.get_string('markcomplete', 'completion').'" /></a></td>';
659 print '<td class="completion-progresscell">'.
660 '<img src="'.$OUTPUT->pix_url('i/'.$completionicon).
661 '" alt="'.$describe.'" class="icon" title="'.$fulldescribe.'" /></td>';
666 // Handle overall course completion
668 // Load course completion
670 'userid' => $user->id,
671 'course' => $course->id
674 $ccompletion = new completion_completion($params);
675 $completiontype = $ccompletion->is_complete() ? 'y' : 'n';
677 $describe = get_string('completion-alt-auto-'.$completiontype, 'completion');
680 $a->state = $describe;
682 $a->user = fullname($user);
683 $a->activity = strip_tags(get_string('coursecomplete', 'completion'));
684 $fulldescribe = get_string('progress-title', 'completion', $a);
687 print $sep.csv_quote($describe);
690 print '<td class="completion-progresscell">';
692 // Display course completion status icon
693 print '<img src="'.$OUTPUT->pix_url('i/completion-auto-'.$completiontype).
694 '" alt="'.$describe.'" class="icon" title="'.$fulldescribe.'" />';
712 print '<ul class="progress-actions"><li><a href="index.php?course='.$course->id.
713 '&format=csv">'.get_string('csvdownload','completion').'</a></li>
714 <li><a href="index.php?course='.$course->id.'&format=excelcsv">'.
715 get_string('excelcsvdownload','completion').'</a></li></ul>';
717 echo $OUTPUT->footer($course);