Merge branch 'MDL-78974-402' of https://github.com/paulholden/moodle into MOODLE_402_...
[moodle.git] / grade / import / xml / lib.php
blob3984cca059f556cf94c5c28da00777196273cd23
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.
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) {
24 global $USER, $DB;
26 $importcode = get_new_importcode();
28 $status = true;
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
40 $status = false;
41 $error = get_string('errincorrectgradeidnumber', 'gradeimport_xml', $gradeidnumber);
42 break;
43 } else if (count($grade_items) != 1) {
44 $status = false;
45 $error = get_string('errduplicategradeidnumber', 'gradeimport_xml', $gradeidnumber);
46 break;
47 } else {
48 $grade_item = reset($grade_items);
51 // grade item locked, abort
52 if ($grade_item->is_locked()) {
53 $status = false;
54 $error = get_string('gradeitemlocked', 'grades');
55 break;
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
62 $status = false;
63 $error = get_string('errincorrectuseridnumber', 'gradeimport_xml', $useridnumber);
64 break;
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
72 $status = false;
73 $error = get_string('gradelocked', 'grades');
74 break;
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]['#'];
88 } else {
89 $status = false;
90 $error = get_string('badgrade', 'grades');
91 break;
93 } else {
94 $newgrade->finalgrade = NULL;
97 // check grade feedback exists
98 if (isset($result['#']['feedback'][0]['#'])) {
99 $newgrade->feedback = $result['#']['feedback'][0]['#'];
100 } else {
101 $newgrade->feedback = NULL;
104 // insert this grade into a temp table
105 $DB->insert_record('grade_import_values', $newgrade);
108 } else {
109 // no results section found in xml,
110 // assuming bad format, abort import
111 $status = false;
112 $error = get_string('errbadxmlformat', 'gradeimport_xml');
115 if ($status) {
116 return $importcode;
118 } else {
119 import_cleanup($importcode);
120 return false;