Fixes bug MDL-8234, "New groups code & AS keyword"
[moodle-pu.git] / course / grades.php
blob53efe9573756d3152488e392431dfdaa1e7555fc
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 if ($groupmode = groupmode($course)) { // Groups are being used
26 $currentgroup = get_and_set_current_group($course, $groupmode, $changegroup);
27 } else {
28 $currentgroup = false;
31 /// Get a list of all students
32 if ($currentgroup) {
33 if (!$students = get_group_students($currentgroup, "u.lastname ASC")) {
34 print_header("$course->shortname: $strgrades", "$course->fullname",
35 "<a href=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->shortname</a>
36 -> $strgrades");
37 setup_and_print_groups($course, $groupmode, "grades.php?id=$course->id");
38 notice(get_string("nostudentsingroup"), "$CFG->wwwroot/course/view.php?id=$course->id");
39 print_footer($course);
40 exit;
42 } else {
43 if (!$students = get_course_students($course->id, "u.lastname ASC")) {
44 print_header("$course->shortname: $strgrades", "$course->fullname",
45 "<a href=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->shortname</a>
46 -> $strgrades");
47 notice(get_string("nostudentsyet"), "$CFG->wwwroot/course/view.php?id=$course->id");
48 print_footer($course);
49 exit;
53 foreach ($students as $student) {
54 $grades[$student->id] = array(); // Collect all grades in this array
55 $gradeshtml[$student->id] = array(); // Collect all grades html formatted in this array
56 $totals[$student->id] = array(); // Collect all totals in this array
58 $columns = array(); // Accumulate column names in this array.
59 $columnhtml = array(); // Accumulate column html in this array.
62 /// Collect modules data
63 get_all_mods($course->id, $mods, $modnames, $modnamesplural, $modnamesused);
66 /// Search through all the modules, pulling out grade data
67 $sections = get_all_sections($course->id); // Sort everything the same as the course
68 for ($i=0; $i<=$course->numsections; $i++) {
69 if (isset($sections[$i])) { // should always be true
70 $section = $sections[$i];
71 if ($section->sequence) {
72 $sectionmods = explode(",", $section->sequence);
73 foreach ($sectionmods as $sectionmod) {
74 $mod = $mods[$sectionmod];
75 $instance = get_record("$mod->modname", "id", "$mod->instance");
76 $libfile = "$CFG->dirroot/mod/$mod->modname/lib.php";
77 if (file_exists($libfile)) {
78 require_once($libfile);
79 $gradefunction = $mod->modname."_grades";
80 if (function_exists($gradefunction)) { // Skip modules without grade function
81 if ($modgrades = $gradefunction($mod->instance)) {
83 if (!empty($modgrades->maxgrade)) {
84 if ($mod->visible) {
85 $maxgrade = "$strmax: $modgrades->maxgrade";
86 $maxgradehtml = "<br />$strmax: $modgrades->maxgrade";
87 } else {
88 $maxgrade = "$strmax: $modgrades->maxgrade";
89 $maxgradehtml = "<br /><font class=\"dimmed_text\">$strmax: $modgrades->maxgrade</font>";
91 } else {
92 $maxgrade = "";
93 $maxgradehtml = "";
96 $image = "<a href=\"$CFG->wwwroot/mod/$mod->modname/view.php?id=$mod->id\"".
97 " title=\"$mod->modfullname\">".
98 "<img src=\"../mod/$mod->modname/icon.gif\" ".
99 "class=\"icon\" alt=\"$mod->modfullname\" /></a>";
100 if ($mod->visible) {
101 $columnhtml[] = "$image ".
102 "<a href=\"$CFG->wwwroot/mod/$mod->modname/view.php?id=$mod->id\">".
103 format_string($instance->name,true).
104 "</a>$maxgradehtml";
105 } else {
106 $columnhtml[] = "$image ".
107 "<a class=\"dimmed\" href=\"$CFG->wwwroot/mod/$mod->modname/view.php?id=$mod->id\">".
108 format_string($instance->name,true).
109 "</a>$maxgradehtml";
111 $columns[] = "$mod->modfullname: ".format_string($instance->name)." - $maxgrade";
113 foreach ($students as $student) {
114 if (!empty($modgrades->grades[$student->id])) {
115 $grades[$student->id][] = $currentstudentgrade = $modgrades->grades[$student->id];
116 if ($mod->visible) {
117 $gradeshtml[$student->id][] = $modgrades->grades[$student->id];
118 } else {
119 $gradeshtml[$student->id][] = "<font class=\"dimmed_text\">".
120 $modgrades->grades[$student->id].
121 "</font>";
123 } else {
124 $grades[$student->id][] = $currentstudentgrade = "";
125 $gradeshtml[$student->id][] = "";
127 if (!empty($modgrades->maxgrade)) {
128 $totals[$student->id] = (float)($totals[$student->id]) + (float)($currentstudentgrade);
129 } else {
130 $totals[$student->id] = (float)($totals[$student->id]) + 0;
139 } // a new Moodle nesting record? ;-)
142 /// OK, we have all the data, now present it to the user
143 if ($download == "ods" and confirm_sesskey()) {
144 require_once("../lib/odslib.class.php");
146 /// Calculate file name
147 $downloadfilename = clean_filename("$course->shortname $strgrades.ods");
148 /// Creating a workbook
149 $workbook = new MoodleODSWorkbook("-");
150 /// Sending HTTP headers
151 $workbook->send($downloadfilename);
152 /// Adding the worksheet
153 $myxls =& $workbook->add_worksheet($strgrades);
155 /// Print names of all the fields
156 $myxls->write_string(0,0,get_string("firstname"));
157 $myxls->write_string(0,1,get_string("lastname"));
158 $myxls->write_string(0,2,get_string("idnumber"));
159 $myxls->write_string(0,3,get_string("institution"));
160 $myxls->write_string(0,4,get_string("department"));
161 $myxls->write_string(0,5,get_string("email"));
162 $pos=6;
163 foreach ($columns as $column) {
164 $myxls->write_string(0,$pos++,strip_tags($column));
166 $myxls->write_string(0,$pos,get_string("total"));
169 /// Print all the lines of data.
170 $i = 0;
171 foreach ($grades as $studentid => $studentgrades) {
172 $i++;
173 $student = $students[$studentid];
174 if (empty($totals[$student->id])) {
175 $totals[$student->id] = '';
178 $myxls->write_string($i,0,$student->firstname);
179 $myxls->write_string($i,1,$student->lastname);
180 $myxls->write_string($i,2,$student->idnumber);
181 $myxls->write_string($i,3,$student->institution);
182 $myxls->write_string($i,4,$student->department);
183 $myxls->write_string($i,5,$student->email);
184 $j=6;
185 foreach ($studentgrades as $grade) {
186 $myxls->write_string($i,$j++,strip_tags($grade));
188 $myxls->write_number($i,$j,$totals[$student->id]);
191 /// Close the workbook
192 $workbook->close();
194 exit;
196 } else if ($download == "xls" and confirm_sesskey()) {
197 require_once("../lib/excellib.class.php");
199 /// Calculate file name
200 $downloadfilename = clean_filename("$course->shortname $strgrades.xls");
201 /// Creating a workbook
202 $workbook = new MoodleExcelWorkbook("-");
203 /// Sending HTTP headers
204 $workbook->send($downloadfilename);
205 /// Adding the worksheet
206 $myxls =& $workbook->add_worksheet($strgrades);
208 /// Print names of all the fields
209 $myxls->write_string(0,0,get_string("firstname"));
210 $myxls->write_string(0,1,get_string("lastname"));
211 $myxls->write_string(0,2,get_string("idnumber"));
212 $myxls->write_string(0,3,get_string("institution"));
213 $myxls->write_string(0,4,get_string("department"));
214 $myxls->write_string(0,5,get_string("email"));
215 $pos=6;
216 foreach ($columns as $column) {
217 $myxls->write_string(0,$pos++,strip_tags($column));
219 $myxls->write_string(0,$pos,get_string("total"));
222 /// Print all the lines of data.
223 $i = 0;
224 foreach ($grades as $studentid => $studentgrades) {
225 $i++;
226 $student = $students[$studentid];
227 if (empty($totals[$student->id])) {
228 $totals[$student->id] = '';
231 $myxls->write_string($i,0,$student->firstname);
232 $myxls->write_string($i,1,$student->lastname);
233 $myxls->write_string($i,2,$student->idnumber);
234 $myxls->write_string($i,3,$student->institution);
235 $myxls->write_string($i,4,$student->department);
236 $myxls->write_string($i,5,$student->email);
237 $j=6;
238 foreach ($studentgrades as $grade) {
239 $myxls->write_string($i,$j++,strip_tags($grade));
241 $myxls->write_number($i,$j,$totals[$student->id]);
244 /// Close the workbook
245 $workbook->close();
247 exit;
249 } else if ($download == "txt" and confirm_sesskey()) {
251 /// Print header to force download
253 header("Content-Type: application/download\n");
254 $downloadfilename = clean_filename("$course->shortname $strgrades");
255 header("Content-Disposition: attachment; filename=\"$downloadfilename.txt\"");
257 /// Print names of all the fields
259 echo get_string("firstname")."\t".
260 get_string("lastname")."\t".
261 get_string("idnumber")."\t".
262 get_string("institution")."\t".
263 get_string("department")."\t".
264 get_string("email");
265 foreach ($columns as $column) {
266 $column = strip_tags($column);
267 echo "\t$column";
269 echo "\t".get_string("total")."\n";
271 /// Print all the lines of data.
273 foreach ($grades as $studentid => $studentgrades) {
274 $student = $students[$studentid];
275 if (empty($totals[$student->id])) {
276 $totals[$student->id] = '';
278 echo "$student->firstname\t$student->lastname\t$student->idnumber\t$student->institution\t$student->department\t$student->email";
279 foreach ($studentgrades as $grade) {
280 $grade = strip_tags($grade);
281 echo "\t$grade";
283 echo "\t".$totals[$student->id];
284 echo "\n";
287 exit;
290 } else { // Just print the web page
292 print_header("$course->shortname: $strgrades", "$course->fullname",
293 "<a href=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->shortname</a>
294 -> $strgrades");
296 print_heading($strgrades);
298 setup_and_print_groups($course, $groupmode, "grades.php?id=$course->id");
300 echo "<table border=\"0\" align=\"center\"><tr>";
301 echo "<td>";
302 $options = array();
303 $options["id"] = "$course->id";
304 $options["download"] = "ods";
305 $options["sesskey"] = $USER->sesskey;
306 print_single_button("grades.php", $options, get_string("downloadods"));
307 echo "<td>";
308 $options["download"] = "xls";
309 print_single_button("grades.php", $options, get_string("downloadexcel"));
310 echo "<td>";
311 $options = array();
312 $options["download"] = "txt";
313 print_single_button("grades.php", $options, get_string("downloadtext"));
314 echo "</table>";
317 $table->head = array_merge(array ("", get_string("firstname"), get_string("lastname")), $columnhtml, array(get_string("total")));
318 $table->width = array(35, "");
319 $table->align = array("LEFT", "RIGHT", "LEFT");
320 foreach ($columns as $column) {
321 $table->width[] = "";
322 $table->align[] = "CENTER";
324 $table->width[] = "";
325 $table->align[] = "CENTER";
327 foreach ($students as $key => $student) {
328 $studentgrades = $gradeshtml[$student->id];
329 if (empty($totals[$student->id])) {
330 $totals[$student->id] = '';
332 $picture = print_user_picture($student->id, $course->id, $student->picture, false, true);
333 $name = array ("$picture", "$student->firstname", "$student->lastname");
334 $total = array ($totals[$student->id]);
336 $table->data[] = array_merge($name, $studentgrades, $total);
339 print_table($table);
341 print_footer($course);