adding some strings
[moodle-linuxchix.git] / grade / import / xml / index.php
blobb859173896231f254a4e0606563e1029d860a469
1 <?php //$Id$
3 /**
4 * code in development
5 * does xml plugin need some flexibility/mapping of columns?
6 */
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);
25 // print header
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
38 $newgrades = array();
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
43 // to free up memory.
44 @set_time_limit(0);
45 @raise_memory_limit("192M");
46 if (function_exists('apache_child_terminate')) {
47 @apache_child_terminate();
50 $text = my_file_get_contents($filename);
52 // trim utf-8 bom
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
67 $importcode = time();
68 $status = true;
70 $numlines = 0;
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])) {
78 echo '<tr>';
79 foreach ($result['#'] as $fieldname => $val) {
80 echo '<td>'.$fieldname.' > '.$val[0]['#'].'</td>';
82 echo '</tr>';
83 $numlines ++;
84 } else if ($numlines == $formdata->previewrows || !isset($results[$i+1])) {
85 echo '</table>';
86 $numlines ++;
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
93 $status = false;
94 break;
97 // grade item locked, abort
98 if ($gradeitem->locked) {
99 $status = false;
100 notify(get_string('gradeitemlocked', 'grades'));
101 break 3;
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
108 $status = false;
109 notify(get_string('gradegradeslocked', 'grades'));
110 break 2;
113 unset($newgrade);
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
130 $status = false;
131 import_cleanup($importcode);
132 notify(get_string('baduserid', 'grades'));
133 notify(get_string('importfailed', 'grades'));
134 break;
137 // check grade value is a numeric grade
138 if (!is_numeric($newgrade->rawgrade)) {
139 $status = false;
140 import_cleanup($importcode);
141 notify(get_string('badgrade', 'grades'));
142 break;
145 // insert this grade into a temp table
146 $newgrade->import_code = $importcode;
147 if (!insert_record('grade_import_values', $newgrade)) {
148 $status = false;
149 // could not insert into temp table
150 import_cleanup($importcode);
151 notify(get_string('importfailed', 'grades'));
152 break;
157 // if all ok, we proceed
158 if ($status) {
159 /// comit the code if we are up this far
160 grade_import_commit($id, $importcode);
162 } else {
163 // no results section found in xml,
164 // assuming bad format, abort import
165 notify('badxmlformat', 'grade');
167 } else {
168 // display the standard upload file form
169 $mform->display();
172 print_footer();