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 $shortname = format_string($course->shortname, true, array('context' => $context));
147 $textlib = textlib_get_instance();
148 header('Content-Disposition: attachment; filename=progress.'.
149 preg_replace('/[^a-z0-9-]/','_',$textlib->strtolower(strip_tags($shortname))).'.csv');
150 // Unicode byte-order mark for Excel
152 header('Content-Type: text/csv; charset=UTF-16LE');
153 print chr(0xFF).chr(0xFE);
157 header('Content-Type: text/csv; charset=UTF-8');
163 // Navigation and header
164 $strcompletion = get_string('coursecompletion');
166 $PAGE->set_title($strcompletion);
167 $PAGE->set_heading($course->fullname);
169 echo $OUTPUT->header();
171 $PAGE->requires->yui2_lib(
180 $PAGE->requires->js('/course/report/completion/textrotate.js');
182 // Handle groups (if enabled)
183 groups_print_course_menu($course, $CFG->wwwroot.'/course/report/completion/?course='.$course->id);
187 // Generate where clause
189 $where_params = array();
191 if ($sifirst !== 'all') {
192 $where[] = $DB->sql_like('u.firstname', ':sifirst', false);
193 $where_params['sifirst'] = $sifirst.'%';
196 if ($silast !== 'all') {
197 $where[] = $DB->sql_like('u.lastname', ':silast', false);
198 $where_params['silast'] = $silast.'%';
201 // Get user match count
202 $total = $completion->get_num_tracked_users(implode(' AND ', $where), $where_params, $group);
205 $grandtotal = $completion->get_num_tracked_users('', array(), $group);
207 // If no users in this course what-so-ever
209 echo $OUTPUT->container(get_string('err_nousers', 'completion'), 'errorbox errorboxcontent');
210 echo $OUTPUT->footer();
218 $progress = $completion->get_progress_all(
219 implode(' AND ', $where),
222 $firstnamesort ? 'u.firstname ASC' : 'u.lastname ASC',
223 $csv ? 0 : COMPLETION_REPORT_PAGE,
229 // Build link for paging
230 $link = $CFG->wwwroot.'/course/report/completion/?course='.$course->id;
232 $link .= '&sort='.$sort;
234 $link .= '&start=';
236 // Build the the page by Initial bar
237 $initials = array('first', 'last');
238 $alphabet = explode(',', get_string('alphabet', 'langconfig'));
241 foreach ($initials as $initial) {
242 $var = 'si'.$initial;
244 $othervar = $initial == 'first' ? 'silast' : 'sifirst';
245 $othervar = $$othervar != 'all' ? "&{$othervar}={$$othervar}" : '';
247 $pagingbar .= ' <div class="initialbar '.$initial.'initial">';
248 $pagingbar .= get_string($initial.'name').': ';
250 if ($$var == 'all') {
251 $pagingbar .= '<strong>'.get_string('all').'</strong> ';
254 $pagingbar .= "<a href=\"{$link}{$othervar}\">".get_string('all').'</a> ';
257 foreach ($alphabet as $letter) {
258 if ($$var === $letter) {
259 $pagingbar .= '<strong>'.$letter.'</strong> ';
262 $pagingbar .= "<a href=\"$link&$var={$letter}{$othervar}\">$letter</a> ";
266 $pagingbar .= '</div>';
269 // Do we need a paging bar?
270 if($total > COMPLETION_REPORT_PAGE) {
273 $pagingbar .= '<div class="paging">';
274 $pagingbar .= get_string('page').': ';
276 $sistrings = array();
277 $sistrings[] = $sifirst != 'all' ? "sifirst={$sifirst}" : null;
278 $sistrings[] = $silast != 'all' ? "silast={$silast}" : null;
279 $sistring = !empty($sistrings) ? implode('&', $sistrings) : '';
281 // Display previous link
283 $pstart = max($start - COMPLETION_REPORT_PAGE, 0);
284 $pagingbar .= "(<a class=\"previous\" href=\"{$link}{$pstart}{$sistring}\">".get_string('previous').'</a>) ';
290 while ($curstart < $total) {
293 if ($curstart == $start) {
294 $pagingbar .= ' '.$curpage.' ';
297 $pagingbar .= " <a href=\"{$link}{$curstart}{$sistring}\">$curpage</a> ";
300 $curstart += COMPLETION_REPORT_PAGE;
304 $nstart = $start + COMPLETION_REPORT_PAGE;
305 if ($nstart < $total) {
306 $pagingbar .= " (<a class=\"next\" href=\"{$link}{$nstart}{$sistring}\">".get_string('next').'</a>)';
309 $pagingbar .= '</div>';
319 print '<br class="clearer"/>'; // ugh
321 $total_header = ($total == $grandtotal) ? $total : "{$total}/{$grandtotal}";
322 echo $OUTPUT->heading(get_string('allparticipants').": {$total_header}", 3);
327 echo $OUTPUT->heading(get_string('nothingtodisplay'), 2);
328 echo $OUTPUT->footer();
332 print '<table id="completion-progress" class="generaltable flexible boxaligncenter completionreport" style="text-align: left" cellpadding="5" border="1">';
334 // Print criteria group names
335 print PHP_EOL.'<tr style="vertical-align: top">';
336 print '<th scope="row" class="rowheader">'.get_string('criteriagroup', 'completion').'</th>';
338 $current_group = false;
340 for ($i = 0; $i <= count($criteria); $i++) {
342 if (isset($criteria[$i])) {
343 $criterion = $criteria[$i];
345 if ($current_group && $criterion->criteriatype === $current_group->criteriatype) {
353 print '<th scope="col" colspan="'.$col_count.'" class="colheader criteriagroup">'.$current_group->get_type_title().'</th>';
356 if (isset($criteria[$i])) {
357 // Move to next criteria type
358 $current_group = $criterion;
363 // Overall course completion status
364 print '<th style="text-align: center;">'.get_string('course').'</th>';
368 // Print aggregation methods
369 print PHP_EOL.'<tr style="vertical-align: top">';
370 print '<th scope="row" class="rowheader">'.get_string('aggregationmethod', 'completion').'</th>';
372 $current_group = false;
374 for ($i = 0; $i <= count($criteria); $i++) {
376 if (isset($criteria[$i])) {
377 $criterion = $criteria[$i];
379 if ($current_group && $criterion->criteriatype === $current_group->criteriatype) {
388 COMPLETION_CRITERIA_TYPE_COURSE,
389 COMPLETION_CRITERIA_TYPE_ACTIVITY,
390 COMPLETION_CRITERIA_TYPE_ROLE,
393 if (in_array($current_group->criteriatype, $has_agg)) {
394 // Try load a aggregation method
395 $method = $completion->get_aggregation_method($current_group->criteriatype);
397 $method = $method == 1 ? get_string('all') : get_string('any');
403 print '<th scope="col" colspan="'.$col_count.'" class="colheader aggheader">'.$method.'</th>';
406 if (isset($criteria[$i])) {
407 // Move to next criteria type
408 $current_group = $criterion;
413 // Overall course aggregation method
414 print '<th scope="col" class="colheader aggheader aggcriteriacourse">';
416 // Get course aggregation
417 $method = $completion->get_aggregation_method();
419 print $method == 1 ? get_string('all') : get_string('any');
425 // Print criteria titles
426 if (COMPLETION_REPORT_COL_TITLES) {
428 print PHP_EOL.'<tr>';
429 print '<th scope="row" class="rowheader">'.get_string('criteria', 'completion').'</th>';
431 foreach ($criteria as $criterion) {
432 // Get criteria details
433 $details = $criterion->get_title_detailed();
434 print '<th scope="col" class="colheader criterianame">';
435 print '<span class="completion-criterianame">'.$details.'</span>';
439 // Overall course completion status
440 print '<th scope="col" class="colheader criterianame">';
442 print '<span class="completion-criterianame">'.get_string('coursecomplete', 'completion').'</span>';
447 // Print user heading and icons
450 // User heading / sort option
451 print '<th scope="col" class="completion-sortchoice" style="clear: both;">';
453 $sistring = "&silast={$silast}&sifirst={$sifirst}";
457 get_string('firstname')." / <a href=\"./?course={$course->id}{$sistring}\">".
458 get_string('lastname').'</a>';
460 print "<a href=\"./?course={$course->id}&sort=firstname{$sistring}\">".
461 get_string('firstname').'</a> / '.
462 get_string('lastname');
467 // Print user id number column
469 print '<th>'.get_string('idnumber').'</th>';
473 /// Print criteria icons
475 foreach ($criteria as $criterion) {
477 // Generate icon details
480 $icontitle = ''; // Required if $iconlink set
481 $iconalt = ''; // Required
482 switch ($criterion->criteriatype) {
484 case COMPLETION_CRITERIA_TYPE_ACTIVITY:
486 $activity = $criterion->get_mod_instance();
489 $icon = $OUTPUT->pix_url('icon', $criterion->module);
490 $iconlink = $CFG->wwwroot.'/mod/'.$criterion->module.'/view.php?id='.$criterion->moduleinstance;
491 $icontitle = $activity->name;
492 $iconalt = get_string('modulename', $criterion->module);
495 case COMPLETION_CRITERIA_TYPE_COURSE:
497 $crs = $DB->get_record('course', array('id' => $criterion->courseinstance));
500 $iconlink = $CFG->wwwroot.'/course/view.php?id='.$criterion->courseinstance;
501 $icontitle = format_string($crs->fullname, true, array('context' => get_context_instance(CONTEXT_COURSE, $crs->id, MUST_EXIST)));
502 $iconalt = format_string($crs->shortname, true, array('context' => get_context_instance(CONTEXT_COURSE, $crs->id)));
505 case COMPLETION_CRITERIA_TYPE_ROLE:
507 $role = $DB->get_record('role', array('id' => $criterion->role));
510 $iconalt = $role->name;
514 // Print icon and cell
515 print '<th class="criteriaicon">';
517 // Create icon if not supplied
519 $icon = $OUTPUT->pix_url('i/'.$COMPLETION_CRITERIA_TYPES[$criterion->criteriatype]);
522 print ($iconlink ? '<a href="'.$iconlink.'" title="'.$icontitle.'">' : '');
523 print '<img src="'.$icon.'" class="icon" alt="'.$iconalt.'" '.(!$iconlink ? 'title="'.$iconalt.'"' : '').' />';
524 print ($iconlink ? '</a>' : '');
529 // Overall course completion status
530 print '<th class="criteriaicon">';
531 print '<img src="'.$OUTPUT->pix_url('i/course').'" class="icon" alt="'.get_string('course').'" title="'.get_string('coursecomplete', 'completion').'" />';
546 /// Display a row for each user
548 foreach ($progress as $user) {
552 print csv_quote(fullname($user));
554 print $sep.csv_quote($user->idnumber);
557 print PHP_EOL.'<tr id="user-'.$user->id.'">';
559 print '<th scope="row"><a href="'.$CFG->wwwroot.'/user/view.php?id='.
560 $user->id.'&course='.$course->id.'">'.fullname($user).'</a></th>';
562 print '<td>'.htmlspecialchars($user->idnumber).'</td>';
566 // Progress for each course completion criteria
567 foreach ($criteria as $criterion) {
569 // Handle activity completion differently
570 if ($criterion->criteriatype == COMPLETION_CRITERIA_TYPE_ACTIVITY) {
573 $mod = $criterion->get_mod_instance();
574 $activity = $DB->get_record('course_modules', array('id' => $criterion->moduleinstance));
575 $activity->name = $mod->name;
578 // Get progress information and state
579 if(array_key_exists($activity->id,$user->progress)) {
580 $thisprogress=$user->progress[$activity->id];
581 $state=$thisprogress->completionstate;
582 $date=userdate($thisprogress->timemodified);
584 $state=COMPLETION_INCOMPLETE;
588 $criteria_completion = $completion->get_user_completion($user->id, $criterion);
590 // Work out how it corresponds to an icon
592 case COMPLETION_INCOMPLETE : $completiontype='n'; break;
593 case COMPLETION_COMPLETE : $completiontype='y'; break;
594 case COMPLETION_COMPLETE_PASS : $completiontype='pass'; break;
595 case COMPLETION_COMPLETE_FAIL : $completiontype='fail'; break;
598 $completionicon='completion-'.
599 ($activity->completion==COMPLETION_TRACKING_AUTOMATIC ? 'auto' : 'manual').
602 $describe=get_string('completion-alt-auto-'.$completiontype,'completion');
606 $a->user=fullname($user);
607 $a->activity=strip_tags($activity->name);
608 $fulldescribe=get_string('progress-title','completion',$a);
611 print $sep.csv_quote($describe).$sep.csv_quote($date);
613 print '<td class="completion-progresscell">';
615 print '<img src="'.$OUTPUT->pix_url('i/'.$completionicon).
616 '" alt="'.$describe.'" class="icon" title="'.$fulldescribe.'" />';
624 // Handle all other criteria
625 $criteria_completion = $completion->get_user_completion($user->id, $criterion);
626 $is_complete = $criteria_completion->is_complete();
628 $completiontype = $is_complete ? 'y' : 'n';
629 $completionicon = 'completion-auto-'.$completiontype;
631 $describe = get_string('completion-alt-auto-'.$completiontype, 'completion');
634 $a->state = $describe;
635 $a->date = $is_complete ? userdate($criteria_completion->timecompleted) : '';
636 $a->user = fullname($user);
637 $a->activity = strip_tags($criterion->get_title());
638 $fulldescribe = get_string('progress-title', 'completion', $a);
641 print $sep.csv_quote($describe);
644 if ($allow_marking_criteria === $criterion->id) {
645 $describe = get_string('completion-alt-auto-'.$completiontype,'completion');
647 print '<td class="completion-progresscell">'.
648 '<a href="'.$CFG->wwwroot.'/course/togglecompletion.php?user='.$user->id.'&course='.$course->id.'&rolec='.$allow_marking_criteria.'&sesskey='.sesskey().'">'.
649 '<img src="'.$OUTPUT->pix_url('i/completion-manual-'.($is_complete ? 'y' : 'n')).
650 '" alt="'.$describe.'" class="icon" title="'.get_string('markcomplete', 'completion').'" /></a></td>';
652 print '<td class="completion-progresscell">'.
653 '<img src="'.$OUTPUT->pix_url('i/'.$completionicon).
654 '" alt="'.$describe.'" class="icon" title="'.$fulldescribe.'" /></td>';
659 // Handle overall course completion
661 // Load course completion
663 'userid' => $user->id,
664 'course' => $course->id
667 $ccompletion = new completion_completion($params);
668 $completiontype = $ccompletion->is_complete() ? 'y' : 'n';
670 $describe = get_string('completion-alt-auto-'.$completiontype, 'completion');
673 $a->state = $describe;
675 $a->user = fullname($user);
676 $a->activity = strip_tags(get_string('coursecomplete', 'completion'));
677 $fulldescribe = get_string('progress-title', 'completion', $a);
680 print $sep.csv_quote($describe);
683 print '<td class="completion-progresscell">';
685 // Display course completion status icon
686 print '<img src="'.$OUTPUT->pix_url('i/completion-auto-'.$completiontype).
687 '" alt="'.$describe.'" class="icon" title="'.$fulldescribe.'" />';
705 print '<ul class="progress-actions"><li><a href="index.php?course='.$course->id.
706 '&format=csv">'.get_string('csvdownload','completion').'</a></li>
707 <li><a href="index.php?course='.$course->id.'&format=excelcsv">'.
708 get_string('excelcsvdownload','completion').'</a></li></ul>';
710 echo $OUTPUT->footer($course);