2007-06-08 09:01:19 +00:00
|
|
|
<?php // $Id$
|
|
|
|
|
|
|
|
/**
|
2007-07-18 19:56:07 +00:00
|
|
|
* given an import code, commits all entries in buffer tables
|
2007-06-08 09:01:19 +00:00
|
|
|
* (grade_import_value and grade_import_newitem)
|
2007-07-18 19:56:07 +00:00
|
|
|
* If this function is called, we assume that all data collected
|
2007-06-08 09:01:19 +00:00
|
|
|
* up to this point is fine and we can go ahead and commit
|
|
|
|
* @param int courseid - id of the course
|
|
|
|
* @param string importcode - import batch identifier
|
2007-09-26 09:42:28 +00:00
|
|
|
* @param feedback print feedback and continue button
|
|
|
|
* @return bool success
|
2007-06-08 09:01:19 +00:00
|
|
|
*/
|
2007-10-07 10:22:21 +00:00
|
|
|
function grade_import_commit($courseid, $importcode, $importfeedback=true, $verbose=true) {
|
2007-06-08 09:01:19 +00:00
|
|
|
global $CFG;
|
2007-07-18 19:56:07 +00:00
|
|
|
|
2007-06-08 09:01:19 +00:00
|
|
|
include_once($CFG->libdir.'/gradelib.php');
|
2007-07-13 08:06:30 +00:00
|
|
|
include_once($CFG->libdir.'/grade/grade_item.php');
|
2007-06-08 09:01:19 +00:00
|
|
|
$commitstart = time(); // start time in case we need to roll back
|
|
|
|
$newitemids = array(); // array to hold new grade_item ids from grade_import_newitem table, mapping array
|
2007-07-18 19:56:07 +00:00
|
|
|
|
2007-06-08 09:01:19 +00:00
|
|
|
/// first select distinct new grade_items with this batch
|
2007-07-18 19:56:07 +00:00
|
|
|
|
|
|
|
if ($newitems = get_records_sql("SELECT *
|
2007-06-08 09:01:19 +00:00
|
|
|
FROM {$CFG->prefix}grade_import_newitem
|
|
|
|
WHERE import_code = $importcode")) {
|
2007-07-18 19:56:07 +00:00
|
|
|
|
|
|
|
// instances of the new grade_items created, cached
|
2007-06-08 09:01:19 +00:00
|
|
|
// in case grade_update fails, so that we can remove them
|
|
|
|
$instances = array();
|
2007-09-26 10:00:51 +00:00
|
|
|
$failed = false;
|
2007-06-08 09:01:19 +00:00
|
|
|
foreach ($newitems as $newitem) {
|
|
|
|
// get all grades with this item
|
2007-07-18 19:56:07 +00:00
|
|
|
|
|
|
|
if ($grades = get_records('grade_import_values', 'newgradeitem', $newitem->id)) {
|
2007-09-26 10:00:51 +00:00
|
|
|
/// create a new grade item for this - must use false as second param!
|
|
|
|
/// TODO: we need some bounds here too
|
|
|
|
$gradeitem = new grade_item(array('courseid'=>$courseid, 'itemtype'=>'manual', 'itemname'=>$newitem->itemname), false);
|
|
|
|
$gradeitem->insert('import');
|
|
|
|
$instances[] = $gradeitem;
|
2007-07-13 08:06:30 +00:00
|
|
|
|
|
|
|
// insert each individual grade to this new grade item
|
2007-07-18 19:56:07 +00:00
|
|
|
foreach ($grades as $grade) {
|
2007-09-26 10:00:51 +00:00
|
|
|
if (!$gradeitem->update_final_grade($grade->userid, $grade->finalgrade, 'import', NULL, $grade->feedback)) {
|
|
|
|
$failed = true;
|
|
|
|
break 2;
|
2007-06-08 09:01:19 +00:00
|
|
|
}
|
2007-07-13 08:06:30 +00:00
|
|
|
}
|
2007-06-08 09:01:19 +00:00
|
|
|
}
|
2007-07-17 03:32:10 +00:00
|
|
|
}
|
2007-09-26 10:00:51 +00:00
|
|
|
|
|
|
|
if ($failed) {
|
|
|
|
foreach ($instances as $instance) {
|
|
|
|
$gradeitem->delete('import');
|
|
|
|
}
|
|
|
|
import_cleanup($importcode);
|
|
|
|
return false;
|
|
|
|
}
|
2007-06-08 09:01:19 +00:00
|
|
|
}
|
2007-07-17 03:32:10 +00:00
|
|
|
|
2007-06-08 09:01:19 +00:00
|
|
|
/// then find all existing items
|
|
|
|
|
2007-07-18 19:56:07 +00:00
|
|
|
if ($gradeitems = get_records_sql("SELECT DISTINCT (itemid)
|
2007-06-08 09:01:19 +00:00
|
|
|
FROM {$CFG->prefix}grade_import_values
|
2007-07-13 08:06:30 +00:00
|
|
|
WHERE import_code = $importcode
|
|
|
|
AND itemid > 0")) {
|
|
|
|
|
2007-06-08 09:01:19 +00:00
|
|
|
$modifieditems = array();
|
2007-07-18 19:56:07 +00:00
|
|
|
|
2007-07-13 08:06:30 +00:00
|
|
|
foreach ($gradeitems as $itemid=>$iteminfo) {
|
2007-07-18 19:56:07 +00:00
|
|
|
|
2007-07-13 08:06:30 +00:00
|
|
|
if (!$gradeitem = new grade_item(array('id'=>$itemid))) {
|
|
|
|
// not supposed to happen, but just in case
|
|
|
|
import_cleanup($importcode);
|
|
|
|
return false;
|
2007-06-08 09:01:19 +00:00
|
|
|
}
|
|
|
|
// get all grades with this item
|
2007-07-13 08:06:30 +00:00
|
|
|
if ($grades = get_records('grade_import_values', 'itemid', $itemid)) {
|
2007-07-17 03:32:10 +00:00
|
|
|
|
2007-08-13 04:19:01 +00:00
|
|
|
// make the grades array for update_grade
|
2007-06-08 09:01:19 +00:00
|
|
|
foreach ($grades as $grade) {
|
2007-10-07 10:22:21 +00:00
|
|
|
if (!$importfeedback) {
|
|
|
|
$grade->feedback = false; // ignore it
|
|
|
|
}
|
2007-09-26 10:00:51 +00:00
|
|
|
if (!$gradeitem->update_final_grade($grade->userid, $grade->finalgrade, 'import', NULL, $grade->feedback)) {
|
2007-07-13 08:06:30 +00:00
|
|
|
$failed = 1;
|
2007-07-17 03:32:10 +00:00
|
|
|
break 2;
|
2007-07-13 08:06:30 +00:00
|
|
|
}
|
2007-06-08 09:01:19 +00:00
|
|
|
}
|
2007-07-17 03:32:10 +00:00
|
|
|
//$itemdetails -> idnumber = $gradeitem->idnumber;
|
|
|
|
$modifieditems[] = $itemid;
|
2007-07-13 08:06:30 +00:00
|
|
|
|
2007-07-17 03:32:10 +00:00
|
|
|
}
|
2007-07-18 19:56:07 +00:00
|
|
|
|
2007-07-13 08:06:30 +00:00
|
|
|
if (!empty($failed)) {
|
|
|
|
import_cleanup($importcode);
|
2007-07-17 03:32:10 +00:00
|
|
|
return false;
|
|
|
|
}
|
2007-06-08 09:01:19 +00:00
|
|
|
}
|
|
|
|
}
|
2007-07-17 03:32:10 +00:00
|
|
|
|
2007-10-07 10:22:21 +00:00
|
|
|
if ($verbose) {
|
2007-09-26 09:42:28 +00:00
|
|
|
notify(get_string('importsuccess', 'grades'), 'notifysuccess');
|
|
|
|
print_continue($CFG->wwwroot.'/grade/index.php?id='.$courseid);
|
|
|
|
}
|
2007-06-08 09:01:19 +00:00
|
|
|
// clean up
|
|
|
|
import_cleanup($importcode);
|
2007-09-26 09:42:28 +00:00
|
|
|
|
|
|
|
return true;
|
2007-06-08 09:01:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* removes entries from grade import buffer tables grade_import_value and grade_import_newitem
|
|
|
|
* after a successful import, or during an import abort
|
|
|
|
* @param string importcode - import batch identifier
|
|
|
|
*/
|
|
|
|
function import_cleanup($importcode) {
|
2007-07-17 03:32:10 +00:00
|
|
|
// remove entries from buffer table
|
2007-06-08 09:01:19 +00:00
|
|
|
delete_records('grade_import_values', 'import_code', $importcode);
|
2007-07-17 03:32:10 +00:00
|
|
|
delete_records('grade_import_newitem', 'import_code', $importcode);
|
2007-06-08 09:01:19 +00:00
|
|
|
}
|
2007-06-11 09:00:17 +00:00
|
|
|
|
|
|
|
/// Returns the file as one big long string
|
|
|
|
function my_file_get_contents($filename, $use_include_path = 0) {
|
|
|
|
|
|
|
|
$data = "";
|
|
|
|
$file = @fopen($filename, "rb", $use_include_path);
|
|
|
|
if ($file) {
|
|
|
|
while (!feof($file)) {
|
|
|
|
$data .= fread($file, 1024);
|
|
|
|
}
|
|
|
|
fclose($file);
|
|
|
|
}
|
|
|
|
return $data;
|
|
|
|
}
|
2007-08-13 04:19:01 +00:00
|
|
|
?>
|