c36deb277a44a085b8a23ba5f0e3d9f5a59256b3
[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 !== 0) {
50     $url->param('start', $start);
51 }
52 $PAGE->set_url($url);
53 $PAGE->set_pagelayout('admin');
55 require_login($course);
57 // Check basic permission
58 $context=get_context_instance(CONTEXT_COURSE,$course->id);
59 require_capability('coursereport/progress:view',$context);
61 // Get group mode
62 $group=groups_get_course_group($course,true); // Supposed to verify group
63 if($group===0 && $course->groupmode==SEPARATEGROUPS) {
64     require_capability('moodle/site:accessallgroups',$context);
65 }
67 // Get data on activities and progress of all users, and give error if we've
68 // nothing to display (no users or no activities)
69 $reportsurl=$CFG->wwwroot.'/course/report.php?id='.$course->id;
70 $completion=new completion_info($course);
71 $activities=$completion->get_activities();
72 if(count($activities)==0) {
73     print_error('err_noactivities','completion',$reportsurl);
74 }
76 $progress=$completion->get_progress_all($firstnamesort,$group,
77     $csv ? 0 :COMPLETION_REPORT_PAGE,$csv ? 0 : $start);
79 if($csv) {
80     header('Content-Disposition: attachment; filename=progress.'.
81         preg_replace('/[^a-z0-9-]/','_',strtolower($course->shortname)).'.csv');
82     // Unicode byte-order mark for Excel
83     if($excel) {
84         header('Content-Type: text/csv; charset=UTF-16LE');
85         print chr(0xFF).chr(0xFE);
86         $sep="\t".chr(0);
87         $line="\n".chr(0);
88     } else {
89         header('Content-Type: text/csv; charset=UTF-8');
90         $sep=",";
91         $line="\n";
92     }
93 } else {
94     // Use SVG to draw sideways text if supported
95     $svgcleverness = can_use_rotated_text();
97     // Navigation and header
98     $strreports = get_string("reports");
99     $strcompletion = get_string('completionreport','completion');
101     $PAGE->set_title($strcompletion);
102     $PAGE->set_heading($course->fullname);
103     echo $OUTPUT->header();
105     if($svgcleverness) {
106         $PAGE->requires->yui2_lib('event');
107         $PAGE->requires->js('/course/report/progress/textrotate.js');
108     }
110     // Handle groups (if enabled)
111     groups_print_course_menu($course,$CFG->wwwroot.'/course/report/progress/?course='.$course->id);
114 // Do we need a paging bar?
115 if($progress->total > COMPLETION_REPORT_PAGE) {
116     $pagingbar='<div class="completion_pagingbar">';
118     if($start>0) {
119         $newstart=$start-COMPLETION_REPORT_PAGE;
120         if($newstart<0) {
121             $newstart=0;
122         }
123         $pagingbar.=link_arrow_left(get_string('previous'),'./?course='.$course->id.
124             ($newstart ? '&amp;start='.$newstart : ''),false,'completion_prev');
125     }
127     $a=new StdClass;
128     $a->from=$start+1;
129     $a->to=$start+COMPLETION_REPORT_PAGE;
130     $a->total=$progress->total;
131     $pagingbar.='<p>'.get_string('reportpage','completion',$a).'</p>';
133     if($start+COMPLETION_REPORT_PAGE < $progress->total) {
134         $pagingbar.=link_arrow_right(get_string('next'),'./?course='.$course->id.
135             '&amp;start='.($start+COMPLETION_REPORT_PAGE),false,'completion_next');
136     }
138     $pagingbar.='</div>';
139 } else {
140     $pagingbar='';
143 // Okay, let's draw the table of progress info,
145 // Start of table
146 if(!$csv) {
147     print '<br class="clearer"/>'; // ugh
148     if(count($progress->users)==0) {
149         print '<p class="nousers">'.get_string('err_nousers','completion').'</p>';
150         print '<p><a href="'.$reportsurl.'">'.get_string('continue').'</a></p>';
151         echo $OUTPUT->footer();
152         exit;
153     }
154     print $pagingbar;
155     print '<table id="completion-progress" class="generaltable flexible boxaligncenter" style="text-align:left"><tr style="vertical-align:top">';
157     // User heading / sort option
158     print '<th scope="col" class="completion-sortchoice">';
159     if($firstnamesort) {
160         print
161             get_string('firstname').' / <a href="./?course='.$course->id.'">'.
162             get_string('lastname').'</a>';
163     } else {
164         print '<a href="./?course='.$course->id.'&amp;sort=firstname">'.
165             get_string('firstname').'</a> / '.
166             get_string('lastname');
167     }
168     print '</th>';
170     if($idnumbers) {
171         print '<th>'.get_string('idnumber').'</th>';
172     }
174 } else {
175     if($idnumbers) {
176         print $sep;
177     }
180 // Activities
181 foreach($activities as $activity) {
182     $activity->datepassed = $activity->completionexpected && $activity->completionexpected <= time();
183     $activity->datepassedclass=$activity->datepassed ? 'completion-expired' : '';
185     if($activity->completionexpected) {
186         $datetext=userdate($activity->completionexpected,get_string('strftimedate','langconfig'));
187     } else {
188         $datetext='';
189     }
191     // Some names (labels) come URL-encoded and can be very long, so shorten them
192     $activity->name = shorten_text($activity->name);
194     if($csv) {
195         print $sep.csv_quote(strip_tags($activity->name)).$sep.csv_quote($datetext);
196     } else {
197         print '<th scope="col" class="'.$activity->datepassedclass.'">'.
198             '<a href="'.$CFG->wwwroot.'/mod/'.$activity->modname.
199             '/view.php?id='.$activity->id.'">'.
200             '<img src="'.$OUTPUT->pix_url('icon', $activity->modname).'/icon.gif" alt="'.
201             get_string('modulename',$activity->modname).'" /> <span class="completion-activityname">'.
202             format_string($activity->name).'</span></a>';
203         if($activity->completionexpected) {
204             print '<div class="completion-expected"><span>'.$datetext.'</span></div>';
205         }
206         print '</th>';
207     }
210 if($csv) {
211     print $line;
212 } else {
213     print '</tr>';
216 // Row for each user
217 foreach($progress->users as $user) {
218     // User name
219     if($csv) {
220         print csv_quote(fullname($user));
221         if($idnumbers) {
222             print $sep.csv_quote($user->idnumber);
223         }
224     } else {
225         print '<tr><th scope="row"><a href="'.$CFG->wwwroot.'/user/view.php?id='.
226             $user->id.'&amp;course='.$course->id.'">'.fullname($user).'</a></th>';
227         if($idnumbers) {
228             print '<td>'.htmlspecialchars($user->idnumber).'</td>';
229         }
230     }
232     // Progress for each activity
233     foreach($activities as $activity) {
235         // Get progress information and state
236         if(array_key_exists($activity->id,$user->progress)) {
237             $thisprogress=$user->progress[$activity->id];
238             $state=$thisprogress->completionstate;
239             $date=userdate($thisprogress->timemodified);
240         } else {
241             $state=COMPLETION_INCOMPLETE;
242             $date='';
243         }
245         // Work out how it corresponds to an icon
246         switch($state) {
247             case COMPLETION_INCOMPLETE : $completiontype='n'; break;
248             case COMPLETION_COMPLETE : $completiontype='y'; break;
249             case COMPLETION_COMPLETE_PASS : $completiontype='pass'; break;
250             case COMPLETION_COMPLETE_FAIL : $completiontype='fail'; break;
251         }
253         $completionicon='completion-'.
254             ($activity->completion==COMPLETION_TRACKING_AUTOMATIC ? 'auto' : 'manual').
255             '-'.$completiontype;
257         $describe=get_string('completion-alt-auto-'.$completiontype,'completion');
258         $a=new StdClass;
259         $a->state=$describe;
260         $a->date=$date;
261         $a->user=fullname($user);
262         $a->activity=strip_tags($activity->name);
263         $fulldescribe=get_string('progress-title','completion',$a);
265         if($csv) {
266             print $sep.csv_quote($describe).$sep.csv_quote($date);
267         } else {
268             print '<td class="completion-progresscell '.$activity->datepassedclass.'">'.
269                 '<img src="'.$OUTPUT->pix_url('i/'.$completionicon).
270                 '" alt="'.$describe.'" title="'.$fulldescribe.'" /></td>';
271         }
272     }
274     if($csv) {
275         print $line;
276     } else {
277         print '</tr>';
278     }
281 if($csv) {
282     exit;
284 print '</table>';
285 print $pagingbar;
287 print '<ul class="progress-actions"><li><a href="index.php?course='.$course->id.
288     '&amp;format=csv">'.get_string('csvdownload','completion').'</a></li>
289     <li><a href="index.php?course='.$course->id.'&amp;format=excelcsv">'.
290     get_string('excelcsvdownload','completion').'</a></li></ul>';
292 echo $OUTPUT->footer();