MDL-21665 Minor fix to breadcrumb trail in completion progress report
[moodle.git] / course / report / progress / index.php
CommitLineData
4e781c7b 1<?php
4e781c7b 2require_once('../../../config.php');
4e1132a8 3require_once($CFG->libdir . '/completionlib.php');
4e781c7b 4
35da0e87 5define('COMPLETION_REPORT_PAGE',50);
6
4e781c7b 7// Get course
dfab77a2 8$id = required_param('course',PARAM_INT);
9$course=$DB->get_record('course',array('id'=>$id));
4e781c7b 10if(!$course) {
63391854 11 print_error('invalidcourseid');
4e781c7b 12}
13
14// Sort (default lastname, optionally firstname)
15$sort=optional_param('sort','',PARAM_ALPHA);
16$firstnamesort=$sort=='firstname';
17
18// CSV format
b3aa7ae8 19$format=optional_param('format','',PARAM_ALPHA);
20$excel=$format=='excelcsv';
21$csv=$format=='csv' || $excel;
4e781c7b 22
35da0e87 23// Whether to start at a particular position
24$start=optional_param('start',0,PARAM_INT);
25
f9498855 26// Whether to show idnumber
63391854 27// TODO: This should really not be using a config option 'intended' for
a5a63515 28// gradebook, but that option is also used in quiz reports as well. There ought
29// to be a generic option somewhere.
f9498855 30$idnumbers=$CFG->grade_report_showuseridnumber;
31
4e781c7b 32function csv_quote($value) {
b3aa7ae8 33 global $excel;
34 if($excel) {
35 $tl=textlib_get_instance();
36 return $tl->convert('"'.str_replace('"',"'",$value).'"','UTF-8','UTF-16LE');
37 } else {
38 return '"'.str_replace('"',"'",$value).'"';
39 }
4e781c7b 40}
41
a6855934 42$url = new moodle_url('/course/report/progress/index.php', array('course'=>$id));
dfab77a2 43if ($sort !== '') {
44 $url->param('sort', $sort);
45}
46if ($format !== '') {
47 $url->param('format', $format);
48}
49if ($start !== '') {
50 $url->param('start', $start);
51}
52$PAGE->set_url($url);
53
a2e4bf7f 54require_login($course);
4e781c7b 55
56// Check basic permission
57$context=get_context_instance(CONTEXT_COURSE,$course->id);
0d130857 58require_capability('coursereport/progress:view',$context);
4e781c7b 59
60// Get group mode
61$group=groups_get_course_group($course,true); // Supposed to verify group
62if($group===0 && $course->groupmode==SEPARATEGROUPS) {
63 require_capability('moodle/site:accessallgroups',$context);
64}
65
66// Get data on activities and progress of all users, and give error if we've
67// nothing to display (no users or no activities)
68$reportsurl=$CFG->wwwroot.'/course/report.php?id='.$course->id;
69$completion=new completion_info($course);
70$activities=$completion->get_activities();
71if(count($activities)==0) {
72 print_error('err_noactivities','completion',$reportsurl);
73}
4e781c7b 74
10130df6 75$progress=$completion->get_progress_all($firstnamesort,$group,
76 $csv ? 0 :COMPLETION_REPORT_PAGE,$csv ? 0 : $start);
b3aa7ae8 77
4e781c7b 78if($csv) {
4e781c7b 79 header('Content-Disposition: attachment; filename=progress.'.
80 preg_replace('/[^a-z0-9-]/','_',strtolower($course->shortname)).'.csv');
b3aa7ae8 81 // Unicode byte-order mark for Excel
82 if($excel) {
83 header('Content-Type: text/csv; charset=UTF-16LE');
84 print chr(0xFF).chr(0xFE);
85 $sep="\t".chr(0);
63391854 86 $line="\n".chr(0);
b3aa7ae8 87 } else {
88 header('Content-Type: text/csv; charset=UTF-8');
89 $sep=",";
90 $line="\n";
91 }
4e781c7b 92} else {
93 // Use SVG to draw sideways text if supported
0eab1dc0 94 $svgcleverness = can_use_rotated_text();
4e781c7b 95
96 // Navigation and header
97 $strreports = get_string("reports");
98 $strcompletion = get_string('completionreport','completion');
0a122046 99
100 $PAGE->set_title($strcompletion);
101 $PAGE->set_heading($course->fullname);
a7382d3e
SM
102 if (has_capability('moodle/site:viewreports', $context)) {
103 $PAGE->navbar->add($strreports, new moodle_url('/course/report.php', array('id'=>$course->id)));
104 }
0a122046 105 $PAGE->navbar->add($strcompletion);
106 echo $OUTPUT->header();
107
4e781c7b 108 if($svgcleverness) {
f44b10ed 109 $PAGE->requires->yui2_lib('event');
9dec75db 110 $PAGE->requires->js('/course/report/progress/textrotate.js');
4e781c7b 111 }
112
113 // Handle groups (if enabled)
114 groups_print_course_menu($course,$CFG->wwwroot.'/course/report/progress/?course='.$course->id);
115}
116
9b0e2d66 117// Do we need a paging bar?
35da0e87 118if($progress->total > COMPLETION_REPORT_PAGE) {
119 $pagingbar='<div class="completion_pagingbar">';
120
121 if($start>0) {
122 $newstart=$start-COMPLETION_REPORT_PAGE;
123 if($newstart<0) {
124 $newstart=0;
125 }
126 $pagingbar.=link_arrow_left(get_string('previous'),'./?course='.$course->id.
127 ($newstart ? '&amp;start='.$newstart : ''),false,'completion_prev');
128 }
129
130 $a=new StdClass;
131 $a->from=$start+1;
132 $a->to=$start+COMPLETION_REPORT_PAGE;
133 $a->total=$progress->total;
134 $pagingbar.='<p>'.get_string('reportpage','completion',$a).'</p>';
135
136 if($start+COMPLETION_REPORT_PAGE < $progress->total) {
137 $pagingbar.=link_arrow_right(get_string('next'),'./?course='.$course->id.
138 '&amp;start='.($start+COMPLETION_REPORT_PAGE),false,'completion_next');
139 }
140
63391854 141 $pagingbar.='</div>';
35da0e87 142} else {
143 $pagingbar='';
144}
145
4e781c7b 146// Okay, let's draw the table of progress info,
147
63391854 148// Start of table
4e781c7b 149if(!$csv) {
150 print '<br class="clearer"/>'; // ugh
35da0e87 151 if(count($progress->users)==0) {
4e781c7b 152 print '<p class="nousers">'.get_string('err_nousers','completion').'</p>';
153 print '<p><a href="'.$reportsurl.'">'.get_string('continue').'</a></p>';
d60c1124 154 echo $OUTPUT->footer();
4e781c7b 155 exit;
156 }
35da0e87 157 print $pagingbar;
4e781c7b 158 print '<table id="completion-progress" class="generaltable flexible boxaligncenter" style="text-align:left"><tr style="vertical-align:top">';
159
160 // User heading / sort option
161 print '<th scope="col" class="completion-sortchoice">';
162 if($firstnamesort) {
63391854 163 print
4e781c7b 164 get_string('firstname').' / <a href="./?course='.$course->id.'">'.
165 get_string('lastname').'</a>';
166 } else {
c215d9d4 167 print '<a href="./?course='.$course->id.'&amp;sort=firstname">'.
4e781c7b 168 get_string('firstname').'</a> / '.
169 get_string('lastname');
170 }
171 print '</th>';
63391854 172
f9498855 173 if($idnumbers) {
63391854 174 print '<th>'.get_string('idnumber').'</th>';
f9498855 175 }
63391854 176
f9498855 177} else {
178 if($idnumbers) {
179 print $sep;
180 }
4e781c7b 181}
182
183// Activities
184foreach($activities as $activity) {
185 $activity->datepassed = $activity->completionexpected && $activity->completionexpected <= time();
186 $activity->datepassedclass=$activity->datepassed ? 'completion-expired' : '';
187
188 if($activity->completionexpected) {
189 $datetext=userdate($activity->completionexpected,get_string('strftimedate','langconfig'));
190 } else {
191 $datetext='';
192 }
63391854 193
82fd829d 194 // Some names (labels) come URL-encoded and can be very long, so shorten them
9a9012dc 195 $activity->name = shorten_text($activity->name);
4e781c7b 196
197 if($csv) {
fe94025d 198 print $sep.csv_quote(strip_tags($activity->name)).$sep.csv_quote($datetext);
4e781c7b 199 } else {
200 print '<th scope="col" class="'.$activity->datepassedclass.'">'.
201 '<a href="'.$CFG->wwwroot.'/mod/'.$activity->modname.
202 '/view.php?id='.$activity->id.'">'.
b5d0cafc 203 '<img src="'.$OUTPUT->pix_url('icon', $activity->modname).'/icon.gif" alt="'.
4e781c7b 204 get_string('modulename',$activity->modname).'" /> <span class="completion-activityname">'.
205 format_string($activity->name).'</span></a>';
206 if($activity->completionexpected) {
207 print '<div class="completion-expected"><span>'.$datetext.'</span></div>';
208 }
209 print '</th>';
210 }
211}
212
213if($csv) {
b3aa7ae8 214 print $line;
4e781c7b 215} else {
216 print '</tr>';
217}
218
219// Row for each user
35da0e87 220foreach($progress->users as $user) {
4e781c7b 221 // User name
222 if($csv) {
223 print csv_quote(fullname($user));
f9498855 224 if($idnumbers) {
225 print $sep.csv_quote($user->idnumber);
226 }
4e781c7b 227 } else {
228 print '<tr><th scope="row"><a href="'.$CFG->wwwroot.'/user/view.php?id='.
229 $user->id.'&amp;course='.$course->id.'">'.fullname($user).'</a></th>';
f9498855 230 if($idnumbers) {
231 print '<td>'.htmlspecialchars($user->idnumber).'</td>';
232 }
4e781c7b 233 }
234
235 // Progress for each activity
236 foreach($activities as $activity) {
237
238 // Get progress information and state
239 if(array_key_exists($activity->id,$user->progress)) {
35da0e87 240 $thisprogress=$user->progress[$activity->id];
241 $state=$thisprogress->completionstate;
242 $date=userdate($thisprogress->timemodified);
4e781c7b 243 } else {
244 $state=COMPLETION_INCOMPLETE;
245 $date='';
246 }
247
248 // Work out how it corresponds to an icon
4e781c7b 249 switch($state) {
dbb8902e 250 case COMPLETION_INCOMPLETE : $completiontype='n'; break;
251 case COMPLETION_COMPLETE : $completiontype='y'; break;
252 case COMPLETION_COMPLETE_PASS : $completiontype='pass'; break;
253 case COMPLETION_COMPLETE_FAIL : $completiontype='fail'; break;
63391854 254 }
4e781c7b 255
dbb8902e 256 $completionicon='completion-'.
257 ($activity->completion==COMPLETION_TRACKING_AUTOMATIC ? 'auto' : 'manual').
258 '-'.$completiontype;
63391854 259
dbb8902e 260 $describe=get_string('completion-alt-auto-'.$completiontype,'completion');
4e781c7b 261 $a=new StdClass;
262 $a->state=$describe;
263 $a->date=$date;
264 $a->user=fullname($user);
d352141e 265 $a->activity=strip_tags($activity->name);
4e781c7b 266 $fulldescribe=get_string('progress-title','completion',$a);
267
268 if($csv) {
b3aa7ae8 269 print $sep.csv_quote($describe).$sep.csv_quote($date);
4e781c7b 270 } else {
271 print '<td class="completion-progresscell '.$activity->datepassedclass.'">'.
b5d0cafc 272 '<img src="'.$OUTPUT->pix_url('i/'.$completionicon).
666e8458 273 '" alt="'.$describe.'" title="'.$fulldescribe.'" /></td>';
4e781c7b 274 }
275 }
276
277 if($csv) {
b3aa7ae8 278 print $line;
4e781c7b 279 } else {
280 print '</tr>';
281 }
282}
283
284if($csv) {
285 exit;
286}
287print '</table>';
35da0e87 288print $pagingbar;
4e781c7b 289
290print '<ul class="progress-actions"><li><a href="index.php?course='.$course->id.
b3aa7ae8 291 '&amp;format=csv">'.get_string('csvdownload','completion').'</a></li>
292 <li><a href="index.php?course='.$course->id.'&amp;format=excelcsv">'.
293 get_string('excelcsvdownload','completion').'</a></li></ul>';
4e781c7b 294
d60c1124 295echo $OUTPUT->footer();
aa6c1ced 296