| 1 | 
           efrain | 
           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\report;
  | 
        
        
            | 
            | 
           20 | 
              | 
        
        
           | 1441 | 
           ariadna | 
           21 | 
           use core\exception\coding_exception;
  | 
        
        
            | 
            | 
           22 | 
           use core\lang_string;
  | 
        
        
            | 
            | 
           23 | 
           use core\output\help_icon;
  | 
        
        
            | 
            | 
           24 | 
           use core_reportbuilder\local\helpers\{aggregation, database, join_trait};
  | 
        
        
           | 1 | 
           efrain | 
           25 | 
           use core_reportbuilder\local\aggregation\base;
  | 
        
        
            | 
            | 
           26 | 
           use core_reportbuilder\local\models\column as column_model;
  | 
        
        
            | 
            | 
           27 | 
              | 
        
        
            | 
            | 
           28 | 
           /**
  | 
        
        
            | 
            | 
           29 | 
            * Class to represent a report column
  | 
        
        
            | 
            | 
           30 | 
            *
  | 
        
        
            | 
            | 
           31 | 
            * @package     core_reportbuilder
  | 
        
        
            | 
            | 
           32 | 
            * @copyright   2020 Paul Holden <paulh@moodle.com>
  | 
        
        
            | 
            | 
           33 | 
            * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  | 
        
        
            | 
            | 
           34 | 
            */
  | 
        
        
            | 
            | 
           35 | 
           final class column {
  | 
        
        
            | 
            | 
           36 | 
              | 
        
        
           | 1441 | 
           ariadna | 
           37 | 
               use join_trait;
  | 
        
        
            | 
            | 
           38 | 
              | 
        
        
           | 1 | 
           efrain | 
           39 | 
               /** @var int Column type is integer */
  | 
        
        
            | 
            | 
           40 | 
               public const TYPE_INTEGER = 1;
  | 
        
        
            | 
            | 
           41 | 
              | 
        
        
            | 
            | 
           42 | 
               /** @var int Column type is text */
  | 
        
        
            | 
            | 
           43 | 
               public const TYPE_TEXT = 2;
  | 
        
        
            | 
            | 
           44 | 
              | 
        
        
            | 
            | 
           45 | 
               /** @var int Column type is timestamp */
  | 
        
        
            | 
            | 
           46 | 
               public const TYPE_TIMESTAMP = 3;
  | 
        
        
            | 
            | 
           47 | 
              | 
        
        
            | 
            | 
           48 | 
               /** @var int Column type is boolean */
  | 
        
        
            | 
            | 
           49 | 
               public const TYPE_BOOLEAN = 4;
  | 
        
        
            | 
            | 
           50 | 
              | 
        
        
            | 
            | 
           51 | 
               /** @var int Column type is float */
  | 
        
        
            | 
            | 
           52 | 
               public const TYPE_FLOAT = 5;
  | 
        
        
            | 
            | 
           53 | 
              | 
        
        
            | 
            | 
           54 | 
               /** @var int Column type is long text */
  | 
        
        
            | 
            | 
           55 | 
               public const TYPE_LONGTEXT = 6;
  | 
        
        
            | 
            | 
           56 | 
              | 
        
        
            | 
            | 
           57 | 
               /** @var int $index Column index within a report */
  | 
        
        
            | 
            | 
           58 | 
               private $index;
  | 
        
        
            | 
            | 
           59 | 
              | 
        
        
            | 
            | 
           60 | 
               /** @var bool $hascustomcolumntitle Used to store if the column has been given a custom title */
  | 
        
        
            | 
            | 
           61 | 
               private $hascustomcolumntitle = false;
  | 
        
        
            | 
            | 
           62 | 
              | 
        
        
            | 
            | 
           63 | 
               /** @var int $type Column data type (one of the TYPE_* class constants) */
  | 
        
        
            | 
            | 
           64 | 
               private $type = self::TYPE_TEXT;
  | 
        
        
            | 
            | 
           65 | 
              | 
        
        
            | 
            | 
           66 | 
               /** @var array $fields */
  | 
        
        
            | 
            | 
           67 | 
               private $fields = [];
  | 
        
        
            | 
            | 
           68 | 
              | 
        
        
            | 
            | 
           69 | 
               /** @var array $params  */
  | 
        
        
            | 
            | 
           70 | 
               private $params = [];
  | 
        
        
            | 
            | 
           71 | 
              | 
        
        
            | 
            | 
           72 | 
               /** @var string $groupbysql */
  | 
        
        
            | 
            | 
           73 | 
               private $groupbysql;
  | 
        
        
            | 
            | 
           74 | 
              | 
        
        
            | 
            | 
           75 | 
               /** @var array[] $callbacks Array of [callable, additionalarguments] */
  | 
        
        
            | 
            | 
           76 | 
               private $callbacks = [];
  | 
        
        
            | 
            | 
           77 | 
              | 
        
        
            | 
            | 
           78 | 
               /** @var base|null $aggregation Aggregation type to apply to column */
  | 
        
        
           | 1441 | 
           ariadna | 
           79 | 
               private base|null $aggregation = null;
  | 
        
        
           | 1 | 
           efrain | 
           80 | 
              | 
        
        
           | 1441 | 
           ariadna | 
           81 | 
               /** @var array[] $aggregationoptions Aggregation type options */
  | 
        
        
            | 
            | 
           82 | 
               private array $aggregationoptions = [];
  | 
        
        
            | 
            | 
           83 | 
              | 
        
        
           | 1 | 
           efrain | 
           84 | 
               /** @var array $disabledaggregation Aggregation types explicitly disabled  */
  | 
        
        
            | 
            | 
           85 | 
               private $disabledaggregation = [];
  | 
        
        
            | 
            | 
           86 | 
              | 
        
        
            | 
            | 
           87 | 
               /** @var bool $issortable Used to indicate if a column is sortable */
  | 
        
        
            | 
            | 
           88 | 
               private $issortable = false;
  | 
        
        
            | 
            | 
           89 | 
              | 
        
        
            | 
            | 
           90 | 
               /** @var array $sortfields Fields to sort the column by */
  | 
        
        
            | 
            | 
           91 | 
               private $sortfields = [];
  | 
        
        
            | 
            | 
           92 | 
              | 
        
        
            | 
            | 
           93 | 
               /** @var array $attributes */
  | 
        
        
            | 
            | 
           94 | 
               private $attributes = [];
  | 
        
        
            | 
            | 
           95 | 
              | 
        
        
           | 1441 | 
           ariadna | 
           96 | 
               /** @var help_icon|null $helpicon */
  | 
        
        
            | 
            | 
           97 | 
               private $helpicon = null;
  | 
        
        
            | 
            | 
           98 | 
              | 
        
        
           | 1 | 
           efrain | 
           99 | 
               /** @var bool $available Used to know if column is available to the current user or not */
  | 
        
        
           | 1441 | 
           ariadna | 
           100 | 
               private $available = true;
  | 
        
        
           | 1 | 
           efrain | 
           101 | 
              | 
        
        
            | 
            | 
           102 | 
               /** @var bool $deprecated */
  | 
        
        
           | 1441 | 
           ariadna | 
           103 | 
               private $deprecated = false;
  | 
        
        
           | 1 | 
           efrain | 
           104 | 
              | 
        
        
            | 
            | 
           105 | 
               /** @var string $deprecatedmessage */
  | 
        
        
           | 1441 | 
           ariadna | 
           106 | 
               private $deprecatedmessage;
  | 
        
        
           | 1 | 
           efrain | 
           107 | 
              | 
        
        
            | 
            | 
           108 | 
               /** @var column_model $persistent */
  | 
        
        
           | 1441 | 
           ariadna | 
           109 | 
               private $persistent;
  | 
        
        
           | 1 | 
           efrain | 
           110 | 
              | 
        
        
            | 
            | 
           111 | 
               /**
  | 
        
        
            | 
            | 
           112 | 
                * Column constructor
  | 
        
        
            | 
            | 
           113 | 
                *
  | 
        
        
            | 
            | 
           114 | 
                * For better readability use chainable methods, for example:
  | 
        
        
            | 
            | 
           115 | 
                *
  | 
        
        
            | 
            | 
           116 | 
                * $report->add_column(
  | 
        
        
            | 
            | 
           117 | 
                *    (new column('name', new lang_string('name'), 'user'))
  | 
        
        
            | 
            | 
           118 | 
                *    ->add_join('left join {table} t on t.id = p.tableid')
  | 
        
        
            | 
            | 
           119 | 
                *    ->add_field('t.name')
  | 
        
        
            | 
            | 
           120 | 
                *    ->add_callback([format::class, 'format_string']));
  | 
        
        
            | 
            | 
           121 | 
                *
  | 
        
        
            | 
            | 
           122 | 
                * @param string $name Internal name of the column
  | 
        
        
            | 
            | 
           123 | 
                * @param lang_string|null $title Title of the column used in reports (null for blank)
  | 
        
        
            | 
            | 
           124 | 
                * @param string $entityname Name of the entity this column belongs to. Typically when creating columns within entities
  | 
        
        
            | 
            | 
           125 | 
                *      this value should be the result of calling {@see get_entity_name}, however if creating columns inside reports directly
  | 
        
        
            | 
            | 
           126 | 
                *      it should be the name of the entity as passed to {@see \core_reportbuilder\local\report\base::annotate_entity}
  | 
        
        
            | 
            | 
           127 | 
                */
  | 
        
        
           | 1441 | 
           ariadna | 
           128 | 
               public function __construct(
  | 
        
        
            | 
            | 
           129 | 
                   /** @var string Internal name of the column */
  | 
        
        
            | 
            | 
           130 | 
                   private string $name,
  | 
        
        
            | 
            | 
           131 | 
                   /** @var lang_string|null Title of the column used in reports */
  | 
        
        
            | 
            | 
           132 | 
                   private ?lang_string $title,
  | 
        
        
            | 
            | 
           133 | 
                   /** @var string Name of the entity this column belongs to */
  | 
        
        
            | 
            | 
           134 | 
                   private readonly string $entityname,
  | 
        
        
            | 
            | 
           135 | 
               ) {
  | 
        
        
            | 
            | 
           136 | 
              | 
        
        
           | 1 | 
           efrain | 
           137 | 
               }
  | 
        
        
            | 
            | 
           138 | 
              | 
        
        
            | 
            | 
           139 | 
               /**
  | 
        
        
            | 
            | 
           140 | 
                * Set column name
  | 
        
        
            | 
            | 
           141 | 
                *
  | 
        
        
            | 
            | 
           142 | 
                * @param string $name
  | 
        
        
            | 
            | 
           143 | 
                * @return self
  | 
        
        
            | 
            | 
           144 | 
                */
  | 
        
        
            | 
            | 
           145 | 
               public function set_name(string $name): self {
  | 
        
        
           | 1441 | 
           ariadna | 
           146 | 
                   $this->name = $name;
  | 
        
        
           | 1 | 
           efrain | 
           147 | 
                   return $this;
  | 
        
        
            | 
            | 
           148 | 
               }
  | 
        
        
            | 
            | 
           149 | 
              | 
        
        
            | 
            | 
           150 | 
               /**
  | 
        
        
            | 
            | 
           151 | 
                * Return column name
  | 
        
        
            | 
            | 
           152 | 
                *
  | 
        
        
            | 
            | 
           153 | 
                * @return mixed
  | 
        
        
            | 
            | 
           154 | 
                */
  | 
        
        
            | 
            | 
           155 | 
               public function get_name(): string {
  | 
        
        
           | 1441 | 
           ariadna | 
           156 | 
                   return $this->name;
  | 
        
        
           | 1 | 
           efrain | 
           157 | 
               }
  | 
        
        
            | 
            | 
           158 | 
              | 
        
        
            | 
            | 
           159 | 
               /**
  | 
        
        
            | 
            | 
           160 | 
                * Set column title
  | 
        
        
            | 
            | 
           161 | 
                *
  | 
        
        
            | 
            | 
           162 | 
                * @param lang_string|null $title
  | 
        
        
            | 
            | 
           163 | 
                * @return self
  | 
        
        
            | 
            | 
           164 | 
                */
  | 
        
        
            | 
            | 
           165 | 
               public function set_title(?lang_string $title): self {
  | 
        
        
           | 1441 | 
           ariadna | 
           166 | 
                   $this->title = $title;
  | 
        
        
           | 1 | 
           efrain | 
           167 | 
                   $this->hascustomcolumntitle = true;
  | 
        
        
            | 
            | 
           168 | 
                   return $this;
  | 
        
        
            | 
            | 
           169 | 
               }
  | 
        
        
            | 
            | 
           170 | 
              | 
        
        
            | 
            | 
           171 | 
               /**
  | 
        
        
            | 
            | 
           172 | 
                * Return column title
  | 
        
        
            | 
            | 
           173 | 
                *
  | 
        
        
            | 
            | 
           174 | 
                * @return string
  | 
        
        
            | 
            | 
           175 | 
                */
  | 
        
        
            | 
            | 
           176 | 
               public function get_title(): string {
  | 
        
        
           | 1441 | 
           ariadna | 
           177 | 
                   return $this->title ? (string) $this->title : '';
  | 
        
        
           | 1 | 
           efrain | 
           178 | 
               }
  | 
        
        
            | 
            | 
           179 | 
              | 
        
        
            | 
            | 
           180 | 
               /**
  | 
        
        
            | 
            | 
           181 | 
                * Check whether this column has been given a custom title
  | 
        
        
            | 
            | 
           182 | 
                *
  | 
        
        
            | 
            | 
           183 | 
                * @return bool
  | 
        
        
            | 
            | 
           184 | 
                */
  | 
        
        
            | 
            | 
           185 | 
               public function has_custom_title(): bool {
  | 
        
        
            | 
            | 
           186 | 
                   return $this->hascustomcolumntitle;
  | 
        
        
            | 
            | 
           187 | 
               }
  | 
        
        
            | 
            | 
           188 | 
              | 
        
        
            | 
            | 
           189 | 
               /**
  | 
        
        
            | 
            | 
           190 | 
                * Get column entity name
  | 
        
        
            | 
            | 
           191 | 
                *
  | 
        
        
            | 
            | 
           192 | 
                * @return string
  | 
        
        
            | 
            | 
           193 | 
                */
  | 
        
        
            | 
            | 
           194 | 
               public function get_entity_name(): string {
  | 
        
        
            | 
            | 
           195 | 
                   return $this->entityname;
  | 
        
        
            | 
            | 
           196 | 
               }
  | 
        
        
            | 
            | 
           197 | 
              | 
        
        
            | 
            | 
           198 | 
              | 
        
        
            | 
            | 
           199 | 
               /**
  | 
        
        
            | 
            | 
           200 | 
                * Return unique identifier for this column
  | 
        
        
            | 
            | 
           201 | 
                *
  | 
        
        
            | 
            | 
           202 | 
                * @return string
  | 
        
        
            | 
            | 
           203 | 
                */
  | 
        
        
            | 
            | 
           204 | 
               public function get_unique_identifier(): string {
  | 
        
        
            | 
            | 
           205 | 
                   return $this->get_entity_name() . ':' . $this->get_name();
  | 
        
        
            | 
            | 
           206 | 
               }
  | 
        
        
            | 
            | 
           207 | 
              | 
        
        
            | 
            | 
           208 | 
               /**
  | 
        
        
            | 
            | 
           209 | 
                * Set the column index within the current report
  | 
        
        
            | 
            | 
           210 | 
                *
  | 
        
        
            | 
            | 
           211 | 
                * @param int $index
  | 
        
        
            | 
            | 
           212 | 
                * @return self
  | 
        
        
            | 
            | 
           213 | 
                */
  | 
        
        
            | 
            | 
           214 | 
               public function set_index(int $index): self {
  | 
        
        
            | 
            | 
           215 | 
                   $this->index = $index;
  | 
        
        
            | 
            | 
           216 | 
                   return $this;
  | 
        
        
            | 
            | 
           217 | 
               }
  | 
        
        
            | 
            | 
           218 | 
              | 
        
        
            | 
            | 
           219 | 
               /**
  | 
        
        
            | 
            | 
           220 | 
                * Set the column type, if not called then the type will be assumed to be {@see TYPE_TEXT}
  | 
        
        
            | 
            | 
           221 | 
                *
  | 
        
        
            | 
            | 
           222 | 
                * The type of a column is used to cast the first column field passed to any callbacks {@see add_callback} as well as the
  | 
        
        
           | 1441 | 
           ariadna | 
           223 | 
                * aggregation options available for the column. It should represent how the column content is returned from callbacks
  | 
        
        
           | 1 | 
           efrain | 
           224 | 
                *
  | 
        
        
           | 1441 | 
           ariadna | 
           225 | 
                *
  | 
        
        
           | 1 | 
           efrain | 
           226 | 
                * @param int $type
  | 
        
        
            | 
            | 
           227 | 
                * @return self
  | 
        
        
            | 
            | 
           228 | 
                * @throws coding_exception
  | 
        
        
            | 
            | 
           229 | 
                */
  | 
        
        
            | 
            | 
           230 | 
               public function set_type(int $type): self {
  | 
        
        
            | 
            | 
           231 | 
                   $allowedtypes = [
  | 
        
        
            | 
            | 
           232 | 
                       self::TYPE_INTEGER,
  | 
        
        
            | 
            | 
           233 | 
                       self::TYPE_TEXT,
  | 
        
        
            | 
            | 
           234 | 
                       self::TYPE_TIMESTAMP,
  | 
        
        
            | 
            | 
           235 | 
                       self::TYPE_BOOLEAN,
  | 
        
        
            | 
            | 
           236 | 
                       self::TYPE_FLOAT,
  | 
        
        
            | 
            | 
           237 | 
                       self::TYPE_LONGTEXT,
  | 
        
        
            | 
            | 
           238 | 
                   ];
  | 
        
        
            | 
            | 
           239 | 
                   if (!in_array($type, $allowedtypes)) {
  | 
        
        
            | 
            | 
           240 | 
                       throw new coding_exception('Invalid column type', $type);
  | 
        
        
            | 
            | 
           241 | 
                   }
  | 
        
        
            | 
            | 
           242 | 
              | 
        
        
            | 
            | 
           243 | 
                   $this->type = $type;
  | 
        
        
            | 
            | 
           244 | 
                   return $this;
  | 
        
        
            | 
            | 
           245 | 
               }
  | 
        
        
            | 
            | 
           246 | 
              | 
        
        
            | 
            | 
           247 | 
               /**
  | 
        
        
            | 
            | 
           248 | 
                * Return column type, that being one of the TYPE_* class constants
  | 
        
        
            | 
            | 
           249 | 
                *
  | 
        
        
            | 
            | 
           250 | 
                * @return int
  | 
        
        
            | 
            | 
           251 | 
                */
  | 
        
        
            | 
            | 
           252 | 
               public function get_type(): int {
  | 
        
        
            | 
            | 
           253 | 
                   return $this->type;
  | 
        
        
            | 
            | 
           254 | 
               }
  | 
        
        
            | 
            | 
           255 | 
              | 
        
        
            | 
            | 
           256 | 
               /**
  | 
        
        
            | 
            | 
           257 | 
                * Adds a field to be queried from the database that is necessary for this column
  | 
        
        
            | 
            | 
           258 | 
                *
  | 
        
        
            | 
            | 
           259 | 
                * Multiple fields can be added per column, this method may be called several times. Field aliases must be unique inside
  | 
        
        
            | 
            | 
           260 | 
                * any given column, but there will be no conflicts if the same aliases are used in other columns in the same report
  | 
        
        
            | 
            | 
           261 | 
                *
  | 
        
        
            | 
            | 
           262 | 
                * @param string $sql SQL query, this may be a simple "tablealias.fieldname" or a complex sub-query that returns only one field
  | 
        
        
            | 
            | 
           263 | 
                * @param string $alias
  | 
        
        
            | 
            | 
           264 | 
                * @param array $params
  | 
        
        
            | 
            | 
           265 | 
                * @return self
  | 
        
        
            | 
            | 
           266 | 
                * @throws coding_exception
  | 
        
        
            | 
            | 
           267 | 
                */
  | 
        
        
            | 
            | 
           268 | 
               public function add_field(string $sql, string $alias = '', array $params = []): self {
  | 
        
        
            | 
            | 
           269 | 
                   database::validate_params($params);
  | 
        
        
            | 
            | 
           270 | 
              | 
        
        
            | 
            | 
           271 | 
                   // SQL ends with a space and a word - this looks like an alias was passed as part of the field.
  | 
        
        
            | 
            | 
           272 | 
                   if (preg_match('/ \w+$/', $sql) && empty($alias)) {
  | 
        
        
            | 
            | 
           273 | 
                       throw new coding_exception('Column alias must be passed as a separate argument', $sql);
  | 
        
        
            | 
            | 
           274 | 
                   }
  | 
        
        
            | 
            | 
           275 | 
              | 
        
        
            | 
            | 
           276 | 
                   // If no alias was specified, auto-detect it based on common patterns ("table.column" or just "column").
  | 
        
        
            | 
            | 
           277 | 
                   if (empty($alias) && preg_match('/^(\w+\.)?(?<fieldname>\w+)$/', $sql, $matches)) {
  | 
        
        
            | 
            | 
           278 | 
                       $alias = $matches['fieldname'];
  | 
        
        
            | 
            | 
           279 | 
                   }
  | 
        
        
            | 
            | 
           280 | 
              | 
        
        
            | 
            | 
           281 | 
                   if (empty($alias)) {
  | 
        
        
            | 
            | 
           282 | 
                       throw new coding_exception('Complex columns must have an alias', $sql);
  | 
        
        
            | 
            | 
           283 | 
                   }
  | 
        
        
            | 
            | 
           284 | 
              | 
        
        
            | 
            | 
           285 | 
                   $this->fields[$alias] = $sql;
  | 
        
        
            | 
            | 
           286 | 
                   $this->params += $params;
  | 
        
        
            | 
            | 
           287 | 
              | 
        
        
            | 
            | 
           288 | 
                   return $this;
  | 
        
        
            | 
            | 
           289 | 
               }
  | 
        
        
            | 
            | 
           290 | 
              | 
        
        
            | 
            | 
           291 | 
               /**
  | 
        
        
            | 
            | 
           292 | 
                * Add a list of comma-separated fields
  | 
        
        
            | 
            | 
           293 | 
                *
  | 
        
        
            | 
            | 
           294 | 
                * @param string $sql
  | 
        
        
            | 
            | 
           295 | 
                * @param array $params
  | 
        
        
            | 
            | 
           296 | 
                * @return self
  | 
        
        
            | 
            | 
           297 | 
                */
  | 
        
        
            | 
            | 
           298 | 
               public function add_fields(string $sql, array $params = []): self {
  | 
        
        
            | 
            | 
           299 | 
                   database::validate_params($params);
  | 
        
        
            | 
            | 
           300 | 
              | 
        
        
            | 
            | 
           301 | 
                   // Split SQL into separate fields (separated by comma).
  | 
        
        
            | 
            | 
           302 | 
                   $fields = preg_split('/\s*,\s*/', $sql);
  | 
        
        
            | 
            | 
           303 | 
                   foreach ($fields as $field) {
  | 
        
        
            | 
            | 
           304 | 
                       // Split each field into expression, <field> <as> <alias> where "as" and "alias" are optional.
  | 
        
        
            | 
            | 
           305 | 
                       $fieldparts = preg_split('/\s+/', $field);
  | 
        
        
            | 
            | 
           306 | 
              | 
        
        
            | 
            | 
           307 | 
                       if (count($fieldparts) == 2 || (count($fieldparts) == 3 && strtolower($fieldparts[1]) === 'as')) {
  | 
        
        
            | 
            | 
           308 | 
                           $sql = reset($fieldparts);
  | 
        
        
            | 
            | 
           309 | 
                           $alias = array_pop($fieldparts);
  | 
        
        
            | 
            | 
           310 | 
                           $this->add_field($sql, $alias);
  | 
        
        
            | 
            | 
           311 | 
                       } else {
  | 
        
        
            | 
            | 
           312 | 
                           $this->add_field($field);
  | 
        
        
            | 
            | 
           313 | 
                       }
  | 
        
        
            | 
            | 
           314 | 
                   }
  | 
        
        
            | 
            | 
           315 | 
              | 
        
        
            | 
            | 
           316 | 
                   $this->params += $params;
  | 
        
        
            | 
            | 
           317 | 
              | 
        
        
            | 
            | 
           318 | 
                   return $this;
  | 
        
        
            | 
            | 
           319 | 
               }
  | 
        
        
            | 
            | 
           320 | 
              | 
        
        
            | 
            | 
           321 | 
               /**
  | 
        
        
            | 
            | 
           322 | 
                * Given a param name, add a unique prefix to ensure that the same column with params can be added multiple times to a report
  | 
        
        
            | 
            | 
           323 | 
                *
  | 
        
        
            | 
            | 
           324 | 
                * @param string $name
  | 
        
        
            | 
            | 
           325 | 
                * @return string
  | 
        
        
            | 
            | 
           326 | 
                */
  | 
        
        
            | 
            | 
           327 | 
               private function unique_param_name(string $name): string {
  | 
        
        
            | 
            | 
           328 | 
                   return "p{$this->index}_{$name}";
  | 
        
        
            | 
            | 
           329 | 
               }
  | 
        
        
            | 
            | 
           330 | 
              | 
        
        
            | 
            | 
           331 | 
               /**
  | 
        
        
            | 
            | 
           332 | 
                * Helper method to take all fields added to the column, and return appropriate SQL and alias
  | 
        
        
            | 
            | 
           333 | 
                *
  | 
        
        
            | 
            | 
           334 | 
                * @return array[]
  | 
        
        
            | 
            | 
           335 | 
                */
  | 
        
        
            | 
            | 
           336 | 
               private function get_fields_sql_alias(): array {
  | 
        
        
            | 
            | 
           337 | 
                   $fields = [];
  | 
        
        
            | 
            | 
           338 | 
              | 
        
        
            | 
            | 
           339 | 
                   foreach ($this->fields as $alias => $sql) {
  | 
        
        
            | 
            | 
           340 | 
              | 
        
        
            | 
            | 
           341 | 
                       // Ensure parameter names within SQL are prefixed with column index.
  | 
        
        
            | 
            | 
           342 | 
                       $params = array_keys($this->params);
  | 
        
        
            | 
            | 
           343 | 
                       $sql = database::sql_replace_parameter_names($sql, $params, function(string $param): string {
  | 
        
        
            | 
            | 
           344 | 
                           return $this->unique_param_name($param);
  | 
        
        
            | 
            | 
           345 | 
                       });
  | 
        
        
            | 
            | 
           346 | 
              | 
        
        
            | 
            | 
           347 | 
                       $fields[$alias] = [
  | 
        
        
            | 
            | 
           348 | 
                           'sql' => $sql,
  | 
        
        
            | 
            | 
           349 | 
                           'alias' => substr("c{$this->index}_{$alias}", 0, 30),
  | 
        
        
            | 
            | 
           350 | 
                       ];
  | 
        
        
            | 
            | 
           351 | 
                   }
  | 
        
        
            | 
            | 
           352 | 
              | 
        
        
            | 
            | 
           353 | 
                   return $fields;
  | 
        
        
            | 
            | 
           354 | 
               }
  | 
        
        
            | 
            | 
           355 | 
              | 
        
        
            | 
            | 
           356 | 
               /**
  | 
        
        
            | 
            | 
           357 | 
                * Return array of SQL expressions for each field of this column
  | 
        
        
            | 
            | 
           358 | 
                *
  | 
        
        
            | 
            | 
           359 | 
                * @return array
  | 
        
        
            | 
            | 
           360 | 
                */
  | 
        
        
            | 
            | 
           361 | 
               public function get_fields(): array {
  | 
        
        
            | 
            | 
           362 | 
                   $fieldsalias = $this->get_fields_sql_alias();
  | 
        
        
            | 
            | 
           363 | 
              | 
        
        
           | 1441 | 
           ariadna | 
           364 | 
                   if ($this->aggregation !== null) {
  | 
        
        
           | 1 | 
           efrain | 
           365 | 
                       $fieldsaliassql = array_column($fieldsalias, 'sql');
  | 
        
        
            | 
            | 
           366 | 
                       $field = reset($fieldsalias);
  | 
        
        
            | 
            | 
           367 | 
              | 
        
        
            | 
            | 
           368 | 
                       // If aggregating the column, generate SQL from column fields and use it to generate aggregation SQL.
  | 
        
        
           | 1441 | 
           ariadna | 
           369 | 
                       $aggregationfieldsql = $this->get_field_aggregation_sql($fieldsaliassql);
  | 
        
        
           | 1 | 
           efrain | 
           370 | 
              | 
        
        
            | 
            | 
           371 | 
                       $fields = ["{$aggregationfieldsql} AS {$field['alias']}"];
  | 
        
        
            | 
            | 
           372 | 
                   } else {
  | 
        
        
            | 
            | 
           373 | 
                       $fields = array_map(static function(array $field): string {
  | 
        
        
            | 
            | 
           374 | 
                           return "{$field['sql']} AS {$field['alias']}";
  | 
        
        
            | 
            | 
           375 | 
                       }, $fieldsalias);
  | 
        
        
            | 
            | 
           376 | 
                   }
  | 
        
        
            | 
            | 
           377 | 
              | 
        
        
            | 
            | 
           378 | 
                   return array_values($fields);
  | 
        
        
            | 
            | 
           379 | 
               }
  | 
        
        
            | 
            | 
           380 | 
              | 
        
        
            | 
            | 
           381 | 
               /**
  | 
        
        
           | 1441 | 
           ariadna | 
           382 | 
                * Return aggregated field SQL for the column
  | 
        
        
            | 
            | 
           383 | 
                *
  | 
        
        
            | 
            | 
           384 | 
                * @param string[] $sqlfields
  | 
        
        
            | 
            | 
           385 | 
                * @return string
  | 
        
        
            | 
            | 
           386 | 
                * @throws coding_exception
  | 
        
        
            | 
            | 
           387 | 
                */
  | 
        
        
            | 
            | 
           388 | 
               private function get_field_aggregation_sql(array $sqlfields): string {
  | 
        
        
            | 
            | 
           389 | 
                   if ($this->aggregation === null) {
  | 
        
        
            | 
            | 
           390 | 
                       throw new coding_exception('Column aggregation is undefined');
  | 
        
        
            | 
            | 
           391 | 
                   }
  | 
        
        
            | 
            | 
           392 | 
              | 
        
        
            | 
            | 
           393 | 
                   $columnfieldsql = $this->aggregation::get_column_field_sql($sqlfields);
  | 
        
        
            | 
            | 
           394 | 
                   return $this->aggregation::get_field_sql($columnfieldsql, $this->get_type());
  | 
        
        
            | 
            | 
           395 | 
               }
  | 
        
        
            | 
            | 
           396 | 
              | 
        
        
            | 
            | 
           397 | 
               /**
  | 
        
        
           | 1 | 
           efrain | 
           398 | 
                * Return column parameters, prefixed by the current index to allow the column to be added multiple times to a report
  | 
        
        
            | 
            | 
           399 | 
                *
  | 
        
        
            | 
            | 
           400 | 
                * @return array
  | 
        
        
            | 
            | 
           401 | 
                */
  | 
        
        
            | 
            | 
           402 | 
               public function get_params(): array {
  | 
        
        
            | 
            | 
           403 | 
                   $params = [];
  | 
        
        
            | 
            | 
           404 | 
              | 
        
        
            | 
            | 
           405 | 
                   foreach ($this->params as $name => $value) {
  | 
        
        
            | 
            | 
           406 | 
                       $paramname = $this->unique_param_name($name);
  | 
        
        
            | 
            | 
           407 | 
                       $params[$paramname] = $value;
  | 
        
        
            | 
            | 
           408 | 
                   }
  | 
        
        
            | 
            | 
           409 | 
              | 
        
        
            | 
            | 
           410 | 
                   return $params;
  | 
        
        
            | 
            | 
           411 | 
               }
  | 
        
        
            | 
            | 
           412 | 
              | 
        
        
            | 
            | 
           413 | 
               /**
  | 
        
        
            | 
            | 
           414 | 
                * Return an alias for this column (the generated alias of it's first field)
  | 
        
        
            | 
            | 
           415 | 
                *
  | 
        
        
            | 
            | 
           416 | 
                * @return string
  | 
        
        
            | 
            | 
           417 | 
                * @throws coding_exception
  | 
        
        
            | 
            | 
           418 | 
                */
  | 
        
        
            | 
            | 
           419 | 
               public function get_column_alias(): string {
  | 
        
        
            | 
            | 
           420 | 
                   if (!$fields = $this->get_fields_sql_alias()) {
  | 
        
        
            | 
            | 
           421 | 
                       throw new coding_exception('Column ' . $this->get_unique_identifier() . ' contains no fields');
  | 
        
        
            | 
            | 
           422 | 
                   }
  | 
        
        
            | 
            | 
           423 | 
              | 
        
        
            | 
            | 
           424 | 
                   return reset($fields)['alias'];
  | 
        
        
            | 
            | 
           425 | 
               }
  | 
        
        
            | 
            | 
           426 | 
              | 
        
        
            | 
            | 
           427 | 
               /**
  | 
        
        
            | 
            | 
           428 | 
                * Define suitable SQL fragment for grouping by the columns fields. This will be returned from {@see get_groupby_sql} if set
  | 
        
        
            | 
            | 
           429 | 
                *
  | 
        
        
            | 
            | 
           430 | 
                * @param string $groupbysql
  | 
        
        
            | 
            | 
           431 | 
                * @return self
  | 
        
        
            | 
            | 
           432 | 
                */
  | 
        
        
            | 
            | 
           433 | 
               public function set_groupby_sql(string $groupbysql): self {
  | 
        
        
            | 
            | 
           434 | 
                   $this->groupbysql = $groupbysql;
  | 
        
        
            | 
            | 
           435 | 
                   return $this;
  | 
        
        
            | 
            | 
           436 | 
               }
  | 
        
        
            | 
            | 
           437 | 
              | 
        
        
            | 
            | 
           438 | 
               /**
  | 
        
        
            | 
            | 
           439 | 
                * Return suitable SQL fragment for grouping by the column fields (during aggregation)
  | 
        
        
            | 
            | 
           440 | 
                *
  | 
        
        
            | 
            | 
           441 | 
                * @return array
  | 
        
        
            | 
            | 
           442 | 
                */
  | 
        
        
            | 
            | 
           443 | 
               public function get_groupby_sql(): array {
  | 
        
        
            | 
            | 
           444 | 
                   global $DB;
  | 
        
        
            | 
            | 
           445 | 
              | 
        
        
           | 1441 | 
           ariadna | 
           446 | 
                   // We can reference field aliases in GROUP BY only in MySQL/Postgres (MDL-78783).
  | 
        
        
            | 
            | 
           447 | 
                   $usealias = in_array($DB->get_dbfamily(), ['mysql', 'postgres']);
  | 
        
        
            | 
            | 
           448 | 
              | 
        
        
            | 
            | 
           449 | 
                   $fieldsalias = $this->get_fields_sql_alias();
  | 
        
        
            | 
            | 
           450 | 
              | 
        
        
            | 
            | 
           451 | 
                   // To ensure cross-platform support for column aggregation, where the aggregation should also be grouped, we need
  | 
        
        
            | 
            | 
           452 | 
                   // to generate SQL from column fields and use it to generate aggregation SQL.
  | 
        
        
            | 
            | 
           453 | 
                   if ($this->aggregation !== null && $this->aggregation::column_groupby()) {
  | 
        
        
            | 
            | 
           454 | 
                       if ($usealias) {
  | 
        
        
            | 
            | 
           455 | 
                           $this->set_groupby_sql($this->get_column_alias());
  | 
        
        
            | 
            | 
           456 | 
                       } else {
  | 
        
        
            | 
            | 
           457 | 
                           $fieldsaliassql = array_column($fieldsalias, 'sql');
  | 
        
        
            | 
            | 
           458 | 
                           $this->set_groupby_sql($this->get_field_aggregation_sql($fieldsaliassql));
  | 
        
        
            | 
            | 
           459 | 
                       }
  | 
        
        
            | 
            | 
           460 | 
                   }
  | 
        
        
            | 
            | 
           461 | 
              | 
        
        
            | 
            | 
           462 | 
                   // Return defined value if it's been set.
  | 
        
        
           | 1 | 
           efrain | 
           463 | 
                   if (!empty($this->groupbysql)) {
  | 
        
        
            | 
            | 
           464 | 
                       return [$this->groupbysql];
  | 
        
        
            | 
            | 
           465 | 
                   }
  | 
        
        
            | 
            | 
           466 | 
              | 
        
        
            | 
            | 
           467 | 
                   $columnname = $usealias ? 'alias' : 'sql';
  | 
        
        
            | 
            | 
           468 | 
                   return array_column($fieldsalias, $columnname);
  | 
        
        
            | 
            | 
           469 | 
               }
  | 
        
        
            | 
            | 
           470 | 
              | 
        
        
            | 
            | 
           471 | 
               /**
  | 
        
        
            | 
            | 
           472 | 
                * Adds column callback (in the case there are multiple, they will be called iteratively - the result of each passed
  | 
        
        
            | 
            | 
           473 | 
                * along to the next in the chain)
  | 
        
        
            | 
            | 
           474 | 
                *
  | 
        
        
            | 
            | 
           475 | 
                * The callback should implement the following signature (where $value is the first column field, $row is all column
  | 
        
        
            | 
            | 
           476 | 
                * fields, $additionalarguments are those passed to this method, and $aggregation indicates the current aggregation type
  | 
        
        
            | 
            | 
           477 | 
                * being applied to the column):
  | 
        
        
            | 
            | 
           478 | 
                *
  | 
        
        
            | 
            | 
           479 | 
                * function($value, stdClass $row, $additionalarguments, ?string $aggregation): string
  | 
        
        
            | 
            | 
           480 | 
                *
  | 
        
        
            | 
            | 
           481 | 
                * The type of the $value parameter passed to the callback is determined by calling {@see set_type}, this type is preserved
  | 
        
        
            | 
            | 
           482 | 
                * if the column is part of a report source and is being aggregated. For entities that can be left joined to a report, the
  | 
        
        
            | 
            | 
           483 | 
                * first argument of the callback must be nullable (as it should also be if the first column field is itself nullable).
  | 
        
        
            | 
            | 
           484 | 
                *
  | 
        
        
            | 
            | 
           485 | 
                * @param callable $callable
  | 
        
        
            | 
            | 
           486 | 
                * @param mixed $additionalarguments
  | 
        
        
            | 
            | 
           487 | 
                * @return self
  | 
        
        
            | 
            | 
           488 | 
                */
  | 
        
        
            | 
            | 
           489 | 
               public function add_callback(callable $callable, $additionalarguments = null): self {
  | 
        
        
            | 
            | 
           490 | 
                   $this->callbacks[] = [$callable, $additionalarguments];
  | 
        
        
            | 
            | 
           491 | 
                   return $this;
  | 
        
        
            | 
            | 
           492 | 
               }
  | 
        
        
            | 
            | 
           493 | 
              | 
        
        
            | 
            | 
           494 | 
               /**
  | 
        
        
            | 
            | 
           495 | 
                * Sets column callback. This will overwrite any previously added callbacks {@see add_callback}
  | 
        
        
            | 
            | 
           496 | 
                *
  | 
        
        
            | 
            | 
           497 | 
                * @param callable $callable
  | 
        
        
            | 
            | 
           498 | 
                * @param mixed $additionalarguments
  | 
        
        
            | 
            | 
           499 | 
                * @return self
  | 
        
        
            | 
            | 
           500 | 
                */
  | 
        
        
            | 
            | 
           501 | 
               public function set_callback(callable $callable, $additionalarguments = null): self {
  | 
        
        
            | 
            | 
           502 | 
                   $this->callbacks = [];
  | 
        
        
            | 
            | 
           503 | 
                   return $this->add_callback($callable, $additionalarguments);
  | 
        
        
            | 
            | 
           504 | 
               }
  | 
        
        
            | 
            | 
           505 | 
              | 
        
        
            | 
            | 
           506 | 
               /**
  | 
        
        
            | 
            | 
           507 | 
                * Set column aggregation type
  | 
        
        
            | 
            | 
           508 | 
                *
  | 
        
        
            | 
            | 
           509 | 
                * @param string|null $aggregation Type of aggregation, e.g. 'sum', 'count', etc
  | 
        
        
           | 1441 | 
           ariadna | 
           510 | 
                * @param array|null $options Aggregation type options
  | 
        
        
           | 1 | 
           efrain | 
           511 | 
                * @return self
  | 
        
        
            | 
            | 
           512 | 
                * @throws coding_exception For invalid aggregation type, or one that is incompatible with column type
  | 
        
        
            | 
            | 
           513 | 
                */
  | 
        
        
           | 1441 | 
           ariadna | 
           514 | 
               public function set_aggregation(?string $aggregation, ?array $options = null): self {
  | 
        
        
            | 
            | 
           515 | 
                   if ((string) $aggregation !== '') {
  | 
        
        
            | 
            | 
           516 | 
              | 
        
        
            | 
            | 
           517 | 
                       // Convert aggregation to full class instance for internal storage.
  | 
        
        
            | 
            | 
           518 | 
                       $aggregationclasspath = aggregation::get_full_classpath($aggregation);
  | 
        
        
            | 
            | 
           519 | 
                       if (!aggregation::valid($aggregationclasspath) || !$aggregationclasspath::compatible($this->get_type())) {
  | 
        
        
           | 1 | 
           efrain | 
           520 | 
                           throw new coding_exception('Invalid column aggregation', $aggregation);
  | 
        
        
            | 
            | 
           521 | 
                       }
  | 
        
        
           | 1441 | 
           ariadna | 
           522 | 
              | 
        
        
            | 
            | 
           523 | 
                       $options ??= $this->get_aggregation_options($aggregation);
  | 
        
        
            | 
            | 
           524 | 
                       $this->aggregation = new $aggregationclasspath($options);
  | 
        
        
            | 
            | 
           525 | 
                   } else {
  | 
        
        
            | 
            | 
           526 | 
                       $this->aggregation = null;
  | 
        
        
           | 1 | 
           efrain | 
           527 | 
                   }
  | 
        
        
            | 
            | 
           528 | 
              | 
        
        
            | 
            | 
           529 | 
                   return $this;
  | 
        
        
            | 
            | 
           530 | 
               }
  | 
        
        
            | 
            | 
           531 | 
              | 
        
        
            | 
            | 
           532 | 
               /**
  | 
        
        
            | 
            | 
           533 | 
                * Get column aggregation type
  | 
        
        
            | 
            | 
           534 | 
                *
  | 
        
        
            | 
            | 
           535 | 
                * @return base|null
  | 
        
        
            | 
            | 
           536 | 
                */
  | 
        
        
           | 1441 | 
           ariadna | 
           537 | 
               public function get_aggregation(): ?base {
  | 
        
        
           | 1 | 
           efrain | 
           538 | 
                   return $this->aggregation;
  | 
        
        
            | 
            | 
           539 | 
               }
  | 
        
        
            | 
            | 
           540 | 
              | 
        
        
            | 
            | 
           541 | 
               /**
  | 
        
        
           | 1441 | 
           ariadna | 
           542 | 
                * Set options for the given aggregation type
  | 
        
        
            | 
            | 
           543 | 
                *
  | 
        
        
            | 
            | 
           544 | 
                * @param string $aggregation Type of aggregation, e.g. 'sum', 'count', etc
  | 
        
        
            | 
            | 
           545 | 
                * @param array $options Aggregation type options
  | 
        
        
            | 
            | 
           546 | 
                * @return self
  | 
        
        
            | 
            | 
           547 | 
                */
  | 
        
        
            | 
            | 
           548 | 
               public function set_aggregation_options(string $aggregation, array $options): self {
  | 
        
        
            | 
            | 
           549 | 
                   $this->aggregationoptions[$aggregation] = $options;
  | 
        
        
            | 
            | 
           550 | 
                   return $this;
  | 
        
        
            | 
            | 
           551 | 
               }
  | 
        
        
            | 
            | 
           552 | 
              | 
        
        
            | 
            | 
           553 | 
               /**
  | 
        
        
            | 
            | 
           554 | 
                * Get options for the given aggregation type
  | 
        
        
            | 
            | 
           555 | 
                *
  | 
        
        
            | 
            | 
           556 | 
                * @param string|null $aggregation Type of aggregation, e.g. 'sum', 'count', etc
  | 
        
        
            | 
            | 
           557 | 
                * @return array
  | 
        
        
            | 
            | 
           558 | 
                */
  | 
        
        
            | 
            | 
           559 | 
               public function get_aggregation_options(?string $aggregation): array {
  | 
        
        
            | 
            | 
           560 | 
                   return $this->aggregationoptions[$aggregation] ?? [];
  | 
        
        
            | 
            | 
           561 | 
               }
  | 
        
        
            | 
            | 
           562 | 
              | 
        
        
            | 
            | 
           563 | 
               /**
  | 
        
        
           | 1 | 
           efrain | 
           564 | 
                * Set disabled aggregation methods for the column. Typically only those methods suitable for the current column type are
  | 
        
        
            | 
            | 
           565 | 
                * available: {@see aggregation::get_column_aggregations}, however in some cases we may want to disable specific methods
  | 
        
        
            | 
            | 
           566 | 
                *
  | 
        
        
            | 
            | 
           567 | 
                * @param array $disabledaggregation Array of types, e.g. ['min', 'sum']
  | 
        
        
            | 
            | 
           568 | 
                * @return self
  | 
        
        
            | 
            | 
           569 | 
                */
  | 
        
        
            | 
            | 
           570 | 
               public function set_disabled_aggregation(array $disabledaggregation): self {
  | 
        
        
            | 
            | 
           571 | 
                   $this->disabledaggregation = $disabledaggregation;
  | 
        
        
            | 
            | 
           572 | 
                   return $this;
  | 
        
        
            | 
            | 
           573 | 
               }
  | 
        
        
            | 
            | 
           574 | 
              | 
        
        
            | 
            | 
           575 | 
               /**
  | 
        
        
            | 
            | 
           576 | 
                * Disable all aggregation methods for the column, for instance when current database can't aggregate fields that contain
  | 
        
        
            | 
            | 
           577 | 
                * sub-queries
  | 
        
        
            | 
            | 
           578 | 
                *
  | 
        
        
            | 
            | 
           579 | 
                * @return self
  | 
        
        
            | 
            | 
           580 | 
                */
  | 
        
        
            | 
            | 
           581 | 
               public function set_disabled_aggregation_all(): self {
  | 
        
        
            | 
            | 
           582 | 
                   $aggregationnames = array_map(static function(string $aggregation): string {
  | 
        
        
            | 
            | 
           583 | 
                       return $aggregation::get_class_name();
  | 
        
        
            | 
            | 
           584 | 
                   }, aggregation::get_aggregations());
  | 
        
        
            | 
            | 
           585 | 
              | 
        
        
            | 
            | 
           586 | 
                   return $this->set_disabled_aggregation($aggregationnames);
  | 
        
        
            | 
            | 
           587 | 
               }
  | 
        
        
            | 
            | 
           588 | 
              | 
        
        
            | 
            | 
           589 | 
               /**
  | 
        
        
            | 
            | 
           590 | 
                * Return those aggregations methods explicitly disabled for the column
  | 
        
        
            | 
            | 
           591 | 
                *
  | 
        
        
            | 
            | 
           592 | 
                * @return array
  | 
        
        
            | 
            | 
           593 | 
                */
  | 
        
        
            | 
            | 
           594 | 
               public function get_disabled_aggregation(): array {
  | 
        
        
            | 
            | 
           595 | 
                   return $this->disabledaggregation;
  | 
        
        
            | 
            | 
           596 | 
               }
  | 
        
        
            | 
            | 
           597 | 
              | 
        
        
            | 
            | 
           598 | 
               /**
  | 
        
        
            | 
            | 
           599 | 
                * Sets the column as sortable
  | 
        
        
            | 
            | 
           600 | 
                *
  | 
        
        
            | 
            | 
           601 | 
                * @param bool $issortable
  | 
        
        
           | 1441 | 
           ariadna | 
           602 | 
                * @param array $sortfields Define the fields that should be used when the column is sorted. Must be a subset of the fields
  | 
        
        
           | 1 | 
           efrain | 
           603 | 
                *      selected for the column, via {@see add_field}. If omitted then the first selected field is used
  | 
        
        
            | 
            | 
           604 | 
                * @return self
  | 
        
        
            | 
            | 
           605 | 
                */
  | 
        
        
            | 
            | 
           606 | 
               public function set_is_sortable(bool $issortable, array $sortfields = []): self {
  | 
        
        
            | 
            | 
           607 | 
                   $this->issortable = $issortable;
  | 
        
        
            | 
            | 
           608 | 
                   $this->sortfields = $sortfields;
  | 
        
        
            | 
            | 
           609 | 
                   return $this;
  | 
        
        
            | 
            | 
           610 | 
               }
  | 
        
        
            | 
            | 
           611 | 
              | 
        
        
            | 
            | 
           612 | 
               /**
  | 
        
        
            | 
            | 
           613 | 
                * Return sortable status of column
  | 
        
        
            | 
            | 
           614 | 
                *
  | 
        
        
            | 
            | 
           615 | 
                * @return bool
  | 
        
        
            | 
            | 
           616 | 
                */
  | 
        
        
            | 
            | 
           617 | 
               public function get_is_sortable(): bool {
  | 
        
        
            | 
            | 
           618 | 
              | 
        
        
            | 
            | 
           619 | 
                   // Defer sortable status to aggregation type if column is being aggregated.
  | 
        
        
           | 1441 | 
           ariadna | 
           620 | 
                   if ($this->aggregation !== null) {
  | 
        
        
           | 1 | 
           efrain | 
           621 | 
                       return $this->aggregation::sortable($this->issortable);
  | 
        
        
            | 
            | 
           622 | 
                   }
  | 
        
        
            | 
            | 
           623 | 
              | 
        
        
            | 
            | 
           624 | 
                   return $this->issortable;
  | 
        
        
            | 
            | 
           625 | 
               }
  | 
        
        
            | 
            | 
           626 | 
              | 
        
        
            | 
            | 
           627 | 
               /**
  | 
        
        
            | 
            | 
           628 | 
                * Return fields to use for sorting of the column, where available the field aliases will be returned
  | 
        
        
            | 
            | 
           629 | 
                *
  | 
        
        
            | 
            | 
           630 | 
                * @return array
  | 
        
        
            | 
            | 
           631 | 
                */
  | 
        
        
            | 
            | 
           632 | 
               public function get_sort_fields(): array {
  | 
        
        
            | 
            | 
           633 | 
                   $fieldsalias = $this->get_fields_sql_alias();
  | 
        
        
            | 
            | 
           634 | 
              | 
        
        
            | 
            | 
           635 | 
                   return array_map(static function(string $sortfield) use ($fieldsalias): string {
  | 
        
        
            | 
            | 
           636 | 
              | 
        
        
            | 
            | 
           637 | 
                       // Check whether sortfield refers to a defined field alias.
  | 
        
        
            | 
            | 
           638 | 
                       if (array_key_exists($sortfield, $fieldsalias)) {
  | 
        
        
            | 
            | 
           639 | 
                           return $fieldsalias[$sortfield]['alias'];
  | 
        
        
            | 
            | 
           640 | 
                       }
  | 
        
        
            | 
            | 
           641 | 
              | 
        
        
            | 
            | 
           642 | 
                       // Check whether sortfield refers to field SQL.
  | 
        
        
           | 1441 | 
           ariadna | 
           643 | 
                       return str_ireplace(
  | 
        
        
            | 
            | 
           644 | 
                           array_column($fieldsalias, 'sql'),
  | 
        
        
            | 
            | 
           645 | 
                           array_column($fieldsalias, 'alias'),
  | 
        
        
            | 
            | 
           646 | 
                           $sortfield,
  | 
        
        
            | 
            | 
           647 | 
                       );
  | 
        
        
           | 1 | 
           efrain | 
           648 | 
                   }, $this->sortfields);
  | 
        
        
            | 
            | 
           649 | 
               }
  | 
        
        
            | 
            | 
           650 | 
              | 
        
        
            | 
            | 
           651 | 
               /**
  | 
        
        
            | 
            | 
           652 | 
                * Extract all values from given row for this column
  | 
        
        
            | 
            | 
           653 | 
                *
  | 
        
        
            | 
            | 
           654 | 
                * @param array $row
  | 
        
        
            | 
            | 
           655 | 
                * @return array
  | 
        
        
            | 
            | 
           656 | 
                */
  | 
        
        
            | 
            | 
           657 | 
               private function get_values(array $row): array {
  | 
        
        
            | 
            | 
           658 | 
                   $values = [];
  | 
        
        
            | 
            | 
           659 | 
              | 
        
        
            | 
            | 
           660 | 
                   // During aggregation we only get a single alias back, subsequent aliases won't exist.
  | 
        
        
            | 
            | 
           661 | 
                   foreach ($this->get_fields_sql_alias() as $alias => $field) {
  | 
        
        
            | 
            | 
           662 | 
                       $values[$alias] = $row[$field['alias']] ?? null;
  | 
        
        
            | 
            | 
           663 | 
                   }
  | 
        
        
            | 
            | 
           664 | 
              | 
        
        
            | 
            | 
           665 | 
                   return $values;
  | 
        
        
            | 
            | 
           666 | 
               }
  | 
        
        
            | 
            | 
           667 | 
              | 
        
        
            | 
            | 
           668 | 
               /**
  | 
        
        
            | 
            | 
           669 | 
                * Return the default column value, that being the value of it's first field
  | 
        
        
            | 
            | 
           670 | 
                *
  | 
        
        
            | 
            | 
           671 | 
                * @param array $values
  | 
        
        
            | 
            | 
           672 | 
                * @param int $columntype
  | 
        
        
            | 
            | 
           673 | 
                * @return mixed
  | 
        
        
            | 
            | 
           674 | 
                */
  | 
        
        
            | 
            | 
           675 | 
               public static function get_default_value(array $values, int $columntype) {
  | 
        
        
            | 
            | 
           676 | 
                   $value = reset($values);
  | 
        
        
            | 
            | 
           677 | 
                   if ($value === null) {
  | 
        
        
            | 
            | 
           678 | 
                       return $value;
  | 
        
        
            | 
            | 
           679 | 
                   }
  | 
        
        
            | 
            | 
           680 | 
              | 
        
        
            | 
            | 
           681 | 
                   // Ensure default value is cast to it's strict type.
  | 
        
        
            | 
            | 
           682 | 
                   switch ($columntype) {
  | 
        
        
            | 
            | 
           683 | 
                       case self::TYPE_INTEGER:
  | 
        
        
            | 
            | 
           684 | 
                       case self::TYPE_TIMESTAMP:
  | 
        
        
            | 
            | 
           685 | 
                           $value = (int) $value;
  | 
        
        
            | 
            | 
           686 | 
                           break;
  | 
        
        
            | 
            | 
           687 | 
                       case self::TYPE_FLOAT:
  | 
        
        
            | 
            | 
           688 | 
                           $value = (float) $value;
  | 
        
        
            | 
            | 
           689 | 
                           break;
  | 
        
        
            | 
            | 
           690 | 
                       case self::TYPE_BOOLEAN:
  | 
        
        
            | 
            | 
           691 | 
                           $value = (bool) $value;
  | 
        
        
            | 
            | 
           692 | 
                           break;
  | 
        
        
            | 
            | 
           693 | 
                   }
  | 
        
        
            | 
            | 
           694 | 
              | 
        
        
            | 
            | 
           695 | 
                   return $value;
  | 
        
        
            | 
            | 
           696 | 
               }
  | 
        
        
            | 
            | 
           697 | 
              | 
        
        
            | 
            | 
           698 | 
               /**
  | 
        
        
            | 
            | 
           699 | 
                * Return column value based on complete table row
  | 
        
        
            | 
            | 
           700 | 
                *
  | 
        
        
            | 
            | 
           701 | 
                * @param array $row
  | 
        
        
            | 
            | 
           702 | 
                * @return mixed
  | 
        
        
            | 
            | 
           703 | 
                */
  | 
        
        
            | 
            | 
           704 | 
               public function format_value(array $row) {
  | 
        
        
            | 
            | 
           705 | 
                   $values = $this->get_values($row);
  | 
        
        
            | 
            | 
           706 | 
              | 
        
        
            | 
            | 
           707 | 
                   // If column is being aggregated then defer formatting to them, otherwise loop through all column callbacks.
  | 
        
        
           | 1441 | 
           ariadna | 
           708 | 
                   if ($this->aggregation !== null) {
  | 
        
        
            | 
            | 
           709 | 
                       $value = self::get_default_value($values, $this->aggregation::get_column_type($this->get_type()));
  | 
        
        
            | 
            | 
           710 | 
                       $value = $this->aggregation->format_value($value, $values, $this->callbacks, $this->get_type());
  | 
        
        
           | 1 | 
           efrain | 
           711 | 
                   } else {
  | 
        
        
           | 1441 | 
           ariadna | 
           712 | 
                       $value = self::get_default_value($values, $this->get_type());
  | 
        
        
           | 1 | 
           efrain | 
           713 | 
                       foreach ($this->callbacks as $callback) {
  | 
        
        
            | 
            | 
           714 | 
                           [$callable, $arguments] = $callback;
  | 
        
        
            | 
            | 
           715 | 
                           $value = ($callable)($value, (object) $values, $arguments, null);
  | 
        
        
            | 
            | 
           716 | 
                       }
  | 
        
        
            | 
            | 
           717 | 
                   }
  | 
        
        
            | 
            | 
           718 | 
              | 
        
        
            | 
            | 
           719 | 
                   return $value;
  | 
        
        
            | 
            | 
           720 | 
               }
  | 
        
        
            | 
            | 
           721 | 
              | 
        
        
            | 
            | 
           722 | 
               /**
  | 
        
        
            | 
            | 
           723 | 
                * Add column attributes (data-, class, etc.) that will be included in HTML when column is displayed
  | 
        
        
            | 
            | 
           724 | 
                *
  | 
        
        
            | 
            | 
           725 | 
                * @param array $attributes
  | 
        
        
            | 
            | 
           726 | 
                * @return self
  | 
        
        
            | 
            | 
           727 | 
                */
  | 
        
        
            | 
            | 
           728 | 
               public function add_attributes(array $attributes): self {
  | 
        
        
            | 
            | 
           729 | 
                   $this->attributes = $attributes + $this->attributes;
  | 
        
        
            | 
            | 
           730 | 
                   return $this;
  | 
        
        
            | 
            | 
           731 | 
               }
  | 
        
        
            | 
            | 
           732 | 
              | 
        
        
            | 
            | 
           733 | 
               /**
  | 
        
        
            | 
            | 
           734 | 
                * Returns the column HTML attributes
  | 
        
        
            | 
            | 
           735 | 
                *
  | 
        
        
            | 
            | 
           736 | 
                * @return array
  | 
        
        
            | 
            | 
           737 | 
                */
  | 
        
        
            | 
            | 
           738 | 
               public function get_attributes(): array {
  | 
        
        
            | 
            | 
           739 | 
                   return $this->attributes;
  | 
        
        
            | 
            | 
           740 | 
               }
  | 
        
        
            | 
            | 
           741 | 
              | 
        
        
            | 
            | 
           742 | 
               /**
  | 
        
        
           | 1441 | 
           ariadna | 
           743 | 
                * Set column help icon
  | 
        
        
            | 
            | 
           744 | 
                *
  | 
        
        
            | 
            | 
           745 | 
                * @param help_icon $helpicon
  | 
        
        
            | 
            | 
           746 | 
                * @return self
  | 
        
        
            | 
            | 
           747 | 
                */
  | 
        
        
            | 
            | 
           748 | 
               public function set_help_icon(help_icon $helpicon): self {
  | 
        
        
            | 
            | 
           749 | 
                   $this->helpicon = $helpicon;
  | 
        
        
            | 
            | 
           750 | 
                   return $this;
  | 
        
        
            | 
            | 
           751 | 
               }
  | 
        
        
            | 
            | 
           752 | 
              | 
        
        
            | 
            | 
           753 | 
               /**
  | 
        
        
            | 
            | 
           754 | 
                * Return column help icon
  | 
        
        
            | 
            | 
           755 | 
                *
  | 
        
        
            | 
            | 
           756 | 
                * @return help_icon|null
  | 
        
        
            | 
            | 
           757 | 
                */
  | 
        
        
            | 
            | 
           758 | 
               public function get_help_icon(): ?help_icon {
  | 
        
        
            | 
            | 
           759 | 
                   return $this->helpicon;
  | 
        
        
            | 
            | 
           760 | 
               }
  | 
        
        
            | 
            | 
           761 | 
              | 
        
        
            | 
            | 
           762 | 
               /**
  | 
        
        
           | 1 | 
           efrain | 
           763 | 
                * Return available state of the column for the current user. For instance the column may be added to a report with the
  | 
        
        
            | 
            | 
           764 | 
                * expectation that only some users are able to see it
  | 
        
        
            | 
            | 
           765 | 
                *
  | 
        
        
            | 
            | 
           766 | 
                * @return bool
  | 
        
        
            | 
            | 
           767 | 
                */
  | 
        
        
            | 
            | 
           768 | 
               public function get_is_available(): bool {
  | 
        
        
            | 
            | 
           769 | 
                   return $this->available;
  | 
        
        
            | 
            | 
           770 | 
               }
  | 
        
        
            | 
            | 
           771 | 
              | 
        
        
            | 
            | 
           772 | 
               /**
  | 
        
        
            | 
            | 
           773 | 
                * Conditionally set whether the column is available.
  | 
        
        
            | 
            | 
           774 | 
                *
  | 
        
        
            | 
            | 
           775 | 
                * @param bool $available
  | 
        
        
            | 
            | 
           776 | 
                * @return self
  | 
        
        
            | 
            | 
           777 | 
                */
  | 
        
        
            | 
            | 
           778 | 
               public function set_is_available(bool $available): self {
  | 
        
        
            | 
            | 
           779 | 
                   $this->available = $available;
  | 
        
        
            | 
            | 
           780 | 
                   return $this;
  | 
        
        
            | 
            | 
           781 | 
               }
  | 
        
        
            | 
            | 
           782 | 
              | 
        
        
            | 
            | 
           783 | 
               /**
  | 
        
        
            | 
            | 
           784 | 
                * Set deprecated state of the column, in which case it will still be shown when already present in existing reports but
  | 
        
        
            | 
            | 
           785 | 
                * won't be available for selection in the report editor
  | 
        
        
            | 
            | 
           786 | 
                *
  | 
        
        
            | 
            | 
           787 | 
                * @param string $deprecatedmessage
  | 
        
        
            | 
            | 
           788 | 
                * @return self
  | 
        
        
            | 
            | 
           789 | 
                */
  | 
        
        
            | 
            | 
           790 | 
               public function set_is_deprecated(string $deprecatedmessage = ''): self {
  | 
        
        
            | 
            | 
           791 | 
                   $this->deprecated = true;
  | 
        
        
            | 
            | 
           792 | 
                   $this->deprecatedmessage = $deprecatedmessage;
  | 
        
        
            | 
            | 
           793 | 
                   return $this;
  | 
        
        
            | 
            | 
           794 | 
               }
  | 
        
        
            | 
            | 
           795 | 
              | 
        
        
            | 
            | 
           796 | 
               /**
  | 
        
        
            | 
            | 
           797 | 
                * Return deprecated state of the column
  | 
        
        
            | 
            | 
           798 | 
                *
  | 
        
        
            | 
            | 
           799 | 
                * @return bool
  | 
        
        
            | 
            | 
           800 | 
                */
  | 
        
        
            | 
            | 
           801 | 
               public function get_is_deprecated(): bool {
  | 
        
        
            | 
            | 
           802 | 
                   return $this->deprecated;
  | 
        
        
            | 
            | 
           803 | 
               }
  | 
        
        
            | 
            | 
           804 | 
              | 
        
        
            | 
            | 
           805 | 
               /**
  | 
        
        
            | 
            | 
           806 | 
                * Return deprecated message of the column
  | 
        
        
            | 
            | 
           807 | 
                *
  | 
        
        
            | 
            | 
           808 | 
                * @return string
  | 
        
        
            | 
            | 
           809 | 
                */
  | 
        
        
            | 
            | 
           810 | 
               public function get_is_deprecated_message(): string {
  | 
        
        
            | 
            | 
           811 | 
                   return $this->deprecatedmessage;
  | 
        
        
            | 
            | 
           812 | 
               }
  | 
        
        
            | 
            | 
           813 | 
              | 
        
        
            | 
            | 
           814 | 
               /**
  | 
        
        
            | 
            | 
           815 | 
                * Set column persistent
  | 
        
        
            | 
            | 
           816 | 
                *
  | 
        
        
            | 
            | 
           817 | 
                * @param column_model $persistent
  | 
        
        
            | 
            | 
           818 | 
                * @return self
  | 
        
        
            | 
            | 
           819 | 
                */
  | 
        
        
            | 
            | 
           820 | 
               public function set_persistent(column_model $persistent): self {
  | 
        
        
            | 
            | 
           821 | 
                   $this->persistent = $persistent;
  | 
        
        
            | 
            | 
           822 | 
                   return $this;
  | 
        
        
            | 
            | 
           823 | 
               }
  | 
        
        
            | 
            | 
           824 | 
              | 
        
        
            | 
            | 
           825 | 
               /**
  | 
        
        
            | 
            | 
           826 | 
                * Return column persistent
  | 
        
        
            | 
            | 
           827 | 
                *
  | 
        
        
            | 
            | 
           828 | 
                * @return mixed
  | 
        
        
            | 
            | 
           829 | 
                */
  | 
        
        
            | 
            | 
           830 | 
               public function get_persistent(): column_model {
  | 
        
        
            | 
            | 
           831 | 
                   return $this->persistent;
  | 
        
        
            | 
            | 
           832 | 
               }
  | 
        
        
            | 
            | 
           833 | 
           }
  |