source: moodle/trunk/fuentes/grade/export/xml/grade_export_xml.php @ 1331

Last change on this file since 1331 was 1331, checked in by jrpelegrina, 3 years ago

Updated to moodle 3.0.3

File size: 5.1 KB
Line 
1<?php
2
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/>.
17
18require_once($CFG->dirroot.'/grade/export/lib.php');
19require_once($CFG->libdir.'/filelib.php');
20
21class grade_export_xml extends grade_export {
22
23    public $plugin = 'xml';
24    public $updatedgradesonly = false; // default to export ALL grades
25
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');
35
36        $export_tracking = $this->track_exports();
37
38        $strgrades = get_string('grades');
39
40        /// Calculate file name
41        $shortname = format_string($this->course->shortname, true, array('context' => context_course::instance($this->course->id)));
42        $downloadfilename = clean_filename("$shortname $strgrades.xml");
43
44        make_temp_directory('gradeexport');
45        $tempfilename = $CFG->tempdir .'/gradeexport/'. md5(sesskey().microtime().$downloadfilename);
46        if (!$handle = fopen($tempfilename, 'w+b')) {
47            print_error('cannotcreatetempdir');
48        }
49
50        /// time stamp to ensure uniqueness of batch export
51        fwrite($handle,  '<results batch="xml_export_'.time().'">'."\n");
52
53        $export_buffer = array();
54
55        $geub = new grade_export_update_buffer();
56        $gui = new graded_users_iterator($this->course, $this->columns, $this->groupid);
57        $gui->require_active_enrolment($this->onlyactive);
58        $gui->init();
59        while ($userdata = $gui->next_user()) {
60            $user = $userdata->user;
61
62            if (empty($user->idnumber)) {
63                //id number must exist otherwise we cant match up students when importing
64                continue;
65            }
66
67            // studentgrades[] index should match with corresponding $index
68            foreach ($userdata->grades as $itemid => $grade) {
69                $grade_item = $this->grade_items[$itemid];
70                $grade->grade_item =& $grade_item;
71
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                }
79
80                fwrite($handle,  "\t<result>\n");
81
82                if ($export_tracking) {
83                    fwrite($handle,  "\t\t<state>$status</state>\n");
84                }
85
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                // Format and display the grade in the selected display type (real, letter, percentage).
91                if (is_array($this->displaytype)) {
92                    // Grades display type came from the return of export_bulk_export_data() on grade publishing.
93                    foreach ($this->displaytype as $gradedisplayconst) {
94                        $gradestr = $this->format_grade($grade, $gradedisplayconst);
95                        fwrite($handle,  "\t\t<score>$gradestr</score>\n");
96                    }
97                } else {
98                    // Grade display type submitted directly from the grade export form.
99                    $gradestr = $this->format_grade($grade, $this->displaytype);
100                    fwrite($handle,  "\t\t<score>$gradestr</score>\n");
101                }
102
103                if ($this->export_feedback) {
104                    $feedbackstr = $this->format_feedback($userdata->feedbacks[$itemid]);
105                    fwrite($handle,  "\t\t<feedback>$feedbackstr</feedback>\n");
106                }
107                fwrite($handle,  "\t</result>\n");
108            }
109        }
110        fwrite($handle,  "</results>");
111        fclose($handle);
112        $gui->close();
113        $geub->close();
114
115        if (defined('BEHAT_SITE_RUNNING')) {
116            // If behat is running, we cannot test the output if we force a file download.
117            include($tempfilename);
118        } else {
119            @header("Content-type: text/xml; charset=UTF-8");
120            send_temp_file($tempfilename, $downloadfilename, false);
121        }
122    }
123}
124
125
Note: See TracBrowser for help on using the repository browser.