MDL-26882 profile: Applying cap check (site:viewuseridentity) to email field in user...
[moodle.git] / report / progress / index.php
blob7192f2f4901d80f6ce8a33336eae7e579fe7c630
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
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.
8 //
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/>.
17 /**
18 * Activity progress reports
20 * @package report
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);
31 // Get course
32 $id = required_param('course',PARAM_INT);
33 $course = $DB->get_record('course',array('id'=>$id));
34 if (!$course) {
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';
43 // CSV format
44 $format = optional_param('format','',PARAM_ALPHA);
45 $excel = $format == 'excelcsv';
46 $csv = $format == 'csv' || $excel;
48 // Paging
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) {
59 global $excel;
60 if ($excel) {
61 return textlib::convert('"'.str_replace('"',"'",$value).'"','UTF-8','UTF-16LE');
62 } else {
63 return '"'.str_replace('"',"'",$value).'"';
67 $url = new moodle_url('/report/progress/index.php', array('course'=>$id));
68 if ($sort !== '') {
69 $url->param('sort', $sort);
71 if ($format !== '') {
72 $url->param('format', $format);
74 if ($start !== 0) {
75 $url->param('start', $start);
77 $PAGE->set_url($url);
78 $PAGE->set_pagelayout('report');
80 require_login($course);
82 // Check basic permission
83 require_capability('report/progress:view',$context);
85 // Get group mode
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
98 $where = array();
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);
114 // Total user count
115 $grandtotal = $completion->get_num_tracked_users('', array(), $group);
117 // Get user data
118 $progress = array();
120 if ($total) {
121 $progress = $completion->get_progress_all(
122 implode(' AND ', $where),
123 $where_params,
124 $group,
125 $firstnamesort ? 'u.firstname ASC' : 'u.lastname ASC',
126 $csv ? 0 : COMPLETION_REPORT_PAGE,
127 $csv ? 0 : $start,
128 $context
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
138 if ($excel) {
139 header('Content-Type: text/csv; charset=UTF-16LE');
140 print chr(0xFF).chr(0xFE);
141 $sep="\t".chr(0);
142 $line="\n".chr(0);
143 } else {
144 header('Content-Type: text/csv; charset=UTF-8');
145 $sep=",";
146 $line="\n";
148 } else {
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->yui2_lib('event');
162 $PAGE->requires->js('/report/progress/textrotate.js');
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();
172 exit;
175 // If no users in this course what-so-ever
176 if (!$grandtotal) {
177 echo $OUTPUT->container(get_string('err_nousers', 'completion'), 'errorbox errorboxcontent');
178 echo $OUTPUT->footer();
179 exit;
182 // Build link for paging
183 $link = $CFG->wwwroot.'/report/progress/?course='.$course->id;
184 if (strlen($sort)) {
185 $link .= '&amp;sort='.$sort;
187 $link .= '&amp;start=';
189 // Build the the page by Initial bar
190 $initials = array('first', 'last');
191 $alphabet = explode(',', get_string('alphabet', 'langconfig'));
193 $pagingbar = '';
194 foreach ($initials as $initial) {
195 $var = 'si'.$initial;
197 $othervar = $initial == 'first' ? 'silast' : 'sifirst';
198 $othervar = $$othervar != 'all' ? "&amp;{$othervar}={$$othervar}" : '';
200 $pagingbar .= ' <div class="initialbar '.$initial.'initial">';
201 $pagingbar .= get_string($initial.'name').':&nbsp;';
203 if ($$var == 'all') {
204 $pagingbar .= '<strong>'.get_string('all').'</strong> ';
206 else {
207 $pagingbar .= "<a href=\"{$link}{$othervar}\">".get_string('all').'</a> ';
210 foreach ($alphabet as $letter) {
211 if ($$var === $letter) {
212 $pagingbar .= '<strong>'.$letter.'</strong> ';
214 else {
215 $pagingbar .= "<a href=\"$link&amp;$var={$letter}{$othervar}\">$letter</a> ";
219 $pagingbar .= '</div>';
222 // Do we need a paging bar?
223 if ($total > COMPLETION_REPORT_PAGE) {
225 // Paging bar
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) ? '&amp;'.implode('&amp;', $sistrings) : '';
238 // Display previous link
239 if ($start > 0) {
240 $pstart = max($start - COMPLETION_REPORT_PAGE, 0);
241 $pagingbar .= "(<a class=\"previous\" href=\"{$link}{$pstart}{$sistring}\">".get_string('previous').'</a>)&nbsp;';
244 // Create page links
245 $curstart = 0;
246 $curpage = 0;
247 while ($curstart < $total) {
248 $curpage++;
250 if ($curstart == $start) {
251 $pagingbar .= '&nbsp;'.$curpage.'&nbsp;';
252 } else {
253 $pagingbar .= "&nbsp;<a href=\"{$link}{$curstart}{$sistring}\">$curpage</a>&nbsp;";
256 $curstart += COMPLETION_REPORT_PAGE;
259 // Display next link
260 $nstart = $start + COMPLETION_REPORT_PAGE;
261 if ($nstart < $total) {
262 $pagingbar .= "&nbsp;(<a class=\"next\" href=\"{$link}{$nstart}{$sistring}\">".get_string('next').'</a>)';
265 $pagingbar .= '</div>';
268 // Okay, let's draw the table of progress info,
270 // Start of table
271 if (!$csv) {
272 print '<br class="clearer"/>'; // ugh
274 print $pagingbar;
276 if (!$total) {
277 echo $OUTPUT->heading(get_string('nothingtodisplay'));
278 echo $OUTPUT->footer();
279 exit;
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 = "&amp;silast={$silast}&amp;sifirst={$sifirst}";
290 if ($firstnamesort) {
291 print
292 get_string('firstname')." / <a href=\"./?course={$course->id}{$sistring}\">".
293 get_string('lastname').'</a>';
294 } else {
295 print "<a href=\"./?course={$course->id}&amp;sort=firstname{$sistring}\">".
296 get_string('firstname').'</a> / '.
297 get_string('lastname');
299 print '</th>';
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>';
306 } else {
307 foreach ($extrafields as $field) {
308 echo $sep . csv_quote(get_user_field_name($field));
312 // Activities
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'));
319 } else {
320 $datetext='';
323 // Some names (labels) come URL-encoded and can be very long, so shorten them
324 $activity->name = shorten_text($activity->name);
326 if ($csv) {
327 print $sep.csv_quote(strip_tags($activity->name)).$sep.csv_quote($datetext);
328 } else {
329 print '<th scope="col" class="'.$activity->datepassedclass.'">'.
330 '<a href="'.$CFG->wwwroot.'/mod/'.$activity->modname.
331 '/view.php?id='.$activity->id.'">'.
332 '<img src="'.$OUTPUT->pix_url('icon', $activity->modname).'" alt="'.
333 get_string('modulename',$activity->modname).'" /> <span class="completion-activityname">'.
334 format_string($activity->name).'</span></a>';
335 if ($activity->completionexpected) {
336 print '<div class="completion-expected"><span>'.$datetext.'</span></div>';
338 print '</th>';
342 if ($csv) {
343 print $line;
344 } else {
345 print '</tr>';
348 // Row for each user
349 foreach($progress as $user) {
350 // User name
351 if ($csv) {
352 print csv_quote(fullname($user));
353 foreach ($extrafields as $field) {
354 echo $sep . csv_quote($user->{$field});
356 } else {
357 print '<tr><th scope="row"><a href="'.$CFG->wwwroot.'/user/view.php?id='.
358 $user->id.'&amp;course='.$course->id.'">'.fullname($user).'</a></th>';
359 foreach ($extrafields as $field) {
360 echo '<td>' . s($user->{$field}) . '</td>';
364 // Progress for each activity
365 foreach($activities as $activity) {
367 // Get progress information and state
368 if (array_key_exists($activity->id,$user->progress)) {
369 $thisprogress=$user->progress[$activity->id];
370 $state=$thisprogress->completionstate;
371 $date=userdate($thisprogress->timemodified);
372 } else {
373 $state=COMPLETION_INCOMPLETE;
374 $date='';
377 // Work out how it corresponds to an icon
378 switch($state) {
379 case COMPLETION_INCOMPLETE : $completiontype='n'; break;
380 case COMPLETION_COMPLETE : $completiontype='y'; break;
381 case COMPLETION_COMPLETE_PASS : $completiontype='pass'; break;
382 case COMPLETION_COMPLETE_FAIL : $completiontype='fail'; break;
385 $completionicon='completion-'.
386 ($activity->completion==COMPLETION_TRACKING_AUTOMATIC ? 'auto' : 'manual').
387 '-'.$completiontype;
389 $modcontext = context_module::instance($activity->id);
390 $describe = get_string('completion-' . $completiontype, 'completion');
391 $a=new StdClass;
392 $a->state=$describe;
393 $a->date=$date;
394 $a->user=fullname($user);
395 $a->activity = format_string($activity->name, true, array('context' => $modcontext));
396 $fulldescribe=get_string('progress-title','completion',$a);
398 if ($csv) {
399 print $sep.csv_quote($describe).$sep.csv_quote($date);
400 } else {
401 print '<td class="completion-progresscell '.$activity->datepassedclass.'">'.
402 '<img src="'.$OUTPUT->pix_url('i/'.$completionicon).
403 '" alt="'.$describe.'" title="'.$fulldescribe.'" /></td>';
407 if ($csv) {
408 print $line;
409 } else {
410 print '</tr>';
414 if ($csv) {
415 exit;
417 print '</table>';
418 print '</div>';
419 print $pagingbar;
421 print '<ul class="progress-actions"><li><a href="index.php?course='.$course->id.
422 '&amp;format=csv">'.get_string('csvdownload','completion').'</a></li>
423 <li><a href="index.php?course='.$course->id.'&amp;format=excelcsv">'.
424 get_string('excelcsvdownload','completion').'</a></li></ul>';
426 echo $OUTPUT->footer();