some fixes for gradebook imports
This commit is contained in:
+25
-55
@@ -1,11 +1,17 @@
|
||||
<?php
|
||||
|
||||
require_once('../../../config.php');
|
||||
|
||||
// capability check
|
||||
$id = required_param('id', PARAM_INT); // course id
|
||||
// require_capability('moodle/site:uploadusers', get_context_instance(CONTEXT_SYSTEM));
|
||||
$course = get_record('course', 'id', $id); // actual course
|
||||
|
||||
// capability check
|
||||
require_capability('moodle/course:managegrades', get_context_instance(CONTEXT_COURSE, $course->id));
|
||||
|
||||
require_once('../grade_import_form.php');
|
||||
require_once($CFG->dirroot.'/grade/lib.php');
|
||||
require_once('../lib.php');
|
||||
|
||||
// sort out delimiter
|
||||
$csv_encode = '/\&\#44/';
|
||||
if (isset($CFG->CSV_DELIMITER)) {
|
||||
$csv_delimiter = '\\' . $CFG->CSV_DELIMITER;
|
||||
@@ -19,24 +25,19 @@ if (isset($CFG->CSV_DELIMITER)) {
|
||||
$csv_delimiter2 = ",";
|
||||
}
|
||||
|
||||
require_once('../grade_import_form.php');
|
||||
require_once($CFG->dirroot.'/grade/lib.php');
|
||||
require_once('../lib.php');
|
||||
|
||||
$course = get_record('course', 'id', $id);
|
||||
$action = 'importcsv';
|
||||
print_header($course->shortname.': '.get_string('grades'), $course->fullname, grade_nav($course, $action));
|
||||
|
||||
$mform = new grade_import_form();
|
||||
|
||||
//if ($formdata = $mform2->get_data() ) {
|
||||
|
||||
if (($formdata = data_submitted()) && !empty($formdata->map)) {
|
||||
// i am not able to get the mapping[] and map[] array using the following line
|
||||
// they are somehow not returned with get_data()
|
||||
// if ($formdata = $mform2->get_data()) {
|
||||
|
||||
|
||||
if (($formdata = data_submitted()) && !empty($formdata->map)) {
|
||||
|
||||
// if mapping informatioin is supplied
|
||||
|
||||
foreach ($formdata->maps as $i=>$header) {
|
||||
$map[$header] = $formdata->mapping[$i];
|
||||
}
|
||||
@@ -59,7 +60,7 @@ if (($formdata = data_submitted()) && !empty($formdata->map)) {
|
||||
if ($fp = fopen($filename, "r")) {
|
||||
|
||||
// use current time stamp
|
||||
$importcode = time();
|
||||
$importcode = time();
|
||||
|
||||
// --- get header (field names) ---
|
||||
$header = split($csv_delimiter, fgets($fp,1024));
|
||||
@@ -73,10 +74,10 @@ if (($formdata = data_submitted()) && !empty($formdata->map)) {
|
||||
// add something
|
||||
$line = split($csv_delimiter, fgets($fp,1024));
|
||||
|
||||
// each line is a student record
|
||||
unset ($studentid);
|
||||
unset ($studentgrades);
|
||||
// array to hold all grades to be inserted
|
||||
$newgrades = array();
|
||||
|
||||
// each line is a student record
|
||||
foreach ($line as $key => $value) {
|
||||
//decode encoded commas
|
||||
$value = preg_replace($csv_encode,$csv_delimiter2,trim($value));
|
||||
@@ -119,16 +120,16 @@ if (($formdata = data_submitted()) && !empty($formdata->map)) {
|
||||
default:
|
||||
// existing grade items
|
||||
if (!empty($map[$header[$key]])) {
|
||||
// case of an id, only maps idnumber of a grade_item
|
||||
//$studentgrades[$map[$header[$key]]] = $value;
|
||||
|
||||
// case of an id, only maps idnumber of a grade_item
|
||||
include_once($CFG->libdir.'/grade/grade_item.php');
|
||||
$gradeitem = new grade_item(array('idnumber'=>$map[$header[$key]]));
|
||||
unset($newgrade);
|
||||
$newgrade -> itemid = $gradeitem->id;
|
||||
$newgrade -> gradevalue = $value;
|
||||
$newgrades[] = $newgrade;
|
||||
} // otherwise, we ignore this column altogether (e.g. institution, address etc)
|
||||
} // otherwise, we ignore this column altogether
|
||||
// because user has chosen to ignore them (e.g. institution, address etc)
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -146,39 +147,21 @@ if (($formdata = data_submitted()) && !empty($formdata->map)) {
|
||||
$newgrade->userid = $studentid;
|
||||
insert_record('grade_import_values', $newgrade);
|
||||
}
|
||||
}
|
||||
|
||||
/// put all the imported grades for this user into grade_import_values table
|
||||
|
||||
/*
|
||||
if (!empty($studentgrades)) {
|
||||
foreach ($studentgrades as $idnumber => $studentgrade) {
|
||||
|
||||
unset($eventdata);
|
||||
$eventdata->idnumber = $idnumber;
|
||||
$eventdata->userid = $studentid;
|
||||
$eventdata->gradevalue = $studentgrade;
|
||||
events_trigger('grade_update_request', $eventdata);
|
||||
|
||||
debugging("triggering event for $idnumber... student id is $studentid and grade is $studentgrade");
|
||||
}
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
/// at this stage if things are all ok, we commit the changes from temp table
|
||||
/// via events
|
||||
grade_import_commit($course->id, $importcode);
|
||||
|
||||
|
||||
// temporary file can go now
|
||||
unlink($filename);
|
||||
} else {
|
||||
error ('import file '.$filename.' not readable');
|
||||
}
|
||||
|
||||
} else if ($formdata = $mform->get_data() ) {
|
||||
|
||||
} else if ($formdata = $mform->get_data()) {
|
||||
// else if file is just uploaded
|
||||
|
||||
$filename = $mform->get_userfile_name();
|
||||
|
||||
// Large files are likely to take their time and memory. Let PHP know
|
||||
@@ -207,25 +190,12 @@ if (($formdata = data_submitted()) && !empty($formdata->map)) {
|
||||
// --- get header (field names) ---
|
||||
$header = split($csv_delimiter, fgets($fp,1024));
|
||||
|
||||
// print mapping form
|
||||
// display the mapping form with header info processed
|
||||
$mform2 = new grade_import_mapping_form(qualified_me(), array('id'=>$id, 'header'=>$header, 'filename'=>$filename));
|
||||
$mform2->display();
|
||||
} else {
|
||||
// display the standard upload file form
|
||||
$mform->display();
|
||||
}
|
||||
print_footer();
|
||||
|
||||
function my_file_get_contents($filename, $use_include_path = 0) {
|
||||
/// Returns the file as one big long string
|
||||
|
||||
$data = "";
|
||||
$file = @fopen($filename, "rb", $use_include_path);
|
||||
if ($file) {
|
||||
while (!feof($file)) {
|
||||
$data .= fread($file, 1024);
|
||||
}
|
||||
fclose($file);
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
?>
|
||||
@@ -129,4 +129,18 @@ function import_cleanup($importcode) {
|
||||
delete_records('grade_import_values', 'import_code', $importcode);
|
||||
delete_records('grade_import_newitem', 'import_code', $importcode);
|
||||
}
|
||||
|
||||
/// 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;
|
||||
}
|
||||
?>
|
||||
+87
-46
@@ -1,37 +1,32 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
This is development code, and it is not finished
|
||||
|
||||
$form = new custom_form_subclass(qualified_me(), array('somefield' => 'somevalue', 'someotherfield' => 'someothervalue') );
|
||||
and then in your subclass, in definition, you can access
|
||||
$this->_customdata['somefield']
|
||||
*/
|
||||
|
||||
/**
|
||||
* code in development
|
||||
* does xml plugin need some flexibility/mapping of columns?
|
||||
*/
|
||||
require_once('../../../config.php');
|
||||
|
||||
// capability check
|
||||
$id = required_param('id', PARAM_INT); // course id
|
||||
// require_capability('moodle/site:uploadusers', get_context_instance(CONTEXT_SYSTEM));
|
||||
$course = get_record('course', 'id', $id); // actual course
|
||||
|
||||
// capability check
|
||||
require_capability('moodle/course:managegrades', get_context_instance(CONTEXT_COURSE, $course->id));
|
||||
|
||||
require_once('../lib.php');
|
||||
require_once('../grade_import_form.php');
|
||||
|
||||
|
||||
require_once($CFG->dirroot.'/grade/lib.php');
|
||||
$course = get_record('course', 'id', $id);
|
||||
|
||||
$action = 'importxml';
|
||||
print_header($course->shortname.': '.get_string('grades'), $course->fullname, grade_nav($course, $action));
|
||||
|
||||
$mform = new grade_import_form();
|
||||
|
||||
//$mform2 = new grade_import_mapping_form();
|
||||
if ( $formdata = $mform->get_data()) {
|
||||
|
||||
//if ($formdata = $mform2->get_data() ) {
|
||||
|
||||
if ( $formdata = $mform->get_data() ) {
|
||||
// array to hold all grades to be inserted
|
||||
$newgrades = array();
|
||||
|
||||
$filename = $mform->get_userfile_name();
|
||||
|
||||
// Large files are likely to take their time and memory. Let PHP know
|
||||
// that we'll take longer, and that the process should be recycled soon
|
||||
// to free up memory.
|
||||
@@ -42,47 +37,93 @@ if ( $formdata = $mform->get_data() ) {
|
||||
}
|
||||
|
||||
$text = my_file_get_contents($filename);
|
||||
|
||||
// trim utf-8 bom
|
||||
$textlib = new textlib();
|
||||
// converts to propert unicode
|
||||
$text = $textlib->convert($text, $formdata->encoding);
|
||||
$text = $textlib->trim_utf8_bom($text);
|
||||
// Fix mac/dos newlines
|
||||
$text = preg_replace('!\r\n?!',"\n",$text);
|
||||
|
||||
// text is the text, we should xmlize it
|
||||
include_once($CFG->dirroot.'/lib/xmlize.php');
|
||||
$content = xmlize($text);
|
||||
include_once($CFG->dirroot.'/lib/xmlize.php');
|
||||
$content = xmlize($text);
|
||||
|
||||
if ($results = $content['results']['#']['result']) {
|
||||
|
||||
// import batch identifier timestamp
|
||||
$importcode = time();
|
||||
$status = true;
|
||||
|
||||
foreach ($results as $result) {
|
||||
unset ($eventdata);
|
||||
|
||||
include_once($CFG->libdir.'/grade/grade_item.php');
|
||||
if (!$gradeitem = new grade_item(array('idnumber'=>$result['#']['assignment'][0]['#']))) {
|
||||
// gradeitem does not exist
|
||||
// no data in temp table so far, abort
|
||||
$status = false;
|
||||
break;
|
||||
}
|
||||
|
||||
$eventdata->idnumber = $result['#']['assignment'][0]['#'];
|
||||
$eventdata->userid = $result['#']['student'][0]['#'];
|
||||
$eventdata->gradevalue = $result['#']['score'][0]['#'];
|
||||
|
||||
/* TODO: use grade_update() instead
|
||||
trigger_event('grade_updated_external', $eventdata);
|
||||
echo "<br/>triggering event for $eventdata->idnumber... student id is $eventdata->userid and grade is $eventdata->gradevalue";
|
||||
*/
|
||||
}
|
||||
}
|
||||
|
||||
unset($newgrade);
|
||||
|
||||
if (isset($result['#']['score'][0]['#'])) {
|
||||
$newgrade -> itemid = $gradeitem->id;
|
||||
$newgrade -> gradevalue = $result['#']['score'][0]['#'];
|
||||
$newgrade-> userid = $result['#']['student'][0]['#'];
|
||||
$newgrades[] = $newgrade;
|
||||
}
|
||||
}
|
||||
|
||||
// loop through info collected so far
|
||||
if ($status && !empty($newgrades)) {
|
||||
foreach ($newgrades as $newgrade) {
|
||||
|
||||
// check if user exist
|
||||
if (!$user = get_record('user', 'id', $newgrade->userid)) {
|
||||
// no user found, abort
|
||||
$status = false;
|
||||
import_cleanup($importcode);
|
||||
notify(get_string('baduserid', 'grade'));
|
||||
notify(get_string('importfailed', 'grade'));
|
||||
break;
|
||||
}
|
||||
|
||||
// check grade value is a numeric grade
|
||||
if (!is_numeric($newgrade->gradevalue)) {
|
||||
$status = false;
|
||||
import_cleanup($importcode);
|
||||
notify(get_string('badgrade', 'grade'));
|
||||
break;
|
||||
}
|
||||
|
||||
// insert this grade into a temp table
|
||||
$newgrade->import_code = $importcode;
|
||||
if (!insert_record('grade_import_values', $newgrade)) {
|
||||
$status = false;
|
||||
// could not insert into temp table
|
||||
import_cleanup($importcode);
|
||||
notify(get_string('importfailed', 'grade'));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// if all ok, we proceed
|
||||
if ($status) {
|
||||
/// comit the code if we are up this far
|
||||
grade_import_commit($id, $importcode);
|
||||
}
|
||||
} else {
|
||||
// no results section found in xml,
|
||||
// assuming bad format, abort import
|
||||
notify('badxmlformat', 'grade');
|
||||
}
|
||||
} else {
|
||||
// display the standard upload file form
|
||||
$mform->display();
|
||||
}
|
||||
|
||||
print_footer();
|
||||
|
||||
function my_file_get_contents($filename, $use_include_path = 0) {
|
||||
/// Returns the file as one big long string
|
||||
|
||||
$data = "";
|
||||
$file = @fopen($filename, "rb", $use_include_path);
|
||||
if ($file) {
|
||||
while (!feof($file)) {
|
||||
$data .= fread($file, 1024);
|
||||
}
|
||||
fclose($file);
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
?>
|
||||
Reference in New Issue
Block a user