MDL-9628 Fixed sorting
[moodle-pu.git] / grade / export / lib.php
blob437da91d2d52084f4b6aaa43bdddf81dcb06734e
1 <?php
3 ///////////////////////////////////////////////////////////////////////////
4 // //
5 // NOTICE OF COPYRIGHT //
6 // //
7 // Moodle - Modular Object-Oriented Dynamic Learning Environment //
8 // http://moodle.com //
9 // //
10 // Copyright (C) 1999 onwards Martin Dougiamas http://moodle.com //
11 // //
12 // This program is free software; you can redistribute it and/or modify //
13 // it under the terms of the GNU General Public License as published by //
14 // the Free Software Foundation; either version 2 of the License, or //
15 // (at your option) any later version. //
16 // //
17 // This program is distributed in the hope that it will be useful, //
18 // but WITHOUT ANY WARRANTY; without even the implied warranty of //
19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
20 // GNU General Public License for more details: //
21 // //
22 // http://www.gnu.org/copyleft/gpl.html //
23 // //
24 ///////////////////////////////////////////////////////////////////////////
26 require_once($CFG->dirroot.'/lib/gradelib.php');
27 require_once($CFG->dirroot.'/grade/lib.php');
28 /**
29 * Prints all grade items for selection
30 * @input int id - course id
32 function print_gradeitem_selections($id, $params = NULL) {
33 global $CFG;
34 // print all items for selections
35 // make this a standard function in lib maybe
36 include_once('grade_export_form.php');
37 $mform = new grade_export_form(qualified_me(), array('id'=>$id));
38 $mform->display();
41 /**
42 * Base export class
44 class grade_export {
46 var $format = ''; // export format
47 var $id; // course id
48 var $itemids; // comma separated grade_item ids;
49 var $grades = array(); // Collect all grades in this array
50 var $gradeshtml= array(); // Collect all grades html formatted in this array
51 var $comments = array(); // Collect all comments for each grade
52 var $totals = array(); // Collect all totals in this array
53 var $columns = array(); // Accumulate column names in this array.
54 var $columnhtml = array(); // Accumulate column html in this array.
55 var $columnidnumbers = array(); // Collect all gradeitem id numbers
56 var $students = array();
57 var $course; // course
59 // common strings
60 var $strgrades;
61 var $strgrade;
63 /**
64 * Constructor should set up all the private variables ready to be pulled
65 * @input int id - course id
66 * @input string itemids - comma separated value of itemids to process for this export
68 function grade_export($id, $itemids = '') {
70 $this->strgrades = get_string("grades");
71 $this->strgrade = get_string("grade");
72 $this->itemids = $itemids;
74 $strmax = get_string("maximumshort");
76 if (! $course = get_record("course", "id", $id)) {
77 error("Course ID was incorrect");
79 $context = get_context_instance(CONTEXT_COURSE, $id);
80 require_capability('moodle/grade:view', $context);
82 $this->id = $id;
83 $this->course = $course;
85 // first make sure we have all final grades
86 // TODO: check that no grade_item has needsupdate set
87 grade_regrade_final_grades($id);
89 /// Check to see if groups are being used in this course
90 if ($groupmode = groupmode($course)) { // Groups are being used
92 if (isset($_GET['group'])) {
93 $changegroup = $_GET['group']; /// 0 or higher
94 } else {
95 $changegroup = -1; /// This means no group change was specified
98 $currentgroup = get_and_set_current_group($course, $groupmode, $changegroup);
100 } else {
101 $currentgroup = false;
104 if ($currentgroup) {
105 $this->students = get_group_students($currentgroup, "u.lastname ASC");
106 } else {
107 $this->students = get_role_users(@implode(',', $CFG->gradebookroles), $context);
110 if (!empty($this->students)) {
111 foreach ($this->students as $student) {
112 $this->grades[$student->id] = array(); // Collect all grades in this array
113 $this->gradeshtml[$student->id] = array(); // Collect all grades html formatted in this array
114 $this->totals[$student->id] = array(); // Collect all totals in this array
115 $this->comments[$student->id] = array(); // Collect all comments in tihs array
119 // if grade_item ids are specified
120 if ($itemids) {
121 foreach ($itemids as $iid) {
123 if ($iid) {
124 $params->id = clean_param($iid, PARAM_INT);
125 $gradeitems[] = new grade_item($params);
128 } else {
129 // else we get all items for this course
130 $gradeitems = grade_grade::fetch_all(array('courseid'=>$this->id));
133 if ($gradeitems) {
134 foreach ($gradeitems as $gradeitem) {
136 // load as an array of grade_final objects
137 if ($itemgrades = $gradeitem -> get_final()) {
139 $this->columns[$gradeitem->id] = "$gradeitem->itemmodule: ".$gradeitem->get_name()." - $gradeitem->grademax";
141 $this->columnidnumbers[$gradeitem->id] = $gradeitem->idnumber; // this might be needed for some export plugins
143 if (!empty($gradeitem->grademax)) {
144 $maxgrade = "$strmax: $gradeitem->grademax";
145 } else {
146 $maxgrade = "";
149 if (!empty($this->students)) {
150 foreach ($this->students as $student) {
151 unset($studentgrade);
152 // add support for comment here MDL-9634
154 if (!empty($itemgrades[$student->id])) {
155 $studentgrade = $itemgrades[$student->id];
158 if (!empty($studentgrade->finalgrade)) {
159 $this->grades[$student->id][$gradeitem->id] = $currentstudentgrade = $studentgrade->finalgrade;
160 } else {
161 $this->grades[$student->id][$gradeitem->id] = $currentstudentgrade = "";
162 $this->gradeshtml[$student->id][$gradeitem->id] = "";
164 if (!empty($maxgrade)) {
165 $this->totals[$student->id] = (float)($this->totals[$student->id]) + (float)($currentstudentgrade);
166 } else {
167 $this->totals[$student->id] = (float)($this->totals[$student->id]) + 0;
170 if (!empty($comment)) {
171 // load comments here
172 if ($studentgrade) {
173 $studentgrade->load_text();
174 // get the actual comment
175 $comment = $studentgrade->grade_grade_text->feedback;
176 $this->comments[$student->id][$gradeitem->id] = $comment;
178 } else {
179 $this->comments[$student->id][$gradeitem->id] = '';
189 * To be implemented by child classes
191 function print_grades() { }
194 * Displays all the grades on screen as a feedback mechanism
196 function display_grades($feedback=false) {
197 echo '<table>';
198 echo '<tr>';
199 echo '<th>'.get_string("firstname")."</th>".
200 '<th>'.get_string("lastname")."</th>".
201 '<th>'.get_string("idnumber")."</th>".
202 '<th>'.get_string("institution")."</th>".
203 '<th>'.get_string("department")."</th>".
204 '<th>'.get_string("email")."</th>";
205 foreach ($this->columns as $column) {
206 $column = strip_tags($column);
207 echo "<th>$column</th>";
209 /// add a column_feedback column
210 if ($feedback) {
211 echo "<th>{$column}_feedback</th>";
214 echo '<th>'.get_string("total")."</th>";
215 echo '</tr>';
216 /// Print all the lines of data.
219 foreach ($this->grades as $studentid => $studentgrades) {
221 echo '<tr>';
222 $student = $this->students[$studentid];
223 if (empty($this->totals[$student->id])) {
224 $this->totals[$student->id] = '';
228 echo "<td>$student->firstname</td><td>$student->lastname</td><td>$student->idnumber</td><td>$student->institution</td><td>$student->department</td><td>$student->email</td>";
229 foreach ($studentgrades as $grade) {
230 $grade = strip_tags($grade);
231 echo "<td>$grade</td>";
233 if ($feedback) {
234 echo '<td>'.array_shift($this->comments[$student->id]).'</td>';
237 echo '<td>'.$this->totals[$student->id].'</td>';
238 echo "</tr>";
240 echo '</table>';