Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

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

(no description)

File Size: 757 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

return: self
param: string $name

get_name()   X-Ref
Return column name

return: mixed

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

return: self
param: lang_string|null $title

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

return: self
param: int $index

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

return: self
param: int $type

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

return: self
param: string $join

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

return: self
param: string[] $joins

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

return: self
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

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

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

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

return: string
param: string $name

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

return: self
param: string $groupbysql

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 applied one after another)

The callback should implement the following signature (where $value is the first column field, $row is all column
fields, and $additionalarguments are those passed on from this method):

The type of the $value parameter passed to the callback is determined by calling {@see set_type}, however note that
if the column is part of a report source and can be aggregated using one of the "Group concatenation" methods then the
type should be omitted if it's not string
For entities that can to be left joined to a report, the first argument to their column callbacks must be nullable.

function($value, stdClass $row[, $additionalarguments]): string

return: self
param: callable $callable function that takes arguments ($value, \stdClass $row, $additionalarguments)
param: mixed $additionalarguments

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

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

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

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

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

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

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

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

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

return: array
param: array $row

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

return: mixed
param: array $values

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

return: mixed
param: array $row

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

return: self
param: array $attributes

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.

return: self
param: bool $available

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

return: self
param: column_model $persistent

get_persistent()   X-Ref
Return column persistent

return: mixed