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