]> git.mjollnir.org Git - moodle.git/commitdiff
Fixes to prevent users from creating bad quiz questions with not
authormoodler <moodler>
Fri, 14 May 2004 14:36:32 +0000 (14:36 +0000)
committermoodler <moodler>
Fri, 14 May 2004 14:36:32 +0000 (14:36 +0000)
enough answers in them.

Thanks to Shane Elliott (Ikawhero)

lang/en/quiz.php
mod/quiz/lib.php
mod/quiz/question.php

index 800d60245a1e875248f87ff5eb144d0ce9346356..4c5c9e3bceea4dd27f04c0c1dce7cf6f5b1c21f3 100644 (file)
@@ -115,6 +115,7 @@ $string['noresponse'] = 'No Response';
 $string['noreview'] = 'You are not allowed to review this quiz';
 $string['noreviewuntil'] = 'You are not allowed to review this quiz until $a';
 $string['notenoughsubquestions'] = 'Not enough sub-questions have been defined!<br>Do you want to go back and fix this question?';
+$string['notenoughanswers'] = 'This type of question requires at least $a answers';
 $string['numerical'] = 'Numerical';
 $string['paragraphquestion'] = 'Paragraph Question not supported at line $a. The question will be ignored';
 $string['percentcorrect'] = 'Percent Correct';
index 49451352aca9572b0d738a81473312ac6a889acf..a73bc92220a466e1579d0f85d90d8201986ddf94 100644 (file)
@@ -2029,7 +2029,7 @@ function quiz_save_question_options($question) {
 /// be true, but it's something to keep in mind if fiddling with
 /// question.php
 ///
-/// Returns $result->error or $result->notice
+/// Returns $result->error or $result->noticeyesno or $result->notice
     
     switch ($question->qtype) {
         case SHORTANSWER:
@@ -2092,7 +2092,7 @@ function quiz_save_question_options($question) {
             /// Perform sanity checks on fractional grades
             if ($maxfraction != 1) {
                 $maxfraction = $maxfraction * 100;
-                $result->notice = get_string("fractionsnomax", "quiz", $maxfraction);
+                $result->noticeyesno = get_string("fractionsnomax", "quiz", $maxfraction);
                 return $result;
             }
         break;
@@ -2158,7 +2158,7 @@ function quiz_save_question_options($question) {
             /// Perform sanity checks on fractional grades
             if ($maxfraction != 1) {
                 $maxfraction = $maxfraction * 100;
-                $result->notice = get_string("fractionsnomax", "quiz", $maxfraction);
+                $result->noticeyesno = get_string("fractionsnomax", "quiz", $maxfraction);
                 return $result;
             }
         break;
@@ -2237,13 +2237,30 @@ function quiz_save_question_options($question) {
             if (!$oldanswers = get_records("quiz_answers", "question", $question->id, "id ASC")) {
                 $oldanswers = array();
             }
+            
+
+            // following hack to check at least two answers exist
+            $answercount = 0;
+            foreach ($question->answer as $key=>$dataanswer) {
+                if ($dataanswer != "") {
+                    $answercount++;
+                }
+            }
+            $answercount += count($oldanswers);
+            if ($answercount < 2) { // check there are at lest 2 answers for multiple choice
+                $result->notice = get_string("notenoughanswers", "quiz", "2");
+                return $result;
+            }
+            
+            
+
+            // Insert all the new answers
 
             $totalfraction = 0;
             $maxfraction = -1;
 
             $answers = array();
 
-            // Insert all the new answers
             foreach ($question->answer as $key => $dataanswer) {
                 if ($dataanswer != "") {
                     if ($answer = array_shift($oldanswers)) {  // Existing answer, so reuse it
@@ -2298,14 +2315,14 @@ function quiz_save_question_options($question) {
             if ($options->single) {
                 if ($maxfraction != 1) {
                     $maxfraction = $maxfraction * 100;
-                    $result->notice = get_string("fractionsnomax", "quiz", $maxfraction);
+                    $result->noticeyesno = get_string("fractionsnomax", "quiz", $maxfraction);
                     return $result;
                 }
             } else {
                 $totalfraction = round($totalfraction,2);
                 if ($totalfraction != 1) {
                     $totalfraction = $totalfraction * 100;
-                    $result->notice = get_string("fractionsaddwrong", "quiz", $totalfraction);
+                    $result->noticeyesno = get_string("fractionsaddwrong", "quiz", $totalfraction);
                     return $result;
                 }
             }
@@ -2317,6 +2334,23 @@ function quiz_save_question_options($question) {
                 $oldsubquestions = array();
             }
 
+
+            // following hack to check at least three answers exist
+            $answercount = 0;
+            foreach ($question->subquestions as $key=>$questiontext) {
+                $answertext = $question->subanswers[$key];
+                if (!empty($questiontext) and !empty($answertext)) {
+                    $answercount++;
+                }
+            }
+            $answercount += count($oldsubquestions);
+            if ($answercount < 3) { // check there are at lest 3 answers for matching type questions
+                $result->notice = get_string("notenoughanswers", "quiz", "3");
+                return $result;
+            }
+
+
+            
             $subquestions = array();
 
             // Insert all the new question+answer pairs
@@ -2345,7 +2379,7 @@ function quiz_save_question_options($question) {
             }
 
             if (count($subquestions) < 3) {
-                $result->notice = get_string("notenoughsubquestions", "quiz");
+                $result->noticeyesno = get_string("notenoughsubquestions", "quiz");
                 return $result;
             }
 
index baf640b9078b91dadf69d66ee40b3398b3595f3a..59d53379a6b4b47883d0fd4fc6d860bdbd26be8b 100644 (file)
             }
 
             if (!empty($result->notice)) {
-                notice_yesno($result->notice, "question.php?id=$question->id", "edit.php");
+                notice($result->notice, "question.php?id=$question->id");
+            }
+
+            if (!empty($result->noticeyesno)) {
+                notice_yesno($result->noticeyesno, "question.php?id=$question->id", "edit.php");
                 print_footer($course);
                 exit;
             }