Skip to content
Browse files

MDL-11824 - Literal asterisk (\*) in shortanswer question not handled…

… correctly by get_correct_responses + Unit tests. Backported from MOODLE_18_STABLE.

Also backported the fix for MDL-11703.
  • Loading branch information...
1 parent d7414e9 commit 40cc3341ad19f0287abf84f3ee6110c6379af38f tjhunt committed Oct 22, 2007
Showing with 16 additions and 2 deletions.
  1. +16 −2 question/type/shortanswer/questiontype.php
View
18 question/type/shortanswer/questiontype.php
@@ -207,9 +207,12 @@ function compare_string_with_wildcard($string, $pattern, $ignorecase) {
// Break the string on non-escaped asterisks.
$bits = preg_split('/(?<!\\\\)\*/', $pattern);
// Escape regexp special characters in the bits.
- $bits = array_map('preg_quote', $bits);
+ $excapedbits = array();
+ foreach ($bits as $bit) {
+ $excapedbits[] = preg_quote(str_replace('\*', '*', $bit));
+ }
// Put it back together to make the regexp.
- $regexp = '|^' . implode('.*', $bits) . '$|u';
+ $regexp = '|^' . implode('.*', $excapedbits) . '$|u';
// Make the match insensitive if requested to.
if ($ignorecase) {
@@ -219,6 +222,17 @@ function compare_string_with_wildcard($string, $pattern, $ignorecase) {
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 ////////////////////////////
/*

0 comments on commit 40cc334

Please sign in to comment.
Something went wrong with that request. Please try again.