MDL-57727 reports: Activity completion report sorting problem
[moodle.git] / report / progress / index.php
CommitLineData
4e781c7b 1<?php
3afce1a5
PS
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.
13//
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/>.
16
17/**
18 * Activity progress reports
19 *
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
24 */
25
26require('../../config.php');
4e1132a8 27require_once($CFG->libdir . '/completionlib.php');
4e781c7b 28
24a3b341 29define('COMPLETION_REPORT_PAGE', 25);
35da0e87 30
4e781c7b 31// Get course
dfab77a2 32$id = required_param('course',PARAM_INT);
3afce1a5
PS
33$course = $DB->get_record('course',array('id'=>$id));
34if (!$course) {
63391854 35 print_error('invalidcourseid');
4e781c7b 36}
24e14bc7 37$context = context_course::instance($course->id);
4e781c7b 38
39// Sort (default lastname, optionally firstname)
24a3b341
AB
40$sort = optional_param('sort','',PARAM_ALPHA);
41$firstnamesort = $sort == 'firstname';
4e781c7b 42
43// CSV format
24a3b341
AB
44$format = optional_param('format','',PARAM_ALPHA);
45$excel = $format == 'excelcsv';
46$csv = $format == 'csv' || $excel;
4e781c7b 47
24a3b341
AB
48// Paging
49$start = optional_param('start', 0, PARAM_INT);
573558af
AL
50$sifirst = optional_param('sifirst', 'all', PARAM_NOTAGS);
51$silast = optional_param('silast', 'all', PARAM_NOTAGS);
3afce1a5 52$start = optional_param('start', 0, PARAM_INT);
35da0e87 53
24e14bc7 54// Whether to show extra user identity information
55$extrafields = get_extra_user_fields($context);
56$leftcols = 1 + count($extrafields);
f9498855 57
4e781c7b 58function csv_quote($value) {
b3aa7ae8 59 global $excel;
3afce1a5 60 if ($excel) {
2f1e464a 61 return core_text::convert('"'.str_replace('"',"'",$value).'"','UTF-8','UTF-16LE');
b3aa7ae8 62 } else {
63 return '"'.str_replace('"',"'",$value).'"';
64 }
4e781c7b 65}
66
8619857d 67$url = new moodle_url('/report/progress/index.php', array('course'=>$id));
dfab77a2 68if ($sort !== '') {
69 $url->param('sort', $sort);
70}
71if ($format !== '') {
72 $url->param('format', $format);
73}
7c4efe3b 74if ($start !== 0) {
dfab77a2 75 $url->param('start', $start);
76}
a39918da
EEAK
77if ($sifirst !== 'all') {
78 $url->param('sifirst', $sifirst);
79}
80if ($silast !== 'all') {
81 $url->param('silast', $silast);
82}
dfab77a2 83$PAGE->set_url($url);
cf5a0dbd 84$PAGE->set_pagelayout('report');
dfab77a2 85
a2e4bf7f 86require_login($course);
4e781c7b 87
88// Check basic permission
8619857d 89require_capability('report/progress:view',$context);
4e781c7b 90
91// Get group mode
3afce1a5
PS
92$group = groups_get_course_group($course,true); // Supposed to verify group
93if ($group===0 && $course->groupmode==SEPARATEGROUPS) {
4e781c7b 94 require_capability('moodle/site:accessallgroups',$context);
95}
96
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)
3afce1a5
PS
99$reportsurl = $CFG->wwwroot.'/course/report.php?id='.$course->id;
100$completion = new completion_info($course);
101$activities = $completion->get_activities();
4e781c7b 102
8b844f70
IT
103if ($sifirst !== 'all') {
104 set_user_preference('ifirst', $sifirst);
105}
106if ($silast !== 'all') {
107 set_user_preference('ilast', $silast);
108}
109
110if (!empty($USER->preference['ifirst'])) {
111 $sifirst = $USER->preference['ifirst'];
112} else {
113 $sifirst = 'all';
114}
115
116if (!empty($USER->preference['ilast'])) {
117 $silast = $USER->preference['ilast'];
118} else {
119 $silast = 'all';
120}
121
24a3b341
AB
122// Generate where clause
123$where = array();
5642a8e5 124$where_params = array();
24a3b341
AB
125
126if ($sifirst !== 'all') {
5642a8e5
AB
127 $where[] = $DB->sql_like('u.firstname', ':sifirst', false);
128 $where_params['sifirst'] = $sifirst.'%';
24a3b341
AB
129}
130
131if ($silast !== 'all') {
5642a8e5
AB
132 $where[] = $DB->sql_like('u.lastname', ':silast', false);
133 $where_params['silast'] = $silast.'%';
24a3b341
AB
134}
135
136// Get user match count
5642a8e5 137$total = $completion->get_num_tracked_users(implode(' AND ', $where), $where_params, $group);
24a3b341
AB
138
139// Total user count
5642a8e5 140$grandtotal = $completion->get_num_tracked_users('', array(), $group);
24a3b341 141
24a3b341
AB
142// Get user data
143$progress = array();
144
145if ($total) {
146 $progress = $completion->get_progress_all(
147 implode(' AND ', $where),
5642a8e5 148 $where_params,
24a3b341 149 $group,
b68b5f55 150 $firstnamesort ? 'u.firstname ASC, u.lastname ASC' : 'u.lastname ASC, u.firstname ASC',
24a3b341 151 $csv ? 0 : COMPLETION_REPORT_PAGE,
24e14bc7 152 $csv ? 0 : $start,
153 $context
24a3b341
AB
154 );
155}
b3aa7ae8 156
3afce1a5 157if ($csv && $grandtotal && count($activities)>0) { // Only show CSV if there are some users/actvs
8ebbb06a
SH
158
159 $shortname = format_string($course->shortname, true, array('context' => $context));
4e781c7b 160 header('Content-Disposition: attachment; filename=progress.'.
2f1e464a 161 preg_replace('/[^a-z0-9-]/','_',core_text::strtolower(strip_tags($shortname))).'.csv');
b3aa7ae8 162 // Unicode byte-order mark for Excel
3afce1a5 163 if ($excel) {
b3aa7ae8 164 header('Content-Type: text/csv; charset=UTF-16LE');
165 print chr(0xFF).chr(0xFE);
166 $sep="\t".chr(0);
63391854 167 $line="\n".chr(0);
b3aa7ae8 168 } else {
169 header('Content-Type: text/csv; charset=UTF-8');
170 $sep=",";
171 $line="\n";
172 }
4e781c7b 173} else {
4e781c7b 174
175 // Navigation and header
176 $strreports = get_string("reports");
cfc33925 177 $strcompletion = get_string('activitycompletion', 'completion');
0a122046 178
179 $PAGE->set_title($strcompletion);
180 $PAGE->set_heading($course->fullname);
0a122046 181 echo $OUTPUT->header();
b08337c3 182 $PAGE->requires->js_call_amd('report_progress/completion_override', 'init', [fullname($USER)]);
4e781c7b 183
184 // Handle groups (if enabled)
8619857d 185 groups_print_course_menu($course,$CFG->wwwroot.'/report/progress/?course='.$course->id);
4e781c7b 186}
187
3afce1a5 188if (count($activities)==0) {
1c6ed505 189 echo $OUTPUT->container(get_string('err_noactivities', 'completion'), 'errorbox errorboxcontent');
190 echo $OUTPUT->footer();
191 exit;
192}
193
194// If no users in this course what-so-ever
195if (!$grandtotal) {
196 echo $OUTPUT->container(get_string('err_nousers', 'completion'), 'errorbox errorboxcontent');
197 echo $OUTPUT->footer();
198 exit;
199}
200
24a3b341 201// Build link for paging
8619857d 202$link = $CFG->wwwroot.'/report/progress/?course='.$course->id;
24a3b341
AB
203if (strlen($sort)) {
204 $link .= '&amp;sort='.$sort;
205}
206$link .= '&amp;start=';
207
24a3b341 208$pagingbar = '';
24a3b341 209
8b844f70
IT
210// Initials bar.
211$prefixfirst = 'sifirst';
212$prefixlast = 'silast';
213$pagingbar .= $OUTPUT->initials_bar($sifirst, 'firstinitial', get_string('firstname'), $prefixfirst, $url);
214$pagingbar .= $OUTPUT->initials_bar($silast, 'lastinitial', get_string('lastname'), $prefixlast, $url);
24a3b341
AB
215
216// Do we need a paging bar?
3afce1a5 217if ($total > COMPLETION_REPORT_PAGE) {
35da0e87 218
24a3b341
AB
219 // Paging bar
220 $pagingbar .= '<div class="paging">';
221 $pagingbar .= get_string('page').': ';
222
1f186372 223 $sistrings = array();
7229f239
SH
224 if ($sifirst != 'all') {
225 $sistrings[] = "sifirst={$sifirst}";
226 }
227 if ($silast != 'all') {
228 $sistrings[] = "silast={$silast}";
229 }
230 $sistring = !empty($sistrings) ? '&amp;'.implode('&amp;', $sistrings) : '';
1f186372 231
24a3b341
AB
232 // Display previous link
233 if ($start > 0) {
234 $pstart = max($start - COMPLETION_REPORT_PAGE, 0);
1f186372 235 $pagingbar .= "(<a class=\"previous\" href=\"{$link}{$pstart}{$sistring}\">".get_string('previous').'</a>)&nbsp;';
35da0e87 236 }
237
24a3b341
AB
238 // Create page links
239 $curstart = 0;
240 $curpage = 0;
241 while ($curstart < $total) {
242 $curpage++;
243
244 if ($curstart == $start) {
245 $pagingbar .= '&nbsp;'.$curpage.'&nbsp;';
3afce1a5 246 } else {
1f186372 247 $pagingbar .= "&nbsp;<a href=\"{$link}{$curstart}{$sistring}\">$curpage</a>&nbsp;";
24a3b341
AB
248 }
249
250 $curstart += COMPLETION_REPORT_PAGE;
251 }
252
253 // Display next link
254 $nstart = $start + COMPLETION_REPORT_PAGE;
255 if ($nstart < $total) {
1f186372 256 $pagingbar .= "&nbsp;(<a class=\"next\" href=\"{$link}{$nstart}{$sistring}\">".get_string('next').'</a>)';
24a3b341
AB
257 }
258
259 $pagingbar .= '</div>';
35da0e87 260}
261
4e781c7b 262// Okay, let's draw the table of progress info,
263
63391854 264// Start of table
3afce1a5 265if (!$csv) {
4e781c7b 266 print '<br class="clearer"/>'; // ugh
24a3b341
AB
267
268 print $pagingbar;
269
270 if (!$total) {
035f3a4f
SH
271 echo $OUTPUT->heading(get_string('nothingtodisplay'));
272 echo $OUTPUT->footer();
4e781c7b 273 exit;
274 }
24a3b341 275
180bf7d4 276 print '<div id="completion-progress-wrapper" class="no-overflow">';
fea8be42 277 print '<table id="completion-progress" class="generaltable flexible boxaligncenter" style="text-align:left"><thead><tr style="vertical-align:top">';
4e781c7b 278
279 // User heading / sort option
280 print '<th scope="col" class="completion-sortchoice">';
1f186372
TL
281
282 $sistring = "&amp;silast={$silast}&amp;sifirst={$sifirst}";
283
3afce1a5 284 if ($firstnamesort) {
63391854 285 print
1f186372 286 get_string('firstname')." / <a href=\"./?course={$course->id}{$sistring}\">".
4e781c7b 287 get_string('lastname').'</a>';
288 } else {
1f186372 289 print "<a href=\"./?course={$course->id}&amp;sort=firstname{$sistring}\">".
4e781c7b 290 get_string('firstname').'</a> / '.
291 get_string('lastname');
292 }
293 print '</th>';
63391854 294
24e14bc7 295 // Print user identity columns
296 foreach ($extrafields as $field) {
297 echo '<th scope="col" class="completion-identifyfield">' .
298 get_user_field_name($field) . '</th>';
f9498855 299 }
f9498855 300} else {
24e14bc7 301 foreach ($extrafields as $field) {
302 echo $sep . csv_quote(get_user_field_name($field));
f9498855 303 }
4e781c7b 304}
305
306// Activities
88715ba2 307$formattedactivities = array();
4e781c7b 308foreach($activities as $activity) {
88715ba2
MG
309 $datepassed = $activity->completionexpected && $activity->completionexpected <= time();
310 $datepassedclass = $datepassed ? 'completion-expired' : '';
4e781c7b 311
3afce1a5 312 if ($activity->completionexpected) {
4e781c7b 313 $datetext=userdate($activity->completionexpected,get_string('strftimedate','langconfig'));
314 } else {
315 $datetext='';
316 }
63391854 317
82fd829d 318 // Some names (labels) come URL-encoded and can be very long, so shorten them
bdbe385c 319 $displayname = format_string($activity->name, true, array('context' => $activity->context));
4e781c7b 320
3afce1a5 321 if ($csv) {
bdbe385c 322 print $sep.csv_quote($displayname).$sep.csv_quote($datetext);
4e781c7b 323 } else {
bdbe385c 324 $shortenedname = shorten_text($displayname);
c73e3ef1 325 print '<th scope="col" class="completion-header '.$datepassedclass.'">'.
4e781c7b 326 '<a href="'.$CFG->wwwroot.'/mod/'.$activity->modname.
bdbe385c 327 '/view.php?id='.$activity->id.'" title="' . s($displayname) . '">'.
c73e3ef1 328 '<div class="rotated-text-container"><span class="rotated-text">'.$shortenedname.'</span></div>'.
663640f5 329 '<div class="modicon">'.
3e6adcd6 330 $OUTPUT->image_icon('icon', get_string('modulename', $activity->modname), $activity->modname) .
663640f5
DW
331 '</div>'.
332 '</a>';
3afce1a5 333 if ($activity->completionexpected) {
4e781c7b 334 print '<div class="completion-expected"><span>'.$datetext.'</span></div>';
335 }
336 print '</th>';
337 }
88715ba2
MG
338 $formattedactivities[$activity->id] = (object)array(
339 'datepassedclass' => $datepassedclass,
340 'displayname' => $displayname,
341 );
4e781c7b 342}
343
3afce1a5 344if ($csv) {
b3aa7ae8 345 print $line;
4e781c7b 346} else {
fea8be42 347 print '</tr></thead><tbody>';
4e781c7b 348}
349
350// Row for each user
24a3b341 351foreach($progress as $user) {
4e781c7b 352 // User name
3afce1a5 353 if ($csv) {
4e781c7b 354 print csv_quote(fullname($user));
24e14bc7 355 foreach ($extrafields as $field) {
356 echo $sep . csv_quote($user->{$field});
f9498855 357 }
4e781c7b 358 } else {
359 print '<tr><th scope="row"><a href="'.$CFG->wwwroot.'/user/view.php?id='.
360 $user->id.'&amp;course='.$course->id.'">'.fullname($user).'</a></th>';
24e14bc7 361 foreach ($extrafields as $field) {
362 echo '<td>' . s($user->{$field}) . '</td>';
f9498855 363 }
4e781c7b 364 }
365
366 // Progress for each activity
367 foreach($activities as $activity) {
368
369 // Get progress information and state
6f4adc6b
JD
370 if (array_key_exists($activity->id, $user->progress)) {
371 $thisprogress = $user->progress[$activity->id];
372 $state = $thisprogress->completionstate;
a39918da 373 $overrideby = $thisprogress->overrideby;
6f4adc6b 374 $date = userdate($thisprogress->timemodified);
4e781c7b 375 } else {
6f4adc6b 376 $state = COMPLETION_INCOMPLETE;
a39918da 377 $overrideby = 0;
6f4adc6b 378 $date = '';
4e781c7b 379 }
380
381 // Work out how it corresponds to an icon
4e781c7b 382 switch($state) {
a39918da
EEAK
383 case COMPLETION_INCOMPLETE :
384 $completiontype = 'n'.($overrideby ? '-override' : '');
385 break;
386 case COMPLETION_COMPLETE :
387 $completiontype = 'y'.($overrideby ? '-override' : '');
388 break;
389 case COMPLETION_COMPLETE_PASS :
390 $completiontype = 'pass';
391 break;
392 case COMPLETION_COMPLETE_FAIL :
393 $completiontype = 'fail';
394 break;
63391854 395 }
b08337c3
JD
396 $completiontrackingstring = $activity->completion == COMPLETION_TRACKING_AUTOMATIC ? 'auto' : 'manual';
397 $completionicon = 'completion-' . $completiontrackingstring. '-' . $completiontype;
63391854 398
a39918da 399 if ($overrideby) {
8fbc41d8 400 $overridebyuser = \core_user::get_user($overrideby, '*', MUST_EXIST);
a39918da
EEAK
401 $describe = get_string('completion-' . $completiontype, 'completion', fullname($overridebyuser));
402 } else {
403 $describe = get_string('completion-' . $completiontype, 'completion');
404 }
4e781c7b 405 $a=new StdClass;
406 $a->state=$describe;
407 $a->date=$date;
408 $a->user=fullname($user);
bdbe385c 409 $a->activity = $formattedactivities[$activity->id]->displayname;
4e781c7b 410 $fulldescribe=get_string('progress-title','completion',$a);
411
3afce1a5 412 if ($csv) {
b3aa7ae8 413 print $sep.csv_quote($describe).$sep.csv_quote($date);
4e781c7b 414 } else {
6f4adc6b 415 $celltext = $OUTPUT->pix_icon('i/' . $completionicon, s($fulldescribe));
a39918da
EEAK
416 if (has_capability('moodle/course:overridecompletion', $context) &&
417 $state != COMPLETION_COMPLETE_PASS && $state != COMPLETION_COMPLETE_FAIL) {
418 $newstate = ($state == COMPLETION_COMPLETE) ? COMPLETION_INCOMPLETE : COMPLETION_COMPLETE;
419 $changecompl = $user->id . '-' . $activity->id . '-' . $newstate;
8fbc41d8 420 $url = new moodle_url($PAGE->url, ['sesskey' => sesskey()]);
194f1422 421 $celltext = html_writer::link($url, $celltext, array('class' => 'changecompl', 'data-changecompl' => $changecompl,
b08337c3
JD
422 'data-activityname' => $a->activity,
423 'data-userfullname' => $a->user,
424 'data-completiontracking' => $completiontrackingstring,
194f1422 425 'aria-role' => 'button'));
a39918da 426 }
88715ba2 427 print '<td class="completion-progresscell '.$formattedactivities[$activity->id]->datepassedclass.'">'.
a39918da 428 $celltext . '</td>';
4e781c7b 429 }
430 }
431
3afce1a5 432 if ($csv) {
b3aa7ae8 433 print $line;
4e781c7b 434 } else {
435 print '</tr>';
436 }
437}
438
3afce1a5 439if ($csv) {
4e781c7b 440 exit;
441}
fea8be42 442print '</tbody></table>';
180bf7d4 443print '</div>';
35da0e87 444print $pagingbar;
4e781c7b 445
446print '<ul class="progress-actions"><li><a href="index.php?course='.$course->id.
b3aa7ae8 447 '&amp;format=csv">'.get_string('csvdownload','completion').'</a></li>
448 <li><a href="index.php?course='.$course->id.'&amp;format=excelcsv">'.
449 get_string('excelcsvdownload','completion').'</a></li></ul>';
4e781c7b 450
d60c1124 451echo $OUTPUT->footer();
aa6c1ced 452