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 * Activity progress reports
21 * @subpackage progress
22 * @copyright 2008 Sam Marshall
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 require('../../config.php');
27 require_once($CFG->libdir . '/completionlib.php');
29 define('COMPLETION_REPORT_PAGE', 25);
32 $id = required_param('course',PARAM_INT);
33 $course = $DB->get_record('course',array('id'=>$id));
35 print_error('invalidcourseid');
38 // Sort (default lastname, optionally firstname)
39 $sort = optional_param('sort','',PARAM_ALPHA);
40 $firstnamesort = $sort == 'firstname';
43 $format = optional_param('format','',PARAM_ALPHA);
44 $excel = $format == 'excelcsv';
45 $csv = $format == 'csv' || $excel;
48 $start = optional_param('start', 0, PARAM_INT);
49 $sifirst = optional_param('sifirst', 'all', PARAM_ALPHA);
50 $silast = optional_param('silast', 'all', PARAM_ALPHA);
51 $start = optional_param('start', 0, PARAM_INT);
53 // Whether to show idnumber
54 // TODO: This should really not be using a config option 'intended' for
55 // gradebook, but that option is also used in quiz reports as well. There ought
56 // to be a generic option somewhere.
57 $idnumbers = $CFG->grade_report_showuseridnumber;
59 function csv_quote($value) {
62 $tl = textlib_get_instance();
63 return $tl->convert('"'.str_replace('"',"'",$value).'"','UTF-8','UTF-16LE');
65 return '"'.str_replace('"',"'",$value).'"';
69 $url = new moodle_url('/report/progress/index.php', array('course'=>$id));
71 $url->param('sort', $sort);
74 $url->param('format', $format);
77 $url->param('start', $start);
80 $PAGE->set_pagelayout('report');
82 require_login($course);
84 // Check basic permission
85 $context = get_context_instance(CONTEXT_COURSE,$course->id);
86 require_capability('report/progress:view',$context);
89 $group = groups_get_course_group($course,true); // Supposed to verify group
90 if ($group===0 && $course->groupmode==SEPARATEGROUPS) {
91 require_capability('moodle/site:accessallgroups',$context);
94 // Get data on activities and progress of all users, and give error if we've
95 // nothing to display (no users or no activities)
96 $reportsurl = $CFG->wwwroot.'/course/report.php?id='.$course->id;
97 $completion = new completion_info($course);
98 $activities = $completion->get_activities();
100 // Generate where clause
102 $where_params = array();
104 if ($sifirst !== 'all') {
105 $where[] = $DB->sql_like('u.firstname', ':sifirst', false);
106 $where_params['sifirst'] = $sifirst.'%';
109 if ($silast !== 'all') {
110 $where[] = $DB->sql_like('u.lastname', ':silast', false);
111 $where_params['silast'] = $silast.'%';
114 // Get user match count
115 $total = $completion->get_num_tracked_users(implode(' AND ', $where), $where_params, $group);
118 $grandtotal = $completion->get_num_tracked_users('', array(), $group);
124 $progress = $completion->get_progress_all(
125 implode(' AND ', $where),
128 $firstnamesort ? 'u.firstname ASC' : 'u.lastname ASC',
129 $csv ? 0 : COMPLETION_REPORT_PAGE,
134 if ($csv && $grandtotal && count($activities)>0) { // Only show CSV if there are some users/actvs
136 $shortname = format_string($course->shortname, true, array('context' => $context));
137 $textlib = textlib_get_instance();
138 header('Content-Disposition: attachment; filename=progress.'.
139 preg_replace('/[^a-z0-9-]/','_',$textlib->strtolower(strip_tags($shortname))).'.csv');
140 // Unicode byte-order mark for Excel
142 header('Content-Type: text/csv; charset=UTF-16LE');
143 print chr(0xFF).chr(0xFE);
147 header('Content-Type: text/csv; charset=UTF-8');
152 // Use SVG to draw sideways text if supported
153 $svgcleverness = can_use_rotated_text();
155 // Navigation and header
156 $strreports = get_string("reports");
157 $strcompletion = get_string('activitycompletion', 'completion');
159 $PAGE->set_title($strcompletion);
160 $PAGE->set_heading($course->fullname);
161 echo $OUTPUT->header();
163 if ($svgcleverness) {
164 $PAGE->requires->yui2_lib('event');
165 $PAGE->requires->js('/report/progress/textrotate.js');
168 // Handle groups (if enabled)
169 groups_print_course_menu($course,$CFG->wwwroot.'/report/progress/?course='.$course->id);
172 if (count($activities)==0) {
173 echo $OUTPUT->container(get_string('err_noactivities', 'completion'), 'errorbox errorboxcontent');
174 echo $OUTPUT->footer();
178 // If no users in this course what-so-ever
180 echo $OUTPUT->container(get_string('err_nousers', 'completion'), 'errorbox errorboxcontent');
181 echo $OUTPUT->footer();
185 // Build link for paging
186 $link = $CFG->wwwroot.'/report/progress/?course='.$course->id;
188 $link .= '&sort='.$sort;
190 $link .= '&start=';
192 // Build the the page by Initial bar
193 $initials = array('first', 'last');
194 $alphabet = explode(',', get_string('alphabet', 'langconfig'));
197 foreach ($initials as $initial) {
198 $var = 'si'.$initial;
200 $othervar = $initial == 'first' ? 'silast' : 'sifirst';
201 $othervar = $$othervar != 'all' ? "&{$othervar}={$$othervar}" : '';
203 $pagingbar .= ' <div class="initialbar '.$initial.'initial">';
204 $pagingbar .= get_string($initial.'name').': ';
206 if ($$var == 'all') {
207 $pagingbar .= '<strong>'.get_string('all').'</strong> ';
210 $pagingbar .= "<a href=\"{$link}{$othervar}\">".get_string('all').'</a> ';
213 foreach ($alphabet as $letter) {
214 if ($$var === $letter) {
215 $pagingbar .= '<strong>'.$letter.'</strong> ';
218 $pagingbar .= "<a href=\"$link&$var={$letter}{$othervar}\">$letter</a> ";
222 $pagingbar .= '</div>';
225 // Do we need a paging bar?
226 if ($total > COMPLETION_REPORT_PAGE) {
229 $pagingbar .= '<div class="paging">';
230 $pagingbar .= get_string('page').': ';
232 $sistrings = array();
233 if ($sifirst != 'all') {
234 $sistrings[] = "sifirst={$sifirst}";
236 if ($silast != 'all') {
237 $sistrings[] = "silast={$silast}";
239 $sistring = !empty($sistrings) ? '&'.implode('&', $sistrings) : '';
241 // Display previous link
243 $pstart = max($start - COMPLETION_REPORT_PAGE, 0);
244 $pagingbar .= "(<a class=\"previous\" href=\"{$link}{$pstart}{$sistring}\">".get_string('previous').'</a>) ';
250 while ($curstart < $total) {
253 if ($curstart == $start) {
254 $pagingbar .= ' '.$curpage.' ';
256 $pagingbar .= " <a href=\"{$link}{$curstart}{$sistring}\">$curpage</a> ";
259 $curstart += COMPLETION_REPORT_PAGE;
263 $nstart = $start + COMPLETION_REPORT_PAGE;
264 if ($nstart < $total) {
265 $pagingbar .= " (<a class=\"next\" href=\"{$link}{$nstart}{$sistring}\">".get_string('next').'</a>)';
268 $pagingbar .= '</div>';
271 // Okay, let's draw the table of progress info,
275 print '<br class="clearer"/>'; // ugh
280 echo $OUTPUT->heading(get_string('nothingtodisplay'));
281 echo $OUTPUT->footer();
285 print '<table id="completion-progress" class="generaltable flexible boxaligncenter" style="text-align:left"><tr style="vertical-align:top">';
287 // User heading / sort option
288 print '<th scope="col" class="completion-sortchoice">';
290 $sistring = "&silast={$silast}&sifirst={$sifirst}";
292 if ($firstnamesort) {
294 get_string('firstname')." / <a href=\"./?course={$course->id}{$sistring}\">".
295 get_string('lastname').'</a>';
297 print "<a href=\"./?course={$course->id}&sort=firstname{$sistring}\">".
298 get_string('firstname').'</a> / '.
299 get_string('lastname');
304 print '<th>'.get_string('idnumber').'</th>';
314 foreach($activities as $activity) {
315 $activity->datepassed = $activity->completionexpected && $activity->completionexpected <= time();
316 $activity->datepassedclass=$activity->datepassed ? 'completion-expired' : '';
318 if ($activity->completionexpected) {
319 $datetext=userdate($activity->completionexpected,get_string('strftimedate','langconfig'));
324 // Some names (labels) come URL-encoded and can be very long, so shorten them
325 $activity->name = shorten_text($activity->name);
328 print $sep.csv_quote(strip_tags($activity->name)).$sep.csv_quote($datetext);
330 print '<th scope="col" class="'.$activity->datepassedclass.'">'.
331 '<a href="'.$CFG->wwwroot.'/mod/'.$activity->modname.
332 '/view.php?id='.$activity->id.'">'.
333 '<img src="'.$OUTPUT->pix_url('icon', $activity->modname).'" alt="'.
334 get_string('modulename',$activity->modname).'" /> <span class="completion-activityname">'.
335 format_string($activity->name).'</span></a>';
336 if ($activity->completionexpected) {
337 print '<div class="completion-expected"><span>'.$datetext.'</span></div>';
350 foreach($progress as $user) {
353 print csv_quote(fullname($user));
355 print $sep.csv_quote($user->idnumber);
358 print '<tr><th scope="row"><a href="'.$CFG->wwwroot.'/user/view.php?id='.
359 $user->id.'&course='.$course->id.'">'.fullname($user).'</a></th>';
361 print '<td>'.htmlspecialchars($user->idnumber).'</td>';
365 // Progress for each activity
366 foreach($activities as $activity) {
368 // Get progress information and state
369 if (array_key_exists($activity->id,$user->progress)) {
370 $thisprogress=$user->progress[$activity->id];
371 $state=$thisprogress->completionstate;
372 $date=userdate($thisprogress->timemodified);
374 $state=COMPLETION_INCOMPLETE;
378 // Work out how it corresponds to an icon
380 case COMPLETION_INCOMPLETE : $completiontype='n'; break;
381 case COMPLETION_COMPLETE : $completiontype='y'; break;
382 case COMPLETION_COMPLETE_PASS : $completiontype='pass'; break;
383 case COMPLETION_COMPLETE_FAIL : $completiontype='fail'; break;
386 $completionicon='completion-'.
387 ($activity->completion==COMPLETION_TRACKING_AUTOMATIC ? 'auto' : 'manual').
390 $describe=get_string('completion-alt-auto-'.$completiontype,'completion');
394 $a->user=fullname($user);
395 $a->activity=strip_tags($activity->name);
396 $fulldescribe=get_string('progress-title','completion',$a);
399 print $sep.csv_quote($describe).$sep.csv_quote($date);
401 print '<td class="completion-progresscell '.$activity->datepassedclass.'">'.
402 '<img src="'.$OUTPUT->pix_url('i/'.$completionicon).
403 '" alt="'.$describe.'" title="'.$fulldescribe.'" /></td>';
420 print '<ul class="progress-actions"><li><a href="index.php?course='.$course->id.
421 '&format=csv">'.get_string('csvdownload','completion').'</a></li>
422 <li><a href="index.php?course='.$course->id.'&format=excelcsv">'.
423 get_string('excelcsvdownload','completion').'</a></li></ul>';
425 echo $OUTPUT->footer();