This commit is contained in:
Eloy Lafuente (stronk7) 2022-03-01 16:10:37 +01:00
commit fba1593893
7 changed files with 110 additions and 4 deletions

View File

@ -49,6 +49,7 @@ class avg extends base {
return in_array($columntype, [
column::TYPE_INTEGER,
column::TYPE_FLOAT,
column::TYPE_BOOLEAN,
]);
}
@ -72,6 +73,6 @@ class avg extends base {
* @return mixed
*/
public static function format_value($value, array $values, array $callbacks) {
return sprintf('%.1f', (float) reset($values));
return format_float((float) reset($values), 1);
}
}

View File

@ -72,6 +72,6 @@ class percent extends base {
* @return mixed
*/
public static function format_value($value, array $values, array $callbacks) {
return format::percent((float) reset($values));
return format::percent(reset($values));
}
}

View File

@ -61,6 +61,6 @@ class format {
* @return string
*/
public static function percent($value): string {
return sprintf('%.1f%%', (float) $value);
return format_float((float) $value, 1) . '%';
}
}

View File

@ -92,9 +92,31 @@ Feature: Manage custom report columns aggregation
| Count distinct | 2 |
| Maximum | Yes |
| Minimum | No |
| Average | 0.7 |
| Percentage | 66.7% |
| Sum | 2 |
Scenario Outline: Aggregated columns display localised floats
Given the following "language customisations" exist:
| component | stringid | value |
| core_langconfig | decsep | , |
And the following "core_reportbuilder > Reports" exist:
| name | source | default |
| My report | core_user\reportbuilder\datasource\users | 0 |
And the following "core_reportbuilder > Columns" exist:
| report | uniqueidentifier |
| My report | user:lastname |
| My report | user:confirmed |
And I am on the "My report" "reportbuilder > Editor" page logged in as "admin"
And I change window size to "large"
When I set the "Confirmed" column aggregation to "<aggregation>"
Then I should see "Aggregated column 'Confirmed'"
And I should see "<output>" in the "Richie" "table_row"
Examples:
| aggregation | output |
| Average | 0,7 |
| Percentage | 66,7% |
Scenario: Show unique report rows
Given the following "core_reportbuilder > Reports" exist:
| name | source | default | uniquerows |

View File

@ -0,0 +1,76 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
declare(strict_types=1);
namespace core_reportbuilder\local\aggregation;
use core_reportbuilder_testcase;
use core_reportbuilder_generator;
use core_user\reportbuilder\datasource\users;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once("{$CFG->dirroot}/reportbuilder/tests/helpers.php");
/**
* Unit tests for avg aggregation
*
* @package core_reportbuilder
* @covers \core_reportbuilder\local\aggregation\avg
* @copyright 2022 Paul Holden <paulh@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class avg_test extends core_reportbuilder_testcase {
/**
* Test aggregation when applied to column
*/
public function test_column_aggregation(): void {
$this->resetAfterTest();
// Test subjects.
$this->getDataGenerator()->create_user(['firstname' => 'Bob', 'suspended' => 1]);
$this->getDataGenerator()->create_user(['firstname' => 'Bob', 'suspended' => 0]);
/** @var core_reportbuilder_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
$report = $generator->create_report(['name' => 'Users', 'source' => users::class, 'default' => 0]);
// First column, sorted.
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:firstname'])
->set('sortenabled', true)
->update();
// This is the column we'll aggregate.
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:suspended'])
->set('aggregation', avg::get_class_name())
->update();
$content = $this->get_custom_report_content($report->get('id'));
$this->assertEquals([
[
'c0_firstname' => 'Admin',
'c1_suspended' => '0.0',
],
[
'c0_firstname' => 'Bob',
'c1_suspended' => '0.5',
],
], $content);
}
}

View File

@ -28,7 +28,7 @@ global $CFG;
require_once("{$CFG->dirroot}/reportbuilder/tests/helpers.php");
/**
* Unit tests for percent aggregation
* Unit tests for sum aggregation
*
* @package core_reportbuilder
* @covers \core_reportbuilder\local\aggregation\base

View File

@ -64,4 +64,11 @@ class format_test extends advanced_testcase {
public function test_boolean_as_text(bool $value, string $expected): void {
$this->assertEquals($expected, format::boolean_as_text($value));
}
/**
* Test percentage formatting of a float
*/
public function test_percent(): void {
$this->assertEquals('33.3%', format::percent(1 / 3 * 100));
}
}