2 require_once('../../../config.php');
3 require_once($CFG->libdir . '/completionlib.php');
5 define('COMPLETION_REPORT_PAGE', 25);
8 $id = required_param('course',PARAM_INT);
9 $course=$DB->get_record('course',array('id'=>$id));
11 print_error('invalidcourseid');
14 // Sort (default lastname, optionally firstname)
15 $sort = optional_param('sort','',PARAM_ALPHA);
16 $firstnamesort = $sort == 'firstname';
19 $format = optional_param('format','',PARAM_ALPHA);
20 $excel = $format == 'excelcsv';
21 $csv = $format == 'csv' || $excel;
24 $start = optional_param('start', 0, PARAM_INT);
25 $sifirst = optional_param('sifirst', 'all', PARAM_ALPHA);
26 $silast = optional_param('silast', 'all', PARAM_ALPHA);
27 $start = optional_param('start',0,PARAM_INT);
29 // Whether to show idnumber
30 // TODO: This should really not be using a config option 'intended' for
31 // gradebook, but that option is also used in quiz reports as well. There ought
32 // to be a generic option somewhere.
33 $idnumbers = $CFG->grade_report_showuseridnumber;
35 function csv_quote($value) {
38 $tl=textlib_get_instance();
39 return $tl->convert('"'.str_replace('"',"'",$value).'"','UTF-8','UTF-16LE');
41 return '"'.str_replace('"',"'",$value).'"';
45 $url = new moodle_url('/course/report/progress/index.php', array('course'=>$id));
47 $url->param('sort', $sort);
50 $url->param('format', $format);
53 $url->param('start', $start);
56 $PAGE->set_pagelayout('report');
58 require_login($course);
60 // Check basic permission
61 $context=get_context_instance(CONTEXT_COURSE,$course->id);
62 require_capability('coursereport/progress:view',$context);
65 $group=groups_get_course_group($course,true); // Supposed to verify group
66 if($group===0 && $course->groupmode==SEPARATEGROUPS) {
67 require_capability('moodle/site:accessallgroups',$context);
70 // Get data on activities and progress of all users, and give error if we've
71 // nothing to display (no users or no activities)
72 $reportsurl=$CFG->wwwroot.'/course/report.php?id='.$course->id;
73 $completion=new completion_info($course);
74 $activities=$completion->get_activities();
76 // Generate where clause
78 $where_params = array();
80 if ($sifirst !== 'all') {
81 $where[] = $DB->sql_like('u.firstname', ':sifirst', false);
82 $where_params['sifirst'] = $sifirst.'%';
85 if ($silast !== 'all') {
86 $where[] = $DB->sql_like('u.lastname', ':silast', false);
87 $where_params['silast'] = $silast.'%';
90 // Get user match count
91 $total = $completion->get_num_tracked_users(implode(' AND ', $where), $where_params, $group);
94 $grandtotal = $completion->get_num_tracked_users('', array(), $group);
100 $progress = $completion->get_progress_all(
101 implode(' AND ', $where),
104 $firstnamesort ? 'u.firstname ASC' : 'u.lastname ASC',
105 $csv ? 0 : COMPLETION_REPORT_PAGE,
110 if($csv && $grandtotal && count($activities)>0) { // Only show CSV if there are some users/actvs
112 $shortname = format_string($course->shortname, true, array('context' => $context));
113 $textlib = textlib_get_instance();
114 header('Content-Disposition: attachment; filename=progress.'.
115 preg_replace('/[^a-z0-9-]/','_',$textlib->strtolower(strip_tags($shortname))).'.csv');
116 // Unicode byte-order mark for Excel
118 header('Content-Type: text/csv; charset=UTF-16LE');
119 print chr(0xFF).chr(0xFE);
123 header('Content-Type: text/csv; charset=UTF-8');
128 // Use SVG to draw sideways text if supported
129 $svgcleverness = can_use_rotated_text();
131 // Navigation and header
132 $strreports = get_string("reports");
133 $strcompletion = get_string('activitycompletion', 'completion');
135 $PAGE->set_title($strcompletion);
136 $PAGE->set_heading($course->fullname);
137 echo $OUTPUT->header();
140 $PAGE->requires->yui2_lib('event');
141 $PAGE->requires->js('/course/report/progress/textrotate.js');
144 // Handle groups (if enabled)
145 groups_print_course_menu($course,$CFG->wwwroot.'/course/report/progress/?course='.$course->id);
148 if(count($activities)==0) {
149 echo $OUTPUT->container(get_string('err_noactivities', 'completion'), 'errorbox errorboxcontent');
150 echo $OUTPUT->footer();
154 // If no users in this course what-so-ever
156 echo $OUTPUT->container(get_string('err_nousers', 'completion'), 'errorbox errorboxcontent');
157 echo $OUTPUT->footer();
161 // Build link for paging
162 $link = $CFG->wwwroot.'/course/report/progress/?course='.$course->id;
164 $link .= '&sort='.$sort;
166 $link .= '&start=';
168 // Build the the page by Initial bar
169 $initials = array('first', 'last');
170 $alphabet = explode(',', get_string('alphabet', 'langconfig'));
173 foreach ($initials as $initial) {
174 $var = 'si'.$initial;
176 $othervar = $initial == 'first' ? 'silast' : 'sifirst';
177 $othervar = $$othervar != 'all' ? "&{$othervar}={$$othervar}" : '';
179 $pagingbar .= ' <div class="initialbar '.$initial.'initial">';
180 $pagingbar .= get_string($initial.'name').': ';
182 if ($$var == 'all') {
183 $pagingbar .= '<strong>'.get_string('all').'</strong> ';
186 $pagingbar .= "<a href=\"{$link}{$othervar}\">".get_string('all').'</a> ';
189 foreach ($alphabet as $letter) {
190 if ($$var === $letter) {
191 $pagingbar .= '<strong>'.$letter.'</strong> ';
194 $pagingbar .= "<a href=\"$link&$var={$letter}{$othervar}\">$letter</a> ";
198 $pagingbar .= '</div>';
201 // Do we need a paging bar?
202 if($total > COMPLETION_REPORT_PAGE) {
205 $pagingbar .= '<div class="paging">';
206 $pagingbar .= get_string('page').': ';
208 $sistrings = array();
209 $sistrings[] = $sifirst != 'all' ? "sifirst={$sifirst}" : null;
210 $sistrings[] = $silast != 'all' ? "silast={$silast}" : null;
211 $sistring = !empty($sistrings) ? implode('&', $sistrings) : '';
213 // Display previous link
215 $pstart = max($start - COMPLETION_REPORT_PAGE, 0);
216 $pagingbar .= "(<a class=\"previous\" href=\"{$link}{$pstart}{$sistring}\">".get_string('previous').'</a>) ';
222 while ($curstart < $total) {
225 if ($curstart == $start) {
226 $pagingbar .= ' '.$curpage.' ';
229 $pagingbar .= " <a href=\"{$link}{$curstart}{$sistring}\">$curpage</a> ";
232 $curstart += COMPLETION_REPORT_PAGE;
236 $nstart = $start + COMPLETION_REPORT_PAGE;
237 if ($nstart < $total) {
238 $pagingbar .= " (<a class=\"next\" href=\"{$link}{$nstart}{$sistring}\">".get_string('next').'</a>)';
241 $pagingbar .= '</div>';
244 // Okay, let's draw the table of progress info,
248 print '<br class="clearer"/>'; // ugh
253 echo $OUTPUT->heading(get_string('nothingtodisplay'));
254 echo $OUTPUT->footer();
258 print '<table id="completion-progress" class="generaltable flexible boxaligncenter" style="text-align:left"><tr style="vertical-align:top">';
260 // User heading / sort option
261 print '<th scope="col" class="completion-sortchoice">';
263 $sistring = "&silast={$silast}&sifirst={$sifirst}";
267 get_string('firstname')." / <a href=\"./?course={$course->id}{$sistring}\">".
268 get_string('lastname').'</a>';
270 print "<a href=\"./?course={$course->id}&sort=firstname{$sistring}\">".
271 get_string('firstname').'</a> / '.
272 get_string('lastname');
277 print '<th>'.get_string('idnumber').'</th>';
287 foreach($activities as $activity) {
288 $activity->datepassed = $activity->completionexpected && $activity->completionexpected <= time();
289 $activity->datepassedclass=$activity->datepassed ? 'completion-expired' : '';
291 if($activity->completionexpected) {
292 $datetext=userdate($activity->completionexpected,get_string('strftimedate','langconfig'));
297 // Some names (labels) come URL-encoded and can be very long, so shorten them
298 $activity->name = shorten_text($activity->name);
301 print $sep.csv_quote(strip_tags($activity->name)).$sep.csv_quote($datetext);
303 print '<th scope="col" class="'.$activity->datepassedclass.'">'.
304 '<a href="'.$CFG->wwwroot.'/mod/'.$activity->modname.
305 '/view.php?id='.$activity->id.'">'.
306 '<img src="'.$OUTPUT->pix_url('icon', $activity->modname).'" alt="'.
307 get_string('modulename',$activity->modname).'" /> <span class="completion-activityname">'.
308 format_string($activity->name).'</span></a>';
309 if($activity->completionexpected) {
310 print '<div class="completion-expected"><span>'.$datetext.'</span></div>';
323 foreach($progress as $user) {
326 print csv_quote(fullname($user));
328 print $sep.csv_quote($user->idnumber);
331 print '<tr><th scope="row"><a href="'.$CFG->wwwroot.'/user/view.php?id='.
332 $user->id.'&course='.$course->id.'">'.fullname($user).'</a></th>';
334 print '<td>'.htmlspecialchars($user->idnumber).'</td>';
338 // Progress for each activity
339 foreach($activities as $activity) {
341 // Get progress information and state
342 if(array_key_exists($activity->id,$user->progress)) {
343 $thisprogress=$user->progress[$activity->id];
344 $state=$thisprogress->completionstate;
345 $date=userdate($thisprogress->timemodified);
347 $state=COMPLETION_INCOMPLETE;
351 // Work out how it corresponds to an icon
353 case COMPLETION_INCOMPLETE : $completiontype='n'; break;
354 case COMPLETION_COMPLETE : $completiontype='y'; break;
355 case COMPLETION_COMPLETE_PASS : $completiontype='pass'; break;
356 case COMPLETION_COMPLETE_FAIL : $completiontype='fail'; break;
359 $completionicon='completion-'.
360 ($activity->completion==COMPLETION_TRACKING_AUTOMATIC ? 'auto' : 'manual').
363 $describe=get_string('completion-alt-auto-'.$completiontype,'completion');
367 $a->user=fullname($user);
368 $a->activity=strip_tags($activity->name);
369 $fulldescribe=get_string('progress-title','completion',$a);
372 print $sep.csv_quote($describe).$sep.csv_quote($date);
374 print '<td class="completion-progresscell '.$activity->datepassedclass.'">'.
375 '<img src="'.$OUTPUT->pix_url('i/'.$completionicon).
376 '" alt="'.$describe.'" title="'.$fulldescribe.'" /></td>';
393 print '<ul class="progress-actions"><li><a href="index.php?course='.$course->id.
394 '&format=csv">'.get_string('csvdownload','completion').'</a></li>
395 <li><a href="index.php?course='.$course->id.'&format=excelcsv">'.
396 get_string('excelcsvdownload','completion').'</a></li></ul>';
398 echo $OUTPUT->footer();