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('report');
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('coursecompletion');
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 $where_params = array();
189 if ($sifirst !== 'all') {
190 $where[] = $DB->sql_like('u.firstname', ':sifirst', false);
191 $where_params['sifirst'] = $sifirst.'%';
194 if ($silast !== 'all') {
195 $where[] = $DB->sql_like('u.lastname', ':silast', false);
196 $where_params['silast'] = $silast.'%';
199 // Get user match count
200 $total = $completion->get_num_tracked_users(implode(' AND ', $where), $where_params, $group);
203 $grandtotal = $completion->get_num_tracked_users('', array(), $group);
205 // If no users in this course what-so-ever
207 echo $OUTPUT->container(get_string('err_nousers', 'completion'), 'errorbox errorboxcontent');
208 echo $OUTPUT->footer();
216 $progress = $completion->get_progress_all(
217 implode(' AND ', $where),
220 $firstnamesort ? 'u.firstname ASC' : 'u.lastname ASC',
221 $csv ? 0 : COMPLETION_REPORT_PAGE,
227 // Build link for paging
228 $link = $CFG->wwwroot.'/course/report/completion/?course='.$course->id;
230 $link .= '&sort='.$sort;
232 $link .= '&start=';
234 // Build the the page by Initial bar
235 $initials = array('first', 'last');
236 $alphabet = explode(',', get_string('alphabet', 'langconfig'));
239 foreach ($initials as $initial) {
240 $var = 'si'.$initial;
242 $pagingbar .= ' <div class="initialbar '.$initial.'initial">';
243 $pagingbar .= get_string($initial.'name').': ';
245 if ($$var == 'all') {
246 $pagingbar .= '<strong>'.get_string('all').'</strong> ';
249 $pagingbar .= '<a href="'.$link.'">'.get_string('all').'</a> ';
252 foreach ($alphabet as $letter) {
253 if ($$var === $letter) {
254 $pagingbar .= '<strong>'.$letter.'</strong> ';
257 $pagingbar .= '<a href="'.$link.'&'.$var.'='.$letter.'">'.$letter.'</a> ';
261 $pagingbar .= '</div>';
264 // Do we need a paging bar?
265 if($total > COMPLETION_REPORT_PAGE) {
268 $pagingbar .= '<div class="paging">';
269 $pagingbar .= get_string('page').': ';
271 // Display previous link
273 $pstart = max($start - COMPLETION_REPORT_PAGE, 0);
274 $pagingbar .= '(<a class="previous" href="'.$link.$pstart.'">'.get_string('previous').'</a>) ';
280 while ($curstart < $total) {
283 if ($curstart == $start) {
284 $pagingbar .= ' '.$curpage.' ';
287 $pagingbar .= ' <a href="'.$link.$curstart.'">'.$curpage.'</a> ';
290 $curstart += COMPLETION_REPORT_PAGE;
294 $nstart = $start + COMPLETION_REPORT_PAGE;
295 if ($nstart < $total) {
296 $pagingbar .= ' (<a class="next" href="'.$link.$nstart.'">'.get_string('next').'</a>)';
299 $pagingbar .= '</div>';
309 print '<br class="clearer"/>'; // ugh
311 $total_header = ($total == $grandtotal) ? $total : "{$total}/{$grandtotal}";
312 echo $OUTPUT->heading(get_string('allparticipants').": {$total_header}", 3);
317 echo $OUTPUT->heading(get_string('nothingtodisplay'), 2);
318 echo $OUTPUT->footer();
322 print '<table id="completion-progress" class="generaltable flexible boxaligncenter completionreport" style="text-align: left" cellpadding="5" border="1">';
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>';
328 $current_group = false;
330 for ($i = 0; $i <= count($criteria); $i++) {
332 if (isset($criteria[$i])) {
333 $criterion = $criteria[$i];
335 if ($current_group && $criterion->criteriatype === $current_group->criteriatype) {
343 print '<th scope="col" colspan="'.$col_count.'" class="colheader criteriagroup">'.$current_group->get_type_title().'</th>';
346 if (isset($criteria[$i])) {
347 // Move to next criteria type
348 $current_group = $criterion;
353 // Overall course completion status
354 print '<th style="text-align: center;">'.get_string('course').'</th>';
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>';
362 $current_group = false;
364 for ($i = 0; $i <= count($criteria); $i++) {
366 if (isset($criteria[$i])) {
367 $criterion = $criteria[$i];
369 if ($current_group && $criterion->criteriatype === $current_group->criteriatype) {
378 COMPLETION_CRITERIA_TYPE_COURSE,
379 COMPLETION_CRITERIA_TYPE_ACTIVITY,
380 COMPLETION_CRITERIA_TYPE_ROLE,
383 if (in_array($current_group->criteriatype, $has_agg)) {
384 // Try load a aggregation method
385 $method = $completion->get_aggregation_method($current_group->criteriatype);
387 $method = $method == 1 ? 'All' : 'Any';
393 print '<th scope="col" colspan="'.$col_count.'" class="colheader aggheader">'.$method.'</th>';
396 if (isset($criteria[$i])) {
397 // Move to next criteria type
398 $current_group = $criterion;
403 // Overall course aggregation method
404 print '<th scope="col" class="colheader aggheader aggcriteriacourse">';
406 // Get course aggregation
407 $method = $completion->get_aggregation_method();
409 print $method == 1 ? 'All' : 'Any';
415 // Print criteria titles
416 if (COMPLETION_REPORT_COL_TITLES) {
418 print PHP_EOL.'<tr>';
419 print '<th scope="row" class="rowheader">'.get_string('criteria', 'completion').'</th>';
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>';
429 // Overall course completion status
430 print '<th scope="col" class="colheader criterianame">';
432 print '<span class="completion-criterianame">'.get_string('coursecomplete', 'completion').'</span>';
437 // Print user heading and icons
440 // User heading / sort option
441 print '<th scope="col" class="completion-sortchoice" style="clear: both;">';
444 get_string('firstname').' / <a href="./?course='.$course->id.'">'.
445 get_string('lastname').'</a>';
447 print '<a href="./?course='.$course->id.'&sort=firstname">'.
448 get_string('firstname').'</a> / '.
449 get_string('lastname');
454 // Print user id number column
456 print '<th>'.get_string('idnumber').'</th>';
460 /// Print criteria icons
462 foreach ($criteria as $criterion) {
464 // Generate icon details
467 $icontitle = ''; // Required if $iconlink set
468 $iconalt = ''; // Required
469 switch ($criterion->criteriatype) {
471 case COMPLETION_CRITERIA_TYPE_ACTIVITY:
473 $activity = $criterion->get_mod_instance();
476 $icon = $OUTPUT->pix_url('icon', $criterion->module);
477 $iconlink = $CFG->wwwroot.'/mod/'.$criterion->module.'/view.php?id='.$activity->moduleinstance;
478 $icontitle = $activity->name;
479 $iconalt = get_string('modulename', $criterion->module);
482 case COMPLETION_CRITERIA_TYPE_COURSE:
484 $crs = $DB->get_record('course', array('id' => $criterion->courseinstance));
487 $iconlink = $CFG->wwwroot.'/course/view.php?id='.$criterion->courseinstance;
488 $icontitle = $crs->fullname;
489 $iconalt = $crs->shortname;
492 case COMPLETION_CRITERIA_TYPE_ROLE:
494 $role = $DB->get_record('role', array('id' => $criterion->role));
497 $iconalt = $role->name;
501 // Print icon and cell
502 print '<th class="criteriaicon">';
504 // Create icon if not supplied
506 $icon = $OUTPUT->pix_url('i/'.$COMPLETION_CRITERIA_TYPES[$criterion->criteriatype]);
509 print ($iconlink ? '<a href="'.$iconlink.'" title="'.$icontitle.'">' : '');
510 print '<img src="'.$icon.'" class="icon" alt="'.$iconalt.'" '.(!$iconlink ? 'title="'.$iconalt.'"' : '').' />';
511 print ($iconlink ? '</a>' : '');
516 // Overall course completion status
517 print '<th class="criteriaicon">';
518 print '<img src="'.$OUTPUT->pix_url('i/course').'" class="icon" alt="Course" title="Course Complete" />'; //TODO: localize
533 /// Display a row for each user
535 foreach ($progress as $user) {
539 print csv_quote(fullname($user));
541 print $sep.csv_quote($user->idnumber);
544 print PHP_EOL.'<tr id="user-'.$user->id.'">';
546 print '<th scope="row"><a href="'.$CFG->wwwroot.'/user/view.php?id='.
547 $user->id.'&course='.$course->id.'">'.fullname($user).'</a></th>';
549 print '<td>'.htmlspecialchars($user->idnumber).'</td>';
553 // Progress for each course completion criteria
554 foreach ($criteria as $criterion) {
556 // Handle activity completion differently
557 if ($criterion->criteriatype == COMPLETION_CRITERIA_TYPE_ACTIVITY) {
560 $mod = $criterion->get_mod_instance();
561 $activity = $DB->get_record('course_modules', array('id' => $criterion->moduleinstance));
562 $activity->name = $mod->name;
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);
571 $state=COMPLETION_INCOMPLETE;
575 $criteria_completion = $completion->get_user_completion($user->id, $criterion);
577 // Work out how it corresponds to an icon
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;
585 $completionicon='completion-'.
586 ($activity->completion==COMPLETION_TRACKING_AUTOMATIC ? 'auto' : 'manual').
589 $describe=get_string('completion-alt-auto-'.$completiontype,'completion');
593 $a->user=fullname($user);
594 $a->activity=strip_tags($activity->name);
595 $fulldescribe=get_string('progress-title','completion',$a);
598 print $sep.csv_quote($describe).$sep.csv_quote($date);
600 print '<td class="completion-progresscell">';
602 print '<img src="'.$OUTPUT->pix_url('i/'.$completionicon).
603 '" alt="'.$describe.'" class="icon" title="'.$fulldescribe.'" />';
611 // Handle all other criteria
612 $criteria_completion = $completion->get_user_completion($user->id, $criterion);
613 $is_complete = $criteria_completion->is_complete();
615 $completiontype = $is_complete ? 'y' : 'n';
616 $completionicon = 'completion-auto-'.$completiontype;
618 $describe = get_string('completion-alt-auto-'.$completiontype, 'completion');
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);
628 print $sep.csv_quote($describe);
631 if ($allow_marking_criteria === $criterion->id) {
632 $describe = get_string('completion-alt-auto-'.$completiontype,'completion');
634 print '<td class="completion-progresscell">'.
635 '<a href="'.$CFG->wwwroot.'/course/togglecompletion.php?user='.$user->id.'&course='.$course->id.'&rolec='.$allow_marking_criteria.'&sesskey='.sesskey().'">'.
636 '<img src="'.$OUTPUT->pix_url('i/completion-manual-'.($is_complete ? 'y' : 'n')).
637 '" alt="'.$describe.'" class="icon" title="Mark as complete" /></a></td>'; //TODO: localize
639 print '<td class="completion-progresscell">'.
640 '<img src="'.$OUTPUT->pix_url('i/'.$completionicon).
641 '" alt="'.$describe.'" class="icon" title="'.$fulldescribe.'" /></td>';
646 // Handle overall course completion
648 // Load course completion
650 'userid' => $user->id,
651 'course' => $course->id
654 $ccompletion = new completion_completion($params);
655 $completiontype = $ccompletion->is_complete() ? 'y' : 'n';
657 $describe = get_string('completion-alt-auto-'.$completiontype, 'completion');
660 $a->state = $describe;
662 $a->user = fullname($user);
663 $a->activity = strip_tags(get_string('coursecomplete', 'completion'));
664 $fulldescribe = get_string('progress-title', 'completion', $a);
667 print $sep.csv_quote($describe);
670 print '<td class="completion-progresscell">';
672 // Display course completion status icon
673 print '<img src="'.$OUTPUT->pix_url('i/completion-auto-'.$completiontype).
674 '" alt="'.$describe.'" class="icon" title="'.$fulldescribe.'" />';
692 print '<ul class="progress-actions"><li><a href="index.php?course='.$course->id.
693 '&format=csv">'.get_string('csvdownload','completion').'</a></li>
694 <li><a href="index.php?course='.$course->id.'&format=excelcsv">'.
695 get_string('excelcsvdownload','completion').'</a></li></ul>';
697 echo $OUTPUT->footer($course);