From f115f8c858394a55ce60a8bc091c51c12fc747ab Mon Sep 17 00:00:00 2001 From: toyomoyo Date: Tue, 12 Jun 2007 09:12:07 +0000 Subject: [PATCH] adding some strings and checks for grade book import/export --- grade/export/grade_export_form.php | 2 +- grade/export/lib.php | 8 ++- grade/export/txt/grade_export_txt_form.php | 6 +- grade/import/csv/index.php | 84 ++++++++++++++++++---- grade/import/grade_import_form.php | 16 ++--- grade/import/lib.php | 2 +- grade/import/xml/index.php | 8 +-- grade/lib.php | 2 +- lang/en_utf8/grades.php | 20 ++++++ 9 files changed, 114 insertions(+), 34 deletions(-) diff --git a/grade/export/grade_export_form.php b/grade/export/grade_export_form.php index 7e86fae45a31a..cde373fc1a26a 100755 --- a/grade/export/grade_export_form.php +++ b/grade/export/grade_export_form.php @@ -6,7 +6,7 @@ function definition (){ global $CFG; include_once($CFG->libdir.'/pear/HTML/QuickForm/advcheckbox.php'); $mform =& $this->_form; - $mform->addElement('header', 'general', 'Gradeitems to be included'); // TODO: localize + $mform->addElement('header', 'general', get_string('gradeitemsinc', 'grades')); // TODO: localize $id = $this->_customdata['id']; // course id $mform->addElement('hidden', 'id', $id); if ($grade_items = grade_get_items($id)) { diff --git a/grade/export/lib.php b/grade/export/lib.php index 303cd53d62e7c..ed8b8387d6d07 100755 --- a/grade/export/lib.php +++ b/grade/export/lib.php @@ -115,8 +115,11 @@ function grade_export($id, $itemids = '') { // if grade_item ids are specified if ($itemids) { foreach ($itemids as $iid) { - $params->id = $iid; - $gradeitems[] = new grade_item($params); + + if ($iid) { + $params->id = $iid; + $gradeitems[] = new grade_item($params); + } } } else { // else we get all items for this course @@ -124,7 +127,6 @@ function grade_export($id, $itemids = '') { } if ($gradeitems) { - foreach ($gradeitems as $gradeitem) { $gradeitem -> generate_final(); diff --git a/grade/export/txt/grade_export_txt_form.php b/grade/export/txt/grade_export_txt_form.php index fba7b486b2341..ec0156555eb14 100755 --- a/grade/export/txt/grade_export_txt_form.php +++ b/grade/export/txt/grade_export_txt_form.php @@ -19,9 +19,9 @@ function definition (){ include_once($CFG->libdir.'/pear/HTML/QuickForm/radio.php'); $radio = array(); - $radio[] = &MoodleQuickForm::createElement('radio', 'separator', null, get_string('tab'), 'tab'); - $radio[] = &MoodleQuickForm::createElement('radio', 'separator', null, get_string('comma'), 'comma'); - $mform->addGroup($radio, 'separator', get_string('separator'), ' ', false); + $radio[] = &MoodleQuickForm::createElement('radio', 'separator', null, get_string('septab', 'grades'), 'tab'); + $radio[] = &MoodleQuickForm::createElement('radio', 'separator', null, get_string('sepcomma', 'grades'), 'comma'); + $mform->addGroup($radio, 'separator', get_string('separator', 'grades'), ' ', false); $mform->setDefault('separator', 'comma'); $this->add_action_buttons(false, get_string('submit')); diff --git a/grade/import/csv/index.php b/grade/import/csv/index.php index 6679cd63925c6..7e6ee73cf0b47 100755 --- a/grade/import/csv/index.php +++ b/grade/import/csv/index.php @@ -70,6 +70,8 @@ } $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)); @@ -83,19 +85,42 @@ $value = preg_replace($csv_encode,$csv_delimiter2,trim($value)); switch ($map[$header[$key]]) { - case 'userid': // + case 'userid': // + if (!$user = get_record('user','id', $value)) { + // user not found, abort whold import + import_cleanup($importcode); + notify("user mapping error, could not find user with id \"$value\""); + $status = false; + break 3; + } $studentid = $value; break; case 'useridnumber': - $user = get_record('user', 'idnumber', $value); + if (!$user = get_record('user', 'idnumber', $value)) { + // user not found, abort whold import + import_cleanup($importcode); + notify("user mapping error, could not find user with idnumber \"$value\""); + $status = false; + break 3; + } $studentid = $user->id; break; case 'useremail': - $user = get_record('user', 'email', $value); + if (!$user = get_record('user', 'email', $value)) { + import_cleanup($importcode); + notify("user mapping error, could not find user with email address \"$value\""); + $status = false; + break 3; + } $studentid = $user->id; break; case 'username': - $user = get_record('user', 'username', $value); + 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; + } $studentid = $user->id; break; case 'new': @@ -104,8 +129,15 @@ if (empty($newgradeitems[$key])) { $newgradeitem->itemname = $header[$key]; - $newgradeitem->import_code = $importcode; - $newgradeitems[$key] = insert_record('grade_import_newitem', $newgradeitem); + $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; + } // add this to grade_import_newitem table // add the new id to $newgradeitem[$key] } @@ -113,17 +145,33 @@ $newgrade -> newgradeitem = $newgradeitems[$key]; $newgrade -> gradevalue = $value; $newgrades[] = $newgrade; - // if not, put it in + // if not, put it in // else, insert grade into the table break; default: // existing grade items if (!empty($map[$header[$key]])) { + // non numeric grade value supplied, possibly mapped wrong column + if (!is_numeric($value)) { + $status = false; + import_cleanup($importcode); + notify(get_string('badgrade', 'grades')); + break 3; + } + // 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]])); + if (!$gradeitem = new grade_item(array('idnumber'=>$map[$header[$key]]))) { + // supplied bad mapping, should not be possible since user + // had to pick mapping + $status = false; + import_cleanup($importcode); + notify(get_string('importfailed', 'grades')); + break 3; + } + unset($newgrade); $newgrade -> itemid = $gradeitem->id; $newgrade -> gradevalue = $value; @@ -133,11 +181,14 @@ break; } } - + + // no user mapping supplied at all, or user mapping failed if (empty($studentid) || !is_numeric($studentid)) { // user not found, abort whold import + $status = false; import_cleanup($importcode); - error('user mapping error, could not find user!'); + notify('user mapping error, could not find user!'); + break; } // insert results of this students into buffer @@ -145,14 +196,21 @@ foreach ($newgrades as $newgrade) { $newgrade->import_code = $importcode; $newgrade->userid = $studentid; - insert_record('grade_import_values', $newgrade); + if (!insert_record('grade_import_values', $newgrade)) { + // could not insert into temporary table + $status = false; + import_cleanup($importcode); + notify(get_string('importfailed', 'grades')); + break 2; + } } } } /// at this stage if things are all ok, we commit the changes from temp table - grade_import_commit($course->id, $importcode); - + if ($status) { + grade_import_commit($course->id, $importcode); + } // temporary file can go now unlink($filename); } else { diff --git a/grade/import/grade_import_form.php b/grade/import/grade_import_form.php index e955de4eb07aa..4b33d81a75e05 100755 --- a/grade/import/grade_import_form.php +++ b/grade/import/grade_import_form.php @@ -7,17 +7,17 @@ function definition (){ // course id needs to be passed for auth purposes $mform->addElement('hidden', 'id', optional_param('id')); - $mform->addElement('header', 'general', get_string('importfile')); + $mform->addElement('header', 'general', get_string('importfile', 'grades')); // file upload $mform->addElement('file', 'userfile', get_string('file')); $mform->addRule('userfile', null, 'required'); $textlib = new textlib(); $encodings = $textlib->get_encodings(); - $mform->addElement('select', 'encoding', get_string('encoding'), $encodings); + $mform->addElement('select', 'encoding', get_string('encoding', 'grades'), $encodings); $options = array('10'=>10, '20'=>20, '100'=>100, '1000'=>1000, '100000'=>100000); $mform->addElement('select', 'previewrows', 'Preview rows', $options); // TODO: localize - $this->add_action_buttons(false, get_string('uploadgrades')); + $this->add_action_buttons(false, get_string('uploadgrades', 'grades')); } function get_userfile_name(){ @@ -44,7 +44,7 @@ function definition () { // course id $id = $this->_customdata['id']; - $mform->addElement('header', 'general', get_string('identifier')); + $mform->addElement('header', 'general', get_string('identifier', 'grades')); $mapfromoptions = array(); if ($header) { @@ -52,14 +52,14 @@ function definition () { $mapfromoptions[$h] = $h; } } - $mform->addElement('select', 'mapfrom', get_string('mapfrom'), $mapfromoptions); + $mform->addElement('select', 'mapfrom', get_string('mapfrom', 'grades'), $mapfromoptions); //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'), $maptooptions); + $mform->addElement('select', 'mapto', get_string('mapto', 'grades'), $maptooptions); - $mform->addElement('header', 'general', get_string('mappings')); + $mform->addElement('header', 'general', get_string('mappings', 'grades')); $gradeitems = array(); @@ -97,7 +97,7 @@ function definition () { //echo ''; $mform->addElement('hidden', 'filename', $newfilename); - $this->add_action_buttons(false, get_string('uploadgrades')); + $this->add_action_buttons(false, get_string('uploadgrades', 'grades')); } } diff --git a/grade/import/lib.php b/grade/import/lib.php index 47fbdd89a9204..1eaec1956ac6f 100755 --- a/grade/import/lib.php +++ b/grade/import/lib.php @@ -113,7 +113,7 @@ function grade_import_commit($courseid, $importcode) { } } - notify(get_string('importsuccess')); + notify(get_string('importsuccess', 'grades')); print_continue($CFG->wwwroot.'/course/view.php?id='.$courseid); // clean up import_cleanup($importcode); diff --git a/grade/import/xml/index.php b/grade/import/xml/index.php index f9b2e4a420ebd..ab6083134fd0f 100755 --- a/grade/import/xml/index.php +++ b/grade/import/xml/index.php @@ -85,8 +85,8 @@ // no user found, abort $status = false; import_cleanup($importcode); - notify(get_string('baduserid', 'grade')); - notify(get_string('importfailed', 'grade')); + notify(get_string('baduserid', 'grades')); + notify(get_string('importfailed', 'grades')); break; } @@ -94,7 +94,7 @@ if (!is_numeric($newgrade->gradevalue)) { $status = false; import_cleanup($importcode); - notify(get_string('badgrade', 'grade')); + notify(get_string('badgrade', 'grades')); break; } @@ -104,7 +104,7 @@ $status = false; // could not insert into temp table import_cleanup($importcode); - notify(get_string('importfailed', 'grade')); + notify(get_string('importfailed', 'grades')); break; } } diff --git a/grade/lib.php b/grade/lib.php index 12c184576022f..319da4d77b823 100644 --- a/grade/lib.php +++ b/grade/lib.php @@ -1163,7 +1163,7 @@ function grade_nav($course, $action='grades') { case 'exportxls': case 'importcsv': case 'importxml': - $strcurpage = get_string($action); + $strcurpage = get_string($action, 'grades'); break; default: unset($strcurpage); diff --git a/lang/en_utf8/grades.php b/lang/en_utf8/grades.php index d574c577ea43f..41d1d35eebe0b 100644 --- a/lang/en_utf8/grades.php +++ b/lang/en_utf8/grades.php @@ -8,6 +8,8 @@ $string['allgrades'] = 'All grades by category'; $string['allstudents'] = 'All Students'; $string['average'] = 'Average'; +$string['badgrade'] = 'Supplied grade is invalid'; +$string['baduser'] = 'Supplied user is invalid'; $string['bonuspoints'] = 'Bonus Points'; $string['categories'] = 'Categories'; $string['category'] = 'Category'; @@ -25,12 +27,16 @@ $string['dropped'] = 'Dropped'; $string['dropxlowest'] = 'Drop X Lowest'; $string['dropxlowestwarning'] = 'Note: If you use drop x lowest the grading assumes that all items in the category have the same point value. If point values differ results will be unpredictable'; +$string['encoding'] = 'Encoding'; $string['errorgradevaluenonnumeric'] = 'Received non-numeric for low or high grade for'; $string['errornocategorizedid'] = 'Could not get an uncategorized id!'; $string['errornocourse'] = 'Could not get course information'; $string['errorreprintheadersnonnumeric'] = 'Received non-numeric value for reprint-headers'; $string['exceptions'] = 'Exceptions'; $string['excluded'] = 'Excluded'; +$string['exportods'] = 'Export ODS'; +$string['exporttxt'] = 'Export TXT'; +$string['exportxml'] = 'Export XML'; $string['extracredit'] = 'Extra Credit'; $string['extracreditwarning'] = 'Note: Setting all items for a category to extra credit will effectively remove them from the grade calculation. Since there will be no point total'; $string['forelementtypes'] = ' for the selected $a'; @@ -43,6 +49,7 @@ $string['gradehelp'] = 'Grade Help'; $string['gradeitem'] = 'Grade Item'; $string['gradeitemlocked'] = 'Grading locked'; +$string['gradeitemsinc'] = 'Grade items to be included'; $string['gradeitemaddusers'] = 'Exclude from Grading'; $string['gradeitemmembersselected'] = 'Excluded from Grading'; $string['gradeitemnonmembers'] = 'Included in Grading'; @@ -59,6 +66,12 @@ $string['highgradeascending'] = 'Sort by high grade ascending'; $string['highgradedescending'] = 'Sort by high grade descending'; $string['highgradeletter'] = 'High'; +$string['identifier'] = 'Identify user by'; +$string['importcsv'] = 'Import CSV'; +$string['importfailed'] = 'Import failed'; +$string['importfile'] = 'Import file'; +$string['importsuccess'] = 'Grade import success'; +$string['importxml'] = 'Import XML'; $string['incorrectcourseid'] = 'Course ID was incorrect'; $string['item'] = 'Item'; $string['items'] = 'Items'; @@ -68,8 +81,11 @@ $string['lock'] = 'Lock'; $string['lowest'] = 'Lowest'; $string['lowgradeletter'] = 'Low'; +$string['mapfrom'] = 'Map from'; +$string['mapto'] = 'Map to'; $string['max'] = 'Highest'; $string['maxgrade'] = 'Max Grade'; +$string['mappings'] = 'Grade item mappings'; $string['median'] = 'Median'; $string['min'] = 'Lowest'; $string['mode'] = 'Mode'; @@ -103,6 +119,9 @@ $string['savepreferences'] = 'Save Preferences'; $string['scaledpct'] = 'Scaled %%'; $string['selectdestination'] = 'Select destination of $a'; +$string['septab'] = 'Tab'; +$string['sepcomma'] = 'Comma'; +$string['separator'] = 'Separator'; $string['setcategories'] = 'Set Categories'; $string['setcategorieserror'] = 'You must first set the categories for your course before you can give weights to them.'; $string['setgradeletters'] = 'Set Grade Letters'; @@ -126,6 +145,7 @@ $string['totalweightnot100'] = 'The total weight is not equal to 100'; $string['uncategorised'] = 'Uncategorised'; $string['unlock'] = 'Unlock'; +$string['uploadgrades'] = 'Upload grades'; $string['useadvanced'] = 'Use Advanced Features'; $string['usepercent'] = 'Use Percent'; $string['useweighted'] = 'Use Weighted';