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 grardes 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;
52 // if fails, deletes all the created grade_items and grades
54 /// create a new grade item for this
55 $gradeitem = new grade_item(array('courseid'=>$courseid, 'itemtype'=>'manual', 'iteminstance'=>$instance, 'itemname'=>$newitem->itemname
));
58 // insert each individual grade to this new grade item
60 foreach ($grades as $grade) {
61 if (!$gradeitem->update_final_grade($grade->userid
, $grade->finalgrade
, NULL, NULL, $grade->feedback
)) {
67 foreach ($instances as $instance) {
68 $gradeitem = new grade_item(array('courseid'=>$courseid, 'itemtype'=>'manual', 'iteminstance'=>$instance));
69 // this method does not seem to delete all the raw grades and the item itself
70 // which I think should be deleted in this case, can I use sql directly here?
73 import_cleanup($importcode);
80 /// then find all existing items
82 if ($gradeitems = get_records_sql("SELECT DISTINCT (itemid)
83 FROM {$CFG->prefix}grade_import_values
84 WHERE import_code = $importcode
87 $modifieditems = array();
89 foreach ($gradeitems as $itemid=>$iteminfo) {
91 if (!$gradeitem = new grade_item(array('id'=>$itemid))) {
92 // not supposed to happen, but just in case
93 import_cleanup($importcode);
96 // get all grades with this item
97 if ($grades = get_records('grade_import_values', 'itemid', $itemid)) {
99 // make the grardes array for update_grade
100 foreach ($grades as $grade) {
101 if (!$gradeitem->update_final_grade($grade->userid
, $grade->finalgrade
, NULL, NULL, $grade->feedback
)) {
106 //$itemdetails -> idnumber = $gradeitem->idnumber;
107 $modifieditems[] = $itemid;
111 if (!empty($failed)) {
112 import_cleanup($importcode);
118 notify(get_string('importsuccess', 'grades'));
119 print_continue($CFG->wwwroot
.'/course/view.php?id='.$courseid);
121 import_cleanup($importcode);
125 * removes entries from grade import buffer tables grade_import_value and grade_import_newitem
126 * after a successful import, or during an import abort
127 * @param string importcode - import batch identifier
129 function import_cleanup($importcode) {
130 // remove entries from buffer table
131 delete_records('grade_import_values', 'import_code', $importcode);
132 delete_records('grade_import_newitem', 'import_code', $importcode);
135 /// Returns the file as one big long string
136 function my_file_get_contents($filename, $use_include_path = 0) {
139 $file = @fopen
($filename, "rb", $use_include_path);
141 while (!feof($file)) {
142 $data .= fread($file, 1024);