Skip to content

Commit

Permalink
MDL-60632 mod_quiz: Fix chart rendering fail in quiz report
Browse files Browse the repository at this point in the history
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
  • Loading branch information
dragos5436 committed Dec 1, 2023
1 parent abba174 commit 78e3edf
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
8 changes: 8 additions & 0 deletions mod/quiz/report/reportlib.php
Expand Up @@ -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;
}

Expand Down
33 changes: 33 additions & 0 deletions mod/quiz/tests/reportlib_test.php
Expand Up @@ -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)));
}
}

0 comments on commit 78e3edf

Please sign in to comment.