3 // This file is part of Moodle - http://moodle.org/
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.
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.
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->libdir
.'/gradelib.php';
19 require_once($CFG->libdir
.'/xmlize.php');
20 require_once $CFG->dirroot
.'/grade/lib.php';
21 require_once $CFG->dirroot
.'/grade/import/lib.php';
23 function import_xml_grades($text, $course, &$error) {
26 $importcode = get_new_importcode();
30 $content = xmlize($text);
32 if (!empty($content['results']['#']['result'])) {
33 $results = $content['results']['#']['result'];
35 foreach ($results as $i => $result) {
36 $gradeidnumber = $result['#']['assignment'][0]['#'];
37 if (!$grade_items = grade_item
::fetch_all(array('idnumber'=>$gradeidnumber, 'courseid'=>$course->id
))) {
38 // gradeitem does not exist
39 // no data in temp table so far, abort
41 $error = get_string('errincorrectgradeidnumber', 'gradeimport_xml', $gradeidnumber);
43 } else if (count($grade_items) != 1) {
45 $error = get_string('errduplicategradeidnumber', 'gradeimport_xml', $gradeidnumber);
48 $grade_item = reset($grade_items);
51 // grade item locked, abort
52 if ($grade_item->is_locked()) {
54 $error = get_string('gradeitemlocked', 'grades');
58 // check if user exist and convert idnumber to user id
59 $useridnumber = $result['#']['student'][0]['#'];
60 if (!$user = $DB->get_record('user', array('idnumber' =>$useridnumber))) {
61 // no user found, abort
63 $error = get_string('errincorrectuseridnumber', 'gradeimport_xml', $useridnumber);
67 // check if grade_grade is locked and if so, abort
68 if ($grade_grade = new grade_grade(array('itemid'=>$grade_item->id
, 'userid'=>$user->id
))) {
69 $grade_grade->grade_item
=& $grade_item;
70 if ($grade_grade->is_locked()) {
71 // individual grade locked, abort
73 $error = get_string('gradelocked', 'grades');
78 $newgrade = new stdClass();
79 $newgrade->itemid
= $grade_item->id
;
80 $newgrade->userid
= $user->id
;
81 $newgrade->importcode
= $importcode;
82 $newgrade->importer
= $USER->id
;
84 // check grade value exists and is a numeric grade
85 if (isset($result['#']['score'][0]['#']) && $result['#']['score'][0]['#'] !== '-') {
86 if (is_numeric($result['#']['score'][0]['#'])) {
87 $newgrade->finalgrade
= $result['#']['score'][0]['#'];
90 $error = get_string('badgrade', 'grades');
94 $newgrade->finalgrade
= NULL;
97 // check grade feedback exists
98 if (isset($result['#']['feedback'][0]['#'])) {
99 $newgrade->feedback
= $result['#']['feedback'][0]['#'];
101 $newgrade->feedback
= NULL;
104 // insert this grade into a temp table
105 $DB->insert_record('grade_import_values', $newgrade);
109 // no results section found in xml,
110 // assuming bad format, abort import
112 $error = get_string('errbadxmlformat', 'gradeimport_xml');
119 import_cleanup($importcode);