From 7449ad786104058055635323d1ddd636e72fdfca Mon Sep 17 00:00:00 2001 From: Tim Hunt Date: Thu, 7 Jan 2021 22:12:02 +0000 Subject: [PATCH] MDL-70574 qtype_multianswer: fractional scores should be possible --- question/type/multianswer/questiontype.php | 5 ++- .../multianswer/tests/questiontype_test.php | 37 ++++++++++++------- 2 files changed, 26 insertions(+), 16 deletions(-) diff --git a/question/type/multianswer/questiontype.php b/question/type/multianswer/questiontype.php index a1f01f5554f..325aff85da0 100644 --- a/question/type/multianswer/questiontype.php +++ b/question/type/multianswer/questiontype.php @@ -257,7 +257,7 @@ class qtype_multianswer extends question_type { // ANSWER_ALTERNATIVE regexes. define('ANSWER_ALTERNATIVE_FRACTION_REGEX', - '=|%(-?[0-9]+)%'); + '=|%(-?[0-9]+(?:[.,][0-9]*)?)%'); // For the syntax '(?fraction["{$answerindex}"] = '1'; } else if ($percentile = $altregs[ANSWER_ALTERNATIVE_REGEX_PERCENTILE_FRACTION]) { - $wrapped->fraction["{$answerindex}"] = .01 * $percentile; + // Accept either decimal place character. + $wrapped->fraction["{$answerindex}"] = .01 * str_replace(',', '.', $percentile); $hasspecificfraction = true; } else { $wrapped->fraction["{$answerindex}"] = '0'; diff --git a/question/type/multianswer/tests/questiontype_test.php b/question/type/multianswer/tests/questiontype_test.php index c1d46a5fe91..e06c2e9f336 100644 --- a/question/type/multianswer/tests/questiontype_test.php +++ b/question/type/multianswer/tests/questiontype_test.php @@ -298,11 +298,10 @@ class qtype_multianswer_test extends advanced_testcase { } } } + /** * Verify that the multiplechoice variants parameters are correctly interpreted from * the question text - * - * */ public function test_questiontext_extraction_of_multiplechoice_subquestions_variants() { $questiontext = array(); @@ -310,18 +309,18 @@ class qtype_multianswer_test extends advanced_testcase { $questiontext['itemid'] = ''; $questiontext['text'] = '

Match the following cities with the correct state:

'; $q = qtype_multianswer_extract_question($questiontext); @@ -339,6 +338,16 @@ class qtype_multianswer_test extends advanced_testcase { } else if ($key == 9 || $key == 10 || $key == 11 || $key == 12) { $this->assertSame($sub->layout, qtype_multichoice_base::LAYOUT_VERTICAL); } + foreach ($sub->feedback as $key => $feedback) { + if ($feedback['text'] === 'OK') { + $this->assertEquals(1, $sub->fraction[$key]); + } else if ($feedback['text'] === 'Wrong') { + $this->assertEquals(0, $sub->fraction[$key]); + } else { + $this->assertEquals('Not really', $feedback['text']); + $this->assertEquals(0.3333333, $sub->fraction[$key]); + } + } } } }