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 use core\report_helper
;
27 use \report_progress\local\helper
;
29 require('../../config.php');
30 require_once($CFG->libdir
. '/completionlib.php');
33 $id = required_param('course',PARAM_INT
);
34 $course = $DB->get_record('course',array('id'=>$id));
36 print_error('invalidcourseid');
38 $context = context_course
::instance($course->id
);
40 // Sort (default lastname, optionally firstname)
41 $sort = optional_param('sort','',PARAM_ALPHA
);
42 $firstnamesort = $sort == 'firstname';
45 $format = optional_param('format','',PARAM_ALPHA
);
46 $excel = $format == 'excelcsv';
47 $csv = $format == 'csv' ||
$excel;
49 // Paging, sorting and filtering.
50 $page = optional_param('page', 0, PARAM_INT
);
51 $sifirst = optional_param('sifirst', 'all', PARAM_NOTAGS
);
52 $silast = optional_param('silast', 'all', PARAM_NOTAGS
);
53 $groupid = optional_param('group', 0, PARAM_INT
);
54 $activityinclude = optional_param('activityinclude', 'all', PARAM_TEXT
);
55 $activityorder = optional_param('activityorder', 'orderincourse', PARAM_TEXT
);
57 // Whether to show extra user identity information
58 $userfields = \core_user\fields
::for_identity($context);
59 $extrafields = $userfields->get_required_fields([\core_user\fields
::PURPOSE_IDENTITY
]);
60 $leftcols = 1 +
count($extrafields);
62 function csv_quote($value) {
65 return core_text
::convert('"'.str_replace('"',"'",$value).'"','UTF-8','UTF-16LE');
67 return '"'.str_replace('"',"'",$value).'"';
71 $url = new moodle_url('/report/progress/index.php', array('course'=>$id));
73 $url->param('sort', $sort);
76 $url->param('format', $format);
79 $url->param('page', $page);
81 if ($sifirst !== 'all') {
82 $url->param('sifirst', $sifirst);
84 if ($silast !== 'all') {
85 $url->param('silast', $silast);
88 $url->param('group', $groupid);
90 if ($activityinclude !== '') {
91 $url->param('activityinclude', $activityinclude);
93 if ($activityorder !== '') {
94 $url->param('activityorder', $activityorder);
98 $PAGE->set_pagelayout('report');
100 require_login($course);
102 // Check basic permission
103 require_capability('report/progress:view',$context);
106 $group = groups_get_course_group($course,true); // Supposed to verify group
107 if ($group===0 && $course->groupmode
==SEPARATEGROUPS
) {
108 require_capability('moodle/site:accessallgroups',$context);
111 // Get data on activities and progress of all users, and give error if we've
112 // nothing to display (no users or no activities).
113 $completion = new completion_info($course);
114 list($activitytypes, $activities) = helper
::get_activities_to_show($completion, $activityinclude, $activityorder);
115 $output = $PAGE->get_renderer('report_progress');
117 if ($sifirst !== 'all') {
118 set_user_preference('ifirst', $sifirst);
120 if ($silast !== 'all') {
121 set_user_preference('ilast', $silast);
124 if (!empty($USER->preference
['ifirst'])) {
125 $sifirst = $USER->preference
['ifirst'];
130 if (!empty($USER->preference
['ilast'])) {
131 $silast = $USER->preference
['ilast'];
136 // Generate where clause
138 $where_params = array();
140 if ($sifirst !== 'all') {
141 $where[] = $DB->sql_like('u.firstname', ':sifirst', false, false);
142 $where_params['sifirst'] = $sifirst.'%';
145 if ($silast !== 'all') {
146 $where[] = $DB->sql_like('u.lastname', ':silast', false, false);
147 $where_params['silast'] = $silast.'%';
150 // Get user match count
151 $total = $completion->get_num_tracked_users(implode(' AND ', $where), $where_params, $group);
154 $grandtotal = $completion->get_num_tracked_users('', array(), $group);
160 $progress = $completion->get_progress_all(
161 implode(' AND ', $where),
164 $firstnamesort ?
'u.firstname ASC, u.lastname ASC' : 'u.lastname ASC, u.firstname ASC',
165 $csv ?
0 : helper
::COMPLETION_REPORT_PAGE
,
166 $csv ?
0 : $page * helper
::COMPLETION_REPORT_PAGE
,
171 if ($csv && $grandtotal && count($activities)>0) { // Only show CSV if there are some users/actvs
173 $shortname = format_string($course->shortname
, true, array('context' => $context));
174 header('Content-Disposition: attachment; filename=progress.'.
175 preg_replace('/[^a-z0-9-]/','_',core_text
::strtolower(strip_tags($shortname))).'.csv');
176 // Unicode byte-order mark for Excel
178 header('Content-Type: text/csv; charset=UTF-16LE');
179 print chr(0xFF).chr(0xFE);
183 header('Content-Type: text/csv; charset=UTF-8');
189 // Navigation and header
190 $strreports = get_string("reports");
191 $strcompletion = get_string('activitycompletion', 'completion');
193 $PAGE->set_title($strcompletion);
194 $PAGE->set_heading($course->fullname
);
195 echo $OUTPUT->header();
197 // Print the selected dropdown.
198 $pluginname = get_string('pluginname', 'report_progress');
199 report_helper
::print_report_selector($pluginname);
200 $PAGE->requires
->js_call_amd('report_progress/completion_override', 'init', [fullname($USER)]);
202 // Handle groups (if enabled).
203 echo $output->render_groups_select($url, $course);
205 // Display include activity filter.
206 echo $output->render_include_activity_select($url, $activitytypes, $activityinclude);
208 // Display activity order options.
209 echo $output->render_activity_order_select($url, $activityorder);
213 if (count($activities)==0) {
214 echo $OUTPUT->container(get_string('err_noactivities', 'completion'), 'errorbox errorboxcontent');
215 echo $OUTPUT->footer();
219 // If no users in this course what-so-ever
221 echo $OUTPUT->container(get_string('err_nousers', 'completion'), 'errorbox errorboxcontent');
222 echo $OUTPUT->footer();
226 // Build link for paging
227 $link = $CFG->wwwroot
.'/report/progress/?course='.$course->id
;
229 $link .= '&sort='.$sort;
231 $link .= '&start=';
236 $prefixfirst = 'sifirst';
237 $prefixlast = 'silast';
239 // The URL used in the initials bar should reset the 'start' parameter.
240 $initialsbarurl = fullclone($url);
241 $initialsbarurl->remove_params('page');
243 $pagingbar .= $OUTPUT->initials_bar($sifirst, 'firstinitial mt-2', get_string('firstname'), $prefixfirst, $initialsbarurl);
244 $pagingbar .= $OUTPUT->initials_bar($silast, 'lastinitial', get_string('lastname'), $prefixlast, $initialsbarurl);
245 $pagingbar .= $OUTPUT->paging_bar($total, $page, helper
::COMPLETION_REPORT_PAGE
, $url);
247 // Okay, let's draw the table of progress info,
251 print '<br class="clearer"/>'; // ugh
256 echo $OUTPUT->heading(get_string('nothingtodisplay'));
257 echo $OUTPUT->footer();
261 print '<div id="completion-progress-wrapper" class="no-overflow">';
262 print '<table id="completion-progress" class="generaltable flexible boxaligncenter" style="text-align:left"><thead><tr style="vertical-align:top">';
264 // User heading / sort option
265 print '<th scope="col" class="completion-sortchoice">';
267 $sorturl = fullclone($url);
268 if ($firstnamesort) {
269 $sorturl->param('sort', 'lastname');
270 $sortlink = html_writer
::link($sorturl, get_string('lastname'));
272 get_string('firstname') . " / $sortlink";
274 $sorturl->param('sort', 'firstname');
275 $sortlink = html_writer
::link($sorturl, get_string('firstname'));
276 print "$sortlink / " . get_string('lastname');
280 // Print user identity columns
281 foreach ($extrafields as $field) {
282 echo '<th scope="col" class="completion-identifyfield">' .
283 \core_user\fields
::get_display_name($field) . '</th>';
286 foreach ($extrafields as $field) {
287 echo $sep . csv_quote(\core_user\fields
::get_display_name($field));
292 $formattedactivities = array();
293 foreach($activities as $activity) {
294 $datepassed = $activity->completionexpected
&& $activity->completionexpected
<= time();
295 $datepassedclass = $datepassed ?
'completion-expired' : '';
297 if ($activity->completionexpected
) {
299 $datetext = userdate($activity->completionexpected
, "%F %T");
301 $datetext = userdate($activity->completionexpected
, get_string('strftimedate', 'langconfig'));
307 // Some names (labels) come URL-encoded and can be very long, so shorten them
308 $displayname = format_string($activity->name
, true, array('context' => $activity->context
));
311 print $sep.csv_quote($displayname).$sep.csv_quote($datetext);
313 $shortenedname = shorten_text($displayname);
314 print '<th scope="col" class="completion-header '.$datepassedclass.'">'.
315 '<a href="'.$CFG->wwwroot
.'/mod/'.$activity->modname
.
316 '/view.php?id='.$activity->id
.'" title="' . s($displayname) . '">'.
317 '<div class="rotated-text-container"><span class="rotated-text">'.$shortenedname.'</span></div>'.
318 '<div class="modicon">'.
319 $OUTPUT->image_icon('monologo', get_string('modulename', $activity->modname
), $activity->modname
) .
322 if ($activity->completionexpected
) {
323 print '<div class="completion-expected"><span>'.$datetext.'</span></div>';
327 $formattedactivities[$activity->id
] = (object)array(
328 'datepassedclass' => $datepassedclass,
329 'displayname' => $displayname,
336 print '</tr></thead><tbody>';
340 foreach($progress as $user) {
343 print csv_quote(fullname($user, has_capability('moodle/site:viewfullnames', $context)));
344 foreach ($extrafields as $field) {
345 echo $sep . csv_quote($user->{$field});
348 print '<tr><th scope="row"><a href="' . $CFG->wwwroot
. '/user/view.php?id=' .
349 $user->id
. '&course=' . $course->id
. '">' .
350 fullname($user, has_capability('moodle/site:viewfullnames', $context)) . '</a></th>';
351 foreach ($extrafields as $field) {
352 echo '<td>' . s($user->{$field}) . '</td>';
356 // Progress for each activity
357 foreach($activities as $activity) {
359 // Get progress information and state
360 if (array_key_exists($activity->id
, $user->progress
)) {
361 $thisprogress = $user->progress
[$activity->id
];
362 $state = $thisprogress->completionstate
;
363 $overrideby = $thisprogress->overrideby
;
364 $date = userdate($thisprogress->timemodified
);
366 $state = COMPLETION_INCOMPLETE
;
371 // Work out how it corresponds to an icon
373 case COMPLETION_INCOMPLETE
:
374 $completiontype = 'n'.($overrideby ?
'-override' : '');
376 case COMPLETION_COMPLETE
:
377 $completiontype = 'y'.($overrideby ?
'-override' : '');
379 case COMPLETION_COMPLETE_PASS
:
380 $completiontype = 'pass';
382 case COMPLETION_COMPLETE_FAIL
:
383 $completiontype = 'fail';
386 $completiontrackingstring = $activity->completion
== COMPLETION_TRACKING_AUTOMATIC ?
'auto' : 'manual';
387 $completionicon = 'completion-' . $completiontrackingstring. '-' . $completiontype;
390 $overridebyuser = \core_user
::get_user($overrideby, '*', MUST_EXIST
);
391 $describe = get_string('completion-' . $completiontype, 'completion', fullname($overridebyuser));
393 $describe = get_string('completion-' . $completiontype, 'completion');
398 $a->user
= fullname($user, has_capability('moodle/site:viewfullnames', $context));
399 $a->activity
= $formattedactivities[$activity->id
]->displayname
;
400 $fulldescribe=get_string('progress-title','completion',$a);
404 $date = userdate($thisprogress->timemodified
, "%F %T");
406 print $sep.csv_quote($describe).$sep.csv_quote($date);
408 $celltext = $OUTPUT->pix_icon('i/' . $completionicon, s($fulldescribe));
409 if (has_capability('moodle/course:overridecompletion', $context) &&
410 $state != COMPLETION_COMPLETE_PASS
&& $state != COMPLETION_COMPLETE_FAIL
) {
411 $newstate = ($state == COMPLETION_COMPLETE
) ? COMPLETION_INCOMPLETE
: COMPLETION_COMPLETE
;
412 $changecompl = $user->id
. '-' . $activity->id
. '-' . $newstate;
413 $url = new moodle_url($PAGE->url
, ['sesskey' => sesskey()]);
414 $celltext = html_writer
::link($url, $celltext, array('class' => 'changecompl', 'data-changecompl' => $changecompl,
415 'data-activityname' => $a->activity
,
416 'data-userfullname' => $a->user
,
417 'data-completiontracking' => $completiontrackingstring,
418 'role' => 'button'));
420 print '<td class="completion-progresscell '.$formattedactivities[$activity->id
]->datepassedclass
.'">'.
435 print '</tbody></table>';
438 echo $output->render_download_buttons($url);
440 echo $OUTPUT->footer();