publication MDL-25040 fix ugly css (hub logo image has border)
[moodle.git] / grade / export / lib.php
CommitLineData
e060e33d 1<?php
c10ddfb3 2
e060e33d 3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
ce34ed3a 17
89bd8357 18require_once($CFG->dirroot.'/lib/gradelib.php');
19require_once($CFG->dirroot.'/grade/lib.php');
a5bc4e6e 20require_once($CFG->dirroot.'/grade/export/grade_export_form.php');
0eeada79 21
1b074625 22/**
23 * Base export class
24 */
5c75a0a3 25abstract class grade_export {
ba74762b 26
5c75a0a3 27 public $plugin; // plgin name - must be filled in subclasses!
caffc55a 28
5c75a0a3 29 public $grade_items; // list of all course grade items
30 public $groupid; // groupid, 0 means all groups
31 public $course; // course object
32 public $columns; // array of grade_items selected for export
caffc55a 33
5c75a0a3 34 public $previewrows; // number of rows in preview
35 public $export_letters; // export letters
36 public $export_feedback; // export feedback
37 public $userkey; // export using private user key
caffc55a 38
5c75a0a3 39 public $updatedgradesonly; // only export updated grades
40 public $displaytype; // display type (e.g. real, percentages, letter) for exports
41 public $decimalpoints; // number of decimal points for exports
c10ddfb3 42 /**
43 * Constructor should set up all the private variables ready to be pulled
5c75a0a3 44 * @access public
caffc55a 45 * @param object $course
46 * @param int $groupid id of selected group, 0 means all
47 * @param string $itemlist comma separated list of item ids, empty means all
48 * @param boolean $export_feedback
49 * @param boolean $export_letters
0eeada79 50 * @note Exporting as letters will lead to data loss if that exported set it re-imported.
c10ddfb3 51 */
5c75a0a3 52 public function grade_export($course, $groupid=0, $itemlist='', $export_feedback=false, $updatedgradesonly = false, $displaytype = GRADE_DISPLAY_TYPE_REAL, $decimalpoints = 2) {
caffc55a 53 $this->course = $course;
54 $this->groupid = $groupid;
55 $this->grade_items = grade_item::fetch_all(array('courseid'=>$this->course->id));
56
57 $this->columns = array();
58 if (!empty($itemlist)) {
59 $itemids = explode(',', $itemlist);
60 // remove items that are not requested
61 foreach ($itemids as $itemid) {
62 if (array_key_exists($itemid, $this->grade_items)) {
63 $this->columns[$itemid] =& $this->grade_items[$itemid];
64 }
65 }
66 } else {
67 foreach ($this->grade_items as $itemid=>$unused) {
68 $this->columns[$itemid] =& $this->grade_items[$itemid];
69 }
70 }
0e2d708e 71
caffc55a 72 $this->export_feedback = $export_feedback;
73 $this->userkey = '';
74 $this->previewrows = false;
59a7447a 75 $this->updatedgradesonly = $updatedgradesonly;
d24832f9 76
864d1f8c 77 $this->displaytype = $displaytype;
78 $this->decimalpoints = $decimalpoints;
caffc55a 79 }
80
81 /**
82 * Init object based using data from form
83 * @param object $formdata
84 */
85 function process_form($formdata) {
86 global $USER;
87
88 $this->columns = array();
89 if (!empty($formdata->itemids)) {
90 foreach ($formdata->itemids as $itemid=>$selected) {
91 if ($selected and array_key_exists($itemid, $this->grade_items)) {
92 $this->columns[$itemid] =& $this->grade_items[$itemid];
93 }
94 }
95 } else {
96 foreach ($this->grade_items as $itemid=>$unused) {
97 $this->columns[$itemid] =& $this->grade_items[$itemid];
98 }
0e2d708e 99 }
100
0e2d708e 101 if (isset($formdata->key)) {
caffc55a 102 if ($formdata->key == 1 && isset($formdata->iprestriction) && isset($formdata->validuntil)) {
103 // Create a new key
104 $formdata->key = create_user_key('grade/export', $USER->id, $this->course->id, $formdata->iprestriction, $formdata->validuntil);
0e2d708e 105 }
106 $this->userkey = $formdata->key;
107 }
ba74762b 108
caffc55a 109 if (isset($formdata->export_letters)) {
110 $this->export_letters = $formdata->export_letters;
c10ddfb3 111 }
ba74762b 112
caffc55a 113 if (isset($formdata->export_feedback)) {
114 $this->export_feedback = $formdata->export_feedback;
115 }
c10ddfb3 116
caffc55a 117 if (isset($formdata->previewrows)) {
118 $this->previewrows = $formdata->previewrows;
0bfbab47 119 }
120
caffc55a 121 }
122
123 /**
124 * Update exported field in grade_grades table
125 * @return boolean
126 */
5c75a0a3 127 public function track_exports() {
caffc55a 128 global $CFG;
129
130 /// Whether this plugin is entitled to update export time
131 if ($expplugins = explode(",", $CFG->gradeexport)) {
132 if (in_array($this->plugin, $expplugins)) {
133 return true;
11745964 134 } else {
caffc55a 135 return false;
136 }
137 } else {
138 return false;
11745964 139 }
caffc55a 140 }
b8ff92b6 141
caffc55a 142 /**
143 * Returns string representation of final grade
144 * @param $object $grade instance of grade_grade class
145 * @return string
146 */
5c75a0a3 147 public function format_grade($grade) {
864d1f8c 148 return grade_format_gradevalue($grade->finalgrade, $this->grade_items[$grade->itemid], false, $this->displaytype, $this->decimalpoints);
11745964 149 }
d9617050 150
caffc55a 151 /**
152 * Returns the name of column in export
153 * @param object $grade_item
154 * @param boolena $feedback feedback colum
155 * &return string
156 */
5c75a0a3 157 public function format_column_name($grade_item, $feedback=false) {
caffc55a 158 if ($grade_item->itemtype == 'mod') {
995f2d51 159 $name = get_string('modulename', $grade_item->itemmodule).get_string('labelsep', 'langconfig').$grade_item->get_name();
f67e327e 160 } else {
caffc55a 161 $name = $grade_item->get_name();
d9617050 162 }
ba74762b 163
caffc55a 164 if ($feedback) {
165 $name .= ' ('.get_string('feedback').')';
ba74762b 166 }
caffc55a 167
168 return strip_tags($name);
c10ddfb3 169 }
ba74762b 170
c10ddfb3 171 /**
caffc55a 172 * Returns formatted grade feedback
173 * @param object $feedback object with properties feedback and feedbackformat
174 * @return string
c10ddfb3 175 */
5c75a0a3 176 public function format_feedback($feedback) {
caffc55a 177 return strip_tags(format_text($feedback->feedback, $feedback->feedbackformat));
178 }
ba74762b 179
1b074625 180 /**
caffc55a 181 * Implemented by child class
1b074625 182 */
5c75a0a3 183 public abstract function print_grades();
11745964 184
caffc55a 185 /**
186 * Prints preview of exported grades on screen as a feedback mechanism
30f188f0 187 * @param bool $require_user_idnumber true means skip users without idnumber
caffc55a 188 */
30f188f0 189 public function display_preview($require_user_idnumber=false) {
c018f973 190 global $OUTPUT;
191 echo $OUTPUT->heading(get_string('previewrows', 'grades'));
567883c8 192
f5f4967e 193 echo '<table>';
194 echo '<tr>';
195 echo '<th>'.get_string("firstname")."</th>".
196 '<th>'.get_string("lastname")."</th>".
197 '<th>'.get_string("idnumber")."</th>".
198 '<th>'.get_string("institution")."</th>".
199 '<th>'.get_string("department")."</th>".
200 '<th>'.get_string("email")."</th>";
caffc55a 201 foreach ($this->columns as $grade_item) {
202 echo '<th>'.$this->format_column_name($grade_item).'</th>';
ba74762b 203
204 /// add a column_feedback column
caffc55a 205 if ($this->export_feedback) {
206 echo '<th>'.$this->format_column_name($grade_item, true).'</th>';
ba74762b 207 }
1b074625 208 }
f5f4967e 209 echo '</tr>';
1b074625 210 /// Print all the lines of data.
ba74762b 211
f6eb15ad 212 $i = 0;
caffc55a 213 $gui = new graded_users_iterator($this->course, $this->columns, $this->groupid);
214 $gui->init();
215 while ($userdata = $gui->next_user()) {
f6eb15ad 216 // number of preview rows
bb9f1b4a 217 if ($this->previewrows and $this->previewrows <= $i) {
97599c0a 218 break;
f6eb15ad 219 }
caffc55a 220 $user = $userdata->user;
30f188f0 221 if ($require_user_idnumber and empty($user->idnumber)) {
222 // some exports require user idnumber
223 continue;
224 }
d24832f9 225
bb9f1b4a 226 $gradeupdated = false; // if no grade is update at all for this user, do not display this row
227 $rowstr = '';
caffc55a 228 foreach ($this->columns as $itemid=>$unused) {
229 $gradetxt = $this->format_grade($userdata->grades[$itemid]);
d24832f9 230
59a7447a 231 // get the status of this grade, and put it through track to get the status
232 $g = new grade_export_update_buffer();
233 $grade_grade = new grade_grade(array('itemid'=>$itemid, 'userid'=>$user->id));
234 $status = $g->track($grade_grade);
558cb86b 235
59a7447a 236 if ($this->updatedgradesonly && ($status == 'nochange' || $status == 'unknown')) {
bb9f1b4a 237 $rowstr .= '<td>'.get_string('unchangedgrade', 'grades').'</td>';
59a7447a 238 } else {
bb9f1b4a 239 $rowstr .= "<td>$gradetxt</td>";
240 $gradeupdated = true;
59a7447a 241 }
d24832f9 242
caffc55a 243 if ($this->export_feedback) {
bb9f1b4a 244 $rowstr .= '<td>'.$this->format_feedback($userdata->feedbacks[$itemid]).'</td>';
ba74762b 245 }
1b074625 246 }
bb9f1b4a 247
d24832f9 248 // if we are requesting updated grades only, we are not interested in this user at all
bb9f1b4a 249 if (!$gradeupdated && $this->updatedgradesonly) {
d24832f9 250 continue;
bb9f1b4a 251 }
252
253 echo '<tr>';
d24832f9 254 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 255 echo $rowstr;
f5f4967e 256 echo "</tr>";
d24832f9 257
bb9f1b4a 258 $i++; // increment the counter
f5f4967e 259 }
260 echo '</table>';
caffc55a 261 $gui->close();
1b074625 262 }
0e2d708e 263
264 /**
caffc55a 265 * Returns array of parameters used by dump.php and export.php.
266 * @return array
267 */
5c75a0a3 268 public function get_export_params() {
caffc55a 269 $itemids = array_keys($this->columns);
270
59a7447a 271 $params = array('id' =>$this->course->id,
272 'groupid' =>$this->groupid,
273 'itemids' =>implode(',', $itemids),
274 'export_letters' =>$this->export_letters,
275 'export_feedback' =>$this->export_feedback,
864d1f8c 276 'updatedgradesonly' =>$this->updatedgradesonly,
277 'displaytype' =>$this->displaytype,
278 'decimalpoints' =>$this->decimalpoints);
caffc55a 279
280 return $params;
281 }
282
283 /**
284 * Either prints a "Export" box, which will redirect the user to the download page,
285 * or prints the URL for the published data.
0e2d708e 286 * @return void
287 */
5c75a0a3 288 public function print_continue() {
c018f973 289 global $CFG, $OUTPUT;
0e2d708e 290
6c3ef410 291 $params = $this->get_export_params();
567883c8 292
c018f973 293 echo $OUTPUT->heading(get_string('export', 'grades'));
567883c8 294
03fcc729 295 echo $OUTPUT->container_start('gradeexportlink');
296
567883c8 297 if (!$this->userkey) { // this button should trigger a download prompt
a6855934 298 echo $OUTPUT->single_button(new moodle_url('/grade/export/'.$this->plugin.'/export.php', $params), get_string('download', 'admin'));
0e2d708e 299
300 } else {
caffc55a 301 $paramstr = '';
302 $sep = '?';
303 foreach($params as $name=>$value) {
304 $paramstr .= $sep.$name.'='.$value;
03fcc729 305 $sep = '&';
caffc55a 306 }
307
52f1a9ff 308 $link = $CFG->wwwroot.'/grade/export/'.$this->plugin.'/dump.php'.$paramstr.'&key='.$this->userkey;
0e2d708e 309
75015e5f 310 echo get_string('download', 'admin').': ' . html_writer::link($link, $link);
0e2d708e 311 }
03fcc729 312 echo $OUTPUT->container_end();
0e2d708e 313 }
c10ddfb3 314}
315
eb859919 316/**
317 * This class is used to update the exported field in grade_grades.
318 * It does internal buffering to speedup the db operations.
319 */
320class grade_export_update_buffer {
5c75a0a3 321 public $update_list;
322 public $export_time;
eb859919 323
324 /**
325 * Constructor - creates the buffer and initialises the time stamp
326 */
5c75a0a3 327 public function grade_export_update_buffer() {
eb859919 328 $this->update_list = array();
329 $this->export_time = time();
330 }
331
5c75a0a3 332 public function flush($buffersize) {
333 global $CFG, $DB;
eb859919 334
335 if (count($this->update_list) > $buffersize) {
5c75a0a3 336 list($usql, $params) = $DB->get_in_or_equal($this->update_list);
337 $params = array_merge(array($this->export_time), $params);
338
339 $sql = "UPDATE {grade_grades} SET exported = ? WHERE id $usql";
655b09ca 340 $DB->execute($sql, $params);
eb859919 341 $this->update_list = array();
342 }
343 }
344
345 /**
346 * Track grade export status
347 * @param object $grade_grade
348 * @return string $status (unknow, new, regrade, nochange)
349 */
5c75a0a3 350 public function track($grade_grade) {
59a7447a 351
eb859919 352 if (empty($grade_grade->exported) or empty($grade_grade->timemodified)) {
353 if (is_null($grade_grade->finalgrade)) {
354 // grade does not exist yet
355 $status = 'unknown';
356 } else {
357 $status = 'new';
358 $this->update_list[] = $grade_grade->id;
359 }
360
361 } else if ($grade_grade->exported < $grade_grade->timemodified) {
362 $status = 'regrade';
363 $this->update_list[] = $grade_grade->id;
364
365 } else if ($grade_grade->exported >= $grade_grade->timemodified) {
366 $status = 'nochange';
367
368 } else {
369 // something is wrong?
370 $status = 'unknown';
371 }
372
373 $this->flush(100);
374
375 return $status;
376 }
377
378 /**
379 * Flush and close the buffer.
380 */
5c75a0a3 381 public function close() {
eb859919 382 $this->flush(0);
383 }
384}
6c3ef410 385