2 ///////////////////////////////////////////////////////////////////////////
4 // NOTICE OF COPYRIGHT //
6 // Moodle - Modular Object-Oriented Dynamic Learning Environment //
7 // http://moodle.com //
9 // Copyright (C) 1999 onwards Martin Dougiamas http://moodle.com //
11 // This program is free software; you can redistribute it and/or modify //
12 // it under the terms of the GNU General Public License as published by //
13 // the Free Software Foundation; either version 2 of the License, or //
14 // (at your option) any later version. //
16 // This program is distributed in the hope that it will be useful, //
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of //
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
19 // GNU General Public License for more details: //
21 // http://www.gnu.org/copyleft/gpl.html //
23 ///////////////////////////////////////////////////////////////////////////
25 require_once($CFG->dirroot
.'/grade/export/lib.php');
27 class grade_export_xml
extends grade_export
{
32 * To be implemented by child classes
33 * @param boolean $feedback
34 * @param boolean $publish Whether to output directly, or send as a file
37 function print_grades($feedback = false) {
39 require_once($CFG->libdir
.'/filelib.php');
41 $export_tracking = $this->track_exports();
43 $strgrades = get_string('grades', 'grade');
45 /// Calculate file name
46 $downloadfilename = clean_filename("{$this->course->shortname} $strgrades.xml");
48 make_upload_directory('temp/gradeexport', false);
49 $tempfilename = $CFG->dataroot
.'/temp/gradeexport/'. md5(sesskey().microtime().$downloadfilename);
50 if (!$handle = fopen($tempfilename, 'w+b')) {
51 error("Could not create a temporary file into which to dump the XML data.");
55 /// time stamp to ensure uniqueness of batch export
56 fwrite($handle, '<results batch="xml_export_'.time().'">'."\n");
58 $export_buffer = array();
60 $geub = new grade_export_update_buffer();
61 $gui = new graded_users_iterator($this->course
, $this->columns
, $this->groupid
);
63 while ($userdata = $gui->next_user()) {
64 $user = $userdata->user
;
66 // studentgrades[] index should match with corresponding $index
67 foreach ($userdata->grades
as $itemid => $grade) {
68 $grade_item = $this->grade_items
[$itemid];
69 $grade->grade_item
=& $grade_item;
70 $gradestr = $grade->finalgrade
; // no formating for now
72 fwrite($handle, "\t<result>\n");
74 if ($export_tracking) {
75 $status = $geub->track($grade);
76 fwrite($handle, "\t\t<state>$status</state>\n");
79 // only need id number
80 fwrite($handle, "\t\t<assignment>{$grade_item->idnumber}</assignment>\n");
81 // this column should be customizable to use either student id, idnumber, uesrname or email.
82 fwrite($handle, "\t\t<student>{$user->id}</student>\n");
83 fwrite($handle, "\t\t<score>$gradestr</score>\n");
84 if ($this->export_feedback
) {
85 $feedbackstr = $this->format_feedback($userdata->feedbacks
[$itemid]);
86 fwrite($handle, "\t\t<feedback>$feedbackstr</feedback>\n");
88 fwrite($handle, "\t</result>\n");
91 fwrite($handle, "</results>");
96 @header
('Cache-Control: private, must-revalidate, pre-check=0, post-check=0, max-age=0');
97 @header
('Expires: '. gmdate('D, d M Y H:i:s', 0) .' GMT');
98 @header
('Pragma: no-cache');
99 header("Content-type: text/xml; charset=UTF-8");
100 header("Content-Disposition: attachment; filename=\"$downloadfilename\"");
102 readfile_chunked($tempfilename);
104 @unlink
($tempfilename);