MDL-69521 core: Move all comments in code from 4.3 to 4.1
[moodle.git] / report / progress / index.php
blob596692cb7a5c1a5b0df1d557a2ee9da6fe08c924
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_NOTAGS);
51 $silast = optional_param('silast', 'all', PARAM_NOTAGS);
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 core_text::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 if ($sifirst !== 'all') {
78 $url->param('sifirst', $sifirst);
80 if ($silast !== 'all') {
81 $url->param('silast', $silast);
83 $PAGE->set_url($url);
84 $PAGE->set_pagelayout('report');
86 require_login($course);
88 // Check basic permission
89 require_capability('report/progress:view',$context);
91 // Get group mode
92 $group = groups_get_course_group($course,true); // Supposed to verify group
93 if ($group===0 && $course->groupmode==SEPARATEGROUPS) {
94 require_capability('moodle/site:accessallgroups',$context);
97 // Get data on activities and progress of all users, and give error if we've
98 // nothing to display (no users or no activities)
99 $reportsurl = $CFG->wwwroot.'/course/report.php?id='.$course->id;
100 $completion = new completion_info($course);
101 $activities = $completion->get_activities();
103 if ($sifirst !== 'all') {
104 set_user_preference('ifirst', $sifirst);
106 if ($silast !== 'all') {
107 set_user_preference('ilast', $silast);
110 if (!empty($USER->preference['ifirst'])) {
111 $sifirst = $USER->preference['ifirst'];
112 } else {
113 $sifirst = 'all';
116 if (!empty($USER->preference['ilast'])) {
117 $silast = $USER->preference['ilast'];
118 } else {
119 $silast = 'all';
122 // Generate where clause
123 $where = array();
124 $where_params = array();
126 if ($sifirst !== 'all') {
127 $where[] = $DB->sql_like('u.firstname', ':sifirst', false, false);
128 $where_params['sifirst'] = $sifirst.'%';
131 if ($silast !== 'all') {
132 $where[] = $DB->sql_like('u.lastname', ':silast', false, false);
133 $where_params['silast'] = $silast.'%';
136 // Get user match count
137 $total = $completion->get_num_tracked_users(implode(' AND ', $where), $where_params, $group);
139 // Total user count
140 $grandtotal = $completion->get_num_tracked_users('', array(), $group);
142 // Get user data
143 $progress = array();
145 if ($total) {
146 $progress = $completion->get_progress_all(
147 implode(' AND ', $where),
148 $where_params,
149 $group,
150 $firstnamesort ? 'u.firstname ASC, u.lastname ASC' : 'u.lastname ASC, u.firstname ASC',
151 $csv ? 0 : COMPLETION_REPORT_PAGE,
152 $csv ? 0 : $start,
153 $context
157 if ($csv && $grandtotal && count($activities)>0) { // Only show CSV if there are some users/actvs
159 $shortname = format_string($course->shortname, true, array('context' => $context));
160 header('Content-Disposition: attachment; filename=progress.'.
161 preg_replace('/[^a-z0-9-]/','_',core_text::strtolower(strip_tags($shortname))).'.csv');
162 // Unicode byte-order mark for Excel
163 if ($excel) {
164 header('Content-Type: text/csv; charset=UTF-16LE');
165 print chr(0xFF).chr(0xFE);
166 $sep="\t".chr(0);
167 $line="\n".chr(0);
168 } else {
169 header('Content-Type: text/csv; charset=UTF-8');
170 $sep=",";
171 $line="\n";
173 } else {
175 // Navigation and header
176 $strreports = get_string("reports");
177 $strcompletion = get_string('activitycompletion', 'completion');
179 $PAGE->set_title($strcompletion);
180 $PAGE->set_heading($course->fullname);
181 echo $OUTPUT->header();
182 $PAGE->requires->js_call_amd('report_progress/completion_override', 'init', [fullname($USER)]);
184 // Handle groups (if enabled)
185 groups_print_course_menu($course,$CFG->wwwroot.'/report/progress/?course='.$course->id);
188 if (count($activities)==0) {
189 echo $OUTPUT->container(get_string('err_noactivities', 'completion'), 'errorbox errorboxcontent');
190 echo $OUTPUT->footer();
191 exit;
194 // If no users in this course what-so-ever
195 if (!$grandtotal) {
196 echo $OUTPUT->container(get_string('err_nousers', 'completion'), 'errorbox errorboxcontent');
197 echo $OUTPUT->footer();
198 exit;
201 // Build link for paging
202 $link = $CFG->wwwroot.'/report/progress/?course='.$course->id;
203 if (strlen($sort)) {
204 $link .= '&amp;sort='.$sort;
206 $link .= '&amp;start=';
208 $pagingbar = '';
210 // Initials bar.
211 $prefixfirst = 'sifirst';
212 $prefixlast = 'silast';
214 // The URL used in the initials bar should reset the 'start' parameter.
215 $initialsbarurl = new moodle_url($url);
216 $initialsbarurl->remove_params('start');
218 $pagingbar .= $OUTPUT->initials_bar($sifirst, 'firstinitial', get_string('firstname'), $prefixfirst, $initialsbarurl);
219 $pagingbar .= $OUTPUT->initials_bar($silast, 'lastinitial', get_string('lastname'), $prefixlast, $initialsbarurl);
221 // Do we need a paging bar?
222 if ($total > COMPLETION_REPORT_PAGE) {
224 // Paging bar
225 $pagingbar .= '<div class="paging">';
226 $pagingbar .= get_string('page').': ';
228 $sistrings = array();
229 if ($sifirst != 'all') {
230 $sistrings[] = "sifirst={$sifirst}";
232 if ($silast != 'all') {
233 $sistrings[] = "silast={$silast}";
235 $sistring = !empty($sistrings) ? '&amp;'.implode('&amp;', $sistrings) : '';
237 // Display previous link
238 if ($start > 0) {
239 $pstart = max($start - COMPLETION_REPORT_PAGE, 0);
240 $pagingbar .= "(<a class=\"previous\" href=\"{$link}{$pstart}{$sistring}\">".get_string('previous').'</a>)&nbsp;';
243 // Create page links
244 $curstart = 0;
245 $curpage = 0;
246 while ($curstart < $total) {
247 $curpage++;
249 if ($curstart == $start) {
250 $pagingbar .= '&nbsp;'.$curpage.'&nbsp;';
251 } else {
252 $pagingbar .= "&nbsp;<a href=\"{$link}{$curstart}{$sistring}\">$curpage</a>&nbsp;";
255 $curstart += COMPLETION_REPORT_PAGE;
258 // Display next link
259 $nstart = $start + COMPLETION_REPORT_PAGE;
260 if ($nstart < $total) {
261 $pagingbar .= "&nbsp;(<a class=\"next\" href=\"{$link}{$nstart}{$sistring}\">".get_string('next').'</a>)';
264 $pagingbar .= '</div>';
267 // Okay, let's draw the table of progress info,
269 // Start of table
270 if (!$csv) {
271 print '<br class="clearer"/>'; // ugh
273 print $pagingbar;
275 if (!$total) {
276 echo $OUTPUT->heading(get_string('nothingtodisplay'));
277 echo $OUTPUT->footer();
278 exit;
281 print '<div id="completion-progress-wrapper" class="no-overflow">';
282 print '<table id="completion-progress" class="generaltable flexible boxaligncenter" style="text-align:left"><thead><tr style="vertical-align:top">';
284 // User heading / sort option
285 print '<th scope="col" class="completion-sortchoice">';
287 $sistring = "&amp;silast={$silast}&amp;sifirst={$sifirst}";
289 if ($firstnamesort) {
290 print
291 get_string('firstname')." / <a href=\"./?course={$course->id}{$sistring}\">".
292 get_string('lastname').'</a>';
293 } else {
294 print "<a href=\"./?course={$course->id}&amp;sort=firstname{$sistring}\">".
295 get_string('firstname').'</a> / '.
296 get_string('lastname');
298 print '</th>';
300 // Print user identity columns
301 foreach ($extrafields as $field) {
302 echo '<th scope="col" class="completion-identifyfield">' .
303 get_user_field_name($field) . '</th>';
305 } else {
306 foreach ($extrafields as $field) {
307 echo $sep . csv_quote(get_user_field_name($field));
311 // Activities
312 $formattedactivities = array();
313 foreach($activities as $activity) {
314 $datepassed = $activity->completionexpected && $activity->completionexpected <= time();
315 $datepassedclass = $datepassed ? 'completion-expired' : '';
317 if ($activity->completionexpected) {
318 if ($csv) {
319 $datetext = userdate($activity->completionexpected, "%F %T");
320 } else {
321 $datetext = userdate($activity->completionexpected, get_string('strftimedate', 'langconfig'));
323 } else {
324 $datetext='';
327 // Some names (labels) come URL-encoded and can be very long, so shorten them
328 $displayname = format_string($activity->name, true, array('context' => $activity->context));
330 if ($csv) {
331 print $sep.csv_quote($displayname).$sep.csv_quote($datetext);
332 } else {
333 $shortenedname = shorten_text($displayname);
334 print '<th scope="col" class="completion-header '.$datepassedclass.'">'.
335 '<a href="'.$CFG->wwwroot.'/mod/'.$activity->modname.
336 '/view.php?id='.$activity->id.'" title="' . s($displayname) . '">'.
337 '<div class="rotated-text-container"><span class="rotated-text">'.$shortenedname.'</span></div>'.
338 '<div class="modicon">'.
339 $OUTPUT->image_icon('icon', get_string('modulename', $activity->modname), $activity->modname) .
340 '</div>'.
341 '</a>';
342 if ($activity->completionexpected) {
343 print '<div class="completion-expected"><span>'.$datetext.'</span></div>';
345 print '</th>';
347 $formattedactivities[$activity->id] = (object)array(
348 'datepassedclass' => $datepassedclass,
349 'displayname' => $displayname,
353 if ($csv) {
354 print $line;
355 } else {
356 print '</tr></thead><tbody>';
359 // Row for each user
360 foreach($progress as $user) {
361 // User name
362 if ($csv) {
363 print csv_quote(fullname($user));
364 foreach ($extrafields as $field) {
365 echo $sep . csv_quote($user->{$field});
367 } else {
368 print '<tr><th scope="row"><a href="'.$CFG->wwwroot.'/user/view.php?id='.
369 $user->id.'&amp;course='.$course->id.'">'.fullname($user).'</a></th>';
370 foreach ($extrafields as $field) {
371 echo '<td>' . s($user->{$field}) . '</td>';
375 // Progress for each activity
376 foreach($activities as $activity) {
378 // Get progress information and state
379 if (array_key_exists($activity->id, $user->progress)) {
380 $thisprogress = $user->progress[$activity->id];
381 $state = $thisprogress->completionstate;
382 $overrideby = $thisprogress->overrideby;
383 $date = userdate($thisprogress->timemodified);
384 } else {
385 $state = COMPLETION_INCOMPLETE;
386 $overrideby = 0;
387 $date = '';
390 // Work out how it corresponds to an icon
391 switch($state) {
392 case COMPLETION_INCOMPLETE :
393 $completiontype = 'n'.($overrideby ? '-override' : '');
394 break;
395 case COMPLETION_COMPLETE :
396 $completiontype = 'y'.($overrideby ? '-override' : '');
397 break;
398 case COMPLETION_COMPLETE_PASS :
399 $completiontype = 'pass';
400 break;
401 case COMPLETION_COMPLETE_FAIL :
402 $completiontype = 'fail';
403 break;
405 $completiontrackingstring = $activity->completion == COMPLETION_TRACKING_AUTOMATIC ? 'auto' : 'manual';
406 $completionicon = 'completion-' . $completiontrackingstring. '-' . $completiontype;
408 if ($overrideby) {
409 $overridebyuser = \core_user::get_user($overrideby, '*', MUST_EXIST);
410 $describe = get_string('completion-' . $completiontype, 'completion', fullname($overridebyuser));
411 } else {
412 $describe = get_string('completion-' . $completiontype, 'completion');
414 $a=new StdClass;
415 $a->state=$describe;
416 $a->date=$date;
417 $a->user=fullname($user);
418 $a->activity = $formattedactivities[$activity->id]->displayname;
419 $fulldescribe=get_string('progress-title','completion',$a);
421 if ($csv) {
422 if ($date != '') {
423 $date = userdate($thisprogress->timemodified, "%F %T");
425 print $sep.csv_quote($describe).$sep.csv_quote($date);
426 } else {
427 $celltext = $OUTPUT->pix_icon('i/' . $completionicon, s($fulldescribe));
428 if (has_capability('moodle/course:overridecompletion', $context) &&
429 $state != COMPLETION_COMPLETE_PASS && $state != COMPLETION_COMPLETE_FAIL) {
430 $newstate = ($state == COMPLETION_COMPLETE) ? COMPLETION_INCOMPLETE : COMPLETION_COMPLETE;
431 $changecompl = $user->id . '-' . $activity->id . '-' . $newstate;
432 $url = new moodle_url($PAGE->url, ['sesskey' => sesskey()]);
433 $celltext = html_writer::link($url, $celltext, array('class' => 'changecompl', 'data-changecompl' => $changecompl,
434 'data-activityname' => $a->activity,
435 'data-userfullname' => $a->user,
436 'data-completiontracking' => $completiontrackingstring,
437 'role' => 'button'));
439 print '<td class="completion-progresscell '.$formattedactivities[$activity->id]->datepassedclass.'">'.
440 $celltext . '</td>';
444 if ($csv) {
445 print $line;
446 } else {
447 print '</tr>';
451 if ($csv) {
452 exit;
454 print '</tbody></table>';
455 print '</div>';
457 print '<ul class="progress-actions"><li><a href="index.php?course='.$course->id.
458 '&amp;format=csv">'.get_string('csvdownload','completion').'</a></li>
459 <li><a href="index.php?course='.$course->id.'&amp;format=excelcsv">'.
460 get_string('excelcsvdownload','completion').'</a></li></ul>';
462 echo $OUTPUT->footer();