Merge branch 'MDL-70476_310' of https://github.com/timhunt/moodle into MOODLE_310_STABLE
[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 $export_letters; // export letters
35 public $export_feedback; // export feedback
36 public $userkey; // export using private user key
caffc55a 37
5c75a0a3 38 public $updatedgradesonly; // only export updated grades
be289f57
SL
39
40 /**
41 * Grade display type (real, percentages or letter).
42 *
43 * This attribute is an integer for XML file export. Otherwise is an array for all other formats (ODS, XLS and TXT).
44 *
45 * @var $displaytype Grade display type constant (1, 2 or 3) or an array of display types where the key is the name
46 * and the value is the grade display type constant or 0 for unchecked display types.
47 * @access public.
48 */
49 public $displaytype;
5c75a0a3 50 public $decimalpoints; // number of decimal points for exports
78ab98bc 51 public $onlyactive; // only include users with an active enrolment
61c8e0d7
FM
52 public $usercustomfields; // include users custom fields
53
c10ddfb3 54 /**
cca51baa
DW
55 * @deprecated since Moodle 2.8
56 * @var $previewrows Number of rows in preview.
57 */
58 public $previewrows;
59
60 /**
61 * Constructor should set up all the private variables ready to be pulled.
62 *
63 * This constructor used to accept the individual parameters as separate arguments, in
64 * 2.8 this was simplified to just accept the data from the moodle form.
65 *
5c75a0a3 66 * @access public
caffc55a 67 * @param object $course
cca51baa
DW
68 * @param int $groupid
69 * @param stdClass|null $formdata
0eeada79 70 * @note Exporting as letters will lead to data loss if that exported set it re-imported.
c10ddfb3 71 */
cca51baa
DW
72 public function __construct($course, $groupid, $formdata) {
73 if (func_num_args() != 3 || ($formdata != null && get_class($formdata) != "stdClass")) {
74 $args = func_get_args();
75 return call_user_func_array(array($this, "deprecated_constructor"), $args);
76 }
caffc55a 77 $this->course = $course;
78 $this->groupid = $groupid;
cca51baa 79
caffc55a 80 $this->grade_items = grade_item::fetch_all(array('courseid'=>$this->course->id));
81
cca51baa
DW
82 $this->process_form($formdata);
83 }
84
85 /**
86 * Old deprecated constructor.
87 *
88 * This deprecated constructor accepts the individual parameters as separate arguments, in
89 * 2.8 this was simplified to just accept the data from the moodle form.
90 *
91 * @deprecated since 2.8 MDL-46548. Instead call the shortened constructor which accepts the data
92 * directly from the grade_export_form.
93 */
94 protected function deprecated_constructor($course,
95 $groupid=0,
96 $itemlist='',
97 $export_feedback=false,
98 $updatedgradesonly = false,
99 $displaytype = GRADE_DISPLAY_TYPE_REAL,
100 $decimalpoints = 2,
101 $onlyactive = false,
102 $usercustomfields = false) {
103
104 debugging('Many argument constructor for class "grade_export" is deprecated. Call the 3 argument version instead.', DEBUG_DEVELOPER);
105
106 $this->course = $course;
107 $this->groupid = $groupid;
108
109 $this->grade_items = grade_item::fetch_all(array('courseid'=>$this->course->id));
112fdf47
AD
110 //Populating the columns here is required by /grade/export/(whatever)/export.php
111 //however index.php, when the form is submitted, will construct the collection here
112 //with an empty $itemlist then reconstruct it in process_form() using $formdata
caffc55a 113 $this->columns = array();
114 if (!empty($itemlist)) {
112fdf47
AD
115 if ($itemlist=='-1') {
116 //user deselected all items
117 } else {
118 $itemids = explode(',', $itemlist);
119 // remove items that are not requested
120 foreach ($itemids as $itemid) {
121 if (array_key_exists($itemid, $this->grade_items)) {
122 $this->columns[$itemid] =& $this->grade_items[$itemid];
123 }
caffc55a 124 }
125 }
126 } else {
127 foreach ($this->grade_items as $itemid=>$unused) {
128 $this->columns[$itemid] =& $this->grade_items[$itemid];
129 }
130 }
0e2d708e 131
caffc55a 132 $this->export_feedback = $export_feedback;
133 $this->userkey = '';
134 $this->previewrows = false;
59a7447a 135 $this->updatedgradesonly = $updatedgradesonly;
d24832f9 136
864d1f8c 137 $this->displaytype = $displaytype;
138 $this->decimalpoints = $decimalpoints;
78ab98bc 139 $this->onlyactive = $onlyactive;
61c8e0d7 140 $this->usercustomfields = $usercustomfields;
caffc55a 141 }
142
143 /**
144 * Init object based using data from form
145 * @param object $formdata
146 */
147 function process_form($formdata) {
148 global $USER;
149
150 $this->columns = array();
151 if (!empty($formdata->itemids)) {
112fdf47
AD
152 if ($formdata->itemids=='-1') {
153 //user deselected all items
154 } else {
155 foreach ($formdata->itemids as $itemid=>$selected) {
156 if ($selected and array_key_exists($itemid, $this->grade_items)) {
157 $this->columns[$itemid] =& $this->grade_items[$itemid];
158 }
caffc55a 159 }
160 }
161 } else {
162 foreach ($this->grade_items as $itemid=>$unused) {
163 $this->columns[$itemid] =& $this->grade_items[$itemid];
164 }
0e2d708e 165 }
166
0e2d708e 167 if (isset($formdata->key)) {
caffc55a 168 if ($formdata->key == 1 && isset($formdata->iprestriction) && isset($formdata->validuntil)) {
169 // Create a new key
170 $formdata->key = create_user_key('grade/export', $USER->id, $this->course->id, $formdata->iprestriction, $formdata->validuntil);
0e2d708e 171 }
172 $this->userkey = $formdata->key;
173 }
ba74762b 174
cca51baa
DW
175 if (isset($formdata->decimals)) {
176 $this->decimalpoints = $formdata->decimals;
177 }
178
caffc55a 179 if (isset($formdata->export_letters)) {
180 $this->export_letters = $formdata->export_letters;
c10ddfb3 181 }
ba74762b 182
caffc55a 183 if (isset($formdata->export_feedback)) {
184 $this->export_feedback = $formdata->export_feedback;
185 }
c10ddfb3 186
78ab98bc
AD
187 if (isset($formdata->export_onlyactive)) {
188 $this->onlyactive = $formdata->export_onlyactive;
189 }
190
caffc55a 191 if (isset($formdata->previewrows)) {
192 $this->previewrows = $formdata->previewrows;
0bfbab47 193 }
194
924cb72b
DW
195 if (isset($formdata->display)) {
196 $this->displaytype = $formdata->display;
be289f57
SL
197
198 // Used by grade exports which accept multiple display types.
199 // If the checkbox value is 0 (unchecked) then remove it.
200 if (is_array($formdata->display)) {
201 $this->displaytype = array_filter($formdata->display);
202 }
924cb72b
DW
203 }
204
205 if (isset($formdata->updatedgradesonly)) {
206 $this->updatedgradesonly = $formdata->updatedgradesonly;
207 }
caffc55a 208 }
209
210 /**
211 * Update exported field in grade_grades table
212 * @return boolean
213 */
5c75a0a3 214 public function track_exports() {
caffc55a 215 global $CFG;
216
217 /// Whether this plugin is entitled to update export time
218 if ($expplugins = explode(",", $CFG->gradeexport)) {
219 if (in_array($this->plugin, $expplugins)) {
220 return true;
11745964 221 } else {
caffc55a 222 return false;
223 }
224 } else {
225 return false;
11745964 226 }
caffc55a 227 }
b8ff92b6 228
caffc55a 229 /**
230 * Returns string representation of final grade
be289f57
SL
231 * @param object $grade instance of grade_grade class
232 * @param integer $gradedisplayconst grade display type constant.
caffc55a 233 * @return string
234 */
be289f57
SL
235 public function format_grade($grade, $gradedisplayconst = null) {
236 $displaytype = $this->displaytype;
237 if (is_array($this->displaytype) && !is_null($gradedisplayconst)) {
238 $displaytype = $gradedisplayconst;
239 }
394ade5f
EM
240
241 $gradeitem = $this->grade_items[$grade->itemid];
242
243 // We are going to store the min and max so that we can "reset" the grade_item for later.
244 $grademax = $gradeitem->grademax;
245 $grademin = $gradeitem->grademin;
246
247 // Updating grade_item with this grade_grades min and max.
248 $gradeitem->grademax = $grade->get_grade_max();
249 $gradeitem->grademin = $grade->get_grade_min();
250
251 $formattedgrade = grade_format_gradevalue($grade->finalgrade, $gradeitem, false, $displaytype, $this->decimalpoints);
252
253 // Resetting the grade item in case it is reused.
254 $gradeitem->grademax = $grademax;
255 $gradeitem->grademin = $grademin;
256
257 return $formattedgrade;
11745964 258 }
d9617050 259
caffc55a 260 /**
261 * Returns the name of column in export
262 * @param object $grade_item
be289f57
SL
263 * @param boolean $feedback feedback colum
264 * @param string $gradedisplayname grade display name.
265 * @return string
caffc55a 266 */
be289f57
SL
267 public function format_column_name($grade_item, $feedback=false, $gradedisplayname = null) {
268 $column = new stdClass();
952f3748 269
caffc55a 270 if ($grade_item->itemtype == 'mod') {
be289f57 271 $column->name = get_string('modulename', $grade_item->itemmodule).get_string('labelsep', 'langconfig').$grade_item->get_name();
f67e327e 272 } else {
7d4f34cb 273 $column->name = $grade_item->get_name(true);
d9617050 274 }
ba74762b 275
be289f57
SL
276 // We can't have feedback and display type at the same time.
277 $column->extra = ($feedback) ? get_string('feedback') : get_string($gradedisplayname, 'grades');
caffc55a 278
be289f57 279 return html_to_text(get_string('gradeexportcolumntype', 'grades', $column), 0, false);
c10ddfb3 280 }
ba74762b 281
c10ddfb3 282 /**
caffc55a 283 * Returns formatted grade feedback
284 * @param object $feedback object with properties feedback and feedbackformat
a1e8c6d4 285 * @param object $grade Grade object with grade properties
caffc55a 286 * @return string
c10ddfb3 287 */
a1e8c6d4
P
288 public function format_feedback($feedback, $grade = null) {
289 $string = $feedback->feedback;
290 if (!empty($grade)) {
291 // Rewrite links to get the export working for 36, refer MDL-63488.
292 $string = file_rewrite_pluginfile_urls(
293 $feedback->feedback,
294 'pluginfile.php',
295 $grade->get_context()->id,
296 GRADE_FILE_COMPONENT,
297 GRADE_FEEDBACK_FILEAREA,
298 $grade->id
299 );
300 }
301
302 return strip_tags(format_text($string, $feedback->feedbackformat));
caffc55a 303 }
ba74762b 304
1b074625 305 /**
caffc55a 306 * Implemented by child class
1b074625 307 */
5c75a0a3 308 public abstract function print_grades();
11745964 309
caffc55a 310 /**
311 * Prints preview of exported grades on screen as a feedback mechanism
30f188f0 312 * @param bool $require_user_idnumber true means skip users without idnumber
cca51baa 313 * @deprecated since 2.8 MDL-46548. Previews are not useful on export.
caffc55a 314 */
30f188f0 315 public function display_preview($require_user_idnumber=false) {
c018f973 316 global $OUTPUT;
61c8e0d7 317
cca51baa
DW
318 debugging('function grade_export::display_preview is deprecated.', DEBUG_DEVELOPER);
319
61c8e0d7
FM
320 $userprofilefields = grade_helper::get_user_profile_fields($this->course->id, $this->usercustomfields);
321 $formatoptions = new stdClass();
322 $formatoptions->para = false;
323
c018f973 324 echo $OUTPUT->heading(get_string('previewrows', 'grades'));
567883c8 325
f5f4967e 326 echo '<table>';
327 echo '<tr>';
61c8e0d7 328 foreach ($userprofilefields as $field) {
293f42b6
CF
329 echo '<th>' . $field->fullname . '</th>';
330 }
38c1dd19
RT
331 if (!$this->onlyactive) {
332 echo '<th>'.get_string("suspended")."</th>";
333 }
caffc55a 334 foreach ($this->columns as $grade_item) {
335 echo '<th>'.$this->format_column_name($grade_item).'</th>';
ba74762b 336
337 /// add a column_feedback column
caffc55a 338 if ($this->export_feedback) {
339 echo '<th>'.$this->format_column_name($grade_item, true).'</th>';
ba74762b 340 }
1b074625 341 }
f5f4967e 342 echo '</tr>';
1b074625 343 /// Print all the lines of data.
f6eb15ad 344 $i = 0;
caffc55a 345 $gui = new graded_users_iterator($this->course, $this->columns, $this->groupid);
78ab98bc 346 $gui->require_active_enrolment($this->onlyactive);
61c8e0d7 347 $gui->allow_user_custom_fields($this->usercustomfields);
caffc55a 348 $gui->init();
349 while ($userdata = $gui->next_user()) {
f6eb15ad 350 // number of preview rows
bb9f1b4a 351 if ($this->previewrows and $this->previewrows <= $i) {
97599c0a 352 break;
f6eb15ad 353 }
caffc55a 354 $user = $userdata->user;
30f188f0 355 if ($require_user_idnumber and empty($user->idnumber)) {
7ce5df86 356 // some exports require user idnumber so we can match up students when importing the data
30f188f0 357 continue;
358 }
d24832f9 359
bb9f1b4a 360 $gradeupdated = false; // if no grade is update at all for this user, do not display this row
361 $rowstr = '';
caffc55a 362 foreach ($this->columns as $itemid=>$unused) {
363 $gradetxt = $this->format_grade($userdata->grades[$itemid]);
d24832f9 364
59a7447a 365 // get the status of this grade, and put it through track to get the status
366 $g = new grade_export_update_buffer();
367 $grade_grade = new grade_grade(array('itemid'=>$itemid, 'userid'=>$user->id));
368 $status = $g->track($grade_grade);
558cb86b 369
59a7447a 370 if ($this->updatedgradesonly && ($status == 'nochange' || $status == 'unknown')) {
bb9f1b4a 371 $rowstr .= '<td>'.get_string('unchangedgrade', 'grades').'</td>';
59a7447a 372 } else {
bb9f1b4a 373 $rowstr .= "<td>$gradetxt</td>";
374 $gradeupdated = true;
59a7447a 375 }
d24832f9 376
caffc55a 377 if ($this->export_feedback) {
bb9f1b4a 378 $rowstr .= '<td>'.$this->format_feedback($userdata->feedbacks[$itemid]).'</td>';
ba74762b 379 }
1b074625 380 }
bb9f1b4a 381
d24832f9 382 // if we are requesting updated grades only, we are not interested in this user at all
bb9f1b4a 383 if (!$gradeupdated && $this->updatedgradesonly) {
d24832f9 384 continue;
bb9f1b4a 385 }
386
387 echo '<tr>';
61c8e0d7
FM
388 foreach ($userprofilefields as $field) {
389 $fieldvalue = grade_helper::get_user_field_value($user, $field);
390 // @see profile_field_base::display_data().
391 echo '<td>' . format_text($fieldvalue, FORMAT_MOODLE, $formatoptions) . '</td>';
293f42b6 392 }
38c1dd19
RT
393 if (!$this->onlyactive) {
394 $issuspended = ($user->suspendedenrolment) ? get_string('yes') : '';
395 echo "<td>$issuspended</td>";
396 }
bb9f1b4a 397 echo $rowstr;
f5f4967e 398 echo "</tr>";
d24832f9 399
bb9f1b4a 400 $i++; // increment the counter
f5f4967e 401 }
402 echo '</table>';
caffc55a 403 $gui->close();
1b074625 404 }
0e2d708e 405
406 /**
caffc55a 407 * Returns array of parameters used by dump.php and export.php.
408 * @return array
409 */
5c75a0a3 410 public function get_export_params() {
caffc55a 411 $itemids = array_keys($this->columns);
112fdf47
AD
412 $itemidsparam = implode(',', $itemids);
413 if (empty($itemidsparam)) {
414 $itemidsparam = '-1';
415 }
caffc55a 416
58ccdcd2
SL
417 // We have a single grade display type constant.
418 if (!is_array($this->displaytype)) {
419 $displaytypes = $this->displaytype;
420 } else {
421 // Implode the grade display types array as moodle_url function doesn't accept arrays.
422 $displaytypes = implode(',', $this->displaytype);
423 }
c3651482 424
e82aa043
SL
425 if (!empty($this->updatedgradesonly)) {
426 $updatedgradesonly = $this->updatedgradesonly;
427 } else {
428 $updatedgradesonly = 0;
429 }
58ccdcd2
SL
430 $params = array('id' => $this->course->id,
431 'groupid' => $this->groupid,
432 'itemids' => $itemidsparam,
433 'export_letters' => $this->export_letters,
434 'export_feedback' => $this->export_feedback,
e82aa043 435 'updatedgradesonly' => $updatedgradesonly,
58ccdcd2
SL
436 'decimalpoints' => $this->decimalpoints,
437 'export_onlyactive' => $this->onlyactive,
438 'usercustomfields' => $this->usercustomfields,
439 'displaytype' => $displaytypes,
440 'key' => $this->userkey);
caffc55a 441
442 return $params;
443 }
444
445 /**
446 * Either prints a "Export" box, which will redirect the user to the download page,
447 * or prints the URL for the published data.
cca51baa
DW
448 *
449 * @deprecated since 2.8 MDL-46548. Call get_export_url and set the
450 * action of the grade_export_form instead.
0e2d708e 451 * @return void
452 */
5c75a0a3 453 public function print_continue() {
c018f973 454 global $CFG, $OUTPUT;
0e2d708e 455
cca51baa 456 debugging('function grade_export::print_continue is deprecated.', DEBUG_DEVELOPER);
6c3ef410 457 $params = $this->get_export_params();
567883c8 458
c018f973 459 echo $OUTPUT->heading(get_string('export', 'grades'));
567883c8 460
03fcc729 461 echo $OUTPUT->container_start('gradeexportlink');
462
cca51baa
DW
463 if (!$this->userkey) {
464 // This button should trigger a download prompt.
465 $url = new moodle_url('/grade/export/'.$this->plugin.'/export.php', $params);
466 echo $OUTPUT->single_button($url, get_string('download', 'admin'));
0e2d708e 467
468 } else {
caffc55a 469 $paramstr = '';
470 $sep = '?';
471 foreach($params as $name=>$value) {
472 $paramstr .= $sep.$name.'='.$value;
03fcc729 473 $sep = '&';
caffc55a 474 }
475
52f1a9ff 476 $link = $CFG->wwwroot.'/grade/export/'.$this->plugin.'/dump.php'.$paramstr.'&key='.$this->userkey;
0e2d708e 477
75015e5f 478 echo get_string('download', 'admin').': ' . html_writer::link($link, $link);
0e2d708e 479 }
03fcc729 480 echo $OUTPUT->container_end();
cca51baa
DW
481
482 return;
0e2d708e 483 }
58ccdcd2
SL
484
485 /**
486 * Generate the export url.
487 *
488 * Get submitted form data and create the url to be used on the grade publish feature.
489 *
490 * @return moodle_url the url of grade publishing export.
491 */
492 public function get_export_url() {
493 return new moodle_url('/grade/export/'.$this->plugin.'/dump.php', $this->get_export_params());
494 }
495
496 /**
497 * Convert the grade display types parameter into the required array to grade exporting class.
498 *
499 * In order to export, the array key must be the display type name and the value must be the grade display type
500 * constant.
501 *
502 * Note: Added support for combined display types constants like the (GRADE_DISPLAY_TYPE_PERCENTAGE_REAL) as
503 * the $CFG->grade_export_displaytype config is still used on 2.7 in case of missing displaytype url param.
504 * In these cases, the file will be exported with a column for each display type.
505 *
506 * @param string $displaytypes can be a single or multiple display type constants comma separated.
507 * @return array $types
508 */
509 public static function convert_flat_displaytypes_to_array($displaytypes) {
510 $types = array();
511
512 // We have a single grade display type constant.
513 if (is_int($displaytypes)) {
514 $displaytype = clean_param($displaytypes, PARAM_INT);
515
516 // Let's set a default value, will be replaced below by the grade display type constant.
517 $display[$displaytype] = 1;
518 } else {
519 // Multiple grade display types constants.
520 $display = array_flip(explode(',', $displaytypes));
521 }
522
523 // Now, create the array in the required format by grade exporting class.
524 foreach ($display as $type => $value) {
525 $type = clean_param($type, PARAM_INT);
526 if ($type == GRADE_DISPLAY_TYPE_LETTER) {
527 $types['letter'] = GRADE_DISPLAY_TYPE_LETTER;
528 } else if ($type == GRADE_DISPLAY_TYPE_PERCENTAGE) {
529 $types['percentage'] = GRADE_DISPLAY_TYPE_PERCENTAGE;
530 } else if ($type == GRADE_DISPLAY_TYPE_REAL) {
531 $types['real'] = GRADE_DISPLAY_TYPE_REAL;
532 } else if ($type == GRADE_DISPLAY_TYPE_REAL_PERCENTAGE) {
533 $types['real'] = GRADE_DISPLAY_TYPE_REAL;
534 $types['percentage'] = GRADE_DISPLAY_TYPE_PERCENTAGE;
535 } else if ($type == GRADE_DISPLAY_TYPE_REAL_LETTER) {
536 $types['real'] = GRADE_DISPLAY_TYPE_REAL;
537 $types['letter'] = GRADE_DISPLAY_TYPE_LETTER;
538 } else if ($type == GRADE_DISPLAY_TYPE_LETTER_REAL) {
539 $types['letter'] = GRADE_DISPLAY_TYPE_LETTER;
540 $types['real'] = GRADE_DISPLAY_TYPE_REAL;
541 } else if ($type == GRADE_DISPLAY_TYPE_LETTER_PERCENTAGE) {
542 $types['letter'] = GRADE_DISPLAY_TYPE_LETTER;
543 $types['percentage'] = GRADE_DISPLAY_TYPE_PERCENTAGE;
544 } else if ($type == GRADE_DISPLAY_TYPE_PERCENTAGE_LETTER) {
545 $types['percentage'] = GRADE_DISPLAY_TYPE_PERCENTAGE;
546 $types['letter'] = GRADE_DISPLAY_TYPE_LETTER;
547 } else if ($type == GRADE_DISPLAY_TYPE_PERCENTAGE_REAL) {
548 $types['percentage'] = GRADE_DISPLAY_TYPE_PERCENTAGE;
549 $types['real'] = GRADE_DISPLAY_TYPE_REAL;
550 }
551 }
552 return $types;
553 }
554
555 /**
556 * Convert the item ids parameter into the required array to grade exporting class.
557 *
558 * In order to export, the array key must be the grade item id and all values must be one.
559 *
560 * @param string $itemids can be a single item id or many item ids comma separated.
561 * @return array $items correctly formatted array.
562 */
563 public static function convert_flat_itemids_to_array($itemids) {
564 $items = array();
565
566 // We just have one single item id.
567 if (is_int($itemids)) {
568 $itemid = clean_param($itemids, PARAM_INT);
569 $items[$itemid] = 1;
570 } else {
571 // Few grade items.
572 $items = array_flip(explode(',', $itemids));
573 foreach ($items as $itemid => $value) {
574 $itemid = clean_param($itemid, PARAM_INT);
575 $items[$itemid] = 1;
576 }
577 }
578 return $items;
579 }
580
581 /**
582 * Create the html code of the grade publishing feature.
583 *
584 * @return string $output html code of the grade publishing.
585 */
586 public function get_grade_publishing_url() {
587 $url = $this->get_export_url();
588 $output = html_writer::start_div();
589 $output .= html_writer::tag('p', get_string('gradepublishinglink', 'grades', html_writer::link($url, $url)));
590 $output .= html_writer::end_div();
591 return $output;
592 }
593
594 /**
595 * Create a stdClass object from URL parameters to be used by grade_export class.
596 *
597 * @param int $id course id.
598 * @param string $itemids grade items comma separated.
599 * @param bool $exportfeedback export feedback option.
600 * @param bool $onlyactive only enrolled active students.
601 * @param string $displaytype grade display type constants comma separated.
602 * @param int $decimalpoints grade decimal points.
603 * @param null $updatedgradesonly recently updated grades only (Used by XML exporting only).
604 * @param null $separator separator character: tab, comma, colon and semicolon (Used by TXT exporting only).
605 *
606 * @return stdClass $formdata
607 */
608 public static function export_bulk_export_data($id, $itemids, $exportfeedback, $onlyactive, $displaytype,
609 $decimalpoints, $updatedgradesonly = null, $separator = null) {
610
611 $formdata = new \stdClass();
612 $formdata->id = $id;
613 $formdata->itemids = self::convert_flat_itemids_to_array($itemids);
614 $formdata->exportfeedback = $exportfeedback;
615 $formdata->export_onlyactive = $onlyactive;
616 $formdata->display = self::convert_flat_displaytypes_to_array($displaytype);
617 $formdata->decimals = $decimalpoints;
618
619 if (!empty($updatedgradesonly)) {
620 $formdata->updatedgradesonly = $updatedgradesonly;
621 }
622
623 if (!empty($separator)) {
624 $formdata->separator = $separator;
625 }
626
627 return $formdata;
628 }
c10ddfb3 629}
630
eb859919 631/**
632 * This class is used to update the exported field in grade_grades.
633 * It does internal buffering to speedup the db operations.
634 */
635class grade_export_update_buffer {
5c75a0a3 636 public $update_list;
637 public $export_time;
eb859919 638
639 /**
640 * Constructor - creates the buffer and initialises the time stamp
641 */
c4d0b752 642 public function __construct() {
eb859919 643 $this->update_list = array();
644 $this->export_time = time();
645 }
646
9e313e79
DM
647 /**
648 * Old syntax of class constructor. Deprecated in PHP7.
649 *
650 * @deprecated since Moodle 3.1
651 */
652 public function grade_export_update_buffer() {
653 debugging('Use of class name as constructor is deprecated', DEBUG_DEVELOPER);
654 self::__construct();
655 }
656
5c75a0a3 657 public function flush($buffersize) {
658 global $CFG, $DB;
eb859919 659
660 if (count($this->update_list) > $buffersize) {
5c75a0a3 661 list($usql, $params) = $DB->get_in_or_equal($this->update_list);
662 $params = array_merge(array($this->export_time), $params);
663
664 $sql = "UPDATE {grade_grades} SET exported = ? WHERE id $usql";
655b09ca 665 $DB->execute($sql, $params);
eb859919 666 $this->update_list = array();
667 }
668 }
669
670 /**
671 * Track grade export status
672 * @param object $grade_grade
673 * @return string $status (unknow, new, regrade, nochange)
674 */
5c75a0a3 675 public function track($grade_grade) {
59a7447a 676
eb859919 677 if (empty($grade_grade->exported) or empty($grade_grade->timemodified)) {
678 if (is_null($grade_grade->finalgrade)) {
679 // grade does not exist yet
680 $status = 'unknown';
681 } else {
682 $status = 'new';
683 $this->update_list[] = $grade_grade->id;
684 }
685
686 } else if ($grade_grade->exported < $grade_grade->timemodified) {
687 $status = 'regrade';
688 $this->update_list[] = $grade_grade->id;
689
690 } else if ($grade_grade->exported >= $grade_grade->timemodified) {
691 $status = 'nochange';
692
693 } else {
694 // something is wrong?
695 $status = 'unknown';
696 }
697
698 $this->flush(100);
699
700 return $status;
701 }
702
703 /**
704 * Flush and close the buffer.
705 */
5c75a0a3 706 public function close() {
eb859919 707 $this->flush(0);
708 }
709}
6c3ef410 710
459843d4
AD
711/**
712 * Verify that there is a valid set of grades to export.
713 * @param $courseid int The course being exported
714 */
715function export_verify_grades($courseid) {
30be6c84 716 if (grade_needs_regrade_final_grades($courseid)) {
bb4d0576 717 throw new moodle_exception('gradesneedregrading', 'grades');
459843d4
AD
718 }
719}