Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge branch 'MDL-31981_22' of git://github.com/timhunt/moodle into M…

…OODLE_22_STABLE
  • Loading branch information...
commit a7844302882ea7cd70c4f36fe2805d98760164ef 2 parents 08b6184 + 0aa433a
@stronk7 stronk7 authored
View
22 question/type/shortanswer/question.php
@@ -101,6 +101,28 @@ public static function compare_string_with_wildcard($string, $pattern, $ignoreca
return preg_match($regexp, trim($string));
}
+ public function get_correct_response() {
+ $response = parent::get_correct_response();
+ if ($response) {
+ $response['answer'] = $this->clean_response($response['answer']);
+ }
+ return $response;
+ }
+
+ public function clean_response($answer) {
+ // Break the string on non-escaped asterisks.
+ $bits = preg_split('/(?<!\\\\)\*/', $answer);
+
+ // Unescape *s in the bits.
+ $cleanbits = array();
+ foreach ($bits as $bit) {
+ $cleanbits[] = str_replace('\*', '*', $bit);
+ }
+
+ // Put it back together with spaces to look nice.
+ return trim(implode(' ', $cleanbits));
+ }
+
public function check_file_access($qa, $options, $component, $filearea,
$args, $forcedownload) {
if ($component == 'question' && $filearea == 'answerfeedback') {
View
3  question/type/shortanswer/renderer.php
@@ -117,6 +117,7 @@ public function correct_response(question_attempt $qa) {
return '';
}
- return get_string('correctansweris', 'qtype_shortanswer', s($answer->answer));
+ return get_string('correctansweris', 'qtype_shortanswer',
+ s($question->clean_response($answer->answer)));
}
}
View
23 question/type/shortanswer/simpletest/helper.php
@@ -34,7 +34,7 @@
*/
class qtype_shortanswer_test_helper extends question_test_helper {
public function get_test_questions() {
- return array('frogtoad', 'frogonly');
+ return array('frogtoad', 'frogonly', 'escapedwildcards');
}
/**
@@ -130,4 +130,25 @@ public function get_shortanswer_question_data_frogonly() {
return $qdata;
}
+
+ /**
+ * Makes a shortanswer question with just the correct ansewer 'frog', and
+ * no other answer matching.
+ * @return qtype_shortanswer_question
+ */
+ public function make_shortanswer_question_escapedwildcards() {
+ question_bank::load_question_definition_classes('shortanswer');
+ $sa = new qtype_shortanswer_question();
+ test_question_maker::initialise_a_question($sa);
+ $sa->name = 'Question with escaped * in the answer.';
+ $sa->questiontext = 'How to you write x times y in C? __________';
+ $sa->generalfeedback = 'In C, this expression is written x * y.';
+ $sa->usecase = false;
+ $sa->answers = array(
+ 13 => new question_answer(13, '*x\*y*', 1.0, 'Well done.', FORMAT_HTML),
+ );
+ $sa->qtype = question_bank::get_qtype('shortanswer');
+
+ return $sa;
+ }
}
View
6 question/type/shortanswer/simpletest/testquestion.php
@@ -145,6 +145,12 @@ public function test_get_correct_response() {
$question->get_correct_response());
}
+ public function test_get_correct_response_escapedwildcards() {
+ $question = test_question_maker::make_question('shortanswer', 'escapedwildcards');
+
+ $this->assertEqual(array('answer' => 'x*y'), $question->get_correct_response());
+ }
+
public function test_get_question_summary() {
$sa = test_question_maker::make_question('shortanswer');
$qsummary = $sa->get_question_summary();
Please sign in to comment.
Something went wrong with that request. Please try again.