From 90a36f8cdf22098a3e6128609117e94721b498aa Mon Sep 17 00:00:00 2001 From: tjhunt Date: Fri, 3 Aug 2007 14:26:46 +0000 Subject: [PATCH] MDL-10709 - Shortanswer resonses should be trimmed before being saved in the database. Merged from MOODLE_18_STABLE. --- question/type/shortanswer/questiontype.php | 30 ++++++++++------------ 1 file changed, 13 insertions(+), 17 deletions(-) diff --git a/question/type/shortanswer/questiontype.php b/question/type/shortanswer/questiontype.php index a36550f6ab..a303eb3b81 100644 --- a/question/type/shortanswer/questiontype.php +++ b/question/type/shortanswer/questiontype.php @@ -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 ('
'); print_string('correctansweris', 'quiz', s($correctanswer)); @@ -424,12 +426,6 @@ class question_shortanswer_qtype extends default_questiontype { } } } - - - - - - } //// END OF CLASS //// -- 2.39.5