5924bb3586166465e4f61d219608b881d43f5fd6
[moodle.git] / grade / export / xml / grade_export_xml.php
1 <?php
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/>.
18 require_once($CFG->dirroot.'/grade/export/lib.php');
19 require_once($CFG->libdir.'/filelib.php');
21 class grade_export_xml extends grade_export {
23     public $plugin = 'xml';
24     public $updatedgradesonly = false; // default to export ALL grades
26     /**
27      * To be implemented by child classes
28      * @param boolean $feedback
29      * @param boolean $publish Whether to output directly, or send as a file
30      * @return string
31      */
32     public function print_grades($feedback = false) {
33         global $CFG;
34         require_once($CFG->libdir.'/filelib.php');
36         $export_tracking = $this->track_exports();
38         $strgrades = get_string('grades');
40         /// Calculate file name
41         $shortname = format_string($this->course->shortname, true, array('context' => get_context_instance(CONTEXT_COURSE, $this->course->id)));
42         $downloadfilename = clean_filename("$shortname $strgrades.xml");
44         make_upload_directory('temp/gradeexport');
45         $tempfilename = $CFG->dataroot .'/temp/gradeexport/'. md5(sesskey().microtime().$downloadfilename);
46         if (!$handle = fopen($tempfilename, 'w+b')) {
47             print_error('cannotcreatetempdir');
48         }
50         /// time stamp to ensure uniqueness of batch export
51         fwrite($handle,  '<results batch="xml_export_'.time().'">'."\n");
53         $export_buffer = array();
55         $geub = new grade_export_update_buffer();
56         $gui = new graded_users_iterator($this->course, $this->columns, $this->groupid);
57         $gui->init();
58         while ($userdata = $gui->next_user()) {
59             $user = $userdata->user;
61             if (empty($user->idnumber)) {
62                 //id number must exist otherwise we cant match up students when importing
63                 continue;
64             }
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 = $this->format_grade($grade); // no formating for now
72                 // MDL-11669, skip exported grades or bad grades (if setting says so)
73                 if ($export_tracking) {
74                     $status = $geub->track($grade);
75                     if ($this->updatedgradesonly && ($status == 'nochange' || $status == 'unknown')) {
76                         continue;
77                     }
78                 }
80                 fwrite($handle,  "\t<result>\n");
82                 if ($export_tracking) {
83                     fwrite($handle,  "\t\t<state>$status</state>\n");
84                 }
86                 // only need id number
87                 fwrite($handle,  "\t\t<assignment>{$grade_item->idnumber}</assignment>\n");
88                 // this column should be customizable to use either student id, idnumber, uesrname or email.
89                 fwrite($handle,  "\t\t<student>{$user->idnumber}</student>\n");
90                 fwrite($handle,  "\t\t<score>$gradestr</score>\n");
91                 if ($this->export_feedback) {
92                     $feedbackstr = $this->format_feedback($userdata->feedbacks[$itemid]);
93                     fwrite($handle,  "\t\t<feedback>$feedbackstr</feedback>\n");
94                 }
95                 fwrite($handle,  "\t</result>\n");
96             }
97         }
98         fwrite($handle,  "</results>");
99         fclose($handle);
100         $gui->close();
101         $geub->close();
103         @header("Content-type: text/xml; charset=UTF-8");
104         send_temp_file($tempfilename, $downloadfilename, false);
105     }