From ba9cb0848aa36d9feae34578b0b75d621d930ded Mon Sep 17 00:00:00 2001 From: Tim Hunt Date: Wed, 14 Dec 2011 14:29:00 +0000 Subject: [PATCH] MDL-30734 question engine: sum_usage_marks_subquery edge-case. When all qas in a useage are 'gaveup' state, it gives NULL, not 0.0, for the total. --- question/engine/datalib.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/question/engine/datalib.php b/question/engine/datalib.php index 2f41b12d7ff..68a378813a2 100644 --- a/question/engine/datalib.php +++ b/question/engine/datalib.php @@ -851,7 +851,11 @@ ORDER BY * @return string SQL code for the subquery. */ public function sum_usage_marks_subquery($qubaid) { - return "SELECT SUM(qa.maxmark * qas.fraction) + // To explain the COALESCE in the following SQL: SUM(lots of NULLs) gives + // NULL, while SUM(one 0.0 and lots of NULLS) gives 0.0. We don't want that. + // We always want to return a number, so the COALESCE is there to turn the + // NULL total into a 0. + return "SELECT COALESCE(SUM(qa.maxmark * qas.fraction), 0) FROM {question_attempts} qa JOIN {question_attempt_steps} qas ON qas.id = ( SELECT MAX(summarks_qas.id)