MDL-11075 Now saving to temp file, then outputting using filelib's readfile_chunked...
[moodle.git] / grade / export / lib.php
CommitLineData
c10ddfb3 1<?php
2
3///////////////////////////////////////////////////////////////////////////
4// //
5// NOTICE OF COPYRIGHT //
6// //
7// Moodle - Modular Object-Oriented Dynamic Learning Environment //
8// http://moodle.com //
9// //
9d1e2185 10// Copyright (C) 1999 onwards Martin Dougiamas http://moodle.com //
c10ddfb3 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///////////////////////////////////////////////////////////////////////////
ce34ed3a 25
89bd8357 26require_once($CFG->dirroot.'/lib/gradelib.php');
27require_once($CFG->dirroot.'/grade/lib.php');
a5bc4e6e 28require_once($CFG->dirroot.'/grade/export/grade_export_form.php');
0eeada79 29
1b074625 30/**
31 * Base export class
32 */
c10ddfb3 33class grade_export {
ba74762b 34
c10ddfb3 35 var $id; // course id
11745964 36 var $grade_items; // array of grade_items
1e124575 37 var $groupid;
c10ddfb3 38 var $grades = array(); // Collect all grades in this array
f67e327e 39 var $comments = array(); // Collect all comments for each grade
c10ddfb3 40 var $columns = array(); // Accumulate column names in this array.
a216f661 41 var $columnidnumbers = array(); // Collect all gradeitem id numbers
a2422359 42 var $students = array();
c10ddfb3 43 var $course; // course
0e2d708e 44 var $userkey; // Optional MD5 string used to publish this export data via a URL
11745964 45 var $export_letters;
0e2d708e 46 var $itemidsurl; // A string of itemids to add to the URL for the export
ba74762b 47
c10ddfb3 48 // common strings
ba74762b 49 var $strgrades;
50 var $strgrade;
51
c10ddfb3 52 /**
53 * Constructor should set up all the private variables ready to be pulled
11745964 54 * @param int $courseid course id
55 * @param array $itemids array of grade item ids, empty means all
0e2d708e 56 * @param stdClass $formdata Optional object of formdata.
0eeada79 57 * @note Exporting as letters will lead to data loss if that exported set it re-imported.
c10ddfb3 58 */
0e2d708e 59 function grade_export($courseid, $itemids=null, $formdata=null) {
60 global $CFG, $USER, $COURSE;
61
62 $this->export_letters = false;
63 if (isset($formdata->export_letters)) {
64 $this->export_letters = $formdata->export_letters;
65 }
66
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);
71 }
72 $this->userkey = $formdata->key;
73 }
ba74762b 74
c10ddfb3 75 $this->strgrades = get_string("grades");
76 $this->strgrade = get_string("grade");
ba74762b 77
11745964 78 if (!$course = get_record("course", "id", $courseid)) {
c10ddfb3 79 error("Course ID was incorrect");
80 }
11745964 81 $context = get_context_instance(CONTEXT_COURSE, $course->id);
82 require_capability('moodle/grade:export', $context);
ba74762b 83
11745964 84 $this->id = $course->id;
c10ddfb3 85 $this->course = $course;
86
11745964 87 // fetch all grade items
88 if (empty($itemids)) {
89 $this->grade_items = grade_item::fetch_all(array('courseid'=>$this->id));
90 } else {
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;
95 }
96 }
0bfbab47 97 }
98
11745964 99 // init colums
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();
103 } else {
104 $this->columns[$grade_item->id] = $grade_item->get_name();
105 }
106 $this->columnidnumbers[$grade_item->id] = $grade_item->idnumber; // this might be needed for some export plugins
107 }
b8ff92b6 108
c10ddfb3 109 /// Check to see if groups are being used in this course
110 if ($groupmode = groupmode($course)) { // Groups are being used
ba74762b 111
c10ddfb3 112 if (isset($_GET['group'])) {
113 $changegroup = $_GET['group']; /// 0 or higher
114 } else {
115 $changegroup = -1; /// This means no group change was specified
116 }
117
118 $currentgroup = get_and_set_current_group($course, $groupmode, $changegroup);
ba74762b 119
c10ddfb3 120 } else {
121 $currentgroup = false;
122 }
123
1e124575 124 $this->groupid = $currentgroup;
125
c10ddfb3 126 if ($currentgroup) {
a2422359 127 $this->students = get_group_students($currentgroup, "u.lastname ASC");
c10ddfb3 128 } else {
5c2aa198 129 $this->students = get_role_users(@implode(',', $CFG->gradebookroles), $context);
c10ddfb3 130 }
131
a2422359 132 if (!empty($this->students)) {
133 foreach ($this->students as $student) {
c10ddfb3 134 $this->grades[$student->id] = array(); // Collect all grades in this array
f67e327e 135 $this->comments[$student->id] = array(); // Collect all comments in tihs array
c10ddfb3 136 }
137 }
0e2d708e 138
139 if (isset($formdata->itemids)) {
140 // Build itemidsurl for links
141 $itemids = array();
142 if ($formdata->itemids) {
143 foreach ($formdata->itemids as $itemid=>$selected) {
144 if ($selected) {
145 $itemids[] = $itemid;
146 }
147 }
148 $this->itemidsurl = implode(",", $itemids);
149 } else {
150 //error?
151 $this->itemidsurl = '';
152 }
153 }
11745964 154 }
d9617050 155
11745964 156 function load_grades() {
0e2d708e 157 global $CFG;
158
11745964 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);
162
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();
f67e327e 167 } else {
11745964 168 $letters = null;
d9617050 169 }
ba74762b 170
11745964 171 if ($this->grade_items) {
172 foreach ($this->grade_items as $gradeitem) {
c10ddfb3 173 // load as an array of grade_final objects
11745964 174 if ($itemgrades = $gradeitem->get_final() and !empty($this->students)) {
175 foreach ($this->students as $student) {
176 $finalgrade = null;
177 $feedback = '';
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);
0eeada79 183 }
11745964 184 }
0eeada79 185
11745964 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) {
0e2d708e 190 $finalgrade = grade_grade::get_letter($letters, $finalgrade, $gradeitem->grademin, $gradeitem->grademax);
f67e327e 191 }
c10ddfb3 192 }
11745964 193
194 $this->grades[$student->id][$gradeitem->id] = $finalgrade;
195 $this->comments[$student->id][$gradeitem->id] = $feedback;
c10ddfb3 196 }
c10ddfb3 197 }
198 }
ba74762b 199 }
c10ddfb3 200 }
ba74762b 201
c10ddfb3 202 /**
0e2d708e 203 * To be implemented by child class
0eeada79 204 * TODO finish PHPdocs
c10ddfb3 205 */
206 function print_grades() { }
ba74762b 207
1b074625 208 /**
209 * Displays all the grades on screen as a feedback mechanism
0eeada79 210 * TODO finish PHPdoc
1b074625 211 */
f6eb15ad 212 function display_grades($feedback=false, $rows=10) {
11745964 213
214 $this->load_grades();
215
f5f4967e 216 echo '<table>';
217 echo '<tr>';
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>";
1b074625 224 foreach ($this->columns as $column) {
225 $column = strip_tags($column);
f5f4967e 226 echo "<th>$column</th>";
ba74762b 227
228 /// add a column_feedback column
1b074625 229 if ($feedback) {
f5f4967e 230 echo "<th>{$column}_feedback</th>";
ba74762b 231 }
1b074625 232 }
f5f4967e 233 echo '</tr>';
1b074625 234 /// Print all the lines of data.
ba74762b 235
f6eb15ad 236 $i = 0;
ba74762b 237 foreach ($this->grades as $studentid => $studentgrades) {
97599c0a 238
f6eb15ad 239 // number of preview rows
240 if ($i++ == $rows) {
97599c0a 241 break;
f6eb15ad 242 }
f5f4967e 243 echo '<tr>';
985d1ee1 244 $student = $this->students[$studentid];
ba74762b 245
f5f4967e 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>";
11745964 247 foreach ($studentgrades as $itemid=>$grade) {
1b074625 248 $grade = strip_tags($grade);
ba74762b 249 echo "<td>$grade</td>";
250
1b074625 251 if ($feedback) {
11745964 252 echo '<td>'.$this->comments[$studentid][$itemid].'</td>';
ba74762b 253 }
1b074625 254 }
f5f4967e 255 echo "</tr>";
256 }
257 echo '</table>';
1b074625 258 }
0e2d708e 259
260 /**
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.
264 * @return void
265 */
266 function print_continue($plugin) {
267 global $CFG;
268
269 // this redirect should trigger a download prompt
270 if (!$this->userkey) {
271 print_continue('export.php?id='.$this->id.'&amp;itemids='.$this->itemidsurl.'&amp;export_letters='.$this->export_letters);
272
273 } else {
274 $link = $CFG->wwwroot.'/grade/export/'.$plugin.'/dump.php?id='.$this->id.'&amp;itemids='
275 . $this->itemidsurl.'&amp;export_letters='.$this->export_letters.'&amp;key='.$this->userkey;
276
277 echo '<p>';
278 echo '<a href="'.$link.'">'.$link.'</a>';
279 echo '</p>';
280 print_footer();
281 }
282 exit();
283 }
c10ddfb3 284}
285
9d1e2185 286?>