5 * does xml plugin need some flexibility/mapping of columns?
8 require_once '../../../config.php';
9 require_once $CFG->dirroot
.'/grade/lib.php';
10 require_once '../grade_import_form.php';
11 require_once '../lib.php';
13 $id = required_param('id', PARAM_INT
); // course id
15 if (!$course = get_record('course', 'id', $id)) {
16 print_error('nocourseid');
19 require_login($course);
20 $context = get_context_instance(CONTEXT_COURSE
, $id);
21 require_capability('moodle/grade:import', $context);
22 require_capability('gradeimport/xml:view', $context);
26 $strgrades = get_string('grades', 'grades');
27 $actionstr = get_string('importxml', 'grades');
28 $gradenav = "<a href=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->shortname</a>";
29 $gradenav .= " -> <a href=\"$CFG->wwwroot/grade/index.php?id=$course->id\">$strgrades</a>";
30 $gradenav .= " -> $actionstr";
31 print_header($course->shortname
.': '.get_string('grades'), $course->fullname
, $gradenav);
33 $mform = new grade_import_form();
35 if ( $formdata = $mform->get_data()) {
37 // array to hold all grades to be inserted
40 $filename = $mform->get_userfile_name();
41 // Large files are likely to take their time and memory. Let PHP know
42 // that we'll take longer, and that the process should be recycled soon
45 @raise_memory_limit
("192M");
46 if (function_exists('apache_child_terminate')) {
47 @apache_child_terminate
();
50 $text = my_file_get_contents($filename);
53 $textlib = new textlib();
54 // converts to propert unicode
55 $text = $textlib->convert($text, $formdata->encoding
);
56 $text = $textlib->trim_utf8_bom($text);
57 // Fix mac/dos newlines
58 $text = preg_replace('!\r\n?!',"\n",$text);
60 // text is the text, we should xmlize it
61 include_once($CFG->dirroot
.'/lib/xmlize.php');
62 $content = xmlize($text);
64 if ($results = $content['results']['#']['result']) {
66 // import batch identifier timestamp
72 // print some previews
73 print_heading(get_string('importpreview', 'grades'));
75 echo '<table cellpadding="5">';
76 foreach ($results as $i => $result) {
77 if ($numlines < $formdata->previewrows
&& isset($results[$i+
1])) {
79 foreach ($result['#'] as $fieldname => $val) {
80 echo '<td>'.$fieldname.' > '.$val[0]['#'].'</td>';
84 } else if ($numlines == $formdata->previewrows ||
!isset($results[$i+
1])) {
89 include_once($CFG->libdir
.'/grade/grade_item.php');
90 if (!$gradeitem = new grade_item(array('idnumber'=>$result['#']['assignment'][0]['#']))) {
91 // gradeitem does not exist
92 // no data in temp table so far, abort
97 // grade item locked, abort
98 if ($gradeitem->locked
) {
100 notify(get_string('gradeitemlocked', 'grades'));
104 // check if grade_grade is locked and if so, abort
105 if ($grade_grade = new grade_grade(array('itemid'=>$gradeitem->id
, 'userid'=>$result['#']['student'][0]['#']))) {
106 if ($grade_grade->locked
) {
107 // individual grade locked, abort
109 notify(get_string('gradegradeslocked', 'grades'));
115 if (isset($result['#']['score'][0]['#'])) {
116 $newgrade -> itemid
= $gradeitem->id
;
117 $newgrade -> rawgrade
= $result['#']['score'][0]['#'];
118 $newgrade-> userid
= $result['#']['student'][0]['#'];
119 $newgrades[] = $newgrade;
123 // loop through info collected so far
124 if ($status && !empty($newgrades)) {
125 foreach ($newgrades as $newgrade) {
127 // check if user exist
128 if (!$user = get_record('user', 'id', $newgrade->userid
)) {
129 // no user found, abort
131 import_cleanup($importcode);
132 notify(get_string('baduserid', 'grades'));
133 notify(get_string('importfailed', 'grades'));
137 // check grade value is a numeric grade
138 if (!is_numeric($newgrade->rawgrade
)) {
140 import_cleanup($importcode);
141 notify(get_string('badgrade', 'grades'));
145 // insert this grade into a temp table
146 $newgrade->import_code
= $importcode;
147 if (!insert_record('grade_import_values', $newgrade)) {
149 // could not insert into temp table
150 import_cleanup($importcode);
151 notify(get_string('importfailed', 'grades'));
157 // if all ok, we proceed
159 /// comit the code if we are up this far
160 grade_import_commit($id, $importcode);
163 // no results section found in xml,
164 // assuming bad format, abort import
165 notify('badxmlformat', 'grade');
168 // display the standard upload file form