file width.html was added on branch MOODLE_15_STABLE on 2005-07-07 16:14:37 +0000
[moodle.git] / course / grades.php
blob8e7194cf8114ee7d020c9cacf78dadf4c8229483
1 <?php // $Id$
2 // Displays all grades for a course
4 require_once("../config.php");
5 require_once("lib.php");
7 require_variable($id); // course id
8 optional_variable($download, ""); // to download data
10 require_login();
12 if (! $course = get_record("course", "id", $id)) {
13 error("Course ID was incorrect");
16 if (!isteacher($course->id)) {
17 error("Only teachers can use this page!");
20 $strgrades = get_string("grades");
21 $strgrade = get_string("grade");
22 $strmax = get_string("maximumshort");
23 $stractivityreport = get_string("activityreport");
25 /// Check to see if groups are being used in this course
26 if ($groupmode = groupmode($course)) { // Groups are being used
27 if (isset($_GET['group'])) {
28 $changegroup = $_GET['group']; /// 0 or higher
29 } else {
30 $changegroup = -1; /// This means no group change was specified
33 $currentgroup = get_and_set_current_group($course, $groupmode, $changegroup);
34 } else {
35 $currentgroup = false;
38 /// Get a list of all students
39 if ($currentgroup) {
40 if (!$students = get_group_students($currentgroup, "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 setup_and_print_groups($course, $groupmode, "grades.php?id=$course->id");
45 notice(get_string("nostudentsingroup"), "$CFG->wwwroot/course/view.php?id=$course->id");
46 print_footer($course);
47 exit;
49 } else {
50 if (!$students = get_course_students($course->id, "u.lastname ASC")) {
51 print_header("$course->shortname: $strgrades", "$course->fullname",
52 "<A HREF=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->shortname</A>
53 -> $strgrades");
54 notice(get_string("nostudentsyet"), "$CFG->wwwroot/course/view.php?id=$course->id");
55 print_footer($course);
56 exit;
60 foreach ($students as $student) {
61 $grades[$student->id] = array(); // Collect all grades in this array
62 $gradeshtml[$student->id] = array(); // Collect all grades html formatted in this array
63 $totals[$student->id] = array(); // Collect all totals in this array
65 $columns = array(); // Accumulate column names in this array.
66 $columnhtml = array(); // Accumulate column html in this array.
69 /// Collect modules data
70 get_all_mods($course->id, $mods, $modnames, $modnamesplural, $modnamesused);
73 /// Search through all the modules, pulling out grade data
74 $sections = get_all_sections($course->id); // Sort everything the same as the course
75 for ($i=0; $i<=$course->numsections; $i++) {
76 if (isset($sections[$i])) { // should always be true
77 $section = $sections[$i];
78 if ($section->sequence) {
79 $sectionmods = explode(",", $section->sequence);
80 foreach ($sectionmods as $sectionmod) {
81 $mod = $mods[$sectionmod];
82 $instance = get_record("$mod->modname", "id", "$mod->instance");
83 $libfile = "$CFG->dirroot/mod/$mod->modname/lib.php";
84 if (file_exists($libfile)) {
85 require_once($libfile);
86 $gradefunction = $mod->modname."_grades";
87 if (function_exists($gradefunction)) { // Skip modules without grade function
88 if ($modgrades = $gradefunction($mod->instance)) {
90 if (!empty($modgrades->maxgrade)) {
91 if ($mod->visible) {
92 $maxgrade = "$strmax: $modgrades->maxgrade";
93 $maxgradehtml = "<br />$strmax: $modgrades->maxgrade";
94 } else {
95 $maxgrade = "$strmax: $modgrades->maxgrade";
96 $maxgradehtml = "<br /><font class=\"dimmed_text\">$strmax: $modgrades->maxgrade</font>";
98 } else {
99 $maxgrade = "";
100 $maxgradehtml = "";
103 $image = "<a href=\"$CFG->wwwroot/mod/$mod->modname/view.php?id=$mod->id\"".
104 " title=\"$mod->modfullname\">".
105 "<img border=\"0\" valign=\"middle\" src=\"../mod/$mod->modname/icon.gif\" ".
106 "height=\"16\" width=\"16\" alt=\"$mod->modfullname\" /></a>";
107 if ($mod->visible) {
108 $columnhtml[] = "$image ".
109 "<a href=\"$CFG->wwwroot/mod/$mod->modname/view.php?id=$mod->id\">".
110 format_string($instance->name,true).
111 "</a>$maxgradehtml";
112 } else {
113 $columnhtml[] = "$image ".
114 "<a class=\"dimmed\" href=\"$CFG->wwwroot/mod/$mod->modname/view.php?id=$mod->id\">".
115 format_string($instance->name,true).
116 "</a>$maxgradehtml";
118 $columns[] = "$mod->modfullname: ".format_string($instance->name)." - $maxgrade";
120 foreach ($students as $student) {
121 if (!empty($modgrades->grades[$student->id])) {
122 $grades[$student->id][] = $currentstudentgrade = $modgrades->grades[$student->id];
123 if ($mod->visible) {
124 $gradeshtml[$student->id][] = $modgrades->grades[$student->id];
125 } else {
126 $gradeshtml[$student->id][] = "<font class=\"dimmed_text\">".
127 $modgrades->grades[$student->id].
128 "</font>";
130 } else {
131 $grades[$student->id][] = $currentstudentgrade = "";
132 $gradeshtml[$student->id][] = "";
134 if (!empty($modgrades->maxgrade)) {
135 $totals[$student->id] = (float)($totals[$student->id]) + (float)($currentstudentgrade);
136 } else {
137 $totals[$student->id] = (float)($totals[$student->id]) + 0;
146 } // a new Moodle nesting record? ;-)
149 /// OK, we have all the data, now present it to the user
150 if ($download == "xls" and confirm_sesskey()) {
151 require_once("../lib/excel/Worksheet.php");
152 require_once("../lib/excel/Workbook.php");
154 // HTTP headers
155 header("Content-type: application/vnd.ms-excel");
156 $downloadfilename = clean_filename("$course->shortname $strgrades");
157 header("Content-Disposition: attachment; filename=\"$downloadfilename.xls\"");
158 header("Expires: 0");
159 header("Cache-Control: must-revalidate,post-check=0,pre-check=0");
160 header("Pragma: public");
162 /// Creating a workbook
163 $workbook = new Workbook("-");
164 $myxls =& $workbook->add_worksheet($strgrades);
166 /// Print names of all the fields
168 $myxls->write_string(0,0,get_string("firstname"));
169 $myxls->write_string(0,1,get_string("lastname"));
170 $myxls->write_string(0,2,get_string("idnumber"));
171 $myxls->write_string(0,3,get_string("institution"));
172 $myxls->write_string(0,4,get_string("department"));
173 $myxls->write_string(0,5,get_string("email"));
174 $pos=6;
175 foreach ($columns as $column) {
176 $myxls->write_string(0,$pos++,strip_tags($column));
178 $myxls->write_string(0,$pos,get_string("total"));
181 /// Print all the lines of data.
183 $i = 0;
184 foreach ($grades as $studentid => $studentgrades) {
185 $i++;
186 $student = $students[$studentid];
187 if (empty($totals[$student->id])) {
188 $totals[$student->id] = '';
191 $myxls->write_string($i,0,$student->firstname);
192 $myxls->write_string($i,1,$student->lastname);
193 $myxls->write_string($i,2,$student->idnumber);
194 $myxls->write_string($i,3,$student->institution);
195 $myxls->write_string($i,4,$student->department);
196 $myxls->write_string($i,5,$student->email);
197 $j=6;
198 foreach ($studentgrades as $grade) {
199 $myxls->write_string($i,$j++,strip_tags($grade));
201 $myxls->write_number($i,$j,$totals[$student->id]);
204 $workbook->close();
206 exit;
208 } else if ($download == "txt" and confirm_sesskey()) {
210 /// Print header to force download
212 header("Content-Type: application/download\n");
213 $downloadfilename = clean_filename("$course->shortname $strgrades");
214 header("Content-Disposition: attachment; filename=\"$downloadfilename.txt\"");
216 /// Print names of all the fields
218 echo get_string("firstname")."\t".
219 get_string("lastname")."\t".
220 get_string("idnumber")."\t".
221 get_string("institution")."\t".
222 get_string("department")."\t".
223 get_string("email");
224 foreach ($columns as $column) {
225 $column = strip_tags($column);
226 echo "\t$column";
228 echo "\t".get_string("total")."\n";
230 /// Print all the lines of data.
232 foreach ($grades as $studentid => $studentgrades) {
233 $student = $students[$studentid];
234 if (empty($totals[$student->id])) {
235 $totals[$student->id] = '';
237 echo "$student->firstname\t$student->lastname\t$student->idnumber\t$student->institution\t$student->department\t$student->email";
238 foreach ($studentgrades as $grade) {
239 $grade = strip_tags($grade);
240 echo "\t$grade";
242 echo "\t".$totals[$student->id];
243 echo "\n";
246 exit;
249 } else { // Just print the web page
251 print_header("$course->shortname: $strgrades", "$course->fullname",
252 "<a href=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->shortname</a>
253 -> $strgrades");
255 print_heading($strgrades);
257 setup_and_print_groups($course, $groupmode, "grades.php?id=$course->id");
259 echo "<table border=\"0\" align=\"center\"><tr>";
260 echo "<td>";
261 $options["id"] = "$course->id";
262 $options["download"] = "xls";
263 $options["sesskey"] = $USER->sesskey;
264 print_single_button("grades.php", $options, get_string("downloadexcel"));
265 echo "<td>";
266 $options["download"] = "txt";
267 $options["sesskey"] = $USER->sesskey;
268 print_single_button("grades.php", $options, get_string("downloadtext"));
269 echo "</table>";
272 $table->head = array_merge(array ("", get_string("firstname"), get_string("lastname")), $columnhtml, array(get_string("total")));
273 $table->width = array(35, "");
274 $table->align = array("LEFT", "RIGHT", "LEFT");
275 foreach ($columns as $column) {
276 $table->width[] = "";
277 $table->align[] = "CENTER";
279 $table->width[] = "";
280 $table->align[] = "CENTER";
282 foreach ($students as $key => $student) {
283 $studentgrades = $gradeshtml[$student->id];
284 if (empty($totals[$student->id])) {
285 $totals[$student->id] = '';
287 $picture = print_user_picture($student->id, $course->id, $student->picture, false, true);
288 $name = array ("$picture", "$student->firstname", "$student->lastname");
289 $total = array ($totals[$student->id]);
291 $table->data[] = array_merge($name, $studentgrades, $total);
294 print_table($table);
296 print_footer($course);