Differences Between: [Versions 400 and 401] [Versions 400 and 402] [Versions 400 and 403]
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 lang_string; 22 use core_reportbuilder\local\report\column; 23 24 /** 25 * Base class for column aggregation types 26 * 27 * @package core_reportbuilder 28 * @copyright 2021 Paul Holden <paulh@moodle.com> 29 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 30 */ 31 abstract class base { 32 33 /** 34 * Return the class name of the aggregation type 35 * 36 * @return string 37 */ 38 final public static function get_class_name(): string { 39 $namespacedclass = explode('\\', get_called_class()); 40 41 return end($namespacedclass); 42 } 43 44 /** 45 * Return the display name of the aggregation 46 * 47 * @return lang_string 48 */ 49 abstract public static function get_name(): lang_string; 50 51 /** 52 * Whether the aggregation is compatible with the given column type 53 * 54 * @param int $columntype The type as defined by the {@see column::set_type} method 55 * @return bool 56 */ 57 abstract public static function compatible(int $columntype): bool; 58 59 /** 60 * Whether the aggregation is sortable, by default return the sortable status of the column itself 61 * 62 * @param bool $columnsortable 63 * @return bool 64 */ 65 public static function sortable(bool $columnsortable): bool { 66 return $columnsortable; 67 } 68 69 /** 70 * Return SQL suitable for using within {@see get_field_sql} for column fields, by default just the first one 71 * 72 * @param string[] $sqlfields 73 * @return string 74 */ 75 public static function get_column_field_sql(array $sqlfields): string { 76 return reset($sqlfields); 77 } 78 79 /** 80 * Helper method for concatenating given fields for a column, so they are suitable for aggregation 81 * 82 * @param string[] $sqlfields 83 * @param string $delimeter 84 * @param string $coalescechar 85 * @return string 86 */ 87 final protected static function get_column_fields_concat( 88 array $sqlfields, 89 string $delimeter = ',', 90 string $coalescechar = ' ' 91 ): string { 92 global $DB; 93 94 $concatfields = []; 95 foreach ($sqlfields as $sqlfield) { 96 97 // We need to ensure all values are char (this ought to be done in the DML drivers, see MDL-72184). 98 switch ($DB->get_dbfamily()) { 99 case 'mssql' : 100 $sqlfield = $DB->sql_concat("''", $sqlfield); 101 break; 102 case 'postgres' : 103 $sqlfield = "CAST({$sqlfield} AS VARCHAR)"; 104 break; 105 case 'oracle' : 106 $sqlfield = "TO_CHAR({$sqlfield})"; 107 break; 108 } 109 110 // Coalesce all the SQL fields. Ensure cross-DB compatibility, and that we always get string data back. 111 $concatfields[] = "COALESCE({$sqlfield}, '{$coalescechar}')"; 112 $concatfields[] = "'{$delimeter}'"; 113 } 114 115 // Slice off the last delimeter. 116 return $DB->sql_concat(...array_slice($concatfields, 0, -1)); 117 } 118 119 /** 120 * Return the aggregated field SQL 121 * 122 * @param string $field 123 * @param int $columntype 124 * @return string 125 */ 126 abstract public static function get_field_sql(string $field, int $columntype): string; 127 128 /** 129 * Return formatted value for column when applying aggregation, by default executing all callbacks on the value 130 * 131 * Should be overridden in child classes that need to format the column value differently (e.g. 'sum' would just show 132 * a numeric count value) 133 * 134 * @param mixed $value 135 * @param array $values 136 * @param array $callbacks Array of column callbacks, {@see column::add_callback} for definition 137 * @return mixed 138 */ 139 public static function format_value($value, array $values, array $callbacks) { 140 foreach ($callbacks as $callback) { 141 [$callable, $arguments] = $callback; 142 $value = ($callable)($value, (object) $values, $arguments); 143 } 144 145 return $value; 146 } 147 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body