2007-09-26 18:11:48 +00:00
|
|
|
<?php //$Id$
|
|
|
|
|
2007-10-10 06:34:20 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// //
|
|
|
|
// NOTICE OF COPYRIGHT //
|
|
|
|
// //
|
|
|
|
// Moodle - Modular Object-Oriented Dynamic Learning Environment //
|
|
|
|
// http://moodle.com //
|
|
|
|
// //
|
|
|
|
// Copyright (C) 1999 onwards Martin Dougiamas http://moodle.com //
|
|
|
|
// //
|
|
|
|
// This program is free software; you can redistribute it and/or modify //
|
|
|
|
// it under the terms of the GNU General Public License as published by //
|
|
|
|
// the Free Software Foundation; either version 2 of the License, or //
|
|
|
|
// (at your option) any later version. //
|
|
|
|
// //
|
|
|
|
// This program is distributed in the hope that it will be useful, //
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
|
|
|
|
// GNU General Public License for more details: //
|
|
|
|
// //
|
|
|
|
// http://www.gnu.org/copyleft/gpl.html //
|
|
|
|
// //
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2007-09-26 18:11:48 +00:00
|
|
|
require_once $CFG->libdir.'/gradelib.php';
|
|
|
|
require_once($CFG->libdir.'/xmlize.php');
|
|
|
|
require_once $CFG->dirroot.'/grade/lib.php';
|
|
|
|
require_once $CFG->dirroot.'/grade/import/lib.php';
|
|
|
|
|
|
|
|
function import_xml_grades($text, $course, &$error) {
|
2007-10-07 13:04:49 +00:00
|
|
|
global $USER;
|
|
|
|
|
|
|
|
$importcode = get_new_importcode();
|
2007-09-26 18:11:48 +00:00
|
|
|
|
|
|
|
$status = true;
|
|
|
|
|
|
|
|
$content = xmlize($text);
|
2007-10-07 10:22:21 +00:00
|
|
|
|
2008-01-10 14:17:37 +00:00
|
|
|
if (!empty($content['results']['#']['result'])) {
|
|
|
|
$results = $content['results']['#']['result'];
|
2007-10-07 10:22:21 +00:00
|
|
|
|
2007-09-26 18:11:48 +00:00
|
|
|
foreach ($results as $i => $result) {
|
2008-01-10 15:38:48 +00:00
|
|
|
$gradeidnumber = $result['#']['assignment'][0]['#'];
|
|
|
|
if (!$grade_items = grade_item::fetch_all(array('idnumber'=>$gradeidnumber, 'courseid'=>$course->id))) {
|
2007-09-26 18:11:48 +00:00
|
|
|
// gradeitem does not exist
|
|
|
|
// no data in temp table so far, abort
|
|
|
|
$status = false;
|
2008-01-10 15:38:48 +00:00
|
|
|
$error = get_string('errincorrectgradeidnumber', 'gradeimport_xml', $gradeidnumber);
|
2007-09-26 18:11:48 +00:00
|
|
|
break;
|
|
|
|
} else if (count($grade_items) != 1) {
|
|
|
|
$status = false;
|
2008-01-10 15:38:48 +00:00
|
|
|
$error = get_string('errduplicategradeidnumber', 'gradeimport_xml', $gradeidnumber);
|
2007-09-26 18:11:48 +00:00
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
$grade_item = reset($grade_items);
|
|
|
|
}
|
2007-10-07 10:22:21 +00:00
|
|
|
|
2007-09-26 18:11:48 +00:00
|
|
|
// grade item locked, abort
|
2007-10-07 10:22:21 +00:00
|
|
|
if ($grade_item->is_locked()) {
|
2007-09-26 18:11:48 +00:00
|
|
|
$status = false;
|
|
|
|
$error = get_string('gradeitemlocked', 'grades');
|
|
|
|
break;
|
|
|
|
}
|
2007-10-07 10:22:21 +00:00
|
|
|
|
2007-11-26 04:10:08 +00:00
|
|
|
// check if user exist and convert idnumber to user id
|
2008-01-10 15:38:48 +00:00
|
|
|
$useridnumber = $result['#']['student'][0]['#'];
|
|
|
|
if (!$user = get_record('user', 'idnumber', addslashes($useridnumber))) {
|
2007-10-07 10:22:21 +00:00
|
|
|
// no user found, abort
|
|
|
|
$status = false;
|
2008-01-10 15:38:48 +00:00
|
|
|
$error = get_string('errincorrectuseridnumber', 'gradeimport_xml', $useridnumber);
|
2007-10-07 10:22:21 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2007-09-26 18:11:48 +00:00
|
|
|
// check if grade_grade is locked and if so, abort
|
2007-10-07 10:22:21 +00:00
|
|
|
if ($grade_grade = new grade_grade(array('itemid'=>$grade_item->id, 'userid'=>$user->id))) {
|
2007-10-07 13:04:49 +00:00
|
|
|
$grade_grade->grade_item =& $grade_item;
|
2007-10-07 10:22:21 +00:00
|
|
|
if ($grade_grade->is_locked()) {
|
2007-09-26 18:11:48 +00:00
|
|
|
// individual grade locked, abort
|
|
|
|
$status = false;
|
|
|
|
$error = get_string('gradegradeslocked', 'grades');
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2007-10-07 10:22:21 +00:00
|
|
|
|
|
|
|
$newgrade = new object();
|
2007-10-07 13:04:49 +00:00
|
|
|
$newgrade->itemid = $grade_item->id;
|
|
|
|
$newgrade->userid = $user->id;
|
|
|
|
$newgrade->importcode = $importcode;
|
|
|
|
$newgrade->importer = $USER->id;
|
2007-10-07 10:22:21 +00:00
|
|
|
|
|
|
|
// check grade value exists and is a numeric grade
|
2007-09-26 18:11:48 +00:00
|
|
|
if (isset($result['#']['score'][0]['#'])) {
|
2007-10-07 10:22:21 +00:00
|
|
|
if (is_numeric($result['#']['score'][0]['#'])) {
|
|
|
|
$newgrade->finalgrade = $result['#']['score'][0]['#'];
|
|
|
|
} else {
|
2007-09-26 18:11:48 +00:00
|
|
|
$status = false;
|
|
|
|
$error = get_string('badgrade', 'grades');
|
|
|
|
break;
|
|
|
|
}
|
2007-10-07 10:22:21 +00:00
|
|
|
} else {
|
|
|
|
$newgrade->finalgrade = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// check grade feedback exists
|
|
|
|
if (isset($result['#']['feedback'][0]['#'])) {
|
|
|
|
$newgrade->feedback = $result['#']['feedback'][0]['#'];
|
|
|
|
} else {
|
|
|
|
$newgrade->feedback = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// insert this grade into a temp table
|
|
|
|
if (!insert_record('grade_import_values', addslashes_recursive($newgrade))) {
|
|
|
|
$status = false;
|
|
|
|
// could not insert into temp table
|
|
|
|
$error = get_string('importfailed', 'grades');
|
|
|
|
break;
|
2007-09-26 18:11:48 +00:00
|
|
|
}
|
|
|
|
}
|
2007-10-07 10:22:21 +00:00
|
|
|
|
2007-09-26 18:11:48 +00:00
|
|
|
} else {
|
|
|
|
// no results section found in xml,
|
|
|
|
// assuming bad format, abort import
|
|
|
|
$status = false;
|
|
|
|
$error = get_string('errbadxmlformat', 'gradeimport_xml');
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($status) {
|
|
|
|
return $importcode;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
import_cleanup($importcode);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2007-10-10 06:34:20 +00:00
|
|
|
?>
|