MDL-60632 mod_quiz: Fix chart rendering fail in quiz report

If a quiz activity has negative grading enabled for incorrect answers,
students can achieve overall negative grades in this quiz. If at least
one student achieves a negative grade, the chart at the bottom of the
quiz 'Results' tab will fail to display. This patch add a search for
negative results, removes them from the band below 0 and adds them to
the 0 band. This will make the chart render correctly again.

Co-authored-by: Susana Leitão <sleitao@uporto.pt>
Co-authored-by: Matthias Opitz <m.opitz@ucl.ac.uk>

Removed whitespace at end of lines
This commit is contained in:
dragos5436 2023-12-01 14:47:14 +00:00
parent abba174fe1
commit 78e3edf3ef
2 changed files with 41 additions and 0 deletions

View file

@ -248,6 +248,14 @@ ORDER BY
$data[$bands - 1] += $data[$bands];
unset($data[$bands]);
// See MDL-60632. When a quiz participant achieves an overall negative grade the chart fails to render.
foreach ($data as $databand => $datanum) {
if ($databand < 0) {
$data["0"] += $datanum; // Add to band 0.
unset($data[$databand]); // Remove entry below 0.
}
}
return $data;
}

View file

@ -158,4 +158,37 @@ class reportlib_test extends \advanced_testcase {
$bestattempt = reset($bestattempt);
$this->assertEquals(2, $bestattempt->attempt);
}
public function test_quiz_results_never_below_zero() {
global $DB;
$this->resetAfterTest();
$quizid = 7;
$fakegrade = new \stdClass();
$fakegrade->quiz = $quizid;
// Have 5 test grades.
$fakegrade->userid = 10;
$fakegrade->grade = 6.66667;
$DB->insert_record('quiz_grades', $fakegrade);
$fakegrade->userid = 11;
$fakegrade->grade = -2.86;
$DB->insert_record('quiz_grades', $fakegrade);
$fakegrade->userid = 12;
$fakegrade->grade = 10.0;
$DB->insert_record('quiz_grades', $fakegrade);
$fakegrade->userid = 13;
$fakegrade->grade = -5.0;
$DB->insert_record('quiz_grades', $fakegrade);
$fakegrade->userid = 14;
$fakegrade->grade = 33.33333;
$DB->insert_record('quiz_grades', $fakegrade);
$data = quiz_report_grade_bands(5, 20, $quizid);
$this->assertGreaterThanOrEqual(0, min(array_keys($data)));
}
}