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('admin');
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();
75 if(count($activities)==0) {
76 print_error('err_noactivities','completion',$reportsurl);
79 // Generate where clause
81 $ilike = $DB->sql_ilike();
83 if ($sifirst !== 'all') {
84 $where[] = "u.firstname $ilike '$sifirst%'";
87 if ($silast !== 'all') {
88 $where[] = "u.lastname $ilike '$silast%'";
91 // Get user match count
92 $total = $completion->get_num_tracked_users(implode(' AND ', $where), $group);
95 $grandtotal = $completion->get_num_tracked_users('', $group);
97 // If no users in this course what-so-ever
99 print_box_start('errorbox errorboxcontent boxaligncenter boxwidthnormal');
100 print '<p class="nousers">'.get_string('err_nousers','completion').'</p>';
101 print '<p><a href="'.$CFG->wwwroot.'/course/report.php?id='.$course->id.'">'.get_string('continue').'</a></p>';
103 print_footer($course);
111 $progress = $completion->get_progress_all(
112 implode(' AND ', $where),
114 $firstnamesort ? 'u.firstname ASC' : 'u.lastname ASC',
115 $csv ? 0 : COMPLETION_REPORT_PAGE,
121 header('Content-Disposition: attachment; filename=progress.'.
122 preg_replace('/[^a-z0-9-]/','_',strtolower($course->shortname)).'.csv');
123 // Unicode byte-order mark for Excel
125 header('Content-Type: text/csv; charset=UTF-16LE');
126 print chr(0xFF).chr(0xFE);
130 header('Content-Type: text/csv; charset=UTF-8');
135 // Use SVG to draw sideways text if supported
136 $svgcleverness = can_use_rotated_text();
138 // Navigation and header
139 $strreports = get_string("reports");
140 $strcompletion = get_string('completionreport','completion');
142 $PAGE->set_title($strcompletion);
143 $PAGE->set_heading($course->fullname);
144 echo $OUTPUT->header();
147 $PAGE->requires->yui2_lib('event');
148 $PAGE->requires->js('/course/report/progress/textrotate.js');
151 // Handle groups (if enabled)
152 groups_print_course_menu($course,$CFG->wwwroot.'/course/report/progress/?course='.$course->id);
155 // Build link for paging
156 $link = $CFG->wwwroot.'/course/report/progress/?course='.$course->id;
158 $link .= '&sort='.$sort;
160 $link .= '&start=';
162 // Build the the page by Initial bar
163 $initials = array('first', 'last');
164 $alphabet = explode(',', get_string('alphabet', 'langconfig'));
167 foreach ($initials as $initial) {
168 $var = 'si'.$initial;
170 $pagingbar .= ' <div class="initialbar '.$initial.'initial">';
171 $pagingbar .= get_string($initial.'name').': ';
173 if ($$var == 'all') {
174 $pagingbar .= '<strong>'.get_string('all').'</strong> ';
177 $pagingbar .= '<a href="'.$link.'">'.get_string('all').'</a> ';
180 foreach ($alphabet as $letter) {
181 if ($$var === $letter) {
182 $pagingbar .= '<strong>'.$letter.'</strong> ';
185 $pagingbar .= '<a href="'.$link.'&'.$var.'='.$letter.'">'.$letter.'</a> ';
189 $pagingbar .= '</div>';
192 // Do we need a paging bar?
193 if($total > COMPLETION_REPORT_PAGE) {
196 $pagingbar .= '<div class="paging">';
197 $pagingbar .= get_string('page').': ';
199 // Display previous link
201 $pstart = max($start - COMPLETION_REPORT_PAGE, 0);
202 $pagingbar .= '(<a class="previous" href="'.$link.$pstart.'">'.get_string('previous').'</a>) ';
208 while ($curstart < $total) {
211 if ($curstart == $start) {
212 $pagingbar .= ' '.$curpage.' ';
215 $pagingbar .= ' <a href="'.$link.$curstart.'">'.$curpage.'</a> ';
218 $curstart += COMPLETION_REPORT_PAGE;
222 $nstart = $start + COMPLETION_REPORT_PAGE;
223 if ($nstart < $total) {
224 $pagingbar .= ' (<a class="next" href="'.$link.$nstart.'">'.get_string('next').'</a>)';
227 $pagingbar .= '</div>';
230 // Okay, let's draw the table of progress info,
234 print '<br class="clearer"/>'; // ugh
239 print_heading(get_string('nothingtodisplay'));
240 print_footer($course);
244 print '<table id="completion-progress" class="generaltable flexible boxaligncenter" style="text-align:left"><tr style="vertical-align:top">';
246 // User heading / sort option
247 print '<th scope="col" class="completion-sortchoice">';
250 get_string('firstname').' / <a href="./?course='.$course->id.'">'.
251 get_string('lastname').'</a>';
253 print '<a href="./?course='.$course->id.'&sort=firstname">'.
254 get_string('firstname').'</a> / '.
255 get_string('lastname');
260 print '<th>'.get_string('idnumber').'</th>';
270 foreach($activities as $activity) {
271 $activity->datepassed = $activity->completionexpected && $activity->completionexpected <= time();
272 $activity->datepassedclass=$activity->datepassed ? 'completion-expired' : '';
274 if($activity->completionexpected) {
275 $datetext=userdate($activity->completionexpected,get_string('strftimedate','langconfig'));
280 // Some names (labels) come URL-encoded and can be very long, so shorten them
281 $activity->name = shorten_text($activity->name);
284 print $sep.csv_quote(strip_tags($activity->name)).$sep.csv_quote($datetext);
286 print '<th scope="col" class="'.$activity->datepassedclass.'">'.
287 '<a href="'.$CFG->wwwroot.'/mod/'.$activity->modname.
288 '/view.php?id='.$activity->id.'">'.
289 '<img src="'.$OUTPUT->pix_url('icon', $activity->modname).'" alt="'.
290 get_string('modulename',$activity->modname).'" /> <span class="completion-activityname">'.
291 format_string($activity->name).'</span></a>';
292 if($activity->completionexpected) {
293 print '<div class="completion-expected"><span>'.$datetext.'</span></div>';
306 foreach($progress as $user) {
309 print csv_quote(fullname($user));
311 print $sep.csv_quote($user->idnumber);
314 print '<tr><th scope="row"><a href="'.$CFG->wwwroot.'/user/view.php?id='.
315 $user->id.'&course='.$course->id.'">'.fullname($user).'</a></th>';
317 print '<td>'.htmlspecialchars($user->idnumber).'</td>';
321 // Progress for each activity
322 foreach($activities as $activity) {
324 // Get progress information and state
325 if(array_key_exists($activity->id,$user->progress)) {
326 $thisprogress=$user->progress[$activity->id];
327 $state=$thisprogress->completionstate;
328 $date=userdate($thisprogress->timemodified);
330 $state=COMPLETION_INCOMPLETE;
334 // Work out how it corresponds to an icon
336 case COMPLETION_INCOMPLETE : $completiontype='n'; break;
337 case COMPLETION_COMPLETE : $completiontype='y'; break;
338 case COMPLETION_COMPLETE_PASS : $completiontype='pass'; break;
339 case COMPLETION_COMPLETE_FAIL : $completiontype='fail'; break;
342 $completionicon='completion-'.
343 ($activity->completion==COMPLETION_TRACKING_AUTOMATIC ? 'auto' : 'manual').
346 $describe=get_string('completion-alt-auto-'.$completiontype,'completion');
350 $a->user=fullname($user);
351 $a->activity=strip_tags($activity->name);
352 $fulldescribe=get_string('progress-title','completion',$a);
355 print $sep.csv_quote($describe).$sep.csv_quote($date);
357 print '<td class="completion-progresscell '.$activity->datepassedclass.'">'.
358 '<img src="'.$OUTPUT->pix_url('i/'.$completionicon).
359 '" alt="'.$describe.'" title="'.$fulldescribe.'" /></td>';
376 print '<ul class="progress-actions"><li><a href="index.php?course='.$course->id.
377 '&format=csv">'.get_string('csvdownload','completion').'</a></li>
378 <li><a href="index.php?course='.$course->id.'&format=excelcsv">'.
379 get_string('excelcsvdownload','completion').'</a></li></ul>';
381 echo $OUTPUT->footer();