Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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

…OODLE_21_STABLE
  • Loading branch information...
commit b434431b38e3dbd1243d5a17db9ed29badc41067 2 parents 0149696 + 09a5476
Eloy Lafuente (stronk7) stronk7 authored
22 question/type/shortanswer/question.php
@@ -101,6 +101,28 @@ public static function compare_string_with_wildcard($string, $pattern, $ignoreca
101 101 return preg_match($regexp, trim($string));
102 102 }
103 103
  104 + public function get_correct_response() {
  105 + $response = parent::get_correct_response();
  106 + if ($response) {
  107 + $response['answer'] = $this->clean_response($response['answer']);
  108 + }
  109 + return $response;
  110 + }
  111 +
  112 + public function clean_response($answer) {
  113 + // Break the string on non-escaped asterisks.
  114 + $bits = preg_split('/(?<!\\\\)\*/', $answer);
  115 +
  116 + // Unescape *s in the bits.
  117 + $cleanbits = array();
  118 + foreach ($bits as $bit) {
  119 + $cleanbits[] = str_replace('\*', '*', $bit);
  120 + }
  121 +
  122 + // Put it back together with spaces to look nice.
  123 + return trim(implode(' ', $cleanbits));
  124 + }
  125 +
104 126 public function check_file_access($qa, $options, $component, $filearea,
105 127 $args, $forcedownload) {
106 128 if ($component == 'question' && $filearea == 'answerfeedback') {
3  question/type/shortanswer/renderer.php
@@ -117,6 +117,7 @@ public function correct_response(question_attempt $qa) {
117 117 return '';
118 118 }
119 119
120   - return get_string('correctansweris', 'qtype_shortanswer', s($answer->answer));
  120 + return get_string('correctansweris', 'qtype_shortanswer',
  121 + s($question->clean_response($answer->answer)));
121 122 }
122 123 }
23 question/type/shortanswer/simpletest/helper.php
@@ -34,7 +34,7 @@
34 34 */
35 35 class qtype_shortanswer_test_helper extends question_test_helper {
36 36 public function get_test_questions() {
37   - return array('frogtoad', 'frogonly');
  37 + return array('frogtoad', 'frogonly', 'escapedwildcards');
38 38 }
39 39
40 40 /**
@@ -130,4 +130,25 @@ public function get_shortanswer_question_data_frogonly() {
130 130
131 131 return $qdata;
132 132 }
  133 +
  134 + /**
  135 + * Makes a shortanswer question with just the correct ansewer 'frog', and
  136 + * no other answer matching.
  137 + * @return qtype_shortanswer_question
  138 + */
  139 + public function make_shortanswer_question_escapedwildcards() {
  140 + question_bank::load_question_definition_classes('shortanswer');
  141 + $sa = new qtype_shortanswer_question();
  142 + test_question_maker::initialise_a_question($sa);
  143 + $sa->name = 'Question with escaped * in the answer.';
  144 + $sa->questiontext = 'How to you write x times y in C? __________';
  145 + $sa->generalfeedback = 'In C, this expression is written x * y.';
  146 + $sa->usecase = false;
  147 + $sa->answers = array(
  148 + 13 => new question_answer(13, '*x\*y*', 1.0, 'Well done.', FORMAT_HTML),
  149 + );
  150 + $sa->qtype = question_bank::get_qtype('shortanswer');
  151 +
  152 + return $sa;
  153 + }
133 154 }
6 question/type/shortanswer/simpletest/testquestion.php
@@ -145,6 +145,12 @@ public function test_get_correct_response() {
145 145 $question->get_correct_response());
146 146 }
147 147
  148 + public function test_get_correct_response_escapedwildcards() {
  149 + $question = test_question_maker::make_question('shortanswer', 'escapedwildcards');
  150 +
  151 + $this->assertEqual(array('answer' => 'x*y'), $question->get_correct_response());
  152 + }
  153 +
148 154 public function test_get_question_summary() {
149 155 $sa = test_question_maker::make_question('shortanswer');
150 156 $qsummary = $sa->get_question_summary();

0 comments on commit b434431

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