MDL-83052 customfield_number: do not display 0 when not set

This commit is contained in:
Marina Glancy 2024-09-09 10:56:17 +01:00
parent 1938c438ea
commit c1300fe8bc
2 changed files with 26 additions and 7 deletions

View File

@ -143,18 +143,21 @@ class field_controller extends \core_customfield\field_controller {
* @return string|null
*/
public function prepare_field_for_display(mixed $value, ?context $context = null): ?string {
if ((float) $value == 0) {
if ($value === null) {
return null;
}
$decimalplaces = (int) $this->get_configdata_property('decimalplaces');
if (round((float) $value, $decimalplaces) == 0) {
$value = $this->get_configdata_property('displaywhenzero');
if ((string) $value === '') {
return null;
}
} else {
// Let's format the value.
$decimalplaces = (int) $this->get_configdata_property('decimalplaces');
$value = format_float((float) $value, $decimalplaces);
// Apply the display format.
$format = $this->get_configdata_property('display');
$format = $this->get_configdata_property('display') ?? '{value}';
$value = str_replace('{value}', $value, $format);
}
return format_string($value, true, ['context' => $context ?? system::instance()]);

View File

@ -129,7 +129,7 @@ final class data_controller_test extends advanced_testcase {
$template = '<span class="multilang" lang="en">$ {value}</span><span class="multilang" lang="es">€ {value}</span>';
$whenzero = '<span class="multilang" lang="en">Unknown</span><span class="multilang" lang="es">Desconocido</span>';
return [
'Export float value' => [42, 42.0, [
'Export float value' => [42, '42.00', [
'decimalplaces' => 2,
'display' => '{value}',
'displaywhenzero' => 0,
@ -143,6 +143,22 @@ final class data_controller_test extends advanced_testcase {
'display' => '{value}',
'displaywhenzero' => $whenzero,
]],
'Export value when not set' => ['', null, [
'display' => '{value}',
'displaywhenzero' => $whenzero,
]],
'Export almost zero that rounds to non-zero' => [0.0009, '0.001', [
'decimalplaces' => 3,
'display' => '{value}',
'displaywhenzero' => 'Free',
]],
'Export almost zero that rounds to zero' => [0.0004, 'Free', [
'decimalplaces' => 3,
'display' => '{value}',
'displaywhenzero' => 'Free',
]],
'Export when config not set' => [42, '42', []],
'Export zero when config not set' => [0, null, []],
];
}
@ -150,14 +166,14 @@ final class data_controller_test extends advanced_testcase {
* Test exporting instance
*
* @param float|string $datavalue
* @param float|string $expectedvalue
* @param string|null $expectedvalue
* @param array $configdata
*
* @dataProvider export_value_provider
*/
public function test_export_value(
float|string $datavalue,
float|string $expectedvalue,
string|null $expectedvalue,
array $configdata,
): void {
$this->resetAfterTest();
@ -181,6 +197,6 @@ final class data_controller_test extends advanced_testcase {
$data = $generator->add_instance_data($field, (int) $course->id, $datavalue);
$result = \core_customfield\data_controller::create($data->get('id'))->export_value();
$this->assertEquals($expectedvalue, $result);
$this->assertSame($expectedvalue, $result);
}
}