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');
37 $context = context_course
::instance($course->id
);
39 // Sort (default lastname, optionally firstname)
40 $sort = optional_param('sort','',PARAM_ALPHA
);
41 $firstnamesort = $sort == 'firstname';
44 $format = optional_param('format','',PARAM_ALPHA
);
45 $excel = $format == 'excelcsv';
46 $csv = $format == 'csv' ||
$excel;
49 $start = optional_param('start', 0, PARAM_INT
);
50 $sifirst = optional_param('sifirst', 'all', PARAM_ALPHA
);
51 $silast = optional_param('silast', 'all', PARAM_ALPHA
);
52 $start = optional_param('start', 0, PARAM_INT
);
54 // Whether to show extra user identity information
55 $extrafields = get_extra_user_fields($context);
56 $leftcols = 1 +
count($extrafields);
58 function csv_quote($value) {
61 return textlib
::convert('"'.str_replace('"',"'",$value).'"','UTF-8','UTF-16LE');
63 return '"'.str_replace('"',"'",$value).'"';
67 $url = new moodle_url('/report/progress/index.php', array('course'=>$id));
69 $url->param('sort', $sort);
72 $url->param('format', $format);
75 $url->param('start', $start);
78 $PAGE->set_pagelayout('report');
80 require_login($course);
82 // Check basic permission
83 require_capability('report/progress:view',$context);
86 $group = groups_get_course_group($course,true); // Supposed to verify group
87 if ($group===0 && $course->groupmode
==SEPARATEGROUPS
) {
88 require_capability('moodle/site:accessallgroups',$context);
91 // Get data on activities and progress of all users, and give error if we've
92 // nothing to display (no users or no activities)
93 $reportsurl = $CFG->wwwroot
.'/course/report.php?id='.$course->id
;
94 $completion = new completion_info($course);
95 $activities = $completion->get_activities();
97 // Generate where clause
99 $where_params = array();
101 if ($sifirst !== 'all') {
102 $where[] = $DB->sql_like('u.firstname', ':sifirst', false);
103 $where_params['sifirst'] = $sifirst.'%';
106 if ($silast !== 'all') {
107 $where[] = $DB->sql_like('u.lastname', ':silast', false);
108 $where_params['silast'] = $silast.'%';
111 // Get user match count
112 $total = $completion->get_num_tracked_users(implode(' AND ', $where), $where_params, $group);
115 $grandtotal = $completion->get_num_tracked_users('', array(), $group);
121 $progress = $completion->get_progress_all(
122 implode(' AND ', $where),
125 $firstnamesort ?
'u.firstname ASC' : 'u.lastname ASC',
126 $csv ?
0 : COMPLETION_REPORT_PAGE
,
132 if ($csv && $grandtotal && count($activities)>0) { // Only show CSV if there are some users/actvs
134 $shortname = format_string($course->shortname
, true, array('context' => $context));
135 header('Content-Disposition: attachment; filename=progress.'.
136 preg_replace('/[^a-z0-9-]/','_',textlib
::strtolower(strip_tags($shortname))).'.csv');
137 // Unicode byte-order mark for Excel
139 header('Content-Type: text/csv; charset=UTF-16LE');
140 print chr(0xFF).chr(0xFE);
144 header('Content-Type: text/csv; charset=UTF-8');
149 // Use SVG to draw sideways text if supported
150 $svgcleverness = can_use_rotated_text();
152 // Navigation and header
153 $strreports = get_string("reports");
154 $strcompletion = get_string('activitycompletion', 'completion');
156 $PAGE->set_title($strcompletion);
157 $PAGE->set_heading($course->fullname
);
158 echo $OUTPUT->header();
160 if ($svgcleverness) {
161 $PAGE->requires
->js('/report/progress/textrotate.js');
162 $PAGE->requires
->js_function_call('textrotate_init', null, true);
165 // Handle groups (if enabled)
166 groups_print_course_menu($course,$CFG->wwwroot
.'/report/progress/?course='.$course->id
);
169 if (count($activities)==0) {
170 echo $OUTPUT->container(get_string('err_noactivities', 'completion'), 'errorbox errorboxcontent');
171 echo $OUTPUT->footer();
175 // If no users in this course what-so-ever
177 echo $OUTPUT->container(get_string('err_nousers', 'completion'), 'errorbox errorboxcontent');
178 echo $OUTPUT->footer();
182 // Build link for paging
183 $link = $CFG->wwwroot
.'/report/progress/?course='.$course->id
;
185 $link .= '&sort='.$sort;
187 $link .= '&start=';
189 // Build the the page by Initial bar
190 $initials = array('first', 'last');
191 $alphabet = explode(',', get_string('alphabet', 'langconfig'));
194 foreach ($initials as $initial) {
195 $var = 'si'.$initial;
197 $othervar = $initial == 'first' ?
'silast' : 'sifirst';
198 $othervar = $
$othervar != 'all' ?
"&{$othervar}={$$othervar}" : '';
200 $pagingbar .= ' <div class="initialbar '.$initial.'initial">';
201 $pagingbar .= get_string($initial.'name').': ';
203 if ($
$var == 'all') {
204 $pagingbar .= '<strong>'.get_string('all').'</strong> ';
207 $pagingbar .= "<a href=\"{$link}{$othervar}\">".get_string('all').'</a> ';
210 foreach ($alphabet as $letter) {
211 if ($
$var === $letter) {
212 $pagingbar .= '<strong>'.$letter.'</strong> ';
215 $pagingbar .= "<a href=\"$link&$var={$letter}{$othervar}\">$letter</a> ";
219 $pagingbar .= '</div>';
222 // Do we need a paging bar?
223 if ($total > COMPLETION_REPORT_PAGE
) {
226 $pagingbar .= '<div class="paging">';
227 $pagingbar .= get_string('page').': ';
229 $sistrings = array();
230 if ($sifirst != 'all') {
231 $sistrings[] = "sifirst={$sifirst}";
233 if ($silast != 'all') {
234 $sistrings[] = "silast={$silast}";
236 $sistring = !empty($sistrings) ?
'&'.implode('&', $sistrings) : '';
238 // Display previous link
240 $pstart = max($start - COMPLETION_REPORT_PAGE
, 0);
241 $pagingbar .= "(<a class=\"previous\" href=\"{$link}{$pstart}{$sistring}\">".get_string('previous').'</a>) ';
247 while ($curstart < $total) {
250 if ($curstart == $start) {
251 $pagingbar .= ' '.$curpage.' ';
253 $pagingbar .= " <a href=\"{$link}{$curstart}{$sistring}\">$curpage</a> ";
256 $curstart +
= COMPLETION_REPORT_PAGE
;
260 $nstart = $start + COMPLETION_REPORT_PAGE
;
261 if ($nstart < $total) {
262 $pagingbar .= " (<a class=\"next\" href=\"{$link}{$nstart}{$sistring}\">".get_string('next').'</a>)';
265 $pagingbar .= '</div>';
268 // Okay, let's draw the table of progress info,
272 print '<br class="clearer"/>'; // ugh
277 echo $OUTPUT->heading(get_string('nothingtodisplay'));
278 echo $OUTPUT->footer();
282 print '<div id="completion-progress-wrapper" class="no-overflow">';
283 print '<table id="completion-progress" class="generaltable flexible boxaligncenter" style="text-align:left"><tr style="vertical-align:top">';
285 // User heading / sort option
286 print '<th scope="col" class="completion-sortchoice">';
288 $sistring = "&silast={$silast}&sifirst={$sifirst}";
290 if ($firstnamesort) {
292 get_string('firstname')." / <a href=\"./?course={$course->id}{$sistring}\">".
293 get_string('lastname').'</a>';
295 print "<a href=\"./?course={$course->id}&sort=firstname{$sistring}\">".
296 get_string('firstname').'</a> / '.
297 get_string('lastname');
301 // Print user identity columns
302 foreach ($extrafields as $field) {
303 echo '<th scope="col" class="completion-identifyfield">' .
304 get_user_field_name($field) . '</th>';
307 foreach ($extrafields as $field) {
308 echo $sep . csv_quote(get_user_field_name($field));
313 foreach($activities as $activity) {
314 $activity->datepassed
= $activity->completionexpected
&& $activity->completionexpected
<= time();
315 $activity->datepassedclass
=$activity->datepassed ?
'completion-expired' : '';
317 if ($activity->completionexpected
) {
318 $datetext=userdate($activity->completionexpected
,get_string('strftimedate','langconfig'));
323 // Some names (labels) come URL-encoded and can be very long, so shorten them
324 $activity->name
= shorten_text($activity->name
);
327 print $sep.csv_quote(strip_tags($activity->name
)).$sep.csv_quote($datetext);
329 $formattedactivityname = format_string($activity->name
, true, array('context' => $context));
330 print '<th scope="col" class="'.$activity->datepassedclass
.'">'.
331 '<a href="'.$CFG->wwwroot
.'/mod/'.$activity->modname
.
332 '/view.php?id='.$activity->id
.'" title="' . $formattedactivityname . '">'.
333 '<img src="'.$OUTPUT->pix_url('icon', $activity->modname
).'" alt="'.
334 get_string('modulename',$activity->modname
).'" /> <span class="completion-activityname">'.
335 $formattedactivityname.'</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));
354 foreach ($extrafields as $field) {
355 echo $sep . csv_quote($user->{$field});
358 print '<tr><th scope="row"><a href="'.$CFG->wwwroot
.'/user/view.php?id='.
359 $user->id
.'&course='.$course->id
.'">'.fullname($user).'</a></th>';
360 foreach ($extrafields as $field) {
361 echo '<td>' . s($user->{$field}) . '</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 $modcontext = context_module
::instance($activity->id
);
391 $describe = get_string('completion-' . $completiontype, 'completion');
395 $a->user
=fullname($user);
396 $a->activity
= format_string($activity->name
, true, array('context' => $modcontext));
397 $fulldescribe=get_string('progress-title','completion',$a);
400 print $sep.csv_quote($describe).$sep.csv_quote($date);
402 print '<td class="completion-progresscell '.$activity->datepassedclass
.'">'.
403 '<img src="'.$OUTPUT->pix_url('i/'.$completionicon).
404 '" alt="'.$describe.'" title="'.$fulldescribe.'" /></td>';
422 print '<ul class="progress-actions"><li><a href="index.php?course='.$course->id
.
423 '&format=csv">'.get_string('csvdownload','completion').'</a></li>
424 <li><a href="index.php?course='.$course->id
.'&format=excelcsv">'.
425 get_string('excelcsvdownload','completion').'</a></li></ul>';
427 echo $OUTPUT->footer();