589b6c2412671a486bb6e799448bac86c8b47e28
[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     $PAGE->navbar->add($strreports, new moodle_url('/course/report.php', array('id'=>$course->id)));
103     $PAGE->navbar->add($strcompletion);
104     echo $OUTPUT->header();
106     if($svgcleverness) {
107         $PAGE->requires->yui2_lib('event');
108         $PAGE->requires->js('/course/report/progress/textrotate.js');
109     }
111     // Handle groups (if enabled)
112     groups_print_course_menu($course,$CFG->wwwroot.'/course/report/progress/?course='.$course->id);
115 // Do we need a paging bar?
116 if($progress->total > COMPLETION_REPORT_PAGE) {
117     $pagingbar='<div class="completion_pagingbar">';
119     if($start>0) {
120         $newstart=$start-COMPLETION_REPORT_PAGE;
121         if($newstart<0) {
122             $newstart=0;
123         }
124         $pagingbar.=link_arrow_left(get_string('previous'),'./?course='.$course->id.
125             ($newstart ? '&amp;start='.$newstart : ''),false,'completion_prev');
126     }
128     $a=new StdClass;
129     $a->from=$start+1;
130     $a->to=$start+COMPLETION_REPORT_PAGE;
131     $a->total=$progress->total;
132     $pagingbar.='<p>'.get_string('reportpage','completion',$a).'</p>';
134     if($start+COMPLETION_REPORT_PAGE < $progress->total) {
135         $pagingbar.=link_arrow_right(get_string('next'),'./?course='.$course->id.
136             '&amp;start='.($start+COMPLETION_REPORT_PAGE),false,'completion_next');
137     }
139     $pagingbar.='</div>';
140 } else {
141     $pagingbar='';
144 // Okay, let's draw the table of progress info,
146 // Start of table
147 if(!$csv) {
148     print '<br class="clearer"/>'; // ugh
149     if(count($progress->users)==0) {
150         print '<p class="nousers">'.get_string('err_nousers','completion').'</p>';
151         print '<p><a href="'.$reportsurl.'">'.get_string('continue').'</a></p>';
152         echo $OUTPUT->footer();
153         exit;
154     }
155     print $pagingbar;
156     print '<table id="completion-progress" class="generaltable flexible boxaligncenter" style="text-align:left"><tr style="vertical-align:top">';
158     // User heading / sort option
159     print '<th scope="col" class="completion-sortchoice">';
160     if($firstnamesort) {
161         print
162             get_string('firstname').' / <a href="./?course='.$course->id.'">'.
163             get_string('lastname').'</a>';
164     } else {
165         print '<a href="./?course='.$course->id.'&amp;sort=firstname">'.
166             get_string('firstname').'</a> / '.
167             get_string('lastname');
168     }
169     print '</th>';
171     if($idnumbers) {
172         print '<th>'.get_string('idnumber').'</th>';
173     }
175 } else {
176     if($idnumbers) {
177         print $sep;
178     }
181 // Activities
182 foreach($activities as $activity) {
183     $activity->datepassed = $activity->completionexpected && $activity->completionexpected <= time();
184     $activity->datepassedclass=$activity->datepassed ? 'completion-expired' : '';
186     if($activity->completionexpected) {
187         $datetext=userdate($activity->completionexpected,get_string('strftimedate','langconfig'));
188     } else {
189         $datetext='';
190     }
192     // Some names (labels) come URL-encoded and can be very long, so shorten them
193     $activity->name = shorten_text($activity->name);
195     if($csv) {
196         print $sep.csv_quote(strip_tags($activity->name)).$sep.csv_quote($datetext);
197     } else {
198         print '<th scope="col" class="'.$activity->datepassedclass.'">'.
199             '<a href="'.$CFG->wwwroot.'/mod/'.$activity->modname.
200             '/view.php?id='.$activity->id.'">'.
201             '<img src="'.$OUTPUT->pix_url('icon', $activity->modname).'/icon.gif" alt="'.
202             get_string('modulename',$activity->modname).'" /> <span class="completion-activityname">'.
203             format_string($activity->name).'</span></a>';
204         if($activity->completionexpected) {
205             print '<div class="completion-expected"><span>'.$datetext.'</span></div>';
206         }
207         print '</th>';
208     }
211 if($csv) {
212     print $line;
213 } else {
214     print '</tr>';
217 // Row for each user
218 foreach($progress->users as $user) {
219     // User name
220     if($csv) {
221         print csv_quote(fullname($user));
222         if($idnumbers) {
223             print $sep.csv_quote($user->idnumber);
224         }
225     } else {
226         print '<tr><th scope="row"><a href="'.$CFG->wwwroot.'/user/view.php?id='.
227             $user->id.'&amp;course='.$course->id.'">'.fullname($user).'</a></th>';
228         if($idnumbers) {
229             print '<td>'.htmlspecialchars($user->idnumber).'</td>';
230         }
231     }
233     // Progress for each activity
234     foreach($activities as $activity) {
236         // Get progress information and state
237         if(array_key_exists($activity->id,$user->progress)) {
238             $thisprogress=$user->progress[$activity->id];
239             $state=$thisprogress->completionstate;
240             $date=userdate($thisprogress->timemodified);
241         } else {
242             $state=COMPLETION_INCOMPLETE;
243             $date='';
244         }
246         // Work out how it corresponds to an icon
247         switch($state) {
248             case COMPLETION_INCOMPLETE : $completiontype='n'; break;
249             case COMPLETION_COMPLETE : $completiontype='y'; break;
250             case COMPLETION_COMPLETE_PASS : $completiontype='pass'; break;
251             case COMPLETION_COMPLETE_FAIL : $completiontype='fail'; break;
252         }
254         $completionicon='completion-'.
255             ($activity->completion==COMPLETION_TRACKING_AUTOMATIC ? 'auto' : 'manual').
256             '-'.$completiontype;
258         $describe=get_string('completion-alt-auto-'.$completiontype,'completion');
259         $a=new StdClass;
260         $a->state=$describe;
261         $a->date=$date;
262         $a->user=fullname($user);
263         $a->activity=strip_tags($activity->name);
264         $fulldescribe=get_string('progress-title','completion',$a);
266         if($csv) {
267             print $sep.csv_quote($describe).$sep.csv_quote($date);
268         } else {
269             print '<td class="completion-progresscell '.$activity->datepassedclass.'">'.
270                 '<img src="'.$OUTPUT->pix_url('i/'.$completionicon).
271                 '" alt="'.$describe.'" title="'.$fulldescribe.'" /></td>';
272         }
273     }
275     if($csv) {
276         print $line;
277     } else {
278         print '</tr>';
279     }
282 if($csv) {
283     exit;
285 print '</table>';
286 print $pagingbar;
288 print '<ul class="progress-actions"><li><a href="index.php?course='.$course->id.
289     '&amp;format=csv">'.get_string('csvdownload','completion').'</a></li>
290     <li><a href="index.php?course='.$course->id.'&amp;format=excelcsv">'.
291     get_string('excelcsvdownload','completion').'</a></li></ul>';
293 echo $OUTPUT->footer();