3 ///////////////////////////////////////////////////////////////////////////
5 // NOTICE OF COPYRIGHT //
7 // Moodle - Modular Object-Oriented Dynamic Learning Environment //
8 // http://moodle.com //
10 // Copyright (C) 1999 onwards Martin Dougiamas http://moodle.com //
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. //
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: //
22 // http://www.gnu.org/copyleft/gpl.html //
24 ///////////////////////////////////////////////////////////////////////////
26 require_once($CFG->dirroot
.'/lib/gradelib.php');
27 require_once($CFG->dirroot
.'/grade/lib.php');
28 require_once($CFG->dirroot
.'/grade/export/grade_export_form.php');
36 var $grade_items; // array of grade_items
38 var $grades = array(); // Collect all grades in this array
39 var $comments = array(); // Collect all comments for each grade
40 var $columns = array(); // Accumulate column names in this array.
41 var $columnidnumbers = array(); // Collect all gradeitem id numbers
42 var $students = array();
43 var $course; // course
44 var $userkey; // Optional MD5 string used to publish this export data via a URL
46 var $itemidsurl; // A string of itemids to add to the URL for the export
53 * Constructor should set up all the private variables ready to be pulled
54 * @param int $courseid course id
55 * @param array $itemids array of grade item ids, empty means all
56 * @param stdClass $formdata Optional object of formdata.
57 * @note Exporting as letters will lead to data loss if that exported set it re-imported.
59 function grade_export($courseid, $itemids=null, $formdata=null) {
60 global $CFG, $USER, $COURSE;
62 $this->export_letters
= false;
63 if (isset($formdata->export_letters
)) {
64 $this->export_letters
= $formdata->export_letters
;
67 $this->userkey
= false;
68 if (isset($formdata->key
)) {
69 if ($formdata->key
== 1 && isset($formdata->iprestriction
) && isset($formdata->validuntil
)) { // Create a new key
70 $formdata->key
= create_user_key('grade/export', $USER->id
, $COURSE->id
, $formdata->iprestriction
, $formdata->validuntil
);
72 $this->userkey
= $formdata->key
;
75 $this->strgrades
= get_string("grades");
76 $this->strgrade
= get_string("grade");
78 if (!$course = get_record("course", "id", $courseid)) {
79 error("Course ID was incorrect");
81 $context = get_context_instance(CONTEXT_COURSE
, $course->id
);
82 require_capability('moodle/grade:export', $context);
84 $this->id
= $course->id
;
85 $this->course
= $course;
87 // fetch all grade items
88 if (empty($itemids)) {
89 $this->grade_items
= grade_item
::fetch_all(array('courseid'=>$this->id
));
91 $this->grade_items
= array();
92 foreach ($itemids as $iid) {
93 if ($grade_item = grade_item
::fetch(array('id'=>(int)$iid, 'courseid'=>$this->id
))) {
94 $this->grade_items
[$grade_item->id
] = $grade_item;
100 foreach ($this->grade_items
as $grade_item) {
101 if ($grade_item->itemtype
== 'mod') {
102 $this->columns
[$grade_item->id
] = get_string('modulename', $grade_item->itemmodule
).': '.$grade_item->get_name();
104 $this->columns
[$grade_item->id
] = $grade_item->get_name();
106 $this->columnidnumbers
[$grade_item->id
] = $grade_item->idnumber
; // this might be needed for some export plugins
109 /// Check to see if groups are being used in this course
110 if ($groupmode = groupmode($course)) { // Groups are being used
112 if (isset($_GET['group'])) {
113 $changegroup = $_GET['group']; /// 0 or higher
115 $changegroup = -1; /// This means no group change was specified
118 $currentgroup = get_and_set_current_group($course, $groupmode, $changegroup);
121 $currentgroup = false;
124 $this->groupid
= $currentgroup;
127 $this->students
= get_group_students($currentgroup, "u.lastname ASC");
129 $this->students
= get_role_users(@implode
(',', $CFG->gradebookroles
), $context);
132 if (!empty($this->students
)) {
133 foreach ($this->students
as $student) {
134 $this->grades
[$student->id
] = array(); // Collect all grades in this array
135 $this->comments
[$student->id
] = array(); // Collect all comments in tihs array
139 if (isset($formdata->itemids
)) {
140 // Build itemidsurl for links
142 if ($formdata->itemids
) {
143 foreach ($formdata->itemids
as $itemid=>$selected) {
145 $itemids[] = $itemid;
148 $this->itemidsurl
= implode(",", $itemids);
151 $this->itemidsurl
= '';
156 function load_grades() {
159 // first make sure we have all final grades
160 // TODO: check that no grade_item has needsupdate set
161 grade_regrade_final_grades($this->id
);
163 if ($this->export_letters
) {
164 require_once($CFG->dirroot
. '/grade/report/lib.php');
165 $report = new grade_report($this->id
, null, null);
166 $letters = $report->get_grade_letters();
171 if ($this->grade_items
) {
172 foreach ($this->grade_items
as $gradeitem) {
173 // load as an array of grade_final objects
174 if ($itemgrades = $gradeitem->get_final() and !empty($this->students
)) {
175 foreach ($this->students
as $student) {
178 if (array_key_exists($student->id
, $itemgrades)) {
179 $finalgrade = $itemgrades[$student->id
]->finalgrade
;
180 $grade = new grade_grade($itemgrades[$student->id
], false);
181 if ($grade_text = $grade->load_text()) {
182 $feedback = format_text($grade_text->feedback
, $grade_text->feedbackformat
);
186 if ($this->export_letters
) {
187 $grade_item_displaytype = $report->get_pref('gradedisplaytype', $gradeitem->id
);
188 // TODO Convert final grade to letter if export option is on, and grade_item is set to letter type MDL-10490
189 if ($grade_item_displaytype == GRADE_REPORT_GRADE_DISPLAY_TYPE_LETTER
) {
190 $finalgrade = grade_grade
::get_letter($letters, $finalgrade, $gradeitem->grademin
, $gradeitem->grademax
);
194 $this->grades
[$student->id
][$gradeitem->id
] = $finalgrade;
195 $this->comments
[$student->id
][$gradeitem->id
] = $feedback;
203 * To be implemented by child class
204 * TODO finish PHPdocs
206 function print_grades() { }
209 * Displays all the grades on screen as a feedback mechanism
212 function display_grades($feedback=false, $rows=10) {
214 $this->load_grades();
218 echo '<th>'.get_string("firstname")."</th>".
219 '<th>'.get_string("lastname")."</th>".
220 '<th>'.get_string("idnumber")."</th>".
221 '<th>'.get_string("institution")."</th>".
222 '<th>'.get_string("department")."</th>".
223 '<th>'.get_string("email")."</th>";
224 foreach ($this->columns
as $column) {
225 $column = strip_tags($column);
226 echo "<th>$column</th>";
228 /// add a column_feedback column
230 echo "<th>{$column}_feedback</th>";
234 /// Print all the lines of data.
237 foreach ($this->grades
as $studentid => $studentgrades) {
239 // number of preview rows
244 $student = $this->students
[$studentid];
246 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>";
247 foreach ($studentgrades as $itemid=>$grade) {
248 $grade = strip_tags($grade);
249 echo "<td>$grade</td>";
252 echo '<td>'.$this->comments
[$studentid][$itemid].'</td>';
261 * Either prints a "continue" box, which will redirect the user to the download page, or prints the URL for the published data.
262 * @note exit() at the end of the method
263 * @param string $plugin Required: name of the plugin calling this method. Used for building the URL.
266 function print_continue($plugin) {
269 // this redirect should trigger a download prompt
270 if (!$this->userkey
) {
271 print_continue('export.php?id='.$this->id
.'&itemids='.$this->itemidsurl
.'&export_letters='.$this->export_letters
);
274 $link = $CFG->wwwroot
.'/grade/export/'.$plugin.'/dump.php?id='.$this->id
.'&itemids='
275 . $this->itemidsurl
.'&export_letters='.$this->export_letters
.'&key='.$this->userkey
;
278 echo '<a href="'.$link.'">'.$link.'</a>';