moodle/grade/import/xml/index.php

173 lines
5.8 KiB
PHP
Raw Normal View History

<?php //$Id$
2007-06-11 09:00:17 +00:00
/**
* code in development
* does xml plugin need some flexibility/mapping of columns?
*/
require_once '../../../config.php';
require_once $CFG->dirroot.'/grade/lib.php';
require_once '../grade_import_form.php';
require_once '../lib.php';
$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/xml:view', $context);
2007-06-11 09:00:17 +00:00
// print header
2007-07-17 03:32:10 +00:00
$strgrades = get_string('grades', 'grades');
$actionstr = get_string('importxml', 'grades');
$gradenav = "<a href=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->shortname</a>";
$gradenav .= " -> <a href=\"$CFG->wwwroot/grade/index.php?id=$course->id\">$strgrades</a>";
$gradenav .= " -> $actionstr";
print_header($course->shortname.': '.get_string('grades'), $course->fullname, $gradenav);
print_grade_plugin_selector($id, 'import', 'xml');
$mform = new grade_import_form();
2007-06-11 09:00:17 +00:00
if ( $formdata = $mform->get_data()) {
2007-06-11 09:00:17 +00:00
// array to hold all grades to be inserted
$newgrades = array();
$filename = $mform->get_userfile_name();
// 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 = my_file_get_contents($filename);
2007-06-11 09:00:17 +00:00
// trim utf-8 bom
$textlib = new textlib();
2007-06-11 09:00:17 +00:00
// 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
2007-06-11 09:00:17 +00:00
include_once($CFG->dirroot.'/lib/xmlize.php');
$content = xmlize($text);
if ($results = $content['results']['#']['result']) {
2007-06-11 09:00:17 +00:00
// 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 ++;
}
2007-06-11 09:00:17 +00:00
include_once($CFG->libdir.'/grade/grade_item.php');
if (!$gradeitem = new grade_item(array('idnumber'=>$result['#']['assignment'][0]['#']))) {
// 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;
}
}
2007-06-11 09:00:17 +00:00
unset($newgrade);
2007-06-11 09:00:17 +00:00
if (isset($result['#']['score'][0]['#'])) {
$newgrade -> itemid = $gradeitem->id;
$newgrade -> rawgrade = $result['#']['score'][0]['#'];
2007-06-11 09:00:17 +00:00
$newgrade-> userid = $result['#']['student'][0]['#'];
$newgrades[] = $newgrade;
}
}
2007-06-11 09:00:17 +00:00
// loop through info collected so far
if ($status && !empty($newgrades)) {
foreach ($newgrades as $newgrade) {
2007-06-11 09:00:17 +00:00
// check if user exist
if (!$user = get_record('user', 'id', $newgrade->userid)) {
// no user found, abort
$status = false;
import_cleanup($importcode);
notify(get_string('baduserid', 'grades'));
notify(get_string('importfailed', 'grades'));
2007-06-11 09:00:17 +00:00
break;
}
2007-06-11 09:00:17 +00:00
// check grade value is a numeric grade
if (!is_numeric($newgrade->rawgrade)) {
2007-06-11 09:00:17 +00:00
$status = false;
import_cleanup($importcode);
notify(get_string('badgrade', 'grades'));
2007-06-11 09:00:17 +00:00
break;
}
2007-06-11 09:00:17 +00:00
// insert this grade into a temp table
$newgrade->import_code = $importcode;
if (!insert_record('grade_import_values', $newgrade)) {
$status = false;
// could not insert into temp table
import_cleanup($importcode);
notify(get_string('importfailed', 'grades'));
2007-06-11 09:00:17 +00:00
break;
}
}
}
2007-06-11 09:00:17 +00:00
// if all ok, we proceed
if ($status) {
/// comit the code if we are up this far
grade_import_commit($id, $importcode);
}
2007-06-11 09:00:17 +00:00
} else {
// no results section found in xml,
// assuming bad format, abort import
notify('badxmlformat', 'grade');
}
2007-06-11 09:00:17 +00:00
} else {
// display the standard upload file form
$mform->display();
}
2007-06-11 09:00:17 +00:00
print_footer();
?>