]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-8475 - Better handling of blank answer - further changes to make it even more...
authortjhunt <tjhunt>
Thu, 28 Aug 2008 01:48:43 +0000 (01:48 +0000)
committertjhunt <tjhunt>
Thu, 28 Aug 2008 01:48:43 +0000 (01:48 +0000)
lang/en_utf8/qtype_shortanswer.php
lib/moodlelib.php
question/type/numerical/edit_numerical_form.php
question/type/numerical/questiontype.php
question/type/shortanswer/edit_shortanswer_form.php
question/type/shortanswer/questiontype.php

index f79f8cd0c9cbcee0e83c99c53c0c8b33267ebd97..9a5d85ba38797bafe145c7fab7e3f55c9a0c7573 100644 (file)
@@ -2,4 +2,5 @@
 $string['addmoreanswerblanks'] = 'Blanks for {no} More Answers';
 $string['answerno'] = 'Answer $a';
 $string['filloutoneanswer'] = 'You must provide at least one possible answer. Answers left blank will not be used. \'*\' can be used as a wildcard to match any characters. The first matching answer will be used to determine the score and feedback.';
+$string['answermustbegiven'] = 'You must enter an answer if there is a grade or feedback.';
 ?>
\ No newline at end of file
index 4a9fe3a27198812a45163dee9646e30a72618761..5b8b52489eb31fb0caa93747b32f9e6642bc4609 100644 (file)
@@ -618,6 +618,19 @@ function is_number($value) {
     }
 }
 
+/**
+ * This function is useful for testing whether something you got back from
+ * the HTML editor actually contains anything. Sometimes the HTML editor
+ * appear to be empty, but actually you get back a <br> tag or something.
+ *
+ * @param string $string a string containing HTML.
+ * @return boolean does the string contain any actual content - that is text,
+ * images, objcts, etc.
+ */
+function html_is_blank($string) {
+    return trim(strip_tags($string, '<img><object><applet><input><select><textarea><hr>')) == '';
+}
+
 /**
  * Set a key in global configuration
  *
index 62b7923ac9e6e7e6c89ee9f98c0c9858bf68b31d..ceb3376b4997d022bbade99c08eca325b0690c15 100644 (file)
@@ -124,14 +124,9 @@ class question_edit_numerical_form extends question_edit_form {
                 if ($data['fraction'][$key] == 1) {
                     $maxgrade = true;
                 }
-            } else {
-                if ($data['fraction'][$key] !=0 || trim($data['feedback'][$key]) != ''){                
-                    $errors["answer[$key]"] = get_string('answermustbenumberorstar', 'qtype_numerical');
-                    $answercount++;
-                    if (trim($data['feedback'][$key]) != ''){
-                        $errors["feedback[$key]"]=get_string('feedback','quiz').'='.htmlspecialchars(trim($data['feedback'][$key]));
-                    }
-                }
+            } else if ($data['fraction'][$key] != 0 || !html_is_blank($data['feedback'][$key])) {
+                $errors["answer[$key]"] = get_string('answermustbenumberorstar', 'qtype_numerical');
+                $answercount++;
             }
         }
         if ($answercount==0){
index 68cca25d1cac56a07e9e3dd0c9c4949b5997f634..706a3cf316bbe2b20e65ff9af2d7d5b4856e3c52 100644 (file)
@@ -108,59 +108,63 @@ class question_numerical_qtype extends question_shortanswer_qtype {
 
         // Insert all the new answers
         foreach ($question->answer as $key => $dataanswer) {
-            if ( !( trim($dataanswer)=='' && $question->fraction[$key]== 0 && trim($question->feedback[$key])=='')) {
-                $answer = new stdClass;
-                $answer->question = $question->id;
-                if (trim($dataanswer) == '*') {
-                    $answer->answer = '*';
-                } else {
-                    $answer->answer = $this->apply_unit($dataanswer, $units);
-                    if ($answer->answer === false) {
-                        $result->notice = get_string('invalidnumericanswer', 'quiz');
-                    }
-                }
-                $answer->fraction = $question->fraction[$key];
-                $answer->feedback = trim($question->feedback[$key]);
-
-                if ($oldanswer = array_shift($oldanswers)) {  // Existing answer, so reuse it
-                    $answer->id = $oldanswer->id;
-                    if (! $DB->update_record("question_answers", $answer)) {
-                        $result->error = "Could not update quiz answer! (id=$answer->id)";
-                        return $result;
-                    }
-                } else { // This is a completely new answer
-                    if (! $answer->id = $DB->insert_record("question_answers", $answer)) {
-                        $result->error = "Could not insert quiz answer!";
-                        return $result;
-                    }
+            // Check for, and ingore, completely blank answer from the form.
+            if (trim($dataanswer) == '' && $question->fraction[$key] == 0 &&
+                    html_is_blank($question->feedback[$key])) {
+                continue;
+            }
+
+            $answer = new stdClass;
+            $answer->question = $question->id;
+            if (trim($dataanswer) == '*') {
+                $answer->answer = '*';
+            } else {
+                $answer->answer = $this->apply_unit($dataanswer, $units);
+                if ($answer->answer === false) {
+                    $result->notice = get_string('invalidnumericanswer', 'quiz');
                 }
+            }
+            $answer->fraction = $question->fraction[$key];
+            $answer->feedback = trim($question->feedback[$key]);
 
-                // Set up the options object
-                if (!$options = array_shift($oldoptions)) {
-                    $options = new stdClass;
+            if ($oldanswer = array_shift($oldanswers)) {  // Existing answer, so reuse it
+                $answer->id = $oldanswer->id;
+                if (! $DB->update_record("question_answers", $answer)) {
+                    $result->error = "Could not update quiz answer! (id=$answer->id)";
+                    return $result;
                 }
-                $options->question  = $question->id;
-                $options->answer    = $answer->id;
-                if (trim($question->tolerance[$key]) == '') {
-                    $options->tolerance = '';
-                } else {
-                    $options->tolerance = $this->apply_unit($question->tolerance[$key], $units);
-                    if ($options->tolerance === false) {
-                        $result->notice = get_string('invalidnumerictolerance', 'quiz');
-                    }
+            } else { // This is a completely new answer
+                if (! $answer->id = $DB->insert_record("question_answers", $answer)) {
+                    $result->error = "Could not insert quiz answer!";
+                    return $result;
                 }
+            }
 
-                // Save options
-                if (isset($options->id)) { // reusing existing record
-                    if (! $DB->update_record('question_numerical', $options)) {
-                        $result->error = "Could not update quiz numerical options! (id=$options->id)";
-                        return $result;
-                    }
-                } else { // new options
-                    if (! $DB->insert_record('question_numerical', $options)) {
-                        $result->error = "Could not insert quiz numerical options!";
-                        return $result;
-                    }
+            // Set up the options object
+            if (!$options = array_shift($oldoptions)) {
+                $options = new stdClass;
+            }
+            $options->question  = $question->id;
+            $options->answer    = $answer->id;
+            if (trim($question->tolerance[$key]) == '') {
+                $options->tolerance = '';
+            } else {
+                $options->tolerance = $this->apply_unit($question->tolerance[$key], $units);
+                if ($options->tolerance === false) {
+                    $result->notice = get_string('invalidnumerictolerance', 'quiz');
+                }
+            }
+
+            // Save options
+            if (isset($options->id)) { // reusing existing record
+                if (! $DB->update_record('question_numerical', $options)) {
+                    $result->error = "Could not update quiz numerical options! (id=$options->id)";
+                    return $result;
+                }
+            } else { // new options
+                if (! $DB->insert_record('question_numerical', $options)) {
+                    $result->error = "Could not insert quiz numerical options!";
+                    return $result;
                 }
             }
         }
index 6df5f11a6475f3a8618e0efc4e20c361db8a757f..f40ee47863c4f2b32b59c5ed620b1d799b7d2fed 100644 (file)
@@ -81,6 +81,9 @@ class question_edit_shortanswer_form extends question_edit_form {
                 if ($data['fraction'][$key] == 1) {
                     $maxgrade = true;
                 }
+            } else if ($data['fraction'][$key] != 0 || !html_is_blank($data['feedback'][$key])) {
+                $errors["answer[$key]"] = get_string('answermustbegiven', 'qtype_shortanswer');
+                $answercount++;
             }
         }
         if ($answercount==0){
index 3322cd8e30a3c3361274bb8865f93341b2972af6..a01d1da30c807bc6e5f524bb1568da24124a6b75 100644 (file)
@@ -51,32 +51,36 @@ class question_shortanswer_qtype extends default_questiontype {
 
         // Insert all the new answers
         foreach ($question->answer as $key => $dataanswer) {
-            if ($dataanswer != "") {
-                if ($oldanswer = array_shift($oldanswers)) {  // Existing answer, so reuse it
-                    $answer = $oldanswer;
-                    $answer->answer   = trim($dataanswer);
-                    $answer->fraction = $question->fraction[$key];
-                    $answer->feedback = $question->feedback[$key];
-                    if (!$DB->update_record("question_answers", $answer)) {
-                        $result->error = "Could not update quiz answer! (id=$answer->id)";
-                        return $result;
-                    }
-                } else {    // This is a completely new answer
-                    $answer = new stdClass;
-                    $answer->answer   = trim($dataanswer);
-                    $answer->question = $question->id;
-                    $answer->fraction = $question->fraction[$key];
-                    $answer->feedback = $question->feedback[$key];
-                    if (!$answer->id = $DB->insert_record("question_answers", $answer)) {
-                        $result->error = "Could not insert quiz answer!";
-                        return $result;
-                    }
+            // Check for, and ingore, completely blank answer from the form.
+            if (trim($dataanswer) == '' && $question->fraction[$key] == 0 &&
+                    html_is_blank($question->feedback[$key])) {
+                continue;
+            }
+
+            if ($oldanswer = array_shift($oldanswers)) {  // Existing answer, so reuse it
+                $answer = $oldanswer;
+                $answer->answer   = trim($dataanswer);
+                $answer->fraction = $question->fraction[$key];
+                $answer->feedback = $question->feedback[$key];
+                if (!$DB->update_record("question_answers", $answer)) {
+                    $result->error = "Could not update quiz answer! (id=$answer->id)";
+                    return $result;
                 }
-                $answers[] = $answer->id;
-                if ($question->fraction[$key] > $maxfraction) {
-                    $maxfraction = $question->fraction[$key];
+            } else {    // This is a completely new answer
+                $answer = new stdClass;
+                $answer->answer   = trim($dataanswer);
+                $answer->question = $question->id;
+                $answer->fraction = $question->fraction[$key];
+                $answer->feedback = $question->feedback[$key];
+                if (!$answer->id = $DB->insert_record("question_answers", $answer)) {
+                    $result->error = "Could not insert quiz answer!";
+                    return $result;
                 }
             }
+            $answers[] = $answer->id;
+            if ($question->fraction[$key] > $maxfraction) {
+                $maxfraction = $question->fraction[$key];
+            }
         }
 
         if ($options = $DB->get_record("question_shortanswer", array("question" => $question->id))) {