MDL-10870 A few more fixes to the file.php page's navigation
[moodle-pu.git] / course / grades.php
blob33928b17dd9d4232d1d984473d3bc3b0e1ef9301
1 <?php // $Id$
2 // Displays all grades for a course
4 require_once("../config.php");
5 require_once("lib.php");
7 $id = required_param('id', PARAM_INT); // course id
8 $download = optional_param('download', '', PARAM_ALPHA);// to download data
9 $changegroup = optional_param('group', -1, PARAM_INT );
11 require_login();
13 if (! $course = get_record("course", "id", $id)) {
14 error("Course ID was incorrect");
17 require_capability('moodle/course:viewcoursegrades', get_context_instance(CONTEXT_COURSE, $id));
19 $strgrades = get_string("grades");
20 $strgrade = get_string("grade");
21 $strmax = get_string("maximumshort");
22 $stractivityreport = get_string("activityreport");
24 /// Check to see if groups are being used in this course
25 $groupmode = groupmode($course);
26 $currentgroup = get_and_set_current_group($course, $groupmode, $changegroup);
28 /// Get a list of all students
29 if ($currentgroup) {
30 if (!$students = get_group_students($currentgroup, "u.lastname ASC")) {
31 print_header("$course->shortname: $strgrades", $course->fullname,
32 "<a href=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->shortname</a>
33 -> $strgrades");
34 setup_and_print_groups($course, $groupmode, "grades.php?id=$course->id");
35 notice(get_string("nostudentsingroup"), "$CFG->wwwroot/course/view.php?id=$course->id");
36 print_footer($course);
37 exit;
39 } else {
40 if (!$students = get_course_students($course->id, "u.lastname ASC")) {
41 print_header("$course->shortname: $strgrades", $course->fullname,
42 "<a href=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->shortname</a>
43 -> $strgrades");
44 notice(get_string("nostudentsyet"), "$CFG->wwwroot/course/view.php?id=$course->id");
45 print_footer($course);
46 exit;
50 foreach ($students as $student) {
51 $grades[$student->id] = array(); // Collect all grades in this array
52 $gradeshtml[$student->id] = array(); // Collect all grades html formatted in this array
53 $totals[$student->id] = array(); // Collect all totals in this array
55 $columns = array(); // Accumulate column names in this array.
56 $columnhtml = array(); // Accumulate column html in this array.
59 /// Collect modules data
60 get_all_mods($course->id, $mods, $modnames, $modnamesplural, $modnamesused);
63 /// Search through all the modules, pulling out grade data
64 $sections = get_all_sections($course->id); // Sort everything the same as the course
65 for ($i=0; $i<=$course->numsections; $i++) {
66 if (isset($sections[$i])) { // should always be true
67 $section = $sections[$i];
68 if ($section->sequence) {
69 $sectionmods = explode(",", $section->sequence);
70 foreach ($sectionmods as $sectionmod) {
71 $mod = $mods[$sectionmod];
72 $instance = get_record("$mod->modname", "id", "$mod->instance");
73 $libfile = "$CFG->dirroot/mod/$mod->modname/lib.php";
74 if (file_exists($libfile)) {
75 require_once($libfile);
76 $gradefunction = $mod->modname."_grades";
77 if (function_exists($gradefunction)) { // Skip modules without grade function
78 if ($modgrades = $gradefunction($mod->instance)) {
80 if (!empty($modgrades->maxgrade)) {
81 if ($mod->visible) {
82 $maxgrade = "$strmax: $modgrades->maxgrade";
83 $maxgradehtml = "<br />$strmax: $modgrades->maxgrade";
84 } else {
85 $maxgrade = "$strmax: $modgrades->maxgrade";
86 $maxgradehtml = "<br /><font class=\"dimmed_text\">$strmax: $modgrades->maxgrade</font>";
88 } else {
89 $maxgrade = "";
90 $maxgradehtml = "";
93 $image = "<a href=\"$CFG->wwwroot/mod/$mod->modname/view.php?id=$mod->id\"".
94 " title=\"$mod->modfullname\">".
95 "<img src=\"../mod/$mod->modname/icon.gif\" ".
96 "class=\"icon\" alt=\"$mod->modfullname\" /></a>";
97 if ($mod->visible) {
98 $columnhtml[] = "$image ".
99 "<a href=\"$CFG->wwwroot/mod/$mod->modname/view.php?id=$mod->id\">".
100 format_string($instance->name,true).
101 "</a>$maxgradehtml";
102 } else {
103 $columnhtml[] = "$image ".
104 "<a class=\"dimmed\" href=\"$CFG->wwwroot/mod/$mod->modname/view.php?id=$mod->id\">".
105 format_string($instance->name,true).
106 "</a>$maxgradehtml";
108 $columns[] = "$mod->modfullname: ".format_string($instance->name)." - $maxgrade";
110 foreach ($students as $student) {
111 if (!empty($modgrades->grades[$student->id])) {
112 $grades[$student->id][] = $currentstudentgrade = $modgrades->grades[$student->id];
113 if ($mod->visible) {
114 $gradeshtml[$student->id][] = $modgrades->grades[$student->id];
115 } else {
116 $gradeshtml[$student->id][] = "<font class=\"dimmed_text\">".
117 $modgrades->grades[$student->id].
118 "</font>";
120 } else {
121 $grades[$student->id][] = $currentstudentgrade = "";
122 $gradeshtml[$student->id][] = "";
124 if (!empty($modgrades->maxgrade)) {
125 $totals[$student->id] = (float)($totals[$student->id]) + (float)($currentstudentgrade);
126 } else {
127 $totals[$student->id] = (float)($totals[$student->id]) + 0;
136 } // a new Moodle nesting record? ;-)
139 /// OK, we have all the data, now present it to the user
140 if ($download == "ods" and confirm_sesskey()) {
141 require_once("../lib/odslib.class.php");
143 /// Calculate file name
144 $downloadfilename = clean_filename("$course->shortname $strgrades.ods");
145 /// Creating a workbook
146 $workbook = new MoodleODSWorkbook("-");
147 /// Sending HTTP headers
148 $workbook->send($downloadfilename);
149 /// Adding the worksheet
150 $myxls =& $workbook->add_worksheet($strgrades);
152 /// Print names of all the fields
153 $myxls->write_string(0,0,get_string("firstname"));
154 $myxls->write_string(0,1,get_string("lastname"));
155 $myxls->write_string(0,2,get_string("idnumber"));
156 $myxls->write_string(0,3,get_string("institution"));
157 $myxls->write_string(0,4,get_string("department"));
158 $myxls->write_string(0,5,get_string("email"));
159 $pos=6;
160 foreach ($columns as $column) {
161 $myxls->write_string(0,$pos++,strip_tags($column));
163 $myxls->write_string(0,$pos,get_string("total"));
166 /// Print all the lines of data.
167 $i = 0;
168 foreach ($grades as $studentid => $studentgrades) {
169 $i++;
170 $student = $students[$studentid];
171 if (empty($totals[$student->id])) {
172 $totals[$student->id] = '';
175 $myxls->write_string($i,0,$student->firstname);
176 $myxls->write_string($i,1,$student->lastname);
177 $myxls->write_string($i,2,$student->idnumber);
178 $myxls->write_string($i,3,$student->institution);
179 $myxls->write_string($i,4,$student->department);
180 $myxls->write_string($i,5,$student->email);
181 $j=6;
182 foreach ($studentgrades as $grade) {
183 $myxls->write_string($i,$j++,strip_tags($grade));
185 $myxls->write_number($i,$j,$totals[$student->id]);
188 /// Close the workbook
189 $workbook->close();
191 exit;
193 } else if ($download == "xls" and confirm_sesskey()) {
194 require_once("../lib/excellib.class.php");
196 /// Calculate file name
197 $downloadfilename = clean_filename("$course->shortname $strgrades.xls");
198 /// Creating a workbook
199 $workbook = new MoodleExcelWorkbook("-");
200 /// Sending HTTP headers
201 $workbook->send($downloadfilename);
202 /// Adding the worksheet
203 $myxls =& $workbook->add_worksheet($strgrades);
205 /// Print names of all the fields
206 $myxls->write_string(0,0,get_string("firstname"));
207 $myxls->write_string(0,1,get_string("lastname"));
208 $myxls->write_string(0,2,get_string("idnumber"));
209 $myxls->write_string(0,3,get_string("institution"));
210 $myxls->write_string(0,4,get_string("department"));
211 $myxls->write_string(0,5,get_string("email"));
212 $pos=6;
213 foreach ($columns as $column) {
214 $myxls->write_string(0,$pos++,strip_tags($column));
216 $myxls->write_string(0,$pos,get_string("total"));
219 /// Print all the lines of data.
220 $i = 0;
221 foreach ($grades as $studentid => $studentgrades) {
222 $i++;
223 $student = $students[$studentid];
224 if (empty($totals[$student->id])) {
225 $totals[$student->id] = '';
228 $myxls->write_string($i,0,$student->firstname);
229 $myxls->write_string($i,1,$student->lastname);
230 $myxls->write_string($i,2,$student->idnumber);
231 $myxls->write_string($i,3,$student->institution);
232 $myxls->write_string($i,4,$student->department);
233 $myxls->write_string($i,5,$student->email);
234 $j=6;
235 foreach ($studentgrades as $grade) {
236 $myxls->write_string($i,$j++,strip_tags($grade));
238 $myxls->write_number($i,$j,$totals[$student->id]);
241 /// Close the workbook
242 $workbook->close();
244 exit;
246 } else if ($download == "txt" and confirm_sesskey()) {
248 /// Print header to force download
250 header("Content-Type: application/download\n");
251 $downloadfilename = clean_filename("$course->shortname $strgrades");
252 header("Content-Disposition: attachment; filename=\"$downloadfilename.txt\"");
254 /// Print names of all the fields
256 echo get_string("firstname")."\t".
257 get_string("lastname")."\t".
258 get_string("idnumber")."\t".
259 get_string("institution")."\t".
260 get_string("department")."\t".
261 get_string("email");
262 foreach ($columns as $column) {
263 $column = strip_tags($column);
264 echo "\t$column";
266 echo "\t".get_string("total")."\n";
268 /// Print all the lines of data.
270 foreach ($grades as $studentid => $studentgrades) {
271 $student = $students[$studentid];
272 if (empty($totals[$student->id])) {
273 $totals[$student->id] = '';
275 echo "$student->firstname\t$student->lastname\t$student->idnumber\t$student->institution\t$student->department\t$student->email";
276 foreach ($studentgrades as $grade) {
277 $grade = strip_tags($grade);
278 echo "\t$grade";
280 echo "\t".$totals[$student->id];
281 echo "\n";
284 exit;
287 } else { // Just print the web page
289 print_header("$course->shortname: $strgrades", $course->fullname,
290 "<a href=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->shortname</a>
291 -> $strgrades");
293 print_heading($strgrades);
295 setup_and_print_groups($course, $groupmode, "grades.php?id=$course->id");
297 echo "<table border=\"0\" align=\"center\"><tr>";
298 echo "<td>";
299 $options = array();
300 $options["id"] = "$course->id";
301 $options["download"] = "ods";
302 $options["sesskey"] = $USER->sesskey;
303 print_single_button("grades.php", $options, get_string("downloadods"));
304 echo "<td>";
305 $options["download"] = "xls";
306 print_single_button("grades.php", $options, get_string("downloadexcel"));
307 echo "<td>";
308 $options["download"] = "txt";
309 print_single_button("grades.php", $options, get_string("downloadtext"));
310 echo "</table>";
313 $table->head = array_merge(array ("", get_string("firstname"), get_string("lastname")), $columnhtml, array(get_string("total")));
314 $table->width = array(35, "");
315 $table->align = array("LEFT", "RIGHT", "LEFT");
316 foreach ($columns as $column) {
317 $table->width[] = "";
318 $table->align[] = "CENTER";
320 $table->width[] = "";
321 $table->align[] = "CENTER";
323 foreach ($students as $key => $student) {
324 $studentgrades = $gradeshtml[$student->id];
325 if (empty($totals[$student->id])) {
326 $totals[$student->id] = '';
328 $picture = print_user_picture($student->id, $course->id, $student->picture, false, true);
329 $name = array ("$picture", "$student->firstname", "$student->lastname");
330 $total = array ($totals[$student->id]);
332 $table->data[] = array_merge($name, $studentgrades, $total);
335 print_table($table);
337 print_footer($course);