Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 400 and 401] [Versions 401 and 402] [Versions 401 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          // We need to ensure all values are char.
  95          $sqlfieldrequirescast = in_array($DB->get_dbfamily(), ['mssql', 'oracle', 'postgres']);
  96  
  97          $concatfields = [];
  98          foreach ($sqlfields as $sqlfield) {
  99              if ($sqlfieldrequirescast) {
 100                  $sqlfield = $DB->sql_cast_to_char($sqlfield);
 101              }
 102  
 103              // Coalesce all the SQL fields. Ensure cross-DB compatibility, and that we always get string data back.
 104              $concatfields[] = "COALESCE({$sqlfield}, '{$coalescechar}')";
 105              $concatfields[] = "'{$delimeter}'";
 106          }
 107  
 108          // Slice off the last delimeter.
 109          return $DB->sql_concat(...array_slice($concatfields, 0, -1));
 110      }
 111  
 112      /**
 113       * Return the aggregated field SQL
 114       *
 115       * @param string $field
 116       * @param int $columntype
 117       * @return string
 118       */
 119      abstract public static function get_field_sql(string $field, int $columntype): string;
 120  
 121      /**
 122       * Return formatted value for column when applying aggregation, by default executing all callbacks on the value
 123       *
 124       * Should be overridden in child classes that need to format the column value differently (e.g. 'sum' would just show
 125       * a numeric count value)
 126       *
 127       * @param mixed $value
 128       * @param array $values
 129       * @param array $callbacks Array of column callbacks, {@see column::add_callback} for definition
 130       * @param int $columntype The original type of the column, to ensure it is preserved for callbacks
 131       * @return mixed
 132       */
 133      public static function format_value($value, array $values, array $callbacks, int $columntype) {
 134          foreach ($callbacks as $callback) {
 135              [$callable, $arguments] = $callback;
 136              $value = ($callable)($value, (object) $values, $arguments);
 137          }
 138  
 139          return $value;
 140      }
 141  }