Skip to content

Commit

Permalink
Merge branch 'MDL-69246' of https://github.com/timhunt/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewnicols committed Sep 3, 2020
2 parents 0d1e8aa + de3d216 commit aa3342b
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 17 deletions.
2 changes: 1 addition & 1 deletion question/behaviour/behaviourbase.php
Original file line number Diff line number Diff line change
Expand Up @@ -784,7 +784,7 @@ public static function adjust_fraction($fraction, $certainty) {
// errors for people who have used TGM's patches.
return 0;
}
if ($fraction <= 0.00000005) {
if ($fraction <= question_utils::MARK_TOLERANCE) {
return self::$wrongscore[$certainty];
} else {
return self::$rightscore[$certainty] * $fraction;
Expand Down
10 changes: 10 additions & 0 deletions question/engine/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -790,6 +790,16 @@ public function __construct($qubaid, $slot, $postdata) {
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
abstract class question_utils {
/**
* @var float tolerance to use when comparing question mark/fraction values.
*
* When comparing floating point numbers in a computer, the representation is not
* necessarily exact. Therefore, we need to allow a tolerance.
* Question marks are stored in the database as decimal numbers with 7 decimal places.
* Therefore, this is the appropriate tolerance to use.
*/
const MARK_TOLERANCE = 0.00000005;

/**
* Tests to see whether two arrays have the same keys, with the same values
* (as compared by ===) for each key. However, the order of the arrays does
Expand Down
5 changes: 3 additions & 2 deletions question/engine/questionattempt.php
Original file line number Diff line number Diff line change
Expand Up @@ -695,7 +695,7 @@ public function get_fraction() {
/** @return bool whether this question attempt has a non-zero maximum mark. */
public function has_marks() {
// Since grades are stored in the database as NUMBER(12,7).
return $this->maxmark >= 0.00000005;
return $this->maxmark >= question_utils::MARK_TOLERANCE;
}

/**
Expand Down Expand Up @@ -1158,7 +1158,8 @@ public function validate_manual_mark($currentmark) {
}

$maxmark = $this->get_max_mark();
if ($mark > $maxmark * $this->get_max_fraction() || $mark < $maxmark * $this->get_min_fraction()) {
if ($mark > $maxmark * $this->get_max_fraction() + question_utils::MARK_TOLERANCE ||
$mark < $maxmark * $this->get_min_fraction() - question_utils::MARK_TOLERANCE) {
return get_string('manualgradeoutofrange', 'question');
}

Expand Down
54 changes: 40 additions & 14 deletions question/engine/tests/questionattempt_with_steps_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,19 +170,45 @@ public function test_cannot_get_max_fraction_before_start() {
$qa->get_max_fraction();
}

public function test_validate_manual_mark() {
$this->qa->set_min_fraction(0);
$this->qa->set_max_fraction(1);
$this->assertSame('', $this->qa->validate_manual_mark(null));
$this->assertSame('', $this->qa->validate_manual_mark(''));
$this->assertSame('', $this->qa->validate_manual_mark('0'));
$this->assertSame('', $this->qa->validate_manual_mark('0.0'));
$this->assertSame('', $this->qa->validate_manual_mark('2,0'));
$this->assertSame(get_string('manualgradeinvalidformat', 'question'),
$this->qa->validate_manual_mark('frog'));
$this->assertSame(get_string('manualgradeoutofrange', 'question'),
$this->qa->validate_manual_mark('2.1'));
$this->assertSame(get_string('manualgradeoutofrange', 'question'),
$this->qa->validate_manual_mark('-0,01'));
/**
* Test cases for {@see test_validate_manual_mark()}.
*
* @return array test cases
*/
public function validate_manual_mark_cases(): array {
// Recall, the DB schema stores question grade information to 7 decimal places.
return [
[0, 1, 2, null, ''],
[0, 1, 2, '', ''],
[0, 1, 2, '0', ''],
[0, 1, 2, '0.0', ''],
[0, 1, 2, '2,0', ''],
[0, 1, 2, 'frog', get_string('manualgradeinvalidformat', 'question')],
[0, 1, 2, '2.1', get_string('manualgradeoutofrange', 'question')],
[0, 1, 2, '-0,01', get_string('manualgradeoutofrange', 'question')],
[-0.3333333, 1, 0.75, '0.75', ''],
[-0.3333333, 1, 0.75, '0.7500001', get_string('manualgradeoutofrange', 'question')],
[-0.3333333, 1, 0.75, '-0.25', ''],
[-0.3333333, 1, 0.75, '-0.2500001', get_string('manualgradeoutofrange', 'question')],
];
}

/**
* Test validate_manual_mark.
*
* @dataProvider validate_manual_mark_cases
*
* @param float $minfraction minimum fraction for the question being attempted.
* @param float $maxfraction maximum fraction for the question being attempted.
* @param float $maxmark marks for the question attempt.
* @param string|null $currentmark submitted mark.
* @param string $expectederror expected error, if any.
*/
public function test_validate_manual_mark(float $minfraction, float $maxfraction,
float $maxmark, ?string $currentmark, string $expectederror) {
$this->qa->set_min_fraction($minfraction);
$this->qa->set_max_fraction($maxfraction);
$this->qa->set_max_mark($maxmark);
$this->assertSame($expectederror, $this->qa->validate_manual_mark($currentmark));
}
}

0 comments on commit aa3342b

Please sign in to comment.