2007-07-24 09:30:46 +00:00
|
|
|
<?php //$Id$
|
2007-05-24 02:16:31 +00:00
|
|
|
|
2007-06-11 09:00:17 +00:00
|
|
|
/**
|
|
|
|
* code in development
|
|
|
|
* does xml plugin need some flexibility/mapping of columns?
|
|
|
|
*/
|
2007-07-24 09:30:46 +00:00
|
|
|
|
|
|
|
require_once '../../../config.php';
|
|
|
|
require_once $CFG->dirroot.'/grade/lib.php';
|
|
|
|
require_once '../grade_import_form.php';
|
|
|
|
require_once '../lib.php';
|
2007-05-24 02:16:31 +00:00
|
|
|
|
|
|
|
$id = required_param('id', PARAM_INT); // course id
|
|
|
|
|
2007-07-24 09:30:46 +00:00
|
|
|
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-05-24 02:16:31 +00:00
|
|
|
|
2007-06-11 09:00:17 +00:00
|
|
|
|
2007-07-17 08:40:35 +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);
|
2007-07-30 06:28:41 +00:00
|
|
|
print_grade_plugin_selector($id, 'import', 'xml');
|
2007-05-24 02:16:31 +00:00
|
|
|
$mform = new grade_import_form();
|
|
|
|
|
2007-06-11 09:00:17 +00:00
|
|
|
if ( $formdata = $mform->get_data()) {
|
2007-05-24 02:16:31 +00:00
|
|
|
|
2007-06-11 09:00:17 +00:00
|
|
|
// array to hold all grades to be inserted
|
|
|
|
$newgrades = array();
|
2007-05-24 02:16:31 +00:00
|
|
|
|
|
|
|
$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
|
|
|
|
2007-05-24 02:16:31 +00:00
|
|
|
// trim utf-8 bom
|
|
|
|
$textlib = new textlib();
|
2007-06-11 09:00:17 +00:00
|
|
|
// converts to propert unicode
|
2007-07-18 19:56:07 +00:00
|
|
|
$text = $textlib->convert($text, $formdata->encoding);
|
2007-05-24 02:16:31 +00:00
|
|
|
$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);
|
2007-07-18 19:56:07 +00:00
|
|
|
|
2007-05-24 02:16:31 +00:00
|
|
|
if ($results = $content['results']['#']['result']) {
|
2007-07-18 19:56:07 +00:00
|
|
|
|
2007-06-11 09:00:17 +00:00
|
|
|
// import batch identifier timestamp
|
|
|
|
$importcode = time();
|
|
|
|
$status = true;
|
2007-07-18 19:56:07 +00:00
|
|
|
|
2007-06-13 09:07:39 +00:00
|
|
|
$numlines = 0;
|
2007-07-18 19:56:07 +00:00
|
|
|
|
2007-06-13 09:07:39 +00:00
|
|
|
// print some previews
|
|
|
|
print_heading(get_string('importpreview', 'grades'));
|
2007-07-18 19:56:07 +00:00
|
|
|
|
|
|
|
echo '<table cellpadding="5">';
|
2007-06-13 09:07:39 +00:00
|
|
|
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;
|
|
|
|
}
|
2007-07-18 19:56:07 +00:00
|
|
|
|
2007-07-17 08:40:35 +00:00
|
|
|
// grade item locked, abort
|
|
|
|
if ($gradeitem->locked) {
|
|
|
|
$status = false;
|
|
|
|
notify(get_string('gradeitemlocked', 'grades'));
|
2007-07-18 19:56:07 +00:00
|
|
|
break 3;
|
|
|
|
}
|
|
|
|
|
2007-07-19 08:15:19 +00:00
|
|
|
// 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) {
|
2007-07-17 08:40:35 +00:00
|
|
|
// individual grade locked, abort
|
|
|
|
$status = false;
|
|
|
|
notify(get_string('gradegradeslocked', 'grades'));
|
|
|
|
break 2;
|
|
|
|
}
|
|
|
|
}
|
2007-06-11 09:00:17 +00:00
|
|
|
unset($newgrade);
|
2007-05-24 02:16:31 +00:00
|
|
|
|
2007-06-11 09:00:17 +00:00
|
|
|
if (isset($result['#']['score'][0]['#'])) {
|
|
|
|
$newgrade -> itemid = $gradeitem->id;
|
2007-06-20 23:06:29 +00:00
|
|
|
$newgrade -> rawgrade = $result['#']['score'][0]['#'];
|
2007-06-11 09:00:17 +00:00
|
|
|
$newgrade-> userid = $result['#']['student'][0]['#'];
|
|
|
|
$newgrades[] = $newgrade;
|
|
|
|
}
|
|
|
|
}
|
2007-07-18 19:56:07 +00:00
|
|
|
|
2007-06-11 09:00:17 +00:00
|
|
|
// loop through info collected so far
|
|
|
|
if ($status && !empty($newgrades)) {
|
|
|
|
foreach ($newgrades as $newgrade) {
|
2007-07-18 19:56:07 +00:00
|
|
|
|
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);
|
2007-06-12 09:12:07 +00:00
|
|
|
notify(get_string('baduserid', 'grades'));
|
|
|
|
notify(get_string('importfailed', 'grades'));
|
2007-06-11 09:00:17 +00:00
|
|
|
break;
|
2007-07-18 19:56:07 +00:00
|
|
|
}
|
|
|
|
|
2007-06-11 09:00:17 +00:00
|
|
|
// check grade value is a numeric grade
|
2007-06-20 23:06:29 +00:00
|
|
|
if (!is_numeric($newgrade->rawgrade)) {
|
2007-06-11 09:00:17 +00:00
|
|
|
$status = false;
|
|
|
|
import_cleanup($importcode);
|
2007-06-12 09:12:07 +00:00
|
|
|
notify(get_string('badgrade', 'grades'));
|
2007-06-11 09:00:17 +00:00
|
|
|
break;
|
|
|
|
}
|
2007-05-24 02:16:31 +00:00
|
|
|
|
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);
|
2007-06-12 09:12:07 +00:00
|
|
|
notify(get_string('importfailed', 'grades'));
|
2007-06-11 09:00:17 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2007-05-24 02:16:31 +00:00
|
|
|
}
|
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-07-18 19:56:07 +00:00
|
|
|
}
|
2007-06-11 09:00:17 +00:00
|
|
|
} else {
|
|
|
|
// no results section found in xml,
|
|
|
|
// assuming bad format, abort import
|
2007-07-18 19:56:07 +00:00
|
|
|
notify('badxmlformat', 'grade');
|
2007-05-24 02:16:31 +00:00
|
|
|
}
|
2007-06-11 09:00:17 +00:00
|
|
|
} else {
|
|
|
|
// display the standard upload file form
|
|
|
|
$mform->display();
|
2007-05-24 02:16:31 +00:00
|
|
|
}
|
2007-06-11 09:00:17 +00:00
|
|
|
|
|
|
|
print_footer();
|
2007-05-24 02:16:31 +00:00
|
|
|
?>
|