Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

Differences Between: [Versions 400 and 402] [Versions 401 and 402] [Versions 402 and 403]

(no description)

File Size: 759 lines (24 kb)
Included or required:0 times
Referenced: 0 times
Includes or requires: 0 files

Defines 1 class


Class: column  - X-Ref

Class to represent a report column

__construct(string $name, ?lang_string $title, string $entityname)   X-Ref
Column constructor

For better readability use chainable methods, for example:

$report->add_column(
(new column('name', new lang_string('name'), 'user'))
->add_join('left join {table} t on t.id = p.tableid')
->add_field('t.name')
->add_callback([format::class, 'format_string']));

param: string $name Internal name of the column
param: lang_string|null $title Title of the column used in reports (null for blank)
param: string $entityname Name of the entity this column belongs to. Typically when creating columns within entities

set_name(string $name)   X-Ref
Set column name

param: string $name
return: self

get_name()   X-Ref
Return column name

return: mixed

set_title(?lang_string $title)   X-Ref
Set column title

param: lang_string|null $title
return: self

get_title()   X-Ref
Return column title

return: string

has_custom_title()   X-Ref
Check whether this column has been given a custom title

return: bool

get_entity_name()   X-Ref
Get column entity name

return: string

get_unique_identifier()   X-Ref
Return unique identifier for this column

return: string

set_index(int $index)   X-Ref
Set the column index within the current report

param: int $index
return: self

set_type(int $type)   X-Ref
Set the column type, if not called then the type will be assumed to be {@see TYPE_TEXT}

The type of a column is used to cast the first column field passed to any callbacks {@see add_callback} as well as the
aggregation options available for the column

param: int $type
return: self

get_type()   X-Ref
Return column type, that being one of the TYPE_* class constants

return: int

add_join(string $join)   X-Ref
Add join clause required for this column to join to existing tables/entities

This is necessary in the case where {@see add_field} is selecting data from a table that isn't otherwise queried

param: string $join
return: self

add_joins(array $joins)   X-Ref
Add multiple join clauses required for this column, passing each to {@see add_join}

Typically when defining columns in entities, you should pass {@see \core_reportbuilder\local\report\base::get_joins} to
this method, so that all entity joins are included in the report when your column is added to it

param: string[] $joins
return: self

get_joins()   X-Ref
Return column joins

return: string[]

add_field(string $sql, string $alias = '', array $params = [])   X-Ref
Adds a field to be queried from the database that is necessary for this column

Multiple fields can be added per column, this method may be called several times. Field aliases must be unique inside
any given column, but there will be no conflicts if the same aliases are used in other columns in the same report

param: string $sql SQL query, this may be a simple "tablealias.fieldname" or a complex sub-query that returns only one field
param: string $alias
param: array $params
return: self

add_fields(string $sql, array $params = [])   X-Ref
Add a list of comma-separated fields

param: string $sql
param: array $params
return: self

unique_param_name(string $name)   X-Ref
Given a param name, add a unique prefix to ensure that the same column with params can be added multiple times to a report

param: string $name
return: string

get_fields_sql_alias()   X-Ref
Helper method to take all fields added to the column, and return appropriate SQL and alias

return: array[]

get_fields()   X-Ref
Return array of SQL expressions for each field of this column

return: array

get_params()   X-Ref
Return column parameters, prefixed by the current index to allow the column to be added multiple times to a report

return: array

get_column_alias()   X-Ref
Return an alias for this column (the generated alias of it's first field)

return: string

set_groupby_sql(string $groupbysql)   X-Ref
Define suitable SQL fragment for grouping by the columns fields. This will be returned from {@see get_groupby_sql} if set

param: string $groupbysql
return: self

get_groupby_sql()   X-Ref
Return suitable SQL fragment for grouping by the column fields (during aggregation)

return: array

add_callback(callable $callable, $additionalarguments = null)   X-Ref
Adds column callback (in the case there are multiple, they will be called iteratively - the result of each passed
along to the next in the chain)

The callback should implement the following signature (where $value is the first column field, $row is all column
fields, $additionalarguments are those passed to this method, and $aggregation indicates the current aggregation type
being applied to the column):

function($value, stdClass $row, $additionalarguments, ?string $aggregation): string

The type of the $value parameter passed to the callback is determined by calling {@see set_type}, this type is preserved
if the column is part of a report source and is being aggregated. For entities that can be left joined to a report, the
first argument of the callback must be nullable (as it should also be if the first column field is itself nullable).

param: callable $callable
param: mixed $additionalarguments
return: self

set_callback(callable $callable, $additionalarguments = null)   X-Ref
Sets column callback. This will overwrite any previously added callbacks {@see add_callback}

param: callable $callable
param: mixed $additionalarguments
return: self

set_aggregation(?string $aggregation)   X-Ref
Set column aggregation type

param: string|null $aggregation Type of aggregation, e.g. 'sum', 'count', etc
return: self

get_aggregation()   X-Ref
Get column aggregation type

return: base|null

set_disabled_aggregation(array $disabledaggregation)   X-Ref
Set disabled aggregation methods for the column. Typically only those methods suitable for the current column type are
available: {@see aggregation::get_column_aggregations}, however in some cases we may want to disable specific methods

param: array $disabledaggregation Array of types, e.g. ['min', 'sum']
return: self

set_disabled_aggregation_all()   X-Ref
Disable all aggregation methods for the column, for instance when current database can't aggregate fields that contain
sub-queries

return: self

get_disabled_aggregation()   X-Ref
Return those aggregations methods explicitly disabled for the column

return: array

set_is_sortable(bool $issortable, array $sortfields = [])   X-Ref
Sets the column as sortable

param: bool $issortable
param: array $sortfields Define the fields that should be used when the column is sorted, typically a subset of the fields
return: self

get_is_sortable()   X-Ref
Return sortable status of column

return: bool

get_sort_fields()   X-Ref
Return fields to use for sorting of the column, where available the field aliases will be returned

return: array

get_values(array $row)   X-Ref
Extract all values from given row for this column

param: array $row
return: array

get_default_value(array $values, int $columntype)   X-Ref
Return the default column value, that being the value of it's first field

param: array $values
param: int $columntype
return: mixed

format_value(array $row)   X-Ref
Return column value based on complete table row

param: array $row
return: mixed

add_attributes(array $attributes)   X-Ref
Add column attributes (data-, class, etc.) that will be included in HTML when column is displayed

param: array $attributes
return: self

get_attributes()   X-Ref
Returns the column HTML attributes

return: array

get_is_available()   X-Ref
Return available state of the column for the current user. For instance the column may be added to a report with the
expectation that only some users are able to see it

return: bool

set_is_available(bool $available)   X-Ref
Conditionally set whether the column is available.

param: bool $available
return: self

set_persistent(column_model $persistent)   X-Ref
Set column persistent

param: column_model $persistent
return: self

get_persistent()   X-Ref
Return column persistent

return: mixed