Merge branch 'MDL-31981' of git://github.com/timhunt/moodle

This commit is contained in:
Eloy Lafuente (stronk7) 2012-03-13 18:08:40 +01:00
commit 9b89dc4f7d
4 changed files with 52 additions and 2 deletions

View File

@ -101,6 +101,28 @@ class qtype_shortanswer_question extends question_graded_by_strategy
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 File

@ -117,6 +117,7 @@ class qtype_shortanswer_renderer extends qtype_renderer {
return '';
}
return get_string('correctansweris', 'qtype_shortanswer', s($answer->answer));
return get_string('correctansweris', 'qtype_shortanswer',
s($question->clean_response($answer->answer)));
}
}

View File

@ -34,7 +34,7 @@ defined('MOODLE_INTERNAL') || die();
*/
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 @@ class qtype_shortanswer_test_helper extends question_test_helper {
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 File

@ -145,6 +145,12 @@ class qtype_shortanswer_question_test extends UnitTestCase {
$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();