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
10 * @param feedback print feedback and continue button
11 * @return bool success
13 function grade_import_commit($courseid, $importcode, $feedback=true) {
16 include_once($CFG->libdir
.'/gradelib.php');
17 include_once($CFG->libdir
.'/grade/grade_item.php');
18 $commitstart = time(); // start time in case we need to roll back
19 $newitemids = array(); // array to hold new grade_item ids from grade_import_newitem table, mapping array
21 /// first select distinct new grade_items with this batch
23 if ($newitems = get_records_sql("SELECT *
24 FROM {$CFG->prefix}grade_import_newitem
25 WHERE import_code = $importcode")) {
27 // instances of the new grade_items created, cached
28 // in case grade_update fails, so that we can remove them
31 foreach ($newitems as $newitem) {
32 // get all grades with this item
34 if ($grades = get_records('grade_import_values', 'newgradeitem', $newitem->id
)) {
35 /// create a new grade item for this - must use false as second param!
36 /// TODO: we need some bounds here too
37 $gradeitem = new grade_item(array('courseid'=>$courseid, 'itemtype'=>'manual', 'itemname'=>$newitem->itemname
), false);
38 $gradeitem->insert('import');
39 $instances[] = $gradeitem;
41 // insert each individual grade to this new grade item
42 foreach ($grades as $grade) {
43 if (!$gradeitem->update_final_grade($grade->userid
, $grade->finalgrade
, 'import', NULL, $grade->feedback
)) {
52 foreach ($instances as $instance) {
53 $gradeitem->delete('import');
55 import_cleanup($importcode);
60 /// then find all existing items
62 if ($gradeitems = get_records_sql("SELECT DISTINCT (itemid)
63 FROM {$CFG->prefix}grade_import_values
64 WHERE import_code = $importcode
67 $modifieditems = array();
69 foreach ($gradeitems as $itemid=>$iteminfo) {
71 if (!$gradeitem = new grade_item(array('id'=>$itemid))) {
72 // not supposed to happen, but just in case
73 import_cleanup($importcode);
76 // get all grades with this item
77 if ($grades = get_records('grade_import_values', 'itemid', $itemid)) {
79 // make the grades array for update_grade
80 foreach ($grades as $grade) {
81 if (!$gradeitem->update_final_grade($grade->userid
, $grade->finalgrade
, 'import', NULL, $grade->feedback
)) {
86 //$itemdetails -> idnumber = $gradeitem->idnumber;
87 $modifieditems[] = $itemid;
91 if (!empty($failed)) {
92 import_cleanup($importcode);
99 notify(get_string('importsuccess', 'grades'), 'notifysuccess');
100 print_continue($CFG->wwwroot
.'/grade/index.php?id='.$courseid);
103 import_cleanup($importcode);
109 * removes entries from grade import buffer tables grade_import_value and grade_import_newitem
110 * after a successful import, or during an import abort
111 * @param string importcode - import batch identifier
113 function import_cleanup($importcode) {
114 // remove entries from buffer table
115 delete_records('grade_import_values', 'import_code', $importcode);
116 delete_records('grade_import_newitem', 'import_code', $importcode);
119 /// Returns the file as one big long string
120 function my_file_get_contents($filename, $use_include_path = 0) {
123 $file = @fopen
($filename, "rb", $use_include_path);
125 while (!feof($file)) {
126 $data .= fread($file, 1024);