mirror of
https://github.com/moodle/moodle.git
synced 2025-01-18 22:08:20 +01:00
MDL-10985 merged the xml and xmlurl grade import plug-in
This commit is contained in:
parent
c6f9234ce3
commit
1843999a77
@ -7,15 +7,20 @@ class grade_import_form extends moodleform {
|
||||
|
||||
$mform =& $this->_form;
|
||||
|
||||
$strrequired = get_string('required');
|
||||
$this->set_upload_manager(new upload_manager('userfile', false, false, null, false, 0, true, true, false));
|
||||
|
||||
// course id needs to be passed for auth purposes
|
||||
$mform->addElement('hidden', 'id', optional_param('id'));
|
||||
$mform->setType('id', PARAM_INT);
|
||||
$mform->addElement('header', 'general', get_string('importfile', 'grades'));
|
||||
$mform->disabledIf('url', 'userfile', 'noteq', '');
|
||||
|
||||
// file upload
|
||||
$mform->addElement('text', 'url', get_string('importfile', 'grades'), 'size="80"');
|
||||
$mform->addRule('url', $strrequired, 'required', null, 'client');
|
||||
$mform->addElement('file', 'userfile', get_string('file'));
|
||||
$mform->setType('userfile', PARAM_FILE);
|
||||
$mform->disabledIf('userfile', 'url', 'noteq', '');
|
||||
|
||||
$mform->addElement('text', 'url', get_string('fileurl', 'gradeimport_xml'), 'size="80"');
|
||||
|
||||
if (!empty($CFG->gradepublishing)) {
|
||||
$mform->addElement('header', 'publishing', get_string('publishing', 'grades'));
|
||||
@ -38,8 +43,13 @@ class grade_import_form extends moodleform {
|
||||
$mform->addElement('date_time_selector', 'validuntil', get_string('keyvaliduntil', 'userkey'), array('optional'=>true));
|
||||
$mform->setHelpButton('validuntil', array(false, get_string('keyvaliduntil', 'userkey'),
|
||||
false, true, false, get_string("keyvaliduntilhelp", 'userkey')));
|
||||
$mform->disabledIf('iprestriction', 'key', get_string('createnewkey', 'userkey'));
|
||||
$mform->disabledIf('validuntil', 'key', get_string('createnewkey', 'userkey'));
|
||||
|
||||
$mform->disabledIf('iprestriction', 'key', 'noteq', 1);
|
||||
$mform->disabledIf('validuntil', 'key', 'noteq', 1);
|
||||
|
||||
$mform->disabledIf('iprestriction', 'url', 'eq', '');
|
||||
$mform->disabledIf('validuntil', 'url', 'eq', '');
|
||||
$mform->disabledIf('key', 'url', 'eq', '');
|
||||
}
|
||||
|
||||
$this->add_action_buttons(false, get_string('uploadgrades', 'grades'));
|
||||
@ -48,7 +58,9 @@ class grade_import_form extends moodleform {
|
||||
function validation($data) {
|
||||
$err = array();
|
||||
|
||||
if ($data['url'] != clean_param($data['url'], PARAM_URL)) {
|
||||
$strrequired = get_string('required');
|
||||
|
||||
if (array_key_exists('url', $data) and $data['url'] != clean_param($data['url'], PARAM_URL)) {
|
||||
$err['url'] = get_string('error');
|
||||
}
|
||||
|
72
grade/import/xmlurl/index.php → grade/import/xml/import.php
Executable file → Normal file
72
grade/import/xmlurl/index.php → grade/import/xml/import.php
Executable file → Normal file
@ -24,11 +24,11 @@
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
|
||||
require_once '../../../config.php';
|
||||
require_once $CFG->dirroot.'/grade/lib.php';
|
||||
require_once 'grade_import_form.php';
|
||||
require_once '../lib.php';
|
||||
require_once 'lib.php';
|
||||
require_once $CFG->libdir.'/filelib.php';
|
||||
|
||||
$id = required_param('id', PARAM_INT); // course id
|
||||
$url = required_param('url', PARAM_URL); // only real urls here
|
||||
$id = required_param('id', PARAM_INT); // course id
|
||||
|
||||
if (!$course = get_record('course', 'id', $id)) {
|
||||
print_error('nocourseid');
|
||||
@ -36,43 +36,55 @@ if (!$course = get_record('course', 'id', $id)) {
|
||||
|
||||
require_login($course);
|
||||
$context = get_context_instance(CONTEXT_COURSE, $id);
|
||||
|
||||
require_capability('moodle/grade:import', $context);
|
||||
require_capability('gradeimport/xml_getch:view', $context);
|
||||
require_capability('gradeimport/xml:view', $context);
|
||||
|
||||
// print header
|
||||
$strgrades = get_string('grades', 'grades');
|
||||
$actionstr = get_string('modulename', 'gradeimport_xmlurl');
|
||||
$navigation = grade_build_nav(__FILE__, $actionstr, array('courseid' => $course->id));
|
||||
|
||||
$mform = new grade_import_form();
|
||||
// Large files are likely to take their time and memory. Let PHP know
|
||||
// that we'll take longer, and that the process should be recycled soon
|
||||
// to free up memory.
|
||||
@set_time_limit(0);
|
||||
@raise_memory_limit("256M");
|
||||
if (function_exists('apache_child_terminate')) {
|
||||
@apache_child_terminate();
|
||||
}
|
||||
|
||||
if ($data = $mform->get_data()) {
|
||||
$text = download_file_content($url);
|
||||
if ($text === false) {
|
||||
error('Can not read file');
|
||||
}
|
||||
|
||||
if (empty($data->key)) {
|
||||
redirect('import.php?id='.$id.'&url='.urlencode($data->url));
|
||||
$error = '';
|
||||
$importcode = import_xml_grades($text, $course, $error);
|
||||
|
||||
} else {
|
||||
if ($data->key == 1) {
|
||||
$data->key = create_user_key('grade/import', $USER->id, $course->id, $data->iprestriction, $data->validuntil);
|
||||
if ($importcode !== false) {
|
||||
/// comit the code if we are up this far
|
||||
|
||||
if (defined('USER_KEY_LOGIN')) {
|
||||
if (grade_import_commit($id, $importcode, false)) {
|
||||
echo 'ok';
|
||||
die;
|
||||
} else {
|
||||
error('Grade import error'); //TODO: localize
|
||||
}
|
||||
|
||||
print_header($course->shortname.': '.get_string('grades'), $course->fullname, $navigation);
|
||||
print_grade_plugin_selector($id, 'import', 'xmlurl');
|
||||
} else {
|
||||
$strgrades = get_string('grades', 'grades');
|
||||
$actionstr = get_string('xml', 'grades');
|
||||
$navigation = grade_build_nav(__FILE__, $actionstr, array('courseid' => $course->id));
|
||||
|
||||
print_header($course->shortname.': '.get_string('grades'), $course->fullname, $navigation);
|
||||
print_grade_plugin_selector($id, 'import', 'xml');
|
||||
|
||||
grade_import_commit($id, $importcode);
|
||||
|
||||
echo '<div class="gradeexportlink">';
|
||||
$link = $CFG->wwwroot.'/grade/import/xmlurl/fetch.php?id='.$id.'&url='.urlencode($data->url).'&key='.$data->key;
|
||||
echo get_string('import', 'grades').': <a href="'.$link.'">'.$link.'</a>';
|
||||
echo '</div>';
|
||||
print_footer();
|
||||
die;
|
||||
}
|
||||
|
||||
} else {
|
||||
error($error);
|
||||
}
|
||||
|
||||
print_header($course->shortname.': '.get_string('grades'), $course->fullname, $navigation);
|
||||
print_grade_plugin_selector($id, 'import', 'xmlurl');
|
||||
|
||||
$mform->display();
|
||||
|
||||
print_footer();
|
||||
|
||||
?>
|
||||
?>
|
@ -1,15 +1,31 @@
|
||||
<?php //$Id$
|
||||
|
||||
/**
|
||||
* code in development
|
||||
* does xml plugin need some flexibility/mapping of columns?
|
||||
*/
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
// //
|
||||
// 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 //
|
||||
// //
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
|
||||
require_once '../../../config.php';
|
||||
require_once $CFG->libdir.'/gradelib.php';
|
||||
require_once $CFG->dirroot.'/grade/lib.php';
|
||||
require_once '../grade_import_form.php';
|
||||
require_once '../lib.php';
|
||||
require_once 'lib.php';
|
||||
require_once 'grade_import_form.php';
|
||||
|
||||
$id = required_param('id', PARAM_INT); // course id
|
||||
|
||||
@ -22,151 +38,65 @@ $context = get_context_instance(CONTEXT_COURSE, $id);
|
||||
require_capability('moodle/grade:import', $context);
|
||||
require_capability('gradeimport/xml:view', $context);
|
||||
|
||||
|
||||
// print header
|
||||
$strgrades = get_string('grades', 'grades');
|
||||
$actionstr = get_string('xml', 'grades');
|
||||
$actionstr = get_string('modulename', 'gradeimport_xml');
|
||||
$navigation = grade_build_nav(__FILE__, $actionstr, array('courseid' => $course->id));
|
||||
|
||||
print_header($course->shortname.': '.get_string('grades'), $course->fullname, $navigation);
|
||||
print_grade_plugin_selector($id, 'import', 'xml');
|
||||
$mform = new grade_import_form();
|
||||
|
||||
if ( $formdata = $mform->get_data()) {
|
||||
|
||||
// array to hold all grades to be inserted
|
||||
$newgrades = array();
|
||||
|
||||
$filename = $mform->get_userfile_name();
|
||||
if ($data = $mform->get_data()) {
|
||||
// Large files are likely to take their time and memory. Let PHP know
|
||||
// that we'll take longer, and that the process should be recycled soon
|
||||
// to free up memory.
|
||||
@set_time_limit(0);
|
||||
@raise_memory_limit("192M");
|
||||
@raise_memory_limit("256M");
|
||||
if (function_exists('apache_child_terminate')) {
|
||||
@apache_child_terminate();
|
||||
}
|
||||
|
||||
$text = my_file_get_contents($filename);
|
||||
if ($text = $mform->get_file_content('userfile')) {
|
||||
print_header($course->shortname.': '.get_string('grades'), $course->fullname, $navigation);
|
||||
print_grade_plugin_selector($id, 'import', 'xml');
|
||||
|
||||
// trim utf-8 bom
|
||||
$textlib = textlib_get_instance();
|
||||
// converts to propert unicode
|
||||
$text = $textlib->convert($text, $formdata->encoding);
|
||||
$text = $textlib->trim_utf8_bom($text);
|
||||
// Fix mac/dos newlines
|
||||
$text = preg_replace('!\r\n?!',"\n",$text);
|
||||
|
||||
// text is the text, we should xmlize it
|
||||
include_once($CFG->dirroot.'/lib/xmlize.php');
|
||||
$content = xmlize($text);
|
||||
|
||||
if ($results = $content['results']['#']['result']) {
|
||||
|
||||
// import batch identifier timestamp
|
||||
$importcode = time();
|
||||
$status = true;
|
||||
|
||||
$numlines = 0;
|
||||
|
||||
// print some previews
|
||||
print_heading(get_string('importpreview', 'grades'));
|
||||
|
||||
echo '<table cellpadding="5">';
|
||||
foreach ($results as $i => $result) {
|
||||
if ($numlines < $formdata->previewrows && isset($results[$i+1])) {
|
||||
echo '<tr>';
|
||||
foreach ($result['#'] as $fieldname => $val) {
|
||||
echo '<td>'.$fieldname.' > '.$val[0]['#'].'</td>';
|
||||
}
|
||||
echo '</tr>';
|
||||
$numlines ++;
|
||||
} else if ($numlines == $formdata->previewrows || !isset($results[$i+1])) {
|
||||
echo '</table>';
|
||||
$numlines ++;
|
||||
}
|
||||
|
||||
if (!$gradeitem = new grade_item(array('idnumber'=>$result['#']['assignment'][0]['#'], 'courseid'=>$course->id))) {
|
||||
// gradeitem does not exist
|
||||
// no data in temp table so far, abort
|
||||
$status = false;
|
||||
break;
|
||||
}
|
||||
|
||||
// grade item locked, abort
|
||||
if ($gradeitem->locked) {
|
||||
$status = false;
|
||||
notify(get_string('gradeitemlocked', 'grades'));
|
||||
break 3;
|
||||
}
|
||||
|
||||
// check if grade_grade is locked and if so, abort
|
||||
if ($grade_grade = new grade_grade(array('itemid'=>$gradeitem->id, 'userid'=>$result['#']['student'][0]['#']))) {
|
||||
if ($grade_grade->locked) {
|
||||
// individual grade locked, abort
|
||||
$status = false;
|
||||
notify(get_string('gradegradeslocked', 'grades'));
|
||||
break 2;
|
||||
}
|
||||
}
|
||||
unset($newgrade);
|
||||
|
||||
if (isset($result['#']['score'][0]['#'])) {
|
||||
$newgrade -> itemid = $gradeitem->id;
|
||||
$newgrade -> rawgrade = $result['#']['score'][0]['#'];
|
||||
$newgrade-> userid = $result['#']['student'][0]['#'];
|
||||
$newgrades[] = $newgrade;
|
||||
}
|
||||
}
|
||||
|
||||
// loop through info collected so far
|
||||
if ($status && !empty($newgrades)) {
|
||||
foreach ($newgrades as $newgrade) {
|
||||
|
||||
// check if user exist
|
||||
if (!$user = get_record('user', 'id', addslashes($newgrade->userid))) {
|
||||
// no user found, abort
|
||||
$status = false;
|
||||
import_cleanup($importcode);
|
||||
notify(get_string('baduserid', 'grades'));
|
||||
notify(get_string('importfailed', 'grades'));
|
||||
break;
|
||||
}
|
||||
|
||||
// check grade value is a numeric grade
|
||||
if (!is_numeric($newgrade->rawgrade)) {
|
||||
$status = false;
|
||||
import_cleanup($importcode);
|
||||
notify(get_string('badgrade', 'grades'));
|
||||
break;
|
||||
}
|
||||
|
||||
// insert this grade into a temp table
|
||||
$newgrade->import_code = $importcode;
|
||||
if (!insert_record('grade_import_values', addslashes_recursive($newgrade))) {
|
||||
$status = false;
|
||||
// could not insert into temp table
|
||||
import_cleanup($importcode);
|
||||
notify(get_string('importfailed', 'grades'));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// if all ok, we proceed
|
||||
if ($status) {
|
||||
/// comit the code if we are up this far
|
||||
$error = '';
|
||||
$importcode = import_xml_grades($text, $course, $error);
|
||||
if ($importcode) {
|
||||
grade_import_commit($id, $importcode);
|
||||
print_footer();
|
||||
die;
|
||||
} else {
|
||||
notify($error);
|
||||
print_continue($CFG->wwwroot.'/grade/index.php?id='.$course->id);
|
||||
print_footer();
|
||||
die;
|
||||
}
|
||||
|
||||
} else if (empty($data->key)) {
|
||||
redirect('import.php?id='.$id.'&url='.urlencode($data->url));
|
||||
|
||||
} else {
|
||||
// no results section found in xml,
|
||||
// assuming bad format, abort import
|
||||
notify('badxmlformat', 'grade');
|
||||
if ($data->key == 1) {
|
||||
$data->key = create_user_key('grade/import', $USER->id, $course->id, $data->iprestriction, $data->validuntil);
|
||||
}
|
||||
|
||||
print_header($course->shortname.': '.get_string('grades'), $course->fullname, $navigation);
|
||||
print_grade_plugin_selector($id, 'import', 'xml');
|
||||
|
||||
echo '<div class="gradeexportlink">';
|
||||
$link = $CFG->wwwroot.'/grade/import/xml/fetch.php?id='.$id.'&url='.urlencode($data->url).'&key='.$data->key;
|
||||
echo get_string('import', 'grades').': <a href="'.$link.'">'.$link.'</a>';
|
||||
echo '</div>';
|
||||
print_footer();
|
||||
die;
|
||||
}
|
||||
} else {
|
||||
// display the standard upload file form
|
||||
$mform->display();
|
||||
}
|
||||
|
||||
print_header($course->shortname.': '.get_string('grades'), $course->fullname, $navigation);
|
||||
print_grade_plugin_selector($id, 'import', 'xml');
|
||||
|
||||
$mform->display();
|
||||
|
||||
print_footer();
|
||||
|
||||
?>
|
||||
|
103
grade/import/xml/lib.php
Normal file
103
grade/import/xml/lib.php
Normal file
@ -0,0 +1,103 @@
|
||||
<?php //$Id$
|
||||
|
||||
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) {
|
||||
$importcode = time(); //TODO: fix predictable+colliding import code!
|
||||
$newgrades = array();
|
||||
|
||||
$status = true;
|
||||
|
||||
$content = xmlize($text);
|
||||
|
||||
if ($results = $content['results']['#']['result']) {
|
||||
|
||||
foreach ($results as $i => $result) {
|
||||
if (!$grade_items = grade_item::fetch_all(array('idnumber'=>$result['#']['assignment'][0]['#'], 'courseid'=>$course->id))) {
|
||||
// gradeitem does not exist
|
||||
// no data in temp table so far, abort
|
||||
$status = false;
|
||||
$error = get_string('errincorrectidnumber', 'gradeimport_xml');
|
||||
break;
|
||||
} else if (count($grade_items) != 1) {
|
||||
$status = false;
|
||||
$error = get_string('errduplicateidnumber', 'gradeimport_xml');
|
||||
break;
|
||||
} else {
|
||||
$grade_item = reset($grade_items);
|
||||
}
|
||||
|
||||
// grade item locked, abort
|
||||
if ($grade_item->locked) {
|
||||
$status = false;
|
||||
$error = get_string('gradeitemlocked', 'grades');
|
||||
break;
|
||||
}
|
||||
|
||||
// check if grade_grade is locked and if so, abort
|
||||
if ($grade_grade = new grade_grade(array('itemid'=>$grade_item->id, 'userid'=>$result['#']['student'][0]['#']))) {
|
||||
if ($grade_grade->locked) {
|
||||
// individual grade locked, abort
|
||||
$status = false;
|
||||
$error = get_string('gradegradeslocked', 'grades');
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (isset($result['#']['score'][0]['#'])) {
|
||||
$newgrade = new object();
|
||||
$newgrade->itemid = $grade_item->id;
|
||||
$newgrade->grade = $result['#']['score'][0]['#'];
|
||||
$newgrade->userid = $result['#']['student'][0]['#'];
|
||||
$newgrades[] = $newgrade;
|
||||
}
|
||||
}
|
||||
|
||||
// loop through info collected so far
|
||||
if ($status && !empty($newgrades)) {
|
||||
foreach ($newgrades as $newgrade) {
|
||||
|
||||
// check if user exist
|
||||
if (!$user = get_record('user', 'id', addslashes($newgrade->userid))) {
|
||||
// no user found, abort
|
||||
$status = false;
|
||||
$error = get_string('baduserid', 'grades');
|
||||
break;
|
||||
}
|
||||
|
||||
// check grade value is a numeric grade
|
||||
if (!is_numeric($newgrade->grade)) {
|
||||
$status = false;
|
||||
$error = get_string('badgrade', 'grades');
|
||||
break;
|
||||
}
|
||||
|
||||
// insert this grade into a temp table
|
||||
$newgrade->import_code = $importcode;
|
||||
if (!insert_record('grade_import_values', addslashes_recursive($newgrade))) {
|
||||
$status = false;
|
||||
// could not insert into temp table
|
||||
$error = get_string('importfailed', 'grades');
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
} 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;
|
||||
}
|
||||
}
|
||||
?>
|
@ -1,6 +1,6 @@
|
||||
<?PHP // $Id$
|
||||
|
||||
$plugin->version = 2007072500;
|
||||
$plugin->requires = 2007072402;
|
||||
$plugin->version = 2007092600;
|
||||
$plugin->requires = 2007092002;
|
||||
|
||||
?>
|
||||
|
@ -1,16 +0,0 @@
|
||||
<?php // $Id$
|
||||
|
||||
$gradeimport_xmlurl_capabilities = array(
|
||||
|
||||
'gradeimport/xmlurl:view' => array(
|
||||
'captype' => 'write',
|
||||
'contextlevel' => CONTEXT_COURSE,
|
||||
'legacy' => array(
|
||||
'admin' => CAP_ALLOW
|
||||
)
|
||||
)
|
||||
);
|
||||
|
||||
?>
|
||||
|
||||
|
@ -1,178 +0,0 @@
|
||||
<?php //$Id$
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
// //
|
||||
// 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 //
|
||||
// //
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
|
||||
require_once '../../../config.php';
|
||||
require_once $CFG->libdir.'/filelib.php';
|
||||
require_once($CFG->libdir.'/xmlize.php');
|
||||
require_once($CFG->libdir.'/gradelib.php');
|
||||
require_once $CFG->dirroot.'/grade/lib.php';
|
||||
require_once $CFG->dirroot.'/grade/import/lib.php';
|
||||
|
||||
$url = required_param('url', PARAM_URL); // only real urls here
|
||||
$id = required_param('id', PARAM_INT); // course id
|
||||
|
||||
if (!$course = get_record('course', 'id', $id)) {
|
||||
print_error('nocourseid');
|
||||
}
|
||||
|
||||
require_login($course);
|
||||
$context = get_context_instance(CONTEXT_COURSE, $id);
|
||||
|
||||
require_capability('moodle/grade:import', $context);
|
||||
require_capability('gradeimport/xmlurl:view', $context);
|
||||
|
||||
|
||||
// Large files are likely to take their time and memory. Let PHP know
|
||||
// that we'll take longer, and that the process should be recycled soon
|
||||
// to free up memory.
|
||||
@set_time_limit(0);
|
||||
@raise_memory_limit("192M");
|
||||
if (function_exists('apache_child_terminate')) {
|
||||
@apache_child_terminate();
|
||||
}
|
||||
|
||||
$text = download_file_content($url);
|
||||
if ($text === false) {
|
||||
error('Can not read file');
|
||||
}
|
||||
|
||||
$status = true;
|
||||
$error = '';
|
||||
|
||||
$importcode = time();
|
||||
$newgrades = array();
|
||||
|
||||
$content = xmlize($text);
|
||||
|
||||
if ($results = $content['results']['#']['result']) {
|
||||
|
||||
foreach ($results as $i => $result) {
|
||||
if (!$grade_items = grade_item::fetch_all(array('idnumber'=>$result['#']['assignment'][0]['#'], 'courseid'=>$course->id))) {
|
||||
// gradeitem does not exist
|
||||
// no data in temp table so far, abort
|
||||
$status = false;
|
||||
$error = 'incorrect grade item idnumber'; //TODO: localize
|
||||
break;
|
||||
} else if (count($grade_items) != 1) {
|
||||
$status = false;
|
||||
$error = 'duplicate grade item idnumber'; //TODO: localize
|
||||
break;
|
||||
} else {
|
||||
$grade_item = reset($grade_items);
|
||||
}
|
||||
|
||||
// grade item locked, abort
|
||||
if ($grade_item->locked) {
|
||||
$status = false;
|
||||
$error = get_string('gradeitemlocked', 'grades');
|
||||
break;
|
||||
}
|
||||
|
||||
// check if grade_grade is locked and if so, abort
|
||||
if ($grade_grade = new grade_grade(array('itemid'=>$grade_item->id, 'userid'=>$result['#']['student'][0]['#']))) {
|
||||
if ($grade_grade->locked) {
|
||||
// individual grade locked, abort
|
||||
$status = false;
|
||||
$error = get_string('gradegradeslocked', 'grades');
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (isset($result['#']['score'][0]['#'])) {
|
||||
$newgrade = new object();
|
||||
$newgrade->itemid = $grade_item->id;
|
||||
$newgrade->grade = $result['#']['score'][0]['#'];
|
||||
$newgrade->userid = $result['#']['student'][0]['#'];
|
||||
$newgrades[] = $newgrade;
|
||||
}
|
||||
}
|
||||
|
||||
// loop through info collected so far
|
||||
if ($status && !empty($newgrades)) {
|
||||
foreach ($newgrades as $newgrade) {
|
||||
|
||||
// check if user exist
|
||||
if (!$user = get_record('user', 'id', addslashes($newgrade->userid))) {
|
||||
// no user found, abort
|
||||
$status = false;
|
||||
$error = get_string('baduserid', 'grades');
|
||||
break;
|
||||
}
|
||||
|
||||
// check grade value is a numeric grade
|
||||
if (!is_numeric($newgrade->grade)) {
|
||||
$status = false;
|
||||
$error = get_string('badgrade', 'grades');
|
||||
break;
|
||||
}
|
||||
|
||||
// insert this grade into a temp table
|
||||
$newgrade->import_code = $importcode;
|
||||
if (!insert_record('grade_import_values', addslashes_recursive($newgrade))) {
|
||||
$status = false;
|
||||
// could not insert into temp table
|
||||
$error = get_string('importfailed', 'grades');
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// no results section found in xml,
|
||||
// assuming bad format, abort import
|
||||
$status = false;
|
||||
$error = get_string('badxmlformat', 'grade');
|
||||
}
|
||||
|
||||
if ($status) {
|
||||
/// comit the code if we are up this far
|
||||
|
||||
if (defined('USER_KEY_LOGIN')) {
|
||||
if (grade_import_commit($id, $importcode, false)) {
|
||||
echo 'ok';
|
||||
die;
|
||||
} else {
|
||||
error('Grade import error'); //TODO: localize
|
||||
}
|
||||
|
||||
} else {
|
||||
$strgrades = get_string('grades', 'grades');
|
||||
$actionstr = get_string('xmlurl', 'grades');
|
||||
$navigation = grade_build_nav(__FILE__, $actionstr, array('courseid' => $course->id));
|
||||
|
||||
print_header($course->shortname.': '.get_string('grades'), $course->fullname, $navigation);
|
||||
print_grade_plugin_selector($id, 'import', 'xmlurl');
|
||||
|
||||
grade_import_commit($id, $importcode);
|
||||
|
||||
print_footer();
|
||||
die;
|
||||
}
|
||||
|
||||
} else {
|
||||
import_cleanup($importcode);
|
||||
error($error);
|
||||
}
|
||||
|
||||
?>
|
@ -1,6 +0,0 @@
|
||||
<?PHP // $Id$
|
||||
|
||||
$plugin->version = 2007092501;
|
||||
$plugin->requires = 2007092002;
|
||||
|
||||
?>
|
@ -1,5 +1,9 @@
|
||||
<?PHP // $Id$
|
||||
|
||||
$string['errbadxmlformat'] = 'Error - bad XML format';
|
||||
$string['errduplicateidnumber'] = 'Error - duplicate idnumber';
|
||||
$string['errincorrectidnumber'] = 'Error - incorrect idnumber';
|
||||
$string['fileurl'] = 'Remote file URL';
|
||||
$string['modulename'] = 'XML file';
|
||||
$string['xml:view'] = 'Import grades from XML';
|
||||
|
||||
|
@ -1,6 +0,0 @@
|
||||
<?PHP // $Id$
|
||||
|
||||
$string['modulename'] = 'XML file (URL)';
|
||||
$string['xmlurl:view'] = 'Import grades from XML (URL)';
|
||||
|
||||
?>
|
Loading…
x
Reference in New Issue
Block a user