MDL-56646 assign: Don't rescale any negative grades

This commit is contained in:
John Okely 2017-08-01 15:53:40 +08:00 committed by John Okely
parent 64b52095e5
commit e04be435e0
2 changed files with 31 additions and 1 deletions

View File

@ -1585,7 +1585,8 @@ function assign_rescale_activity_grades($course, $cm, $oldmin, $oldmax, $newmin,
'a' => $cm->instance
);
$sql = 'UPDATE {assign_grades} set grade = (((grade - :p1) * :p2) + :p3) where assignment = :a';
// Only rescale grades that are greater than or equal to 0. Anything else is a special value.
$sql = 'UPDATE {assign_grades} set grade = (((grade - :p1) * :p2) + :p3) where assignment = :a and grade >= 0';
$dbupdate = $DB->execute($sql, $params);
if (!$dbupdate) {
return false;

View File

@ -655,4 +655,33 @@ class mod_assign_lib_testcase extends mod_assign_base_testcase {
$this->assertEquals(mod_assign_get_completion_active_rule_descriptions($moddefaults), $activeruledescriptions);
$this->assertEquals(mod_assign_get_completion_active_rule_descriptions(new stdClass()), []);
}
/**
* Test that if some grades are not set, they are left alone and not rescaled
*/
public function test_assign_rescale_activity_grades_some_unset() {
$this->resetAfterTest();
// As a teacher...
$this->setUser($this->editingteachers[0]);
$assign = $this->create_instance();
// Grade the student.
$data = ['grade' => 50];
$assign->testable_apply_grade_to_user((object)$data, $this->students[0]->id, 0);
// Try getting another students grade. This will give a grade of -1.
$assign->get_user_grade($this->students[1]->id, true);
// Rescale.
assign_rescale_activity_grades($this->course, $assign->get_course_module(), 0, 100, 0, 10);
// Get the grades for both students.
$student0grade = $assign->get_user_grade($this->students[0]->id, true);
$student1grade = $assign->get_user_grade($this->students[1]->id, true);
// Make sure the real grade is scaled, but the -1 stays the same.
$this->assertEquals($student0grade->grade, 5);
$this->assertEquals($student1grade->grade, -1);
}
}