MDL-15498: Completion: added TODO comment re last commit
[moodle.git] / course / report / progress / index.php
1 <?php
3 require_once('../../../config.php');
4 global $DB;
6 // Get course
7 $course=$DB->get_record('course',array('id'=>required_param('course',PARAM_INT)));
8 if(!$course) {
9     error('Specified course not found');    
10 }
12 // Sort (default lastname, optionally firstname)
13 $sort=optional_param('sort','',PARAM_ALPHA);
14 $firstnamesort=$sort=='firstname';
16 // CSV format
17 $format=optional_param('format','',PARAM_ALPHA);
18 $excel=$format=='excelcsv';
19 $csv=$format=='csv' || $excel;
21 // Whether to show idnumber
22 // TODO: This should really not be using a config option 'intended' for 
23 // gradebook, but that option is also used in quiz reports as well. There ought
24 // to be a generic option somewhere.
25 $idnumbers=$CFG->grade_report_showuseridnumber;
27 function csv_quote($value) {
28     global $excel;
29     if($excel) {
30         $tl=textlib_get_instance();
31         return $tl->convert('"'.str_replace('"',"'",$value).'"','UTF-8','UTF-16LE');
32     } else {
33         return '"'.str_replace('"',"'",$value).'"';
34     }
35 }
37 require_login($course->id);
39 // Check basic permission
40 $context=get_context_instance(CONTEXT_COURSE,$course->id);
41 require_capability('moodle/course:viewprogress',$context);
43 // Get group mode
44 $group=groups_get_course_group($course,true); // Supposed to verify group
45 if($group===0 && $course->groupmode==SEPARATEGROUPS) {
46     require_capability('moodle/site:accessallgroups',$context);
47 }
49 // Get data on activities and progress of all users, and give error if we've
50 // nothing to display (no users or no activities)
51 $reportsurl=$CFG->wwwroot.'/course/report.php?id='.$course->id;
52 $completion=new completion_info($course);
53 $activities=$completion->get_activities();
54 if(count($activities)==0) {
55     print_error('err_noactivities','completion',$reportsurl);
56 }
57 $progress=$completion->get_progress_all($firstnamesort,$group);
60 if($csv) {
61     header('Content-Disposition: attachment; filename=progress.'.
62         preg_replace('/[^a-z0-9-]/','_',strtolower($course->shortname)).'.csv');
63     // Unicode byte-order mark for Excel
64     if($excel) {
65         header('Content-Type: text/csv; charset=UTF-16LE');
66         print chr(0xFF).chr(0xFE);
67         $sep="\t".chr(0);
68         $line="\n".chr(0);        
69     } else {
70         header('Content-Type: text/csv; charset=UTF-8');
71         $sep=",";
72         $line="\n";
73     }
74 } else {
75     // Use SVG to draw sideways text if supported
76     $svgcleverness=ajaxenabled(array('Firefox'=>2.0)) && !$USER->screenreader; 
78     // Navigation and header
79     $strreports = get_string("reports");
80     $strcompletion = get_string('completionreport','completion');
81     $navlinks = array();
82     $navlinks[] = array('name' => $strreports, 'link' => "../../report.php?id=$course->id", 'type' => 'misc');
83     $navlinks[] = array('name' => $strcompletion, 'link' => null, 'type' => 'misc');
84     if($svgcleverness) {
85         require_js(array('yui_yahoo','yui_event','yui_dom'));
86     }
87     print_header($strcompletion,$course->fullname,build_navigation($navlinks));
88     if($svgcleverness) {
89         print '<script type="text/javascript" src="textrotate.js"></script>';
90     }
92     // Handle groups (if enabled)
93     groups_print_course_menu($course,$CFG->wwwroot.'/course/report/progress/?course='.$course->id);
94 }
96 // Okay, let's draw the table of progress info,
98 // Start of table  
99 if(!$csv) {
100     print '<br class="clearer"/>'; // ugh
101     if(count($progress)==0) {
102         print '<p class="nousers">'.get_string('err_nousers','completion').'</p>';
103         print '<p><a href="'.$reportsurl.'">'.get_string('continue').'</a></p>';
104         print_footer($course);
105         exit;
106     }
107     print '<table id="completion-progress" class="generaltable flexible boxaligncenter" style="text-align:left"><tr style="vertical-align:top">';
109     // User heading / sort option
110     print '<th scope="col" class="completion-sortchoice">';
111     if($firstnamesort) {
112         print 
113             get_string('firstname').' / <a href="./?course='.$course->id.'">'.
114             get_string('lastname').'</a>';
115     } else {
116         print '<a href="./?course='.$course->id.'&sort=firstname">'.
117             get_string('firstname').'</a> / '.
118             get_string('lastname');
119     }
120     print '</th>';
121     
122     if($idnumbers) {
123         print '<th>'.get_string('idnumber').'</th>';        
124     }
125     
126 } else {
127     if($idnumbers) {
128         print $sep;
129     }
132 // Activities
133 foreach($activities as $activity) {
134     $activity->datepassed = $activity->completionexpected && $activity->completionexpected <= time();
135     $activity->datepassedclass=$activity->datepassed ? 'completion-expired' : '';
137     if($activity->completionexpected) {
138         $datetext=userdate($activity->completionexpected,get_string('strftimedate','langconfig'));
139     } else {
140         $datetext='';
141     }
143     if($csv) {
144         print $sep.csv_quote($activity->name).$sep.csv_quote($datetext);
145     } else {
146         print '<th scope="col" class="'.$activity->datepassedclass.'">'.
147             '<a href="'.$CFG->wwwroot.'/mod/'.$activity->modname.
148             '/view.php?id='.$activity->id.'">'.
149             '<img src="'.$CFG->pixpath.'/mod/'.$activity->modname.'/icon.gif" alt="'.
150             get_string('modulename',$activity->modname).'" /> <span class="completion-activityname">'.
151             format_string($activity->name).'</span></a>';
152         if($activity->completionexpected) {
153             print '<div class="completion-expected"><span>'.$datetext.'</span></div>';
154         }
155         print '</th>';
156     }
159 if($csv) {
160     print $line;
161 } else {
162     print '</tr>';
165 // Row for each user
166 foreach($progress as $user) {
167     // User name
168     if($csv) {
169         print csv_quote(fullname($user));
170         if($idnumbers) {
171             print $sep.csv_quote($user->idnumber);
172         }
173     } else {
174         print '<tr><th scope="row"><a href="'.$CFG->wwwroot.'/user/view.php?id='.
175             $user->id.'&amp;course='.$course->id.'">'.fullname($user).'</a></th>';
176         if($idnumbers) {
177             print '<td>'.htmlspecialchars($user->idnumber).'</td>';
178         }
179     }
181     // Progress for each activity
182     foreach($activities as $activity) {
184         // Get progress information and state
185         if(array_key_exists($activity->id,$user->progress)) {
186             $progress=$user->progress[$activity->id];
187             $state=$progress->completionstate;
188             $date=userdate($progress->timemodified);
189         } else {
190             $state=COMPLETION_INCOMPLETE;
191             $date='';
192         }
194         // Work out how it corresponds to an icon
195         $completiontype=
196             ($activity->completion==COMPLETION_TRACKING_AUTOMATIC ? 'auto' : 'manual').
197             '-';
198         switch($state) {
199             case COMPLETION_INCOMPLETE : $completiontype.='n'; break;
200             case COMPLETION_COMPLETE : $completiontype.='y'; break;
201             case COMPLETION_COMPLETE_PASS : $completiontype.='pass'; break;
202             case COMPLETION_COMPLETE_FAIL : $completiontype.='fail'; break;
203         }        
205         $describe=get_string('completion-alt-'.$completiontype,'completion');
206         $a=new StdClass;
207         $a->state=$describe;
208         $a->date=$date;
209         $a->user=fullname($user);
210         $a->activity=$activity->name;
211         $fulldescribe=get_string('progress-title','completion',$a);
213         if($csv) {
214             print $sep.csv_quote($describe).$sep.csv_quote($date);
215         } else {
216             print '<td class="completion-progresscell '.$activity->datepassedclass.'">'.
217                 '<img src="'.$CFG->pixpath.'/i/completion-'.$completiontype.
218                 '.gif" alt="'.$describe.'" title="'.$fulldescribe.'" /></td>';
219         }
220     }
222     if($csv) {
223         print $line;
224     } else {
225         print '</tr>';
226     }
229 if($csv) {
230     exit;
232 print '</table>';
234 print '<ul class="progress-actions"><li><a href="index.php?course='.$course->id.
235     '&amp;format=csv">'.get_string('csvdownload','completion').'</a></li>
236     <li><a href="index.php?course='.$course->id.'&amp;format=excelcsv">'.
237     get_string('excelcsvdownload','completion').'</a></li></ul>';
239 print_footer($course);
240 ?>