3 ///////////////////////////////////////////////////////////////////////////
5 // NOTICE OF COPYRIGHT //
7 // Moodle - Modular Object-Oriented Dynamic Learning Environment //
8 // http://moodle.com //
10 // Copyright (C) 1999 onwards Martin Dougiamas http://moodle.com //
12 // This program is free software; you can redistribute it and/or modify //
13 // it under the terms of the GNU General Public License as published by //
14 // the Free Software Foundation; either version 2 of the License, or //
15 // (at your option) any later version. //
17 // This program is distributed in the hope that it will be useful, //
18 // but WITHOUT ANY WARRANTY; without even the implied warranty of //
19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
20 // GNU General Public License for more details: //
22 // http://www.gnu.org/copyleft/gpl.html //
24 ///////////////////////////////////////////////////////////////////////////
26 require_once $CFG->libdir
.'/gradelib.php';
27 require_once($CFG->libdir
.'/xmlize.php');
28 require_once $CFG->dirroot
.'/grade/lib.php';
29 require_once $CFG->dirroot
.'/grade/import/lib.php';
31 function import_xml_grades($text, $course, &$error) {
34 $importcode = get_new_importcode();
38 $content = xmlize($text);
40 if (!empty($content['results']['#']['result'])) {
41 $results = $content['results']['#']['result'];
43 foreach ($results as $i => $result) {
44 $gradeidnumber = $result['#']['assignment'][0]['#'];
45 if (!$grade_items = grade_item
::fetch_all(array('idnumber'=>$gradeidnumber, 'courseid'=>$course->id
))) {
46 // gradeitem does not exist
47 // no data in temp table so far, abort
49 $error = get_string('errincorrectgradeidnumber', 'gradeimport_xml', $gradeidnumber);
51 } else if (count($grade_items) != 1) {
53 $error = get_string('errduplicategradeidnumber', 'gradeimport_xml', $gradeidnumber);
56 $grade_item = reset($grade_items);
59 // grade item locked, abort
60 if ($grade_item->is_locked()) {
62 $error = get_string('gradeitemlocked', 'grades');
66 // check if user exist and convert idnumber to user id
67 $useridnumber = $result['#']['student'][0]['#'];
68 if (!$user = get_record('user', 'idnumber', addslashes($useridnumber))) {
69 // no user found, abort
71 $error = get_string('errincorrectuseridnumber', 'gradeimport_xml', $useridnumber);
75 // check if grade_grade is locked and if so, abort
76 if ($grade_grade = new grade_grade(array('itemid'=>$grade_item->id
, 'userid'=>$user->id
))) {
77 $grade_grade->grade_item
=& $grade_item;
78 if ($grade_grade->is_locked()) {
79 // individual grade locked, abort
81 $error = get_string('gradelocked', 'grades');
86 $newgrade = new object();
87 $newgrade->itemid
= $grade_item->id
;
88 $newgrade->userid
= $user->id
;
89 $newgrade->importcode
= $importcode;
90 $newgrade->importer
= $USER->id
;
92 // check grade value exists and is a numeric grade
93 if (isset($result['#']['score'][0]['#'])) {
94 if (is_numeric($result['#']['score'][0]['#'])) {
95 $newgrade->finalgrade
= $result['#']['score'][0]['#'];
98 $error = get_string('badgrade', 'grades');
102 $newgrade->finalgrade
= NULL;
105 // check grade feedback exists
106 if (isset($result['#']['feedback'][0]['#'])) {
107 $newgrade->feedback
= $result['#']['feedback'][0]['#'];
109 $newgrade->feedback
= NULL;
112 // insert this grade into a temp table
113 if (!insert_record('grade_import_values', addslashes_recursive($newgrade))) {
115 // could not insert into temp table
116 $error = get_string('importfailed', 'grades');
122 // no results section found in xml,
123 // assuming bad format, abort import
125 $error = get_string('errbadxmlformat', 'gradeimport_xml');
132 import_cleanup($importcode);