]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-11824 - Literal asterisk (\*) in shortanswer question not handled correctly by...
authortjhunt <tjhunt>
Mon, 22 Oct 2007 15:37:31 +0000 (15:37 +0000)
committertjhunt <tjhunt>
Mon, 22 Oct 2007 15:37:31 +0000 (15:37 +0000)
question/type/shortanswer/questiontype.php
question/type/shortanswer/simpletest/testquestiontype.php

index ffa1b40a5189503300ebe37fbca2483386d35166..290afc9585a5b133f2947c9eb278a70d92b0accc 100644 (file)
@@ -227,6 +227,17 @@ class question_shortanswer_qtype extends default_questiontype {
         return preg_match($regexp, trim($string));
     }
 
+    /*
+     * Override the parent class method, to remove escaping from asterisks.
+     */
+    function get_correct_responses(&$question, &$state) {
+        $response = parent::get_correct_responses($question, $state);
+        if (is_array($response)) {
+            $response[''] = addslashes(str_replace('\*', '*', stripslashes($response[''])));
+        }
+        return $response;
+    }
+
     /// BACKUP FUNCTIONS ////////////////////////////
 
     /*
index 380a97478a23bb5919471efe83bd8157bf6cf8b1..571b9ec0a6754406f6f8723b6ff5819b65a1f4e0 100644 (file)
@@ -67,6 +67,40 @@ class question_shortanswer_qtype_test extends UnitTestCase {
         $this->assertTrue($this->qtype->compare_string_with_wildcard('[a-z]', '[a-z]', false));
         $this->assertTrue($this->qtype->compare_string_with_wildcard('\{}/', '\{}/', true));
     }
+
+    function test_get_correct_responses() {
+        $answer1 = new stdClass;
+        $answer1->id = 17;
+        $answer1->answer = "frog";
+        $answer1->fraction = 1;
+        $answer2 = new stdClass;
+        $answer2->id = 23;
+        $answer2->answer = "f*g";
+        $answer2->fraction = 1;
+        $answer3 = new stdClass;
+        $answer3->id = 29;
+        $answer3->answer = "12\*13";
+        $answer3->fraction = 1;
+        $answer4 = new stdClass;
+        $answer4->id = 31;
+        $answer4->answer = "*";
+        $answer4->fraction = 0;
+        $question = new stdClass;
+        $question->options->answers = array(
+            17 => $answer1,
+            23 => $answer2,
+            29 => $answer3,
+            31 => $answer4
+        );
+        $state = new stdClass;
+        $this->assertEqual($this->qtype->get_correct_responses($question, $state), array('' => 'frog'));
+        $question->options->answers[17]->fraction = 0;
+        $this->assertEqual($this->qtype->get_correct_responses($question, $state), array('' => 'f*g'));
+        $question->options->answers[23]->fraction = 0;
+        $this->assertEqual($this->qtype->get_correct_responses($question, $state), array('' => '12*13'));
+        $question->options->answers[29]->fraction = 0;
+        $this->assertNull($this->qtype->get_correct_responses($question, $state));
+    }
 }
 
 ?>