MDL-45003 qtype numeric : classify invalid response as no response.

This commit is contained in:
James Pratt 2014-04-11 10:53:34 +07:00
parent 1a727e121e
commit f4555f88c8
2 changed files with 26 additions and 8 deletions

View File

@ -172,7 +172,7 @@ class qtype_numerical_question extends question_graded_automatically {
/**
* Get an answer that contains the feedback and fraction that should be
* awarded for this resonse.
* awarded for this response.
* @param number $value the numerical value of a response.
* @param number $multiplier for the unit the student gave, if any. When no
* unit was given, or an unrecognised unit was given, $multiplier will be null.
@ -188,7 +188,7 @@ class qtype_numerical_question extends question_graded_automatically {
} else {
$scaledvalue = $value;
}
foreach ($this->answers as $aid => $answer) {
foreach ($this->answers as $answer) {
if ($answer->within_tolerance($scaledvalue)) {
$answer->unitisright = !is_null($multiplier);
return $answer;
@ -266,7 +266,11 @@ class qtype_numerical_question extends question_graded_automatically {
$resp = $this->ap->add_unit($resp, $unit);
}
if (!$ans) {
if ($value === null) {
// Invalid response shown as no response (but show actual response).
return array($this->id => new question_classified_response(null, $resp, 0));
} else if (!$ans) {
// Does not match any answer.
return array($this->id => new question_classified_response(0, $resp, 0));
}

View File

@ -214,9 +214,10 @@ class qtype_numerical_question_test extends advanced_testcase {
$this->assertEquals(array(
new question_classified_response(13, '3.14', 1.0)),
$num->classify_response(array('answer' => '3.14')));
// Invalid response.
$this->assertEquals(array(
question_classified_response::no_response()),
$num->classify_response(array('answer' => '')));
new question_classified_response(null, 'abc', 0.0)),
$num->classify_response(array('answer' => 'abc')));
}
public function test_classify_response_no_star() {
@ -230,9 +231,10 @@ class qtype_numerical_question_test extends advanced_testcase {
$this->assertEquals(array(
new question_classified_response(0, '42', 0.0)),
$num->classify_response(array('answer' => '42')));
// Invalid response.
$this->assertEquals(array(
question_classified_response::no_response()),
$num->classify_response(array('answer' => '')));
new question_classified_response(null, 'abc', 0.0)),
$num->classify_response(array('answer' => 'abc')));
}
public function test_classify_response_unit() {
@ -260,6 +262,10 @@ class qtype_numerical_question_test extends advanced_testcase {
$this->assertEquals(array(
question_classified_response::no_response()),
$num->classify_response(array('answer' => '')));
// Invalid response.
$this->assertEquals(array(
new question_classified_response(null, 'abc m', 0.0)),
$num->classify_response(array('answer' => 'abc', 'unit' => 'm')));
}
public function test_classify_response_unit_no_star() {
@ -279,6 +285,10 @@ class qtype_numerical_question_test extends advanced_testcase {
$this->assertEquals(array(
question_classified_response::no_response()),
$num->classify_response(array('answer' => '', 'unit' => '')));
// Invalid response.
$this->assertEquals(array(
new question_classified_response(null, 'abc m', 0.0)),
$num->classify_response(array('answer' => 'abc', 'unit' => 'm')));
}
public function test_classify_response_currency() {
@ -291,5 +301,9 @@ class qtype_numerical_question_test extends advanced_testcase {
$this->assertEquals(array(
new question_classified_response(13, '1 332', 0.8)),
$num->classify_response(array('answer' => '1 332')));
// Invalid response.
$this->assertEquals(array(
new question_classified_response(null, '$abc', 0.0)),
$num->classify_response(array('answer' => '$abc')));
}
}
}