|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 00003 // This file is part of Moodle - http://moodle.org/ 00004 // 00005 // Moodle is free software: you can redistribute it and/or modify 00006 // it under the terms of the GNU General Public License as published by 00007 // the Free Software Foundation, either version 3 of the License, or 00008 // (at your option) any later version. 00009 // 00010 // Moodle is distributed in the hope that it will be useful, 00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 // GNU General Public License for more details. 00014 // 00015 // You should have received a copy of the GNU General Public License 00016 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 00017 00018 require_once $CFG->libdir.'/gradelib.php'; 00019 require_once($CFG->libdir.'/xmlize.php'); 00020 require_once $CFG->dirroot.'/grade/lib.php'; 00021 require_once $CFG->dirroot.'/grade/import/lib.php'; 00022 00023 function import_xml_grades($text, $course, &$error) { 00024 global $USER, $DB; 00025 00026 $importcode = get_new_importcode(); 00027 00028 $status = true; 00029 00030 $content = xmlize($text); 00031 00032 if (!empty($content['results']['#']['result'])) { 00033 $results = $content['results']['#']['result']; 00034 00035 foreach ($results as $i => $result) { 00036 $gradeidnumber = $result['#']['assignment'][0]['#']; 00037 if (!$grade_items = grade_item::fetch_all(array('idnumber'=>$gradeidnumber, 'courseid'=>$course->id))) { 00038 // gradeitem does not exist 00039 // no data in temp table so far, abort 00040 $status = false; 00041 $error = get_string('errincorrectgradeidnumber', 'gradeimport_xml', $gradeidnumber); 00042 break; 00043 } else if (count($grade_items) != 1) { 00044 $status = false; 00045 $error = get_string('errduplicategradeidnumber', 'gradeimport_xml', $gradeidnumber); 00046 break; 00047 } else { 00048 $grade_item = reset($grade_items); 00049 } 00050 00051 // grade item locked, abort 00052 if ($grade_item->is_locked()) { 00053 $status = false; 00054 $error = get_string('gradeitemlocked', 'grades'); 00055 break; 00056 } 00057 00058 // check if user exist and convert idnumber to user id 00059 $useridnumber = $result['#']['student'][0]['#']; 00060 if (!$user = $DB->get_record('user', array('idnumber' =>$useridnumber))) { 00061 // no user found, abort 00062 $status = false; 00063 $error = get_string('errincorrectuseridnumber', 'gradeimport_xml', $useridnumber); 00064 break; 00065 } 00066 00067 // check if grade_grade is locked and if so, abort 00068 if ($grade_grade = new grade_grade(array('itemid'=>$grade_item->id, 'userid'=>$user->id))) { 00069 $grade_grade->grade_item =& $grade_item; 00070 if ($grade_grade->is_locked()) { 00071 // individual grade locked, abort 00072 $status = false; 00073 $error = get_string('gradelocked', 'grades'); 00074 break; 00075 } 00076 } 00077 00078 $newgrade = new stdClass(); 00079 $newgrade->itemid = $grade_item->id; 00080 $newgrade->userid = $user->id; 00081 $newgrade->importcode = $importcode; 00082 $newgrade->importer = $USER->id; 00083 00084 // check grade value exists and is a numeric grade 00085 if (isset($result['#']['score'][0]['#'])) { 00086 if (is_numeric($result['#']['score'][0]['#'])) { 00087 $newgrade->finalgrade = $result['#']['score'][0]['#']; 00088 } else { 00089 $status = false; 00090 $error = get_string('badgrade', 'grades'); 00091 break; 00092 } 00093 } else { 00094 $newgrade->finalgrade = NULL; 00095 } 00096 00097 // check grade feedback exists 00098 if (isset($result['#']['feedback'][0]['#'])) { 00099 $newgrade->feedback = $result['#']['feedback'][0]['#']; 00100 } else { 00101 $newgrade->feedback = NULL; 00102 } 00103 00104 // insert this grade into a temp table 00105 $DB->insert_record('grade_import_values', $newgrade); 00106 } 00107 00108 } else { 00109 // no results section found in xml, 00110 // assuming bad format, abort import 00111 $status = false; 00112 $error = get_string('errbadxmlformat', 'gradeimport_xml'); 00113 } 00114 00115 if ($status) { 00116 return $importcode; 00117 00118 } else { 00119 import_cleanup($importcode); 00120 return false; 00121 } 00122 } 00123