6a682f0faf7ddd5c6ac70aa444a53a48d803a689
[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', 25);
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 // Paging
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) {
36     global $excel;
37     if($excel) {
38         $tl=textlib_get_instance();
39         return $tl->convert('"'.str_replace('"',"'",$value).'"','UTF-8','UTF-16LE');
40     } else {
41         return '"'.str_replace('"',"'",$value).'"';
42     }
43 }
45 $url = new moodle_url('/course/report/progress/index.php', array('course'=>$id));
46 if ($sort !== '') {
47     $url->param('sort', $sort);
48 }
49 if ($format !== '') {
50     $url->param('format', $format);
51 }
52 if ($start !== 0) {
53     $url->param('start', $start);
54 }
55 $PAGE->set_url($url);
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);
64 // Get group mode
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);
68 }
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);
77 }
79 // Generate where clause
80 $where = array();
81 $ilike = $DB->sql_ilike();
83 if ($sifirst !== 'all') {
84     $where[] = "u.firstname $ilike '$sifirst%'";
85 }
87 if ($silast !== 'all') {
88     $where[] = "u.lastname $ilike '$silast%'";
89 }
91 // Get user match count
92 $total = $completion->get_num_tracked_users(implode(' AND ', $where), $group);
94 // Total user count
95 $grandtotal = $completion->get_num_tracked_users('', $group);
97 // If no users in this course what-so-ever
98 if (!$grandtotal) {
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>';
102     print_box_end();
103     print_footer($course);
104     exit;
107 // Get user data
108 $progress = array();
110 if ($total) {
111     $progress = $completion->get_progress_all(
112         implode(' AND ', $where),
113         $group,
114         $firstnamesort ? 'u.firstname ASC' : 'u.lastname ASC',
115         $csv ? 0 : COMPLETION_REPORT_PAGE,
116         $csv ? 0 : $start
117     );
120 if($csv) {
121     header('Content-Disposition: attachment; filename=progress.'.
122         preg_replace('/[^a-z0-9-]/','_',strtolower($course->shortname)).'.csv');
123     // Unicode byte-order mark for Excel
124     if($excel) {
125         header('Content-Type: text/csv; charset=UTF-16LE');
126         print chr(0xFF).chr(0xFE);
127         $sep="\t".chr(0);
128         $line="\n".chr(0);
129     } else {
130         header('Content-Type: text/csv; charset=UTF-8');
131         $sep=",";
132         $line="\n";
133     }
134 } else {
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();
146     if($svgcleverness) {
147         $PAGE->requires->yui2_lib('event');
148         $PAGE->requires->js('/course/report/progress/textrotate.js');
149     }
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;
157 if (strlen($sort)) {
158     $link .= '&amp;sort='.$sort;
160 $link .= '&amp;start=';
162 // Build the the page by Initial bar
163 $initials = array('first', 'last');
164 $alphabet = explode(',', get_string('alphabet', 'langconfig'));
166 $pagingbar = '';
167 foreach ($initials as $initial) {
168     $var = 'si'.$initial;
170     $pagingbar .= ' <div class="initialbar '.$initial.'initial">';
171     $pagingbar .= get_string($initial.'name').':&nbsp;';
173     if ($$var == 'all') {
174         $pagingbar .= '<strong>'.get_string('all').'</strong> ';
175     }
176     else {
177         $pagingbar .= '<a href="'.$link.'">'.get_string('all').'</a> ';
178     }
180     foreach ($alphabet as $letter) {
181         if ($$var === $letter) {
182             $pagingbar .= '<strong>'.$letter.'</strong> ';
183         }
184         else {
185             $pagingbar .= '<a href="'.$link.'&amp;'.$var.'='.$letter.'">'.$letter.'</a> ';
186         }
187     }
189     $pagingbar .= '</div>';
192 // Do we need a paging bar?
193 if($total > COMPLETION_REPORT_PAGE) {
195     // Paging bar
196     $pagingbar .= '<div class="paging">';
197     $pagingbar .= get_string('page').': ';
199     // Display previous link
200     if ($start > 0) {
201         $pstart = max($start - COMPLETION_REPORT_PAGE, 0);
202         $pagingbar .= '(<a class="previous" href="'.$link.$pstart.'">'.get_string('previous').'</a>)&nbsp;';
203     }
205     // Create page links
206     $curstart = 0;
207     $curpage = 0;
208     while ($curstart < $total) {
209         $curpage++;
211         if ($curstart == $start) {
212             $pagingbar .= '&nbsp;'.$curpage.'&nbsp;';
213         }
214         else {
215             $pagingbar .= '&nbsp;<a href="'.$link.$curstart.'">'.$curpage.'</a>&nbsp;';
216         }
218         $curstart += COMPLETION_REPORT_PAGE;
219     }
221     // Display next link
222     $nstart = $start + COMPLETION_REPORT_PAGE;
223     if ($nstart < $total) {
224         $pagingbar .= '&nbsp;(<a class="next" href="'.$link.$nstart.'">'.get_string('next').'</a>)';
225     }
227     $pagingbar .= '</div>';
230 // Okay, let's draw the table of progress info,
232 // Start of table
233 if(!$csv) {
234     print '<br class="clearer"/>'; // ugh
236     print $pagingbar;
238     if (!$total) {
239         print_heading(get_string('nothingtodisplay'));
240         print_footer($course);
241         exit;
242     }
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">';
248     if($firstnamesort) {
249         print
250             get_string('firstname').' / <a href="./?course='.$course->id.'">'.
251             get_string('lastname').'</a>';
252     } else {
253         print '<a href="./?course='.$course->id.'&amp;sort=firstname">'.
254             get_string('firstname').'</a> / '.
255             get_string('lastname');
256     }
257     print '</th>';
259     if($idnumbers) {
260         print '<th>'.get_string('idnumber').'</th>';
261     }
263 } else {
264     if($idnumbers) {
265         print $sep;
266     }
269 // Activities
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'));
276     } else {
277         $datetext='';
278     }
280     // Some names (labels) come URL-encoded and can be very long, so shorten them
281     $activity->name = shorten_text($activity->name);
283     if($csv) {
284         print $sep.csv_quote(strip_tags($activity->name)).$sep.csv_quote($datetext);
285     } else {
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>';
294         }
295         print '</th>';
296     }
299 if($csv) {
300     print $line;
301 } else {
302     print '</tr>';
305 // Row for each user
306 foreach($progress as $user) {
307     // User name
308     if($csv) {
309         print csv_quote(fullname($user));
310         if($idnumbers) {
311             print $sep.csv_quote($user->idnumber);
312         }
313     } else {
314         print '<tr><th scope="row"><a href="'.$CFG->wwwroot.'/user/view.php?id='.
315             $user->id.'&amp;course='.$course->id.'">'.fullname($user).'</a></th>';
316         if($idnumbers) {
317             print '<td>'.htmlspecialchars($user->idnumber).'</td>';
318         }
319     }
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);
329         } else {
330             $state=COMPLETION_INCOMPLETE;
331             $date='';
332         }
334         // Work out how it corresponds to an icon
335         switch($state) {
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;
340         }
342         $completionicon='completion-'.
343             ($activity->completion==COMPLETION_TRACKING_AUTOMATIC ? 'auto' : 'manual').
344             '-'.$completiontype;
346         $describe=get_string('completion-alt-auto-'.$completiontype,'completion');
347         $a=new StdClass;
348         $a->state=$describe;
349         $a->date=$date;
350         $a->user=fullname($user);
351         $a->activity=strip_tags($activity->name);
352         $fulldescribe=get_string('progress-title','completion',$a);
354         if($csv) {
355             print $sep.csv_quote($describe).$sep.csv_quote($date);
356         } else {
357             print '<td class="completion-progresscell '.$activity->datepassedclass.'">'.
358                 '<img src="'.$OUTPUT->pix_url('i/'.$completionicon).
359                 '" alt="'.$describe.'" title="'.$fulldescribe.'" /></td>';
360         }
361     }
363     if($csv) {
364         print $line;
365     } else {
366         print '</tr>';
367     }
370 if($csv) {
371     exit;
373 print '</table>';
374 print $pagingbar;
376 print '<ul class="progress-actions"><li><a href="index.php?course='.$course->id.
377     '&amp;format=csv">'.get_string('csvdownload','completion').'</a></li>
378     <li><a href="index.php?course='.$course->id.'&amp;format=excelcsv">'.
379     get_string('excelcsvdownload','completion').'</a></li></ul>';
381 echo $OUTPUT->footer();