From 78e3edf3ef34a720e55fda53eaf22f7d67d21681 Mon Sep 17 00:00:00 2001 From: dragos5436 Date: Fri, 1 Dec 2023 14:47:14 +0000 Subject: [PATCH] MDL-60632 mod_quiz: Fix chart rendering fail in quiz report MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 Co-authored-by: Matthias Opitz Removed whitespace at end of lines --- mod/quiz/report/reportlib.php | 8 ++++++++ mod/quiz/tests/reportlib_test.php | 33 +++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/mod/quiz/report/reportlib.php b/mod/quiz/report/reportlib.php index ff17ccfcafbf5..9919d8c7c4cf2 100644 --- a/mod/quiz/report/reportlib.php +++ b/mod/quiz/report/reportlib.php @@ -248,6 +248,14 @@ function quiz_report_grade_bands($bandwidth, $bands, $quizid, \core\dml\sql_join $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; } diff --git a/mod/quiz/tests/reportlib_test.php b/mod/quiz/tests/reportlib_test.php index 69432ff0eb902..289eb7f8ea676 100644 --- a/mod/quiz/tests/reportlib_test.php +++ b/mod/quiz/tests/reportlib_test.php @@ -158,4 +158,37 @@ public function test_quiz_report_qm_filter_select_first_last_best() { $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))); + } }