MDL-10709 - Shortanswer resonses should be trimmed before being saved in the database...
authortjhunt <tjhunt>
Fri, 3 Aug 2007 14:26:46 +0000 (14:26 +0000)
committertjhunt <tjhunt>
Fri, 3 Aug 2007 14:26:46 +0000 (14:26 +0000)
question/type/shortanswer/questiontype.php

index a36550f6abce8b02a2a2e06a2f714701ed31d6ef..a303eb3b81c5e26d5e4491ddbe87530c41d1807d 100644 (file)
@@ -40,7 +40,7 @@ class question_shortanswer_qtype extends default_questiontype {
 
     function save_question_options($question) {
         $result = new stdClass;
-        
+
         if (!$oldanswers = get_records('question_answers', 'question', $question->id, 'id ASC')) {
             $oldanswers = array();
         }
@@ -169,8 +169,8 @@ class question_shortanswer_qtype extends default_questiontype {
                 }
             }
         }
-        
-        /// Removed correct answer, to be displayed later MDL-7496               
+
+        /// Removed correct answer, to be displayed later MDL-7496
         include("$CFG->dirroot/question/type/shortanswer/display.html");
     }
 
@@ -202,6 +202,8 @@ class question_shortanswer_qtype extends default_questiontype {
     }
 
     function test_response(&$question, $state, $answer) {
+        // Trim the response before it is saved in the database. See MDL-10709
+        $state->responses[''] = trim($state->responses['']);
         return $this->compare_string_with_wildcard(stripslashes_safe($state->responses['']),
                 $answer->answer, !$question->options->usecase);
     }
@@ -213,12 +215,12 @@ class question_shortanswer_qtype extends default_questiontype {
         $bits = array_map('preg_quote', $bits);
         // Put it back together to make the regexp.
         $regexp = '|^' . implode('.*', $bits) . '$|u';
-        
+
         // Make the match insensitive if requested to.
         if ($ignorecase) {
             $regexp .= 'i';
         }
-        
+
         return preg_match($regexp, trim($string));
     }
 
@@ -317,8 +319,8 @@ class question_shortanswer_qtype extends default_questiontype {
 
         return $status;
     }
-    
-    
+
+
         /**
     * Prints the score obtained and maximum score available plus any penalty
     * information
@@ -342,7 +344,7 @@ class question_shortanswer_qtype extends default_questiontype {
         maximum grade available and a warning if a penalty was applied for the
         attempt and displays the overall grade obtained counting all previous
         responses (and penalties) */
-        
+
         // MDL-7496 show correct answer after "Incorrect"
         $correctanswer = '';
         if ($correctanswers = $this->get_correct_responses($question, $state)) {
@@ -354,9 +356,9 @@ class question_shortanswer_qtype extends default_questiontype {
                         $delimiter = ', ';
                     }
                 }
-            }         
+            }
         }
-      
+
         if (QUESTION_EVENTDUPLICATE == $state->event) {
             echo ' ';
             print_string('duplicateresponse', 'quiz');
@@ -386,7 +388,7 @@ class question_shortanswer_qtype extends default_questiontype {
                 } else {
                     echo ' incorrect">';
                     // MDL-7496
-                    print_string('incorrect', 'quiz');                   
+                    print_string('incorrect', 'quiz');
                     if ($correctanswer) {
                         echo ('<div class="correctness">');
                         print_string('correctansweris', 'quiz', s($correctanswer));
@@ -424,12 +426,6 @@ class question_shortanswer_qtype extends default_questiontype {
             }
         }
     }
-    
-    
-    
-    
-    
-
 }
 //// END OF CLASS ////