MDL-11665, users with no grade:viewhidden capability gets a wrong upgrade count becau...
[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
caffc55a 35 var $plugin; // plgin name - must be filled in subclasses!
36
37 var $grade_items; // list of all course grade items
38 var $groupid; // groupid, 0 means all groups
39 var $course; // course object
40 var $columns; // array of grade_items selected for export
41
42 var $previewrows; // number of rows in preview
e9096dc2 43 var $export_letters; // export letters
caffc55a 44 var $export_feedback; // export feedback
45 var $userkey; // export using private user key
46
c10ddfb3 47 /**
48 * Constructor should set up all the private variables ready to be pulled
caffc55a 49 * @param object $course
50 * @param int $groupid id of selected group, 0 means all
51 * @param string $itemlist comma separated list of item ids, empty means all
52 * @param boolean $export_feedback
53 * @param boolean $export_letters
0eeada79 54 * @note Exporting as letters will lead to data loss if that exported set it re-imported.
c10ddfb3 55 */
caffc55a 56 function grade_export($course, $groupid=0, $itemlist='', $export_feedback=false, $export_letters=false) {
57 $this->course = $course;
58 $this->groupid = $groupid;
59 $this->grade_items = grade_item::fetch_all(array('courseid'=>$this->course->id));
60
61 $this->columns = array();
62 if (!empty($itemlist)) {
63 $itemids = explode(',', $itemlist);
64 // remove items that are not requested
65 foreach ($itemids as $itemid) {
66 if (array_key_exists($itemid, $this->grade_items)) {
67 $this->columns[$itemid] =& $this->grade_items[$itemid];
68 }
69 }
70 } else {
71 foreach ($this->grade_items as $itemid=>$unused) {
72 $this->columns[$itemid] =& $this->grade_items[$itemid];
73 }
74 }
0e2d708e 75
caffc55a 76 $this->export_letters = $export_letters;
77 $this->export_feedback = $export_feedback;
78 $this->userkey = '';
79 $this->previewrows = false;
80 }
81
82 /**
83 * Init object based using data from form
84 * @param object $formdata
85 */
86 function process_form($formdata) {
87 global $USER;
88
89 $this->columns = array();
90 if (!empty($formdata->itemids)) {
91 foreach ($formdata->itemids as $itemid=>$selected) {
92 if ($selected and array_key_exists($itemid, $this->grade_items)) {
93 $this->columns[$itemid] =& $this->grade_items[$itemid];
94 }
95 }
96 } else {
97 foreach ($this->grade_items as $itemid=>$unused) {
98 $this->columns[$itemid] =& $this->grade_items[$itemid];
99 }
0e2d708e 100 }
101
0e2d708e 102 if (isset($formdata->key)) {
caffc55a 103 if ($formdata->key == 1 && isset($formdata->iprestriction) && isset($formdata->validuntil)) {
104 // Create a new key
105 $formdata->key = create_user_key('grade/export', $USER->id, $this->course->id, $formdata->iprestriction, $formdata->validuntil);
0e2d708e 106 }
107 $this->userkey = $formdata->key;
108 }
ba74762b 109
caffc55a 110 if (isset($formdata->export_letters)) {
111 $this->export_letters = $formdata->export_letters;
c10ddfb3 112 }
ba74762b 113
caffc55a 114 if (isset($formdata->export_feedback)) {
115 $this->export_feedback = $formdata->export_feedback;
116 }
c10ddfb3 117
caffc55a 118 if (isset($formdata->previewrows)) {
119 $this->previewrows = $formdata->previewrows;
0bfbab47 120 }
121
caffc55a 122 }
123
124 /**
125 * Update exported field in grade_grades table
126 * @return boolean
127 */
128 function track_exports() {
129 global $CFG;
130
131 /// Whether this plugin is entitled to update export time
132 if ($expplugins = explode(",", $CFG->gradeexport)) {
133 if (in_array($this->plugin, $expplugins)) {
134 return true;
11745964 135 } else {
caffc55a 136 return false;
137 }
138 } else {
139 return false;
11745964 140 }
caffc55a 141 }
b8ff92b6 142
caffc55a 143 /**
144 * Returns string representation of final grade
145 * @param $object $grade instance of grade_grade class
146 * @return string
147 */
148 function format_grade($grade) {
e9096dc2 149 $displaytype = null;
150 if ($this->export_letters) {
151 $displaytype = GRADE_DISPLAY_TYPE_LETTER;
8108909a 152 } else {
153 $displaytype = GRADE_DISPLAY_TYPE_REAL;
c10ddfb3 154 }
0e2d708e 155
e9096dc2 156 return grade_format_gradevalue($grade->finalgrade, $this->grade_items[$grade->itemid], false, $displaytype, null);
11745964 157 }
d9617050 158
caffc55a 159 /**
160 * Returns the name of column in export
161 * @param object $grade_item
162 * @param boolena $feedback feedback colum
163 * &return string
164 */
165 function format_column_name($grade_item, $feedback=false) {
166 if ($grade_item->itemtype == 'mod') {
167 $name = get_string('modulename', $grade_item->itemmodule).': '.$grade_item->get_name();
f67e327e 168 } else {
caffc55a 169 $name = $grade_item->get_name();
d9617050 170 }
ba74762b 171
caffc55a 172 if ($feedback) {
173 $name .= ' ('.get_string('feedback').')';
ba74762b 174 }
caffc55a 175
176 return strip_tags($name);
c10ddfb3 177 }
ba74762b 178
c10ddfb3 179 /**
caffc55a 180 * Returns formatted grade feedback
181 * @param object $feedback object with properties feedback and feedbackformat
182 * @return string
c10ddfb3 183 */
caffc55a 184 function format_feedback($feedback) {
185 return strip_tags(format_text($feedback->feedback, $feedback->feedbackformat));
186 }
ba74762b 187
1b074625 188 /**
caffc55a 189 * Implemented by child class
1b074625 190 */
caffc55a 191 function print_grades() { }
11745964 192
caffc55a 193 /**
194 * Prints preview of exported grades on screen as a feedback mechanism
195 */
196 function display_preview() {
567883c8 197
198 print_heading(get_string('previewrows', 'grades'));
199
f5f4967e 200 echo '<table>';
201 echo '<tr>';
202 echo '<th>'.get_string("firstname")."</th>".
203 '<th>'.get_string("lastname")."</th>".
204 '<th>'.get_string("idnumber")."</th>".
205 '<th>'.get_string("institution")."</th>".
206 '<th>'.get_string("department")."</th>".
207 '<th>'.get_string("email")."</th>";
caffc55a 208 foreach ($this->columns as $grade_item) {
209 echo '<th>'.$this->format_column_name($grade_item).'</th>';
ba74762b 210
211 /// add a column_feedback column
caffc55a 212 if ($this->export_feedback) {
213 echo '<th>'.$this->format_column_name($grade_item, true).'</th>';
ba74762b 214 }
1b074625 215 }
f5f4967e 216 echo '</tr>';
1b074625 217 /// Print all the lines of data.
ba74762b 218
f6eb15ad 219 $i = 0;
caffc55a 220 $gui = new graded_users_iterator($this->course, $this->columns, $this->groupid);
221 $gui->init();
222 while ($userdata = $gui->next_user()) {
f6eb15ad 223 // number of preview rows
caffc55a 224 if ($this->previewrows and $this->previewrows < ++$i) {
97599c0a 225 break;
f6eb15ad 226 }
caffc55a 227 $user = $userdata->user;
ba74762b 228
caffc55a 229 echo '<tr>';
230 echo "<td>$user->firstname</td><td>$user->lastname</td><td>$user->idnumber</td><td>$user->institution</td><td>$user->department</td><td>$user->email</td>";
231 foreach ($this->columns as $itemid=>$unused) {
232 $gradetxt = $this->format_grade($userdata->grades[$itemid]);
233 echo "<td>$gradetxt</td>";
ba74762b 234
caffc55a 235 if ($this->export_feedback) {
236 echo '<td>'.$this->format_feedback($userdata->feedbacks[$itemid]).'</td>';
ba74762b 237 }
1b074625 238 }
f5f4967e 239 echo "</tr>";
240 }
241 echo '</table>';
caffc55a 242 $gui->close();
1b074625 243 }
0e2d708e 244
245 /**
caffc55a 246 * Returns array of parameters used by dump.php and export.php.
247 * @return array
248 */
249 function get_export_params() {
250 $itemids = array_keys($this->columns);
251
252 $params = array('id' =>$this->course->id,
253 'groupid' =>$this->groupid,
254 'itemids' =>implode(',', $itemids),
255 'export_letters' =>$this->export_letters,
256 'export_feedback'=>$this->export_feedback);
257
258 return $params;
259 }
260
261 /**
262 * Either prints a "Export" box, which will redirect the user to the download page,
263 * or prints the URL for the published data.
0e2d708e 264 * @return void
265 */
caffc55a 266 function print_continue() {
0e2d708e 267 global $CFG;
268
caffc55a 269 $params = $this->get_export_params();
270
567883c8 271
272 print_heading(get_string('export', 'grades'));
273
274 echo '<div class="gradeexportlink">';
275 if (!$this->userkey) { // this button should trigger a download prompt
e9096dc2 276 print_single_button($CFG->wwwroot.'/grade/export/'.$this->plugin.'/export.php',
567883c8 277 $params, get_string('download', 'admin'));
0e2d708e 278
279 } else {
caffc55a 280 $paramstr = '';
281 $sep = '?';
282 foreach($params as $name=>$value) {
283 $paramstr .= $sep.$name.'='.$value;
284 $sep = '&amp;';
285 }
286
287 $link = $CFG->wwwroot.'/grade/export/'.$this->plugin.'/dump.php'.$paramstr.'&amp;key='.$this->userkey;
0e2d708e 288
567883c8 289 echo get_string('download', 'admin').': <a href="'.$link.'">'.$link.'</a>';
0e2d708e 290 }
567883c8 291 echo '</div>';
0e2d708e 292 }
c10ddfb3 293}
294
eb859919 295/**
296 * This class is used to update the exported field in grade_grades.
297 * It does internal buffering to speedup the db operations.
298 */
299class grade_export_update_buffer {
300 var $update_list;
301 var $export_time;
302
303 /**
304 * Constructor - creates the buffer and initialises the time stamp
305 */
306 function grade_export_update_buffer() {
307 $this->update_list = array();
308 $this->export_time = time();
309 }
310
311 function flush($buffersize) {
312 global $CFG;
313
314 if (count($this->update_list) > $buffersize) {
315 $list = implode(',', $this->update_list);
316 $sql = "UPDATE {$CFG->prefix}grade_grades SET exported = {$this->export_time} WHERE id IN ($list)";
317 execute_sql($sql, false);
318 $this->update_list = array();
319 }
320 }
321
322 /**
323 * Track grade export status
324 * @param object $grade_grade
325 * @return string $status (unknow, new, regrade, nochange)
326 */
327 function track($grade_grade) {
328 if (empty($grade_grade->exported) or empty($grade_grade->timemodified)) {
329 if (is_null($grade_grade->finalgrade)) {
330 // grade does not exist yet
331 $status = 'unknown';
332 } else {
333 $status = 'new';
334 $this->update_list[] = $grade_grade->id;
335 }
336
337 } else if ($grade_grade->exported < $grade_grade->timemodified) {
338 $status = 'regrade';
339 $this->update_list[] = $grade_grade->id;
340
341 } else if ($grade_grade->exported >= $grade_grade->timemodified) {
342 $status = 'nochange';
343
344 } else {
345 // something is wrong?
346 $status = 'unknown';
347 }
348
349 $this->flush(100);
350
351 return $status;
352 }
353
354 /**
355 * Flush and close the buffer.
356 */
357 function close() {
358 $this->flush(0);
359 }
360}
9d1e2185 361?>