regular whitespace cleanup in grade code

This commit is contained in:
skodak 2007-07-18 19:56:07 +00:00
parent e1d2692a73
commit ba74762bda
21 changed files with 297 additions and 297 deletions

View file

@ -40,25 +40,25 @@ $mform = new grade_import_form();
// 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 = data_submitted()) && !empty($formdata->map)) {
// temporary file name supplied by form
$filename = $CFG->dataroot.'/temp/'.clean_param($formdata->filename, PARAM_FILE);
$filename = $CFG->dataroot.'/temp/'.clean_param($formdata->filename, PARAM_FILE);
if ($fp = fopen($filename, "r")) {
// --- get header (field names) ---
$header = split($csv_delimiter, clean_param(fgets($fp,1024), PARAM_RAW));
foreach ($header as $i => $h) {
$h = trim($h); $header[$i] = $h; // remove whitespace
}
}
} else {
error ('could not open file '.$filename);
error ('could not open file '.$filename);
}
$map = array();
// loops mapping_0, mapping_1 .. mapping_n and construct $map array
foreach ($header as $i => $head) {
$map[$i] = $formdata->{'mapping_'.$i};
$map[$i] = $formdata->{'mapping_'.$i};
}
// if mapping informatioin is supplied
@ -69,12 +69,12 @@ if (($formdata = data_submitted()) && !empty($formdata->map)) {
foreach ($map as $i=>$j) {
if ($j == 0) {
// you can have multiple ignores
continue;
continue;
} else {
if (!isset($maperrors[$j])) {
$maperrors[$j] = true;
$maperrors[$j] = true;
} else {
// collision
// collision
unlink($filename); // needs to be uploaded again, sorry
error('mapping collision detected, 2 fields maps to the same grdae item '.$j);
}
@ -89,32 +89,32 @@ if (($formdata = data_submitted()) && !empty($formdata->map)) {
if (function_exists('apache_child_terminate')) {
@apache_child_terminate();
}
// we only operate if file is readable
if ($fp = fopen($filename, "r")) {
// read the first line makes sure this doesn't get read again
$header = split($csv_delimiter, clean_param(fgets($fp,1024), PARAM_RAW));
// use current (non-conflicting) time stamp
$importcode = time();
// use current (non-conflicting) time stamp
$importcode = time();
while (get_record('grade_import_values', 'import_code', $importcode)) {
$importcode = time();
$importcode = time();
}
$newgradeitems = array(); // temporary array to keep track of what new headers are processed
$status = true;
while (!feof ($fp)) {
// add something
$line = split($csv_delimiter, fgets($fp,1024));
$line = split($csv_delimiter, fgets($fp,1024));
// array to hold all grades to be inserted
$newgrades = array();
// array to hold all feedback
$newfeedbacks = array();
$newfeedbacks = array();
// each line is a student record
foreach ($line as $key => $value) {
foreach ($line as $key => $value) {
//decode encoded commas
$value = clean_param($value, PARAM_RAW);
$value = preg_replace($csv_encode,$csv_delimiter2,trim($value));
@ -132,9 +132,9 @@ if (($formdata = data_submitted()) && !empty($formdata->map)) {
if (isset($t[1])) {
$t1 = $t[1];
} else {
$t1 = '';
$t1 = '';
}
switch ($t0) {
case 'userid': //
if (!$user = get_record('user','id', $value)) {
@ -142,7 +142,7 @@ if (($formdata = data_submitted()) && !empty($formdata->map)) {
import_cleanup($importcode);
notify("user mapping error, could not find user with id \"$value\"");
$status = false;
break 3;
break 3;
}
$studentid = $value;
break;
@ -152,7 +152,7 @@ if (($formdata = data_submitted()) && !empty($formdata->map)) {
import_cleanup($importcode);
notify("user mapping error, could not find user with idnumber \"$value\"");
$status = false;
break 3;
break 3;
}
$studentid = $user->id;
break;
@ -161,43 +161,43 @@ if (($formdata = data_submitted()) && !empty($formdata->map)) {
import_cleanup($importcode);
notify("user mapping error, could not find user with email address \"$value\"");
$status = false;
break 3;
break 3;
}
$studentid = $user->id;
$studentid = $user->id;
break;
case 'username':
if (!$user = get_record('user', 'username', $value)) {
import_cleanup($importcode);
notify("user mapping error, could not find user with username \"$value\"");
$status = false;
break 3;
break 3;
}
$studentid = $user->id;
break;
case 'new':
// first check if header is already in temp database
if (empty($newgradeitems[$key])) {
if (empty($newgradeitems[$key])) {
$newgradeitem->itemname = $header[$key];
$newgradeitem->import_code = $importcode;
$newgradeitem->import_code = $importcode;
// failed to insert into new grade item buffer
if (!$newgradeitems[$key] = insert_record('grade_import_newitem', $newgradeitem)) {
$status = false;
import_cleanup($importcode);
notify(get_string('importfailed', 'grades'));
break 3;
break 3;
}
// add this to grade_import_newitem table
// add the new id to $newgradeitem[$key]
}
// add the new id to $newgradeitem[$key]
}
unset($newgrade);
$newgrade -> newgradeitem = $newgradeitems[$key];
$newgrade -> finalgrade = $value;
$newgrade -> finalgrade = $value;
$newgrades[] = $newgrade;
// if not, put it in
// if not, put it in
// else, insert grade into the table
break;
case 'feedback':
@ -207,22 +207,22 @@ if (($formdata = data_submitted()) && !empty($formdata->map)) {
$feedback -> feedback = $value;
$newfeedbacks[] = $feedback;
}
break;
break;
default:
// existing grade items
if (!empty($map[$key]) && $value!=="") {
// non numeric grade value supplied, possibly mapped wrong column
if (!is_numeric($value)) {
echo "<br/>t0 is $t0";
echo "<br/>grade is $value";
$status = false;
$status = false;
import_cleanup($importcode);
notify(get_string('badgrade', 'grades'));
break 3;
}
// case of an id, only maps id of a grade_item
// case of an id, only maps id of a grade_item
// this was idnumber
include_once($CFG->libdir.'/grade/grade_item.php');
if (!$gradeitem = new grade_item(array('id'=>$map[$key]))) {
@ -233,20 +233,20 @@ if (($formdata = data_submitted()) && !empty($formdata->map)) {
notify(get_string('importfailed', 'grades'));
break 3;
}
// check if grade item is locked if so, abort
if ($gradeitem->locked) {
$status = false;
import_cleanup($importcode);
notify(get_string('gradeitemlocked', 'grades'));
break 3;
break 3;
}
unset($newgrade);
$newgrade -> itemid = $gradeitem->id;
$newgrade -> finalgrade = $value;
$newgrade -> finalgrade = $value;
$newgrades[] = $newgrade;
} // otherwise, we ignore this column altogether
} // otherwise, we ignore this column altogether
// because user has chosen to ignore them (e.g. institution, address etc)
break;
}
@ -263,9 +263,9 @@ if (($formdata = data_submitted()) && !empty($formdata->map)) {
// insert results of this students into buffer
if (!empty($newgrades)) {
foreach ($newgrades as $newgrade) {
// check if grade_grades is locked and if so, abort
if ($grade_grades = new grade_grades(array('itemid'=>$newgrade->itemid, 'userid'=>$studentid))) {
if ($grade_grades->locked) {
@ -305,21 +305,21 @@ if (($formdata = data_submitted()) && !empty($formdata->map)) {
}
}
/// at this stage if things are all ok, we commit the changes from temp table
/// at this stage if things are all ok, we commit the changes from temp table
if ($status) {
grade_import_commit($course->id, $importcode);
}
// temporary file can go now
unlink($filename);
} else {
error ('import file '.$filename.' not readable');
error ('import file '.$filename.' not readable');
}
} 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
// that we'll take longer, and that the process should be recycled soon
// to free up memory.
@ -331,9 +331,9 @@ if (($formdata = data_submitted()) && !empty($formdata->map)) {
$text = my_file_get_contents($filename);
// trim utf-8 bom
$textlib = new textlib();
/// normalize line endings and do the encoding conversion
$text = $textlib->convert($text, $formdata->encoding);
$textlib = new textlib();
/// normalize line endings and do the encoding conversion
$text = $textlib->convert($text, $formdata->encoding);
$text = $textlib->trim_utf8_bom($text);
// Fix mac/dos newlines
$text = preg_replace('!\r\n?!',"\n",$text);
@ -341,11 +341,11 @@ if (($formdata = data_submitted()) && !empty($formdata->map)) {
fwrite($fp,$text);
fclose($fp);
$fp = fopen($filename, "r");
$fp = fopen($filename, "r");
// --- get header (field names) ---
$header = split($csv_delimiter, clean_param(fgets($fp,1024), PARAM_RAW));
// print some preview
$numlines = 0; // 0 preview lines displayed
@ -354,11 +354,11 @@ if (($formdata = data_submitted()) && !empty($formdata->map)) {
echo '<tr>';
foreach ($header as $h) {
$h = clean_param($h, PARAM_RAW);
echo '<th>'.$h.'</th>';
echo '<th>'.$h.'</th>';
}
echo '</tr>';
while (!feof ($fp) && $numlines <= $formdata->previewrows) {
$lines = split($csv_delimiter, fgets($fp,1024));
$lines = split($csv_delimiter, fgets($fp,1024));
echo '<tr>';
foreach ($lines as $line) {
echo '<td>'.$line.'</td>';;
@ -367,7 +367,7 @@ if (($formdata = data_submitted()) && !empty($formdata->map)) {
echo '</tr>';
}
echo '</table>';
/// feeding gradeitems into the grade_import_mapping_form
include_once($CFG->libdir.'/grade/grade_item.php');
$gradeitems = array();
@ -376,11 +376,11 @@ if (($formdata = data_submitted()) && !empty($formdata->map)) {
foreach ($grade_items as $grade_item) {
// skip course type and category type
if ($grade_item->itemtype == 'course' || $grade_item->itemtype == 'category') {
continue;
}
continue;
}
// this was idnumber
$gradeitems[$grade_item->id] = $grade_item->itemname;
$gradeitems[$grade_item->id] = $grade_item->itemname;
}
}
}

View file

@ -17,7 +17,7 @@ class grade_import_form extends moodleform {
$encodings = $textlib->get_encodings();
$mform->addElement('select', 'encoding', get_string('encoding', 'grades'), $encodings);
$options = array('10'=>10, '20'=>20, '100'=>100, '1000'=>1000, '100000'=>100000);
$options = array('10'=>10, '20'=>20, '100'=>100, '1000'=>1000, '100000'=>100000);
$mform->addElement('select', 'previewrows', 'Preview rows', $options); // TODO: localize
$mform->setType('previewrows', PARAM_INT);
$this->add_action_buttons(false, get_string('uploadgrades', 'grades'));
@ -34,7 +34,7 @@ class grade_import_form extends moodleform {
}
class grade_import_mapping_form extends moodleform {
function definition () {
global $CFG;
$mform =& $this->_form;
@ -54,35 +54,35 @@ class grade_import_mapping_form extends moodleform {
}
}
$mform->addElement('select', 'mapfrom', get_string('mapfrom', 'grades'), $mapfromoptions);
//choose_from_menu($mapfromoptions, 'mapfrom');
//choose_from_menu($mapfromoptions, 'mapfrom');
$maptooptions = array('userid'=>'userid', 'username'=>'username', 'useridnumber'=>'useridnumber', 'useremail'=>'useremail', '0'=>'ignore');
//choose_from_menu($maptooptions, 'mapto');
$mform->addElement('select', 'mapto', get_string('mapto', 'grades'), $maptooptions);
$mform->addElement('header', 'general', get_string('mappings', 'grades'));
// add a comment option
if ($gradeitems = $this->_customdata['gradeitems']) {
$comments = array();
$comments = array();
foreach ($gradeitems as $itemid => $itemname) {
$comments['feedback_'.$itemid] = 'comments for '.$itemname;
}
$comments['feedback_'.$itemid] = 'comments for '.$itemname;
}
}
include_once($CFG->libdir.'/gradelib.php');
if ($header) {
if ($header) {
$i = 0; // index
foreach ($header as $h) {
$h = trim($h);
// this is what each header maps to
$mform->addElement('selectgroups',
'mapping_'.$i, s($h),
array('others'=>array('0'=>'ignore', 'new'=>'new gradeitem'),
'gradeitems'=>$gradeitems,
// this is what each header maps to
$mform->addElement('selectgroups',
'mapping_'.$i, s($h),
array('others'=>array('0'=>'ignore', 'new'=>'new gradeitem'),
'gradeitems'=>$gradeitems,
'comments'=>$comments));
$i++;
}
@ -105,7 +105,7 @@ class grade_import_mapping_form extends moodleform {
//echo '<input name="filename" value='.$newfilename.' type="hidden" />';
$mform->addElement('hidden', 'filename', $newfilename);
$mform->setType('filename', PARAM_FILE);
$this->add_action_buttons(false, get_string('uploadgrades', 'grades'));
$this->add_action_buttons(false, get_string('uploadgrades', 'grades'));
}
}

View file

@ -1,53 +1,53 @@
<?php // $Id$
/**
* given an import code, commits all entries in buffer tables
* given an import code, commits all entries in buffer tables
* (grade_import_value and grade_import_newitem)
* If this function is called, we assume that all data collected
* If this function is called, we assume that all data collected
* 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
*/
function grade_import_commit($courseid, $importcode) {
global $CFG;
include_once($CFG->libdir.'/gradelib.php');
include_once($CFG->libdir.'/grade/grade_item.php');
$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
/// first select distinct new grade_items with this batch
if ($newitems = get_records_sql("SELECT *
if ($newitems = get_records_sql("SELECT *
FROM {$CFG->prefix}grade_import_newitem
WHERE import_code = $importcode")) {
// instances of the new grade_items created, cached
// instances of the new grade_items created, cached
// in case grade_update fails, so that we can remove them
$instances = array();
foreach ($newitems as $newitem) {
// get all grades with this item
if ($grades = get_records('grade_import_values', 'newgradeitem', $newitem->id)) {
if ($grades = get_records('grade_import_values', 'newgradeitem', $newitem->id)) {
// make the grardes array for update_grade
// find the max instance number of 'manual' grade item
// and increment that number by 1 by hand
// I can not find other ways to make 'manual' type work,
// unless we have a 'new' flag for grade_update to let it
// know that this is a new grade_item, and let grade_item
// handle the instance id in the case of a 'manual' import?
if ($lastimport = get_record_sql("SELECT *
if ($lastimport = get_record_sql("SELECT *
FROM {$CFG->prefix}grade_items
WHERE courseid = $courseid
AND itemtype = 'manual'
ORDER BY iteminstance DESC", true)) {
$instance = $lastimport->iteminstance + 1;
} else {
$instance = 1;
$instance = 1;
}
$instances[] = $instance;
// if fails, deletes all the created grade_items and grades
@ -57,7 +57,7 @@ function grade_import_commit($courseid, $importcode) {
// insert each individual grade to this new grade item
$failed = 0;
foreach ($grades as $grade) {
foreach ($grades as $grade) {
if (!$gradeitem->update_final_grade($grade->userid, $grade->finalgrade, NULL, NULL, $grade->feedback)) {
$failed = 1;
break;
@ -65,7 +65,7 @@ function grade_import_commit($courseid, $importcode) {
}
if ($failed) {
foreach ($instances as $instance) {
$gradeitem = new grade_item(array('courseid'=>$courseid, 'itemtype'=>'manual', 'iteminstance'=>$instance));
$gradeitem = new grade_item(array('courseid'=>$courseid, 'itemtype'=>'manual', 'iteminstance'=>$instance));
// this method does not seem to delete all the raw grades and the item itself
// which I think should be deleted in this case, can I use sql directly here?
$gradeitem->delete();
@ -79,15 +79,15 @@ function grade_import_commit($courseid, $importcode) {
/// then find all existing items
if ($gradeitems = get_records_sql("SELECT DISTINCT (itemid)
if ($gradeitems = get_records_sql("SELECT DISTINCT (itemid)
FROM {$CFG->prefix}grade_import_values
WHERE import_code = $importcode
AND itemid > 0")) {
$modifieditems = array();
foreach ($gradeitems as $itemid=>$iteminfo) {
if (!$gradeitem = new grade_item(array('id'=>$itemid))) {
// not supposed to happen, but just in case
import_cleanup($importcode);
@ -107,7 +107,7 @@ function grade_import_commit($courseid, $importcode) {
$modifieditems[] = $itemid;
}
if (!empty($failed)) {
import_cleanup($importcode);
return false;

View file

@ -47,7 +47,7 @@ if ( $formdata = $mform->get_data()) {
// trim utf-8 bom
$textlib = new textlib();
// converts to propert unicode
$text = $textlib->convert($text, $formdata->encoding);
$text = $textlib->convert($text, $formdata->encoding);
$text = $textlib->trim_utf8_bom($text);
// Fix mac/dos newlines
$text = preg_replace('!\r\n?!',"\n",$text);
@ -55,19 +55,19 @@ if ( $formdata = $mform->get_data()) {
// text is the text, we should xmlize it
include_once($CFG->dirroot.'/lib/xmlize.php');
$content = xmlize($text);
if ($results = $content['results']['#']['result']) {
// import batch identifier timestamp
$importcode = time();
$status = true;
$numlines = 0;
// print some previews
print_heading(get_string('importpreview', 'grades'));
echo '<table cellpadding="5">';
echo '<table cellpadding="5">';
foreach ($results as $i => $result) {
if ($numlines < $formdata->previewrows && isset($results[$i+1])) {
echo '<tr>';
@ -88,14 +88,14 @@ if ( $formdata = $mform->get_data()) {
$status = false;
break;
}
// grade item locked, abort
if ($gradeitem->locked) {
$status = false;
notify(get_string('gradeitemlocked', 'grades'));
break 3;
}
break 3;
}
// check if grade_grades is locked and if so, abort
if ($grade_grades = new grade_grades(array('itemid'=>$gradeitem->id, 'userid'=>$result['#']['student'][0]['#']))) {
if ($grade_grades->locked) {
@ -114,11 +114,11 @@ if ( $formdata = $mform->get_data()) {
$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
@ -127,8 +127,8 @@ if ( $formdata = $mform->get_data()) {
notify(get_string('baduserid', 'grades'));
notify(get_string('importfailed', 'grades'));
break;
}
}
// check grade value is a numeric grade
if (!is_numeric($newgrade->rawgrade)) {
$status = false;
@ -153,11 +153,11 @@ if ( $formdata = $mform->get_data()) {
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');
notify('badxmlformat', 'grade');
}
} else {
// display the standard upload file form