Skip to content

Commit

Permalink
Merge branch 'MDL-31981_22' of git://github.com/timhunt/moodle into M…
Browse files Browse the repository at this point in the history
…OODLE_22_STABLE
  • Loading branch information
stronk7 committed Mar 13, 2012
2 parents 08b6184 + 0aa433a commit a784430
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 2 deletions.
22 changes: 22 additions & 0 deletions question/type/shortanswer/question.php
Expand Up @@ -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') {
Expand Down
3 changes: 2 additions & 1 deletion question/type/shortanswer/renderer.php
Expand Up @@ -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)));
}
}
23 changes: 22 additions & 1 deletion question/type/shortanswer/simpletest/helper.php
Expand Up @@ -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');
}

/**
Expand Down Expand Up @@ -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;
}
}
6 changes: 6 additions & 0 deletions question/type/shortanswer/simpletest/testquestion.php
Expand Up @@ -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();
Expand Down

0 comments on commit a784430

Please sign in to comment.