Grades in imported questions must match grades option list.

Added option to either generate error or match to nearest valid value if they don't
This commit is contained in:
thepurpleblob 2006-03-22 16:30:14 +00:00
parent 8511669cb9
commit 76f0a33403
2 changed files with 44 additions and 2 deletions

View File

@ -37,7 +37,11 @@ class qformat_default {
return true;
}
function importprocess($filename) {
/**
*
* @PARAM $matchgrades string 'error' or 'nearest', mismatched grades handling
*/
function importprocess($filename, $matchgrades='error') {
/// Processes a given file. There's probably little need to change this
if (! $lines = $this->readdata($filename)) {
@ -52,6 +56,10 @@ class qformat_default {
notify( get_string('importingquestions','quiz',count($questions)) );
// get list of valid answer grades
$grades = get_grade_options();
$gradeoptionsfull = $grades->gradeoptionsfull;
$count = 0;
foreach ($questions as $question) { // Process and store each question
@ -59,6 +67,26 @@ class qformat_default {
echo "<hr /><p><b>$count</b>. ".stripslashes($question->questiontext)."</p>";
// check for answer grades validity (must match fixed list of grades)
$fractions = $question->fraction;
$answersvalid = true; // in case they are!
foreach ($fractions as $key => $fraction) {
$newfraction = match_grade_options($gradeoptionsfull, $fraction, $matchgrades);
if ($newfraction===false) {
$answersvalid = false;
}
else {
$fractions[$key] = $newfraction;
}
}
if (!$answersvalid) {
notify( get_string('matcherror','quiz') );
continue;
}
else {
$question->fraction = $fractions;
}
$question->category = $this->category->id;
$question->stamp = make_unique_id_code(); // Set the unique code (not to be changed)
$question->version = 1; // Original version of this question

View File

@ -19,6 +19,7 @@
$categoryid = optional_param('category', 0, PARAM_INT);
$courseid = optional_param('course', 0, PARAM_INT);
$format = optional_param('format','',PARAM_FILE);
$params->matchgrades = optional_param('matchgrades','',PARAM_ALPHA);
// get display strings
$txt = new stdClass();
@ -31,6 +32,9 @@
$txt->importfilearea = get_string('importfilearea','quiz');
$txt->importfileupload = get_string('importfileupload','quiz');
$txt->importquestions = get_string("importquestions", "quiz");
$txt->matchgrades = get_string('matchgrades','quiz');
$txt->matchgradeserror = get_string('matchgradeserror','quiz');
$txt->matchgradesnearest = get_string('matchgradesnearest','quiz');
$txt->modulename = get_string('modulename','quiz');
$txt->modulenameplural = get_string('modulenameplural','quiz');
$txt->nocategory = get_string('nocategory','quiz');
@ -41,6 +45,11 @@
$txt->uploadproblem = get_string('uploadproblem');
$txt->uploadthisfile = get_string('uploadthisfile');
// matching options
$matchgrades = array();
$matchgrades['error'] = $txt->matchgradeserror;
$matchgrades['nearest'] = $txt->matchgradesnearest;
if (!$categoryid) { // try to get category from modform
$showcatmenu = true; // will ensure that user can choose category
if (isset($SESSION->modform)) {
@ -148,7 +157,7 @@
"$CFG->wwwroot/question/import.php?courseid={$course->id}&amp;category=$category->id");
}
if (! $qformat->importprocess($importfile) ) { // Process the uploaded file
if (! $qformat->importprocess($importfile, $params->matchgrades) ) { // Process the uploaded file
error( $txt->importerror ,
"$CFG->wwwroot/question/import.php?courseid={$course->id}&amp;category=$category->id");
}
@ -220,6 +229,11 @@
<td><?php choose_from_menu($fileformatnames, "format", "gift", "");
helpbutton("import", $txt->importquestions, "quiz"); ?></td>
</tr>
<tr>
<td align="right"><?php echo $txt->matchgrades; ?></td>
<td><?php choose_from_menu($matchgrades,'matchgrades',$txt->matchgradeserror,'' );
helpbutton('matchgrades', $txt->matchgrades, 'quiz'); ?></td>
</td>
</table>
<?php
print_simple_box_end();