From dcc44ae4c035934f5a5afb331297e20aee35d0b6 Mon Sep 17 00:00:00 2001 From: Marina Glancy Date: Tue, 2 Aug 2022 17:21:04 +0200 Subject: [PATCH] MDL-75358 reportbuilder: display nulls as empty cells for numeric/bool --- .../local/formatters/completion.php | 4 ++-- reportbuilder/classes/local/helpers/format.php | 18 ++++++++++++------ reportbuilder/classes/local/report/column.php | 6 +++++- 3 files changed, 19 insertions(+), 9 deletions(-) diff --git a/course/classes/reportbuilder/local/formatters/completion.php b/course/classes/reportbuilder/local/formatters/completion.php index ef8b7983340..c031b4e4e8f 100644 --- a/course/classes/reportbuilder/local/formatters/completion.php +++ b/course/classes/reportbuilder/local/formatters/completion.php @@ -63,11 +63,11 @@ class completion { /** * Return number of days for methods daystakingcourse and daysuntilcompletion * - * @param int $value + * @param int|null $value * @param stdClass $row * @return int|null */ - public static function get_days(int $value, stdClass $row): ?int { + public static function get_days(?int $value, stdClass $row): ?int { // Do not show anything if there is no userid. if (!$row->userid) { return null; diff --git a/reportbuilder/classes/local/helpers/format.php b/reportbuilder/classes/local/helpers/format.php index bf16017b804..644d7e76aa1 100644 --- a/reportbuilder/classes/local/helpers/format.php +++ b/reportbuilder/classes/local/helpers/format.php @@ -32,32 +32,38 @@ class format { /** * Returns formatted date. * - * @param int $value Unix timestamp + * @param int|null $value Unix timestamp * @param stdClass $row * @param string|null $format Format string for strftime * @return string */ - public static function userdate(int $value, stdClass $row, ?string $format = null): string { + public static function userdate(?int $value, stdClass $row, ?string $format = null): string { return $value ? userdate($value, $format) : ''; } /** * Returns yes/no string depending on the given value * - * @param bool $value + * @param bool|null $value * @return string */ - public static function boolean_as_text(bool $value): string { + public static function boolean_as_text(?bool $value): string { + if ($value === null) { + return ''; + } return $value ? get_string('yes') : get_string('no'); } /** * Returns float value as a percentage * - * @param float $value + * @param float|null $value * @return string */ - public static function percent(float $value): string { + public static function percent(?float $value): string { + if ($value === null) { + return ''; + } return get_string('percents', 'moodle', format_float($value)); } } diff --git a/reportbuilder/classes/local/report/column.php b/reportbuilder/classes/local/report/column.php index d6240791cfd..5db3f8dd2c4 100644 --- a/reportbuilder/classes/local/report/column.php +++ b/reportbuilder/classes/local/report/column.php @@ -479,7 +479,8 @@ final class column { * fields, and $additionalarguments are those passed on from this method): * * The type of the $value parameter passed to the callback is determined by calling {@see set_type}, this type is preserved - * if the column is part of a report source and is being aggregated + * if the column is part of a report source and is being aggregated. + * For entities that can to be left joined to a report, the first argument to their column callbacks must be nullable. * * function($value, stdClass $row[, $additionalarguments]): string * @@ -649,6 +650,9 @@ final class column { */ public static function get_default_value(array $values, int $columntype) { $value = reset($values); + if ($value === null) { + return $value; + } // Ensure default value is cast to it's strict type. switch ($columntype) {