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 border=\"0\" valign=\"middle\" src=\"../mod/$mod->modname/icon.gif\" ".
99 "height=\"16\" width=\"16\" 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 == "xls" and confirm_sesskey()) {
144 require_once("../lib/excellib.class.php");
146 /// Calculate file name
147 $downloadfilename = clean_filename("$course->shortname $strgrades.xls");
148 /// Creating a workbook
149 $workbook = new MoodleExcelWorkbook("-");
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 == "txt" and confirm_sesskey()) {
198 /// Print header to force download
200 header("Content-Type: application/download\n");
201 $downloadfilename = clean_filename("$course->shortname $strgrades");
202 header("Content-Disposition: attachment; filename=\"$downloadfilename.txt\"");
204 /// Print names of all the fields
206 echo get_string("firstname")."\t".
207 get_string("lastname")."\t".
208 get_string("idnumber")."\t".
209 get_string("institution")."\t".
210 get_string("department")."\t".
212 foreach ($columns as $column) {
213 $column = strip_tags($column);
216 echo "\t".get_string("total")."\n";
218 /// Print all the lines of data.
220 foreach ($grades as $studentid => $studentgrades) {
221 $student = $students[$studentid];
222 if (empty($totals[$student->id
])) {
223 $totals[$student->id
] = '';
225 echo "$student->firstname\t$student->lastname\t$student->idnumber\t$student->institution\t$student->department\t$student->email";
226 foreach ($studentgrades as $grade) {
227 $grade = strip_tags($grade);
230 echo "\t".$totals[$student->id
];
237 } else { // Just print the web page
239 print_header("$course->shortname: $strgrades", "$course->fullname",
240 "<a href=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->shortname</a>
243 print_heading($strgrades);
245 setup_and_print_groups($course, $groupmode, "grades.php?id=$course->id");
247 echo "<table border=\"0\" align=\"center\"><tr>";
249 $options["id"] = "$course->id";
250 $options["download"] = "xls";
251 $options["sesskey"] = $USER->sesskey
;
252 print_single_button("grades.php", $options, get_string("downloadexcel"));
254 $options["download"] = "txt";
255 $options["sesskey"] = $USER->sesskey
;
256 print_single_button("grades.php", $options, get_string("downloadtext"));
260 $table->head
= array_merge(array ("", get_string("firstname"), get_string("lastname")), $columnhtml, array(get_string("total")));
261 $table->width
= array(35, "");
262 $table->align
= array("LEFT", "RIGHT", "LEFT");
263 foreach ($columns as $column) {
264 $table->width
[] = "";
265 $table->align
[] = "CENTER";
267 $table->width
[] = "";
268 $table->align
[] = "CENTER";
270 foreach ($students as $key => $student) {
271 $studentgrades = $gradeshtml[$student->id
];
272 if (empty($totals[$student->id
])) {
273 $totals[$student->id
] = '';
275 $picture = print_user_picture($student->id
, $course->id
, $student->picture
, false, true);
276 $name = array ("$picture", "$student->firstname", "$student->lastname");
277 $total = array ($totals[$student->id
]);
279 $table->data
[] = array_merge($name, $studentgrades, $total);
284 print_footer($course);