Merge branch 'MDL-75358-master' of https://github.com/marinaglancy/moodle

This commit is contained in:
Jun Pataleta
2022-10-31 10:10:16 +08:00
8 changed files with 37 additions and 12 deletions
@@ -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;
@@ -76,6 +76,9 @@ class avg extends base {
* @return mixed
*/
public static function format_value($value, array $values, array $callbacks, int $columntype) {
if (reset($values) === null) {
return null;
}
if ($columntype === column::TYPE_BOOLEAN || empty($callbacks)) {
return format_float((float) reset($values), 1);
}
@@ -111,6 +111,11 @@ class groupconcat extends base {
* @return mixed
*/
public static function format_value($value, array $values, array $callbacks, int $columntype) {
$firstvalue = reset($values);
if ($firstvalue === null) {
return '';
}
$formattedvalues = [];
// Store original names of all values that would be present without aggregation.
@@ -118,7 +123,7 @@ class groupconcat extends base {
$valuenamescount = count($valuenames);
// Loop over each extracted value from the concatenated string.
$values = explode(self::FIELD_VALUE_DELIMETER, (string) reset($values));
$values = explode(self::FIELD_VALUE_DELIMETER, (string)$firstvalue);
foreach ($values as $value) {
// Ensure we have equal number of value names/data, account for truncation by DB.
@@ -73,6 +73,9 @@ class percent extends base {
* @return string
*/
public static function format_value($value, array $values, array $callbacks, int $columntype): string {
if (reset($values) === null) {
return '';
}
return format::percent((float) reset($values));
}
}
@@ -76,9 +76,13 @@ class sum extends base {
* @return mixed
*/
public static function format_value($value, array $values, array $callbacks, int $columntype) {
$firstvalue = reset($values);
if ($firstvalue === null) {
return null;
}
if ($columntype === column::TYPE_BOOLEAN || empty($callbacks)) {
$decimalpoints = (int) ($columntype === column::TYPE_FLOAT);
return format_float((float) reset($values), $decimalpoints);
return format_float((float) $firstvalue, $decimalpoints);
}
return parent::format_value($value, $values, $callbacks, $columntype);
+12 -6
View File
@@ -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));
}
}
@@ -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) {
@@ -206,7 +206,7 @@ class user_profile_fields_test extends core_reportbuilder_testcase {
$this->assertEquals([
[
'c0_firstname' => 'Admin',
'c1_data' => 'No',
'c1_data' => '',
'c2_data' => 'Not set',
'c3_data' => '',
'c4_data' => '',