MDL-21665 Minor fix to breadcrumb trail in completion progress report
[moodle.git] / course / report / progress / index.php
1 <?php
2 require_once('../../../config.php');
3 require_once($CFG->libdir . '/completionlib.php');
5 define('COMPLETION_REPORT_PAGE',50);
7 // Get course
8 $id = required_param('course',PARAM_INT);
9 $course=$DB->get_record('course',array('id'=>$id));
10 if(!$course) {
11     print_error('invalidcourseid');
12 }
14 // Sort (default lastname, optionally firstname)
15 $sort=optional_param('sort','',PARAM_ALPHA);
16 $firstnamesort=$sort=='firstname';
18 // CSV format
19 $format=optional_param('format','',PARAM_ALPHA);
20 $excel=$format=='excelcsv';
21 $csv=$format=='csv' || $excel;
23 // Whether to start at a particular position
24 $start=optional_param('start',0,PARAM_INT);
26 // Whether to show idnumber
27 // TODO: This should really not be using a config option 'intended' for
28 // gradebook, but that option is also used in quiz reports as well. There ought
29 // to be a generic option somewhere.
30 $idnumbers=$CFG->grade_report_showuseridnumber;
32 function csv_quote($value) {
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     }
40 }
42 $url = new moodle_url('/course/report/progress/index.php', array('course'=>$id));
43 if ($sort !== '') {
44     $url->param('sort', $sort);
45 }
46 if ($format !== '') {
47     $url->param('format', $format);
48 }
49 if ($start !== '') {
50     $url->param('start', $start);
51 }
52 $PAGE->set_url($url);
54 require_login($course);
56 // Check basic permission
57 $context=get_context_instance(CONTEXT_COURSE,$course->id);
58 require_capability('coursereport/progress:view',$context);
60 // Get group mode
61 $group=groups_get_course_group($course,true); // Supposed to verify group
62 if($group===0 && $course->groupmode==SEPARATEGROUPS) {
63     require_capability('moodle/site:accessallgroups',$context);
64 }
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();
71 if(count($activities)==0) {
72     print_error('err_noactivities','completion',$reportsurl);
73 }
75 $progress=$completion->get_progress_all($firstnamesort,$group,
76     $csv ? 0 :COMPLETION_REPORT_PAGE,$csv ? 0 : $start);
78 if($csv) {
79     header('Content-Disposition: attachment; filename=progress.'.
80         preg_replace('/[^a-z0-9-]/','_',strtolower($course->shortname)).'.csv');
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);
86         $line="\n".chr(0);
87     } else {
88         header('Content-Type: text/csv; charset=UTF-8');
89         $sep=",";
90         $line="\n";
91     }
92 } else {
93     // Use SVG to draw sideways text if supported
94     $svgcleverness = can_use_rotated_text();
96     // Navigation and header
97     $strreports = get_string("reports");
98     $strcompletion = get_string('completionreport','completion');
100     $PAGE->set_title($strcompletion);
101     $PAGE->set_heading($course->fullname);
102     if (has_capability('moodle/site:viewreports', $context)) {
103         $PAGE->navbar->add($strreports, new moodle_url('/course/report.php', array('id'=>$course->id)));
104     }
105     $PAGE->navbar->add($strcompletion);
106     echo $OUTPUT->header();
108     if($svgcleverness) {
109         $PAGE->requires->yui2_lib('event');
110         $PAGE->requires->js('/course/report/progress/textrotate.js');
111     }
113     // Handle groups (if enabled)
114     groups_print_course_menu($course,$CFG->wwwroot.'/course/report/progress/?course='.$course->id);
117 // Do we need a paging bar?
118 if($progress->total > COMPLETION_REPORT_PAGE) {
119     $pagingbar='<div class="completion_pagingbar">';
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     }
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>';
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     }
141     $pagingbar.='</div>';
142 } else {
143     $pagingbar='';
146 // Okay, let's draw the table of progress info,
148 // Start of table
149 if(!$csv) {
150     print '<br class="clearer"/>'; // ugh
151     if(count($progress->users)==0) {
152         print '<p class="nousers">'.get_string('err_nousers','completion').'</p>';
153         print '<p><a href="'.$reportsurl.'">'.get_string('continue').'</a></p>';
154         echo $OUTPUT->footer();
155         exit;
156     }
157     print $pagingbar;
158     print '<table id="completion-progress" class="generaltable flexible boxaligncenter" style="text-align:left"><tr style="vertical-align:top">';
160     // User heading / sort option
161     print '<th scope="col" class="completion-sortchoice">';
162     if($firstnamesort) {
163         print
164             get_string('firstname').' / <a href="./?course='.$course->id.'">'.
165             get_string('lastname').'</a>';
166     } else {
167         print '<a href="./?course='.$course->id.'&amp;sort=firstname">'.
168             get_string('firstname').'</a> / '.
169             get_string('lastname');
170     }
171     print '</th>';
173     if($idnumbers) {
174         print '<th>'.get_string('idnumber').'</th>';
175     }
177 } else {
178     if($idnumbers) {
179         print $sep;
180     }
183 // Activities
184 foreach($activities as $activity) {
185     $activity->datepassed = $activity->completionexpected && $activity->completionexpected <= time();
186     $activity->datepassedclass=$activity->datepassed ? 'completion-expired' : '';
188     if($activity->completionexpected) {
189         $datetext=userdate($activity->completionexpected,get_string('strftimedate','langconfig'));
190     } else {
191         $datetext='';
192     }
194     // Some names (labels) come URL-encoded and can be very long, so shorten them
195     $activity->name = shorten_text($activity->name);
197     if($csv) {
198         print $sep.csv_quote(strip_tags($activity->name)).$sep.csv_quote($datetext);
199     } else {
200         print '<th scope="col" class="'.$activity->datepassedclass.'">'.
201             '<a href="'.$CFG->wwwroot.'/mod/'.$activity->modname.
202             '/view.php?id='.$activity->id.'">'.
203             '<img src="'.$OUTPUT->pix_url('icon', $activity->modname).'/icon.gif" alt="'.
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     }
213 if($csv) {
214     print $line;
215 } else {
216     print '</tr>';
219 // Row for each user
220 foreach($progress->users as $user) {
221     // User name
222     if($csv) {
223         print csv_quote(fullname($user));
224         if($idnumbers) {
225             print $sep.csv_quote($user->idnumber);
226         }
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>';
230         if($idnumbers) {
231             print '<td>'.htmlspecialchars($user->idnumber).'</td>';
232         }
233     }
235     // Progress for each activity
236     foreach($activities as $activity) {
238         // Get progress information and state
239         if(array_key_exists($activity->id,$user->progress)) {
240             $thisprogress=$user->progress[$activity->id];
241             $state=$thisprogress->completionstate;
242             $date=userdate($thisprogress->timemodified);
243         } else {
244             $state=COMPLETION_INCOMPLETE;
245             $date='';
246         }
248         // Work out how it corresponds to an icon
249         switch($state) {
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;
254         }
256         $completionicon='completion-'.
257             ($activity->completion==COMPLETION_TRACKING_AUTOMATIC ? 'auto' : 'manual').
258             '-'.$completiontype;
260         $describe=get_string('completion-alt-auto-'.$completiontype,'completion');
261         $a=new StdClass;
262         $a->state=$describe;
263         $a->date=$date;
264         $a->user=fullname($user);
265         $a->activity=strip_tags($activity->name);
266         $fulldescribe=get_string('progress-title','completion',$a);
268         if($csv) {
269             print $sep.csv_quote($describe).$sep.csv_quote($date);
270         } else {
271             print '<td class="completion-progresscell '.$activity->datepassedclass.'">'.
272                 '<img src="'.$OUTPUT->pix_url('i/'.$completionicon).
273                 '" alt="'.$describe.'" title="'.$fulldescribe.'" /></td>';
274         }
275     }
277     if($csv) {
278         print $line;
279     } else {
280         print '</tr>';
281     }
284 if($csv) {
285     exit;
287 print '</table>';
288 print $pagingbar;
290 print '<ul class="progress-actions"><li><a href="index.php?course='.$course->id.
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>';
295 echo $OUTPUT->footer();