mirror of
https://github.com/moodle/moodle.git
synced 2025-04-13 04:22:07 +02:00
MDL-82466 reportbuilder: new format helper for formatting time.
This commit is contained in:
parent
554a790bf0
commit
8426610258
7
.upgradenotes/MDL-82466-2024071510553022.yml
Normal file
7
.upgradenotes/MDL-82466-2024071510553022.yml
Normal file
@ -0,0 +1,7 @@
|
||||
issueNumber: MDL-82466
|
||||
notes:
|
||||
core_reportbuilder:
|
||||
- message: >
|
||||
New format helper `format_time` method, for use in column callbacks that
|
||||
represent a duration of time (e.g. "3 days 4 hours")
|
||||
type: improved
|
@ -183,15 +183,7 @@ class task_log extends base {
|
||||
->set_type(column::TYPE_FLOAT)
|
||||
->add_field("{$tablealias}.timeend - {$tablealias}.timestart", 'duration')
|
||||
->set_is_sortable(true)
|
||||
->add_callback(static function(float $value): string {
|
||||
$duration = round($value, 2);
|
||||
if (empty($duration)) {
|
||||
// The format_time function returns 'now' when the difference is exactly 0.
|
||||
// Note: format_time performs concatenation in exactly this fashion so we should do this for consistency.
|
||||
return '0 ' . get_string('secs', 'moodle');
|
||||
}
|
||||
return format_time($duration);
|
||||
});
|
||||
->add_callback([format::class, 'format_time'], 2);
|
||||
|
||||
// Hostname column.
|
||||
$columns[] = (new column(
|
||||
|
@ -145,12 +145,11 @@ class enrol extends base {
|
||||
->set_type(column::TYPE_TIMESTAMP)
|
||||
->add_fields("{$enrolalias}.enrolperiod")
|
||||
->set_is_sortable(true)
|
||||
->set_callback(static function(?int $enrolperiod): string {
|
||||
if (!$enrolperiod) {
|
||||
->set_callback(static function(?int $enrolperiod, stdClass $row): string {
|
||||
if ($enrolperiod === 0) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return format_time($enrolperiod);
|
||||
return format::format_time($enrolperiod, $row);
|
||||
});
|
||||
|
||||
// Start date column.
|
||||
|
@ -41,6 +41,25 @@ class format {
|
||||
return $value ? userdate($value, $format) : '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns formatted time duration (e.g. "3 days 4 hours")
|
||||
*
|
||||
* @param float|null $value
|
||||
* @param stdClass $row
|
||||
* @param int|null $precision
|
||||
* @return string
|
||||
*/
|
||||
public static function format_time(?float $value, stdClass $row, ?int $precision = 0): string {
|
||||
if ($value === null) {
|
||||
return '';
|
||||
}
|
||||
$value = round($value, (int) $precision);
|
||||
if ($value === 0.0) {
|
||||
return '0 ' . get_string('secs', 'moodle');
|
||||
}
|
||||
return format_time($value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns yes/no string depending on the given value
|
||||
*
|
||||
|
@ -29,7 +29,7 @@ use stdClass;
|
||||
* @copyright 2021 Paul Holden <paulh@moodle.com>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class format_test extends advanced_testcase {
|
||||
final class format_test extends advanced_testcase {
|
||||
|
||||
/**
|
||||
* Test userdate method
|
||||
@ -41,12 +41,39 @@ class format_test extends advanced_testcase {
|
||||
$this->assertEquals(userdate($now), $userdate);
|
||||
}
|
||||
|
||||
/**
|
||||
* Data provider for {@see test_format_time}
|
||||
*
|
||||
* @return array[]
|
||||
*/
|
||||
public static function format_time_provider(): array {
|
||||
return [
|
||||
[null, 0, ''],
|
||||
[0, 0, '0 secs'],
|
||||
[2.456, 1, '2.5 secs'],
|
||||
[3.2, null, '3 secs'],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Test format time
|
||||
*
|
||||
* @param float|null $value
|
||||
* @param int|null $precision
|
||||
* @param string $expected
|
||||
*
|
||||
* @dataProvider format_time_provider
|
||||
*/
|
||||
public function test_format_time(?float $value, ?int $precision, string $expected): void {
|
||||
$this->assertEquals($expected, format::format_time($value, (object) [], $precision));
|
||||
}
|
||||
|
||||
/**
|
||||
* Data provider for {@see test_boolean_as_text}
|
||||
*
|
||||
* @return array
|
||||
* @return array[]
|
||||
*/
|
||||
public function boolean_as_text_provider(): array {
|
||||
public static function boolean_as_text_provider(): array {
|
||||
return [
|
||||
[false, get_string('no')],
|
||||
[true, get_string('yes')],
|
||||
|
Loading…
x
Reference in New Issue
Block a user