]> git.mjollnir.org Git - moodle.git/commitdiff
Grades in imported questions must match grades option list.
authorthepurpleblob <thepurpleblob>
Wed, 22 Mar 2006 16:30:14 +0000 (16:30 +0000)
committerthepurpleblob <thepurpleblob>
Wed, 22 Mar 2006 16:30:14 +0000 (16:30 +0000)
Added option to either generate error or match to nearest valid value if they don't

question/format.php
question/import.php

index c00d6f12c04798e63defd43c7818ad71b6ca3781..e6b235439ddcb91c6b6b62f8a4ca6157e90e0d09 100644 (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
index 0b62533367a3007657b6ccf204e04b1af09f2416..1638d3e640dc11a81dfc0a52cb252dcf4bb27fc8 100644 (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');
     $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)) {
                       "$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");
             }
                 <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();