Skip to content

Commit

Permalink
Merge branch 'MDL-75358-master' of https://github.com/marinaglancy/mo…
Browse files Browse the repository at this point in the history
  • Loading branch information
junpataleta committed Oct 31, 2022
2 parents 04d8fc6 + 4bb042c commit 9bdfcc4
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 12 deletions.
4 changes: 2 additions & 2 deletions course/classes/reportbuilder/local/formatters/completion.php
Expand Up @@ -63,11 +63,11 @@ public static function completion_progress(?string $value, stdClass $row): strin
/**
* 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;
Expand Down
3 changes: 3 additions & 0 deletions reportbuilder/classes/local/aggregation/avg.php
Expand Up @@ -76,6 +76,9 @@ public static function get_field_sql(string $field, int $columntype): string {
* @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);
}
Expand Down
7 changes: 6 additions & 1 deletion reportbuilder/classes/local/aggregation/groupconcat.php
Expand Up @@ -111,14 +111,19 @@ public static function get_field_sql(string $field, int $columntype): string {
* @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.
$valuenames = array_keys($values);
$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.
Expand Down
3 changes: 3 additions & 0 deletions reportbuilder/classes/local/aggregation/percent.php
Expand Up @@ -73,6 +73,9 @@ public static function get_field_sql(string $field, int $columntype): string {
* @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));
}
}
6 changes: 5 additions & 1 deletion reportbuilder/classes/local/aggregation/sum.php
Expand Up @@ -76,9 +76,13 @@ public static function get_field_sql(string $field, int $columntype): string {
* @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);
Expand Down
18 changes: 12 additions & 6 deletions reportbuilder/classes/local/helpers/format.php
Expand Up @@ -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));
}
}
6 changes: 5 additions & 1 deletion reportbuilder/classes/local/report/column.php
Expand Up @@ -479,7 +479,8 @@ public function get_groupby_sql(): array {
* 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
*
Expand Down Expand Up @@ -649,6 +650,9 @@ private function get_values(array $row): array {
*/
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) {
Expand Down
Expand Up @@ -206,7 +206,7 @@ public function test_custom_report_content(): void {
$this->assertEquals([
[
'c0_firstname' => 'Admin',
'c1_data' => 'No',
'c1_data' => '',
'c2_data' => 'Not set',
'c3_data' => '',
'c4_data' => '',
Expand Down

0 comments on commit 9bdfcc4

Please sign in to comment.