Differences Between: [Versions 400 and 402] [Versions 401 and 402]
1 <?php 2 // This file is part of Moodle - http://moodle.org/ 3 // 4 // Moodle is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // Moodle is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU General Public License for more details. 13 // 14 // You should have received a copy of the GNU General Public License 15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 16 17 declare(strict_types=1); 18 19 namespace core_reportbuilder\local\aggregation; 20 21 use core_reportbuilder_testcase; 22 use core_reportbuilder_generator; 23 use core_reportbuilder\manager; 24 use core_reportbuilder\local\report\column; 25 use core_user\reportbuilder\datasource\users; 26 use stdClass; 27 28 defined('MOODLE_INTERNAL') || die(); 29 30 global $CFG; 31 require_once("{$CFG->dirroot}/reportbuilder/tests/helpers.php"); 32 33 /** 34 * Unit tests for avg aggregation 35 * 36 * @package core_reportbuilder 37 * @covers \core_reportbuilder\local\aggregation\avg 38 * @copyright 2022 Paul Holden <paulh@moodle.com> 39 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 40 */ 41 class avg_test extends core_reportbuilder_testcase { 42 43 /** 44 * Test aggregation when applied to column 45 */ 46 public function test_column_aggregation(): void { 47 $this->resetAfterTest(); 48 49 // Test subjects. 50 $this->getDataGenerator()->create_user(['firstname' => 'Bob', 'suspended' => 1]); 51 $this->getDataGenerator()->create_user(['firstname' => 'Bob', 'suspended' => 0]); 52 53 /** @var core_reportbuilder_generator $generator */ 54 $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder'); 55 $report = $generator->create_report(['name' => 'Users', 'source' => users::class, 'default' => 0]); 56 57 // First column, sorted. 58 $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:firstname', 'sortenabled' => 1]); 59 60 // This is the column we'll aggregate. 61 $generator->create_column( 62 ['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:suspended', 'aggregation' => avg::get_class_name()] 63 ); 64 65 $content = $this->get_custom_report_content($report->get('id')); 66 $this->assertEquals([ 67 [ 68 'c0_firstname' => 'Admin', 69 'c1_suspended' => '0.0', 70 ], 71 [ 72 'c0_firstname' => 'Bob', 73 'c1_suspended' => '0.5', 74 ], 75 ], $content); 76 } 77 78 /** 79 * Test aggregation when applied to column with callback 80 */ 81 public function test_column_aggregation_with_callback(): void { 82 $this->resetAfterTest(); 83 84 // Test subjects. 85 $this->getDataGenerator()->create_user(['firstname' => 'Bob', 'suspended' => 1]); 86 $this->getDataGenerator()->create_user(['firstname' => 'Bob', 'suspended' => 0]); 87 88 /** @var core_reportbuilder_generator $generator */ 89 $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder'); 90 $report = $generator->create_report(['name' => 'Users', 'source' => users::class, 'default' => 0]); 91 92 // First column, sorted. 93 $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:firstname', 'sortenabled' => 1]); 94 95 // This is the column we'll aggregate. 96 $generator->create_column( 97 ['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:suspended', 'aggregation' => avg::get_class_name()] 98 ); 99 100 // Set callback to format the column (hack column definition to ensure callbacks are executed). 101 $instance = manager::get_report_from_persistent($report); 102 $instance->get_column('user:suspended') 103 ->set_type(column::TYPE_FLOAT) 104 ->set_callback(static function(float $value, stdClass $row, $arguments, ?string $aggregation): string { 105 // Simple callback to return the given value, and append aggregation type. 106 return number_format($value, 1) . " ({$aggregation})"; 107 }); 108 109 $content = $this->get_custom_report_content($report->get('id')); 110 $this->assertEquals([ 111 [ 112 'c0_firstname' => 'Admin', 113 'c1_suspended' => '0.0 (avg)', 114 ], 115 [ 116 'c0_firstname' => 'Bob', 117 'c1_suspended' => '0.5 (avg)', 118 ], 119 ], $content); 120 } 121 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body