4 * given an import code, commits all entries in buffer tables
5 * (grade_import_value and grade_import_newitem)
6 * If this function is called, we assume that all data collected
7 * up to this point is fine and we can go ahead and commit
8 * @param int courseid - id of the course
9 * @param string importcode - import batch identifier
11 function grade_import_commit($courseid, $importcode) {
14 include_once($CFG->libdir
.'/gradelib.php');
15 include_once($CFG->libdir
.'/grade/grade_item.php');
16 $commitstart = time(); // start time in case we need to roll back
17 $newitemids = array(); // array to hold new grade_item ids from grade_import_newitem table, mapping array
19 /// first select distinct new grade_items with this batch
21 if ($newitems = get_records_sql("SELECT *
22 FROM {$CFG->prefix}grade_import_newitem
23 WHERE import_code = $importcode")) {
25 // instances of the new grade_items created, cached
26 // in case grade_update fails, so that we can remove them
28 foreach ($newitems as $newitem) {
29 // get all grades with this item
31 if ($grades = get_records('grade_import_values', 'newgradeitem', $newitem->id
)) {
33 // make the grades array for update_grade
35 // find the max instance number of 'manual' grade item
36 // and increment that number by 1 by hand
37 // I can not find other ways to make 'manual' type work,
38 // unless we have a 'new' flag for grade_update to let it
39 // know that this is a new grade_item, and let grade_item
40 // handle the instance id in the case of a 'manual' import?
41 if ($lastimport = get_record_sql("SELECT *
42 FROM {$CFG->prefix}grade_items
43 WHERE courseid = $courseid
44 AND itemtype = 'manual'
45 ORDER BY iteminstance DESC", true)) {
46 $instance = $lastimport->iteminstance +
1;
51 $instances[] = $instance;
53 // TODO clean up following comment?
54 // if fails, deletes all the created grade_items and grades
56 /// create a new grade item for this
57 $gradeitem = new grade_item(array('courseid'=>$courseid, 'itemtype'=>'manual', 'iteminstance'=>$instance, 'itemname'=>$newitem->itemname
));
60 // insert each individual grade to this new grade item
62 foreach ($grades as $grade) {
63 if (!$gradeitem->update_final_grade($grade->userid
, $grade->finalgrade
, NULL, NULL, $grade->feedback
)) {
69 foreach ($instances as $instance) {
70 $gradeitem = new grade_item(array('courseid'=>$courseid, 'itemtype'=>'manual', 'iteminstance'=>$instance));
71 // TODO this method does not seem to delete all the raw grades and the item itself
72 // which I think should be deleted in this case, can I use sql directly here?
75 import_cleanup($importcode);
82 /// then find all existing items
84 if ($gradeitems = get_records_sql("SELECT DISTINCT (itemid)
85 FROM {$CFG->prefix}grade_import_values
86 WHERE import_code = $importcode
89 $modifieditems = array();
91 foreach ($gradeitems as $itemid=>$iteminfo) {
93 if (!$gradeitem = new grade_item(array('id'=>$itemid))) {
94 // not supposed to happen, but just in case
95 import_cleanup($importcode);
98 // get all grades with this item
99 if ($grades = get_records('grade_import_values', 'itemid', $itemid)) {
101 // make the grades array for update_grade
102 foreach ($grades as $grade) {
103 if (!$gradeitem->update_final_grade($grade->userid
, $grade->finalgrade
, NULL, NULL, $grade->feedback
)) {
108 //$itemdetails -> idnumber = $gradeitem->idnumber;
109 $modifieditems[] = $itemid;
113 if (!empty($failed)) {
114 import_cleanup($importcode);
120 notify(get_string('importsuccess', 'grades'));
121 print_continue($CFG->wwwroot
.'/course/view.php?id='.$courseid);
123 import_cleanup($importcode);
127 * removes entries from grade import buffer tables grade_import_value and grade_import_newitem
128 * after a successful import, or during an import abort
129 * @param string importcode - import batch identifier
131 function import_cleanup($importcode) {
132 // remove entries from buffer table
133 delete_records('grade_import_values', 'import_code', $importcode);
134 delete_records('grade_import_newitem', 'import_code', $importcode);
137 /// Returns the file as one big long string
138 function my_file_get_contents($filename, $use_include_path = 0) {
141 $file = @fopen
($filename, "rb", $use_include_path);
143 while (!feof($file)) {
144 $data .= fread($file, 1024);