2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
17 declare(strict_types
=1);
19 namespace core_cohort\reportbuilder\local\entities
;
25 use core_reportbuilder\local\entities\base
;
26 use core_reportbuilder\local\filters\date
;
27 use core_reportbuilder\local\filters\select
;
28 use core_reportbuilder\local\filters\text
;
29 use core_reportbuilder\local\helpers\format
;
30 use core_reportbuilder\local\report\column
;
31 use core_reportbuilder\local\report\filter
;
36 * @package core_cohort
37 * @copyright 2021 Paul Holden <paulh@moodle.com>
38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
40 class cohort
extends base
{
43 * Database tables that this entity uses and their default aliases
47 protected function get_default_table_aliases(): array {
55 * The default title for this entity
59 protected function get_default_entity_title(): lang_string
{
60 return new lang_string('cohort', 'core_cohort');
64 * Initialise the entity
68 public function initialise(): base
{
69 $columns = $this->get_all_columns();
70 foreach ($columns as $column) {
71 $this->add_column($column);
74 // All the filters defined by the entity can also be used as conditions.
75 $filters = $this->get_all_filters();
76 foreach ($filters as $filter) {
79 ->add_condition($filter);
86 * Returns list of all available columns
90 protected function get_all_columns(): array {
93 $tablealias = $this->get_table_alias('cohort');
94 $contextalias = $this->get_table_alias('context');
96 // Category/context column.
97 $columns[] = (new column(
99 new lang_string('category'),
100 $this->get_entity_name()
102 ->add_joins($this->get_joins())
103 ->add_join("JOIN {context} {$contextalias} ON {$contextalias}.id = {$tablealias}.contextid")
104 ->set_type(column
::TYPE_TEXT
)
105 ->add_fields("{$tablealias}.contextid, " . context_helper
::get_preload_record_columns_sql($contextalias))
106 ->set_is_sortable(true)
107 ->add_callback(static function($contextid, stdClass
$cohort): string {
108 context_helper
::preload_from_record($cohort);
109 return context
::instance_by_id($cohort->contextid
)->get_context_name(false);
113 $columns[] = (new column(
115 new lang_string('name', 'core_cohort'),
116 $this->get_entity_name()
118 ->add_joins($this->get_joins())
119 ->set_type(column
::TYPE_TEXT
)
120 ->add_fields("{$tablealias}.name")
121 ->set_is_sortable(true);
124 $columns[] = (new column(
126 new lang_string('idnumber', 'core_cohort'),
127 $this->get_entity_name()
129 ->add_joins($this->get_joins())
130 ->set_type(column
::TYPE_TEXT
)
131 ->add_fields("{$tablealias}.idnumber")
132 ->set_is_sortable(true);
134 // Description column.
135 $descriptionfieldsql = "{$tablealias}.description";
136 if ($DB->get_dbfamily() === 'oracle') {
137 $descriptionfieldsql = $DB->sql_order_by_text($descriptionfieldsql, 1024);
139 $columns[] = (new column(
141 new lang_string('description'),
142 $this->get_entity_name()
144 ->add_joins($this->get_joins())
145 ->add_join("JOIN {context} {$contextalias} ON {$contextalias}.id = {$tablealias}.contextid")
146 ->set_type(column
::TYPE_LONGTEXT
)
147 ->add_field($descriptionfieldsql, 'description')
148 ->add_fields("{$tablealias}.descriptionformat, {$tablealias}.id, {$tablealias}.contextid")
149 ->add_fields(context_helper
::get_preload_record_columns_sql($contextalias))
150 ->add_callback(static function(?
string $description, stdClass
$cohort): string {
152 require_once("{$CFG->libdir}/filelib.php");
154 if ($description === null) {
158 context_helper
::preload_from_record($cohort);
159 $context = context
::instance_by_id($cohort->contextid
);
161 $description = file_rewrite_pluginfile_urls($description, 'pluginfile.php', $context->id
, 'cohort',
162 'description', $cohort->id
);
164 return format_text($description, $cohort->descriptionformat
, ['context' => $context->id
]);
168 $columns[] = (new column(
170 new lang_string('visible', 'core_cohort'),
171 $this->get_entity_name()
173 ->add_joins($this->get_joins())
174 ->set_type(column
::TYPE_BOOLEAN
)
175 ->add_fields("{$tablealias}.visible")
176 ->set_is_sortable(true)
177 ->set_callback([format
::class, 'boolean_as_text']);
179 // Time created column.
180 $columns[] = (new column(
182 new lang_string('timecreated', 'core_reportbuilder'),
183 $this->get_entity_name()
185 ->add_joins($this->get_joins())
186 ->set_type(column
::TYPE_TIMESTAMP
)
187 ->add_fields("{$tablealias}.timecreated")
188 ->set_is_sortable(true)
189 ->set_callback([format
::class, 'userdate']);
191 // Time modified column.
192 $columns[] = (new column(
194 new lang_string('timemodified', 'core_reportbuilder'),
195 $this->get_entity_name()
197 ->add_joins($this->get_joins())
198 ->set_type(column
::TYPE_TIMESTAMP
)
199 ->add_fields("{$tablealias}.timemodified")
200 ->set_is_sortable(true)
201 ->set_callback([format
::class, 'userdate']);
204 $columns[] = (new column(
206 new lang_string('component', 'core_cohort'),
207 $this->get_entity_name()
209 ->add_joins($this->get_joins())
210 ->set_type(column
::TYPE_TEXT
)
211 ->add_fields("{$tablealias}.component")
212 ->set_is_sortable(true)
213 ->add_callback(static function(string $component): string {
214 return empty($component)
215 ?
get_string('nocomponent', 'cohort')
216 : get_string('pluginname', $component);
220 $columns[] = (new column(
222 new lang_string('theme'),
223 $this->get_entity_name()
225 ->add_joins($this->get_joins())
226 ->set_type(column
::TYPE_TEXT
)
227 ->add_fields("{$tablealias}.theme")
228 ->set_is_sortable(true);
234 * Return list of all available filters
238 protected function get_all_filters(): array {
239 $tablealias = $this->get_table_alias('cohort');
242 $filters[] = (new filter(
245 new lang_string('category'),
246 $this->get_entity_name(),
247 "{$tablealias}.contextid"
249 ->add_joins($this->get_joins())
250 ->set_options_callback(static function(): array {
253 // Load all contexts in which there are cohorts.
254 $ctxfields = context_helper
::get_preload_record_columns_sql('ctx');
255 $contexts = $DB->get_records_sql("
256 SELECT DISTINCT {$ctxfields}, c.contextid
258 JOIN {cohort} c ON c.contextid = ctx.id");
260 // Transform context record into it's name (used as the filter options).
261 return array_map(static function(stdClass
$contextrecord): string {
262 context_helper
::preload_from_record($contextrecord);
264 return context
::instance_by_id($contextrecord->contextid
)
265 ->get_context_name(false);
270 $filters[] = (new filter(
273 new lang_string('name', 'core_cohort'),
274 $this->get_entity_name(),
277 ->add_joins($this->get_joins());
280 $filters[] = (new filter(
283 new lang_string('idnumber', 'core_cohort'),
284 $this->get_entity_name(),
285 "{$tablealias}.idnumber"
287 ->add_joins($this->get_joins());
289 // Time created filter.
290 $filters[] = (new filter(
293 new lang_string('timecreated', 'core_reportbuilder'),
294 $this->get_entity_name(),
295 "{$tablealias}.timecreated"
297 ->add_joins($this->get_joins());