Merge commit 'catalyst/MOODLE_19_STABLE' into mdl19-linuxchix
[moodle-linuxchix.git] / grade / import / xml / lib.php
blob7bf60f6106799dc81e45d6aa5b28f895171aed1b
1 <?php //$Id$
3 ///////////////////////////////////////////////////////////////////////////
4 // //
5 // NOTICE OF COPYRIGHT //
6 // //
7 // Moodle - Modular Object-Oriented Dynamic Learning Environment //
8 // http://moodle.com //
9 // //
10 // Copyright (C) 1999 onwards Martin Dougiamas http://moodle.com //
11 // //
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. //
16 // //
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: //
21 // //
22 // http://www.gnu.org/copyleft/gpl.html //
23 // //
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) {
32 global $USER;
34 $importcode = get_new_importcode();
36 $status = true;
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
48 $status = false;
49 $error = get_string('errincorrectgradeidnumber', 'gradeimport_xml', $gradeidnumber);
50 break;
51 } else if (count($grade_items) != 1) {
52 $status = false;
53 $error = get_string('errduplicategradeidnumber', 'gradeimport_xml', $gradeidnumber);
54 break;
55 } else {
56 $grade_item = reset($grade_items);
59 // grade item locked, abort
60 if ($grade_item->is_locked()) {
61 $status = false;
62 $error = get_string('gradeitemlocked', 'grades');
63 break;
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
70 $status = false;
71 $error = get_string('errincorrectuseridnumber', 'gradeimport_xml', $useridnumber);
72 break;
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
80 $status = false;
81 $error = get_string('gradelocked', 'grades');
82 break;
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]['#'];
96 } else {
97 $status = false;
98 $error = get_string('badgrade', 'grades');
99 break;
101 } else {
102 $newgrade->finalgrade = NULL;
105 // check grade feedback exists
106 if (isset($result['#']['feedback'][0]['#'])) {
107 $newgrade->feedback = $result['#']['feedback'][0]['#'];
108 } else {
109 $newgrade->feedback = NULL;
112 // insert this grade into a temp table
113 if (!insert_record('grade_import_values', addslashes_recursive($newgrade))) {
114 $status = false;
115 // could not insert into temp table
116 $error = get_string('importfailed', 'grades');
117 break;
121 } else {
122 // no results section found in xml,
123 // assuming bad format, abort import
124 $status = false;
125 $error = get_string('errbadxmlformat', 'gradeimport_xml');
128 if ($status) {
129 return $importcode;
131 } else {
132 import_cleanup($importcode);
133 return false;