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
);
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);
28 $currentgroup = false;
31 /// Get a list of all students
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>
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);
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>
47 notice(get_string("nostudentsyet"), "$CFG->wwwroot/course/view.php?id=$course->id");
48 print_footer($course);
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
)) {
85 $maxgrade = "$strmax: $modgrades->maxgrade";
86 $maxgradehtml = "<br />$strmax: $modgrades->maxgrade";
88 $maxgrade = "$strmax: $modgrades->maxgrade";
89 $maxgradehtml = "<br /><font class=\"dimmed_text\">$strmax: $modgrades->maxgrade</font>";
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>";
101 $columnhtml[] = "$image ".
102 "<a href=\"$CFG->wwwroot/mod/$mod->modname/view.php?id=$mod->id\">".
103 format_string($instance->name
,true).
106 $columnhtml[] = "$image ".
107 "<a class=\"dimmed\" href=\"$CFG->wwwroot/mod/$mod->modname/view.php?id=$mod->id\">".
108 format_string($instance->name
,true).
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
];
117 $gradeshtml[$student->id
][] = $modgrades->grades
[$student->id
];
119 $gradeshtml[$student->id
][] = "<font class=\"dimmed_text\">".
120 $modgrades->grades
[$student->id
].
124 $grades[$student->id
][] = $currentstudentgrade = "";
125 $gradeshtml[$student->id
][] = "";
127 if (!empty($modgrades->maxgrade
)) {
128 $totals[$student->id
] = (float)($totals[$student->id
]) +
(float)($currentstudentgrade);
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"));
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.
171 foreach ($grades as $studentid => $studentgrades) {
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
);
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
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"));
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.
224 foreach ($grades as $studentid => $studentgrades) {
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
);
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
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".
265 foreach ($columns as $column) {
266 $column = strip_tags($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);
283 echo "\t".$totals[$student->id
];
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>
296 print_heading($strgrades);
298 setup_and_print_groups($course, $groupmode, "grades.php?id=$course->id");
300 echo "<table border=\"0\" align=\"center\"><tr>";
303 $options["id"] = "$course->id";
304 $options["download"] = "ods";
305 $options["sesskey"] = $USER->sesskey
;
306 print_single_button("grades.php", $options, get_string("downloadods"));
308 $options["download"] = "xls";
309 print_single_button("grades.php", $options, get_string("downloadexcel"));
312 $options["download"] = "txt";
313 print_single_button("grades.php", $options, get_string("downloadtext"));
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);
341 print_footer($course);