Automatic installer.php lang files by installer_builder (20070726)
[moodle-linuxchix.git] / grade / import / lib.php
blobd9b31d69999ad20f66d07c88a89715c761331dc3
1 <?php // $Id$
3 /**
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) {
12 global $CFG;
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
27 $instances = array();
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;
47 } else {
48 $instance = 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));
56 $gradeitem->insert();
58 // insert each individual grade to this new grade item
59 $failed = 0;
60 foreach ($grades as $grade) {
61 if (!$gradeitem->update_final_grade($grade->userid, $grade->finalgrade, NULL, NULL, $grade->feedback)) {
62 $failed = 1;
63 break;
66 if ($failed) {
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?
71 $gradeitem->delete();
73 import_cleanup($importcode);
74 return false;
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
85 AND itemid > 0")) {
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);
94 return false;
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)) {
102 $failed = 1;
103 break 2;
106 //$itemdetails -> idnumber = $gradeitem->idnumber;
107 $modifieditems[] = $itemid;
111 if (!empty($failed)) {
112 import_cleanup($importcode);
113 return false;
118 notify(get_string('importsuccess', 'grades'));
119 print_continue($CFG->wwwroot.'/course/view.php?id='.$courseid);
120 // clean up
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) {
138 $data = "";
139 $file = @fopen($filename, "rb", $use_include_path);
140 if ($file) {
141 while (!feof($file)) {
142 $data .= fread($file, 1024);
144 fclose($file);
146 return $data;