From 76f0a33403013972b2be14596e841697fa25da14 Mon Sep 17 00:00:00 2001 From: thepurpleblob Date: Wed, 22 Mar 2006 16:30:14 +0000 Subject: [PATCH] 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 --- question/format.php | 30 +++++++++++++++++++++++++++++- question/import.php | 16 +++++++++++++++- 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/question/format.php b/question/format.php index c00d6f12c0..e6b235439d 100644 --- a/question/format.php +++ b/question/format.php @@ -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 "

$count. ".stripslashes($question->questiontext)."

"; + // 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 diff --git a/question/import.php b/question/import.php index 0b62533367..1638d3e640 100644 --- a/question/import.php +++ b/question/import.php @@ -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}&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}&category=$category->id"); } @@ -220,6 +229,11 @@ importquestions, "quiz"); ?> + + matchgrades; ?> + matchgradeserror,'' ); + helpbutton('matchgrades', $txt->matchgrades, 'quiz'); ?> +