3 ///////////////////////////////////////////////////////////////////////////
5 // NOTICE OF COPYRIGHT //
7 // Moodle - Modular Object-Oriented Dynamic Learning Environment //
8 // http://moodle.com //
10 // Copyright (C) 1999 onwards Martin Dougiamas http://moodle.com //
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. //
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: //
22 // http://www.gnu.org/copyleft/gpl.html //
24 ///////////////////////////////////////////////////////////////////////////
26 require_once($CFG->dirroot
.'/grade/export/lib.php');
28 class grade_export_xml
extends grade_export
{
31 var $updatedgradesonly = false; // default to export ALL grades
34 * To be implemented by child classes
35 * @param boolean $feedback
36 * @param boolean $publish Whether to output directly, or send as a file
39 function print_grades($feedback = false) {
41 require_once($CFG->libdir
.'/filelib.php');
43 $export_tracking = $this->track_exports();
45 $strgrades = get_string('grades');
47 /// Calculate file name
48 $downloadfilename = clean_filename("{$this->course->shortname} $strgrades.xml");
50 make_upload_directory('temp/gradeexport', false);
51 $tempfilename = $CFG->dataroot
.'/temp/gradeexport/'. md5(sesskey().microtime().$downloadfilename);
52 if (!$handle = fopen($tempfilename, 'w+b')) {
53 error("Could not create a temporary file into which to dump the XML data.");
57 /// time stamp to ensure uniqueness of batch export
58 fwrite($handle, '<results batch="xml_export_'.time().'">'."\n");
60 $export_buffer = array();
62 $geub = new grade_export_update_buffer();
63 $gui = new graded_users_iterator($this->course
, $this->columns
, $this->groupid
);
65 while ($userdata = $gui->next_user()) {
66 $user = $userdata->user
;
68 if (empty($user->idnumber
)) {
69 //id number must exist
73 // studentgrades[] index should match with corresponding $index
74 foreach ($userdata->grades
as $itemid => $grade) {
75 $grade_item = $this->grade_items
[$itemid];
76 $grade->grade_item
=& $grade_item;
77 $gradestr = $this->format_grade($grade); // no formating for now
79 // MDL-11669, skip exported grades or bad grades (if setting says so)
80 if ($export_tracking) {
81 $status = $geub->track($grade);
82 if ($this->updatedgradesonly
&& ($status == 'nochange' ||
$status == 'unknown')) {
87 fwrite($handle, "\t<result>\n");
89 if ($export_tracking) {
90 fwrite($handle, "\t\t<state>$status</state>\n");
93 // only need id number
94 fwrite($handle, "\t\t<assignment>{$grade_item->idnumber}</assignment>\n");
95 // this column should be customizable to use either student id, idnumber, uesrname or email.
96 fwrite($handle, "\t\t<student>{$user->idnumber}</student>\n");
97 fwrite($handle, "\t\t<score>$gradestr</score>\n");
98 if ($this->export_feedback
) {
99 $feedbackstr = $this->format_feedback($userdata->feedbacks
[$itemid]);
100 fwrite($handle, "\t\t<feedback>$feedbackstr</feedback>\n");
102 fwrite($handle, "\t</result>\n");
105 fwrite($handle, "</results>");
110 @header
('Cache-Control: private, must-revalidate, pre-check=0, post-check=0, max-age=0');
111 @header
('Expires: '. gmdate('D, d M Y H:i:s', 0) .' GMT');
112 @header
('Pragma: no-cache');
113 header("Content-type: text/xml; charset=UTF-8");
114 header("Content-Disposition: attachment; filename=\"$downloadfilename\"");
116 readfile_chunked($tempfilename);
118 @unlink
($tempfilename);