MDL-32535 Completion lang string incorrect use, text
[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);
50$sifirst = optional_param('sifirst', 'all', PARAM_ALPHA);
51$silast = optional_param('silast', 'all', PARAM_ALPHA);
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) {
f8311def 61 return textlib::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}
77$PAGE->set_url($url);
cf5a0dbd 78$PAGE->set_pagelayout('report');
dfab77a2 79
a2e4bf7f 80require_login($course);
4e781c7b 81
82// Check basic permission
8619857d 83require_capability('report/progress:view',$context);
4e781c7b 84
85// Get group mode
3afce1a5
PS
86$group = groups_get_course_group($course,true); // Supposed to verify group
87if ($group===0 && $course->groupmode==SEPARATEGROUPS) {
4e781c7b 88 require_capability('moodle/site:accessallgroups',$context);
89}
90
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)
3afce1a5
PS
93$reportsurl = $CFG->wwwroot.'/course/report.php?id='.$course->id;
94$completion = new completion_info($course);
95$activities = $completion->get_activities();
4e781c7b 96
24a3b341
AB
97// Generate where clause
98$where = array();
5642a8e5 99$where_params = array();
24a3b341
AB
100
101if ($sifirst !== 'all') {
5642a8e5
AB
102 $where[] = $DB->sql_like('u.firstname', ':sifirst', false);
103 $where_params['sifirst'] = $sifirst.'%';
24a3b341
AB
104}
105
106if ($silast !== 'all') {
5642a8e5
AB
107 $where[] = $DB->sql_like('u.lastname', ':silast', false);
108 $where_params['silast'] = $silast.'%';
24a3b341
AB
109}
110
111// Get user match count
5642a8e5 112$total = $completion->get_num_tracked_users(implode(' AND ', $where), $where_params, $group);
24a3b341
AB
113
114// Total user count
5642a8e5 115$grandtotal = $completion->get_num_tracked_users('', array(), $group);
24a3b341 116
24a3b341
AB
117// Get user data
118$progress = array();
119
120if ($total) {
121 $progress = $completion->get_progress_all(
122 implode(' AND ', $where),
5642a8e5 123 $where_params,
24a3b341
AB
124 $group,
125 $firstnamesort ? 'u.firstname ASC' : 'u.lastname ASC',
126 $csv ? 0 : COMPLETION_REPORT_PAGE,
24e14bc7 127 $csv ? 0 : $start,
128 $context
24a3b341
AB
129 );
130}
b3aa7ae8 131
3afce1a5 132if ($csv && $grandtotal && count($activities)>0) { // Only show CSV if there are some users/actvs
8ebbb06a
SH
133
134 $shortname = format_string($course->shortname, true, array('context' => $context));
4e781c7b 135 header('Content-Disposition: attachment; filename=progress.'.
f8311def 136 preg_replace('/[^a-z0-9-]/','_',textlib::strtolower(strip_tags($shortname))).'.csv');
b3aa7ae8 137 // Unicode byte-order mark for Excel
3afce1a5 138 if ($excel) {
b3aa7ae8 139 header('Content-Type: text/csv; charset=UTF-16LE');
140 print chr(0xFF).chr(0xFE);
141 $sep="\t".chr(0);
63391854 142 $line="\n".chr(0);
b3aa7ae8 143 } else {
144 header('Content-Type: text/csv; charset=UTF-8');
145 $sep=",";
146 $line="\n";
147 }
4e781c7b 148} else {
149 // Use SVG to draw sideways text if supported
0eab1dc0 150 $svgcleverness = can_use_rotated_text();
4e781c7b 151
152 // Navigation and header
153 $strreports = get_string("reports");
cfc33925 154 $strcompletion = get_string('activitycompletion', 'completion');
0a122046 155
156 $PAGE->set_title($strcompletion);
157 $PAGE->set_heading($course->fullname);
0a122046 158 echo $OUTPUT->header();
159
3afce1a5 160 if ($svgcleverness) {
f44b10ed 161 $PAGE->requires->yui2_lib('event');
8619857d 162 $PAGE->requires->js('/report/progress/textrotate.js');
4e781c7b 163 }
164
165 // Handle groups (if enabled)
8619857d 166 groups_print_course_menu($course,$CFG->wwwroot.'/report/progress/?course='.$course->id);
4e781c7b 167}
168
3afce1a5 169if (count($activities)==0) {
1c6ed505 170 echo $OUTPUT->container(get_string('err_noactivities', 'completion'), 'errorbox errorboxcontent');
171 echo $OUTPUT->footer();
172 exit;
173}
174
175// If no users in this course what-so-ever
176if (!$grandtotal) {
177 echo $OUTPUT->container(get_string('err_nousers', 'completion'), 'errorbox errorboxcontent');
178 echo $OUTPUT->footer();
179 exit;
180}
181
24a3b341 182// Build link for paging
8619857d 183$link = $CFG->wwwroot.'/report/progress/?course='.$course->id;
24a3b341
AB
184if (strlen($sort)) {
185 $link .= '&amp;sort='.$sort;
186}
187$link .= '&amp;start=';
188
189// Build the the page by Initial bar
190$initials = array('first', 'last');
191$alphabet = explode(',', get_string('alphabet', 'langconfig'));
192
193$pagingbar = '';
194foreach ($initials as $initial) {
195 $var = 'si'.$initial;
196
1f186372
TL
197 $othervar = $initial == 'first' ? 'silast' : 'sifirst';
198 $othervar = $$othervar != 'all' ? "&amp;{$othervar}={$$othervar}" : '';
199
24a3b341
AB
200 $pagingbar .= ' <div class="initialbar '.$initial.'initial">';
201 $pagingbar .= get_string($initial.'name').':&nbsp;';
35da0e87 202
24a3b341
AB
203 if ($$var == 'all') {
204 $pagingbar .= '<strong>'.get_string('all').'</strong> ';
205 }
206 else {
1f186372 207 $pagingbar .= "<a href=\"{$link}{$othervar}\">".get_string('all').'</a> ';
24a3b341
AB
208 }
209
210 foreach ($alphabet as $letter) {
211 if ($$var === $letter) {
212 $pagingbar .= '<strong>'.$letter.'</strong> ';
213 }
214 else {
1f186372 215 $pagingbar .= "<a href=\"$link&amp;$var={$letter}{$othervar}\">$letter</a> ";
35da0e87 216 }
35da0e87 217 }
218
24a3b341
AB
219 $pagingbar .= '</div>';
220}
221
222// Do we need a paging bar?
3afce1a5 223if ($total > COMPLETION_REPORT_PAGE) {
35da0e87 224
24a3b341
AB
225 // Paging bar
226 $pagingbar .= '<div class="paging">';
227 $pagingbar .= get_string('page').': ';
228
1f186372 229 $sistrings = array();
7229f239
SH
230 if ($sifirst != 'all') {
231 $sistrings[] = "sifirst={$sifirst}";
232 }
233 if ($silast != 'all') {
234 $sistrings[] = "silast={$silast}";
235 }
236 $sistring = !empty($sistrings) ? '&amp;'.implode('&amp;', $sistrings) : '';
1f186372 237
24a3b341
AB
238 // Display previous link
239 if ($start > 0) {
240 $pstart = max($start - COMPLETION_REPORT_PAGE, 0);
1f186372 241 $pagingbar .= "(<a class=\"previous\" href=\"{$link}{$pstart}{$sistring}\">".get_string('previous').'</a>)&nbsp;';
35da0e87 242 }
243
24a3b341
AB
244 // Create page links
245 $curstart = 0;
246 $curpage = 0;
247 while ($curstart < $total) {
248 $curpage++;
249
250 if ($curstart == $start) {
251 $pagingbar .= '&nbsp;'.$curpage.'&nbsp;';
3afce1a5 252 } else {
1f186372 253 $pagingbar .= "&nbsp;<a href=\"{$link}{$curstart}{$sistring}\">$curpage</a>&nbsp;";
24a3b341
AB
254 }
255
256 $curstart += COMPLETION_REPORT_PAGE;
257 }
258
259 // Display next link
260 $nstart = $start + COMPLETION_REPORT_PAGE;
261 if ($nstart < $total) {
1f186372 262 $pagingbar .= "&nbsp;(<a class=\"next\" href=\"{$link}{$nstart}{$sistring}\">".get_string('next').'</a>)';
24a3b341
AB
263 }
264
265 $pagingbar .= '</div>';
35da0e87 266}
267
4e781c7b 268// Okay, let's draw the table of progress info,
269
63391854 270// Start of table
3afce1a5 271if (!$csv) {
4e781c7b 272 print '<br class="clearer"/>'; // ugh
24a3b341
AB
273
274 print $pagingbar;
275
276 if (!$total) {
035f3a4f
SH
277 echo $OUTPUT->heading(get_string('nothingtodisplay'));
278 echo $OUTPUT->footer();
4e781c7b 279 exit;
280 }
24a3b341 281
180bf7d4 282 print '<div id="completion-progress-wrapper" class="no-overflow">';
4e781c7b 283 print '<table id="completion-progress" class="generaltable flexible boxaligncenter" style="text-align:left"><tr style="vertical-align:top">';
284
285 // User heading / sort option
286 print '<th scope="col" class="completion-sortchoice">';
1f186372
TL
287
288 $sistring = "&amp;silast={$silast}&amp;sifirst={$sifirst}";
289
3afce1a5 290 if ($firstnamesort) {
63391854 291 print
1f186372 292 get_string('firstname')." / <a href=\"./?course={$course->id}{$sistring}\">".
4e781c7b 293 get_string('lastname').'</a>';
294 } else {
1f186372 295 print "<a href=\"./?course={$course->id}&amp;sort=firstname{$sistring}\">".
4e781c7b 296 get_string('firstname').'</a> / '.
297 get_string('lastname');
298 }
299 print '</th>';
63391854 300
24e14bc7 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>';
f9498855 305 }
f9498855 306} else {
24e14bc7 307 foreach ($extrafields as $field) {
308 echo $sep . csv_quote(get_user_field_name($field));
f9498855 309 }
4e781c7b 310}
311
312// Activities
313foreach($activities as $activity) {
314 $activity->datepassed = $activity->completionexpected && $activity->completionexpected <= time();
315 $activity->datepassedclass=$activity->datepassed ? 'completion-expired' : '';
316
3afce1a5 317 if ($activity->completionexpected) {
4e781c7b 318 $datetext=userdate($activity->completionexpected,get_string('strftimedate','langconfig'));
319 } else {
320 $datetext='';
321 }
63391854 322
82fd829d 323 // Some names (labels) come URL-encoded and can be very long, so shorten them
9a9012dc 324 $activity->name = shorten_text($activity->name);
4e781c7b 325
3afce1a5 326 if ($csv) {
fe94025d 327 print $sep.csv_quote(strip_tags($activity->name)).$sep.csv_quote($datetext);
4e781c7b 328 } else {
329 print '<th scope="col" class="'.$activity->datepassedclass.'">'.
330 '<a href="'.$CFG->wwwroot.'/mod/'.$activity->modname.
331 '/view.php?id='.$activity->id.'">'.
f8e9c93a 332 '<img src="'.$OUTPUT->pix_url('icon', $activity->modname).'" alt="'.
4e781c7b 333 get_string('modulename',$activity->modname).'" /> <span class="completion-activityname">'.
334 format_string($activity->name).'</span></a>';
3afce1a5 335 if ($activity->completionexpected) {
4e781c7b 336 print '<div class="completion-expected"><span>'.$datetext.'</span></div>';
337 }
338 print '</th>';
339 }
340}
341
3afce1a5 342if ($csv) {
b3aa7ae8 343 print $line;
4e781c7b 344} else {
345 print '</tr>';
346}
347
348// Row for each user
24a3b341 349foreach($progress as $user) {
4e781c7b 350 // User name
3afce1a5 351 if ($csv) {
4e781c7b 352 print csv_quote(fullname($user));
24e14bc7 353 foreach ($extrafields as $field) {
354 echo $sep . csv_quote($user->{$field});
f9498855 355 }
4e781c7b 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>';
24e14bc7 359 foreach ($extrafields as $field) {
360 echo '<td>' . s($user->{$field}) . '</td>';
f9498855 361 }
4e781c7b 362 }
363
364 // Progress for each activity
365 foreach($activities as $activity) {
366
367 // Get progress information and state
3afce1a5 368 if (array_key_exists($activity->id,$user->progress)) {
35da0e87 369 $thisprogress=$user->progress[$activity->id];
370 $state=$thisprogress->completionstate;
371 $date=userdate($thisprogress->timemodified);
4e781c7b 372 } else {
373 $state=COMPLETION_INCOMPLETE;
374 $date='';
375 }
376
377 // Work out how it corresponds to an icon
4e781c7b 378 switch($state) {
dbb8902e 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;
63391854 383 }
4e781c7b 384
dbb8902e 385 $completionicon='completion-'.
386 ($activity->completion==COMPLETION_TRACKING_AUTOMATIC ? 'auto' : 'manual').
387 '-'.$completiontype;
63391854 388
cfa722c9 389 $describe = get_string('completion-' . $completiontype, 'completion');
4e781c7b 390 $a=new StdClass;
391 $a->state=$describe;
392 $a->date=$date;
393 $a->user=fullname($user);
d352141e 394 $a->activity=strip_tags($activity->name);
4e781c7b 395 $fulldescribe=get_string('progress-title','completion',$a);
396
3afce1a5 397 if ($csv) {
b3aa7ae8 398 print $sep.csv_quote($describe).$sep.csv_quote($date);
4e781c7b 399 } else {
400 print '<td class="completion-progresscell '.$activity->datepassedclass.'">'.
b5d0cafc 401 '<img src="'.$OUTPUT->pix_url('i/'.$completionicon).
666e8458 402 '" alt="'.$describe.'" title="'.$fulldescribe.'" /></td>';
4e781c7b 403 }
404 }
405
3afce1a5 406 if ($csv) {
b3aa7ae8 407 print $line;
4e781c7b 408 } else {
409 print '</tr>';
410 }
411}
412
3afce1a5 413if ($csv) {
4e781c7b 414 exit;
415}
416print '</table>';
180bf7d4 417print '</div>';
35da0e87 418print $pagingbar;
4e781c7b 419
420print '<ul class="progress-actions"><li><a href="index.php?course='.$course->id.
b3aa7ae8 421 '&amp;format=csv">'.get_string('csvdownload','completion').'</a></li>
422 <li><a href="index.php?course='.$course->id.'&amp;format=excelcsv">'.
423 get_string('excelcsvdownload','completion').'</a></li></ul>';
4e781c7b 424
d60c1124 425echo $OUTPUT->footer();
aa6c1ced 426