MDL-75034 reportbuilder: move classes into consistent namespace.
[moodle.git] / cohort / classes / reportbuilder / local / entities / cohort.php
blob462a498f5c4e81caa2ac56c9585054c980e58f56
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.
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;
21 use context;
22 use context_helper;
23 use lang_string;
24 use stdClass;
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;
33 /**
34 * Cohort entity
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 {
42 /**
43 * Database tables that this entity uses and their default aliases
45 * @return array
47 protected function get_default_table_aliases(): array {
48 return [
49 'cohort' => 'c',
50 'context' => 'chctx',
54 /**
55 * The default title for this entity
57 * @return lang_string
59 protected function get_default_entity_title(): lang_string {
60 return new lang_string('cohort', 'core_cohort');
63 /**
64 * Initialise the entity
66 * @return base
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) {
77 $this
78 ->add_filter($filter)
79 ->add_condition($filter);
82 return $this;
85 /**
86 * Returns list of all available columns
88 * @return column[]
90 protected function get_all_columns(): array {
91 global $DB;
93 $tablealias = $this->get_table_alias('cohort');
94 $contextalias = $this->get_table_alias('context');
96 // Category/context column.
97 $columns[] = (new column(
98 'context',
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);
112 // Name column.
113 $columns[] = (new column(
114 'name',
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);
123 // ID number column.
124 $columns[] = (new column(
125 'idnumber',
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(
140 'description',
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 {
151 global $CFG;
152 require_once("{$CFG->libdir}/filelib.php");
154 if ($description === null) {
155 return '';
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]);
167 // Visible column.
168 $columns[] = (new column(
169 'visible',
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(
181 'timecreated',
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(
193 'timemodified',
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']);
203 // Component column.
204 $columns[] = (new column(
205 'component',
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);
219 // Theme column.
220 $columns[] = (new column(
221 'theme',
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);
230 return $columns;
234 * Return list of all available filters
236 * @return filter[]
238 protected function get_all_filters(): array {
239 $tablealias = $this->get_table_alias('cohort');
241 // Context filter.
242 $filters[] = (new filter(
243 select::class,
244 'context',
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 {
251 global $DB;
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
257 FROM {context} ctx
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);
266 }, $contexts);
269 // Name filter.
270 $filters[] = (new filter(
271 text::class,
272 'name',
273 new lang_string('name', 'core_cohort'),
274 $this->get_entity_name(),
275 "{$tablealias}.name"
277 ->add_joins($this->get_joins());
279 // ID number filter.
280 $filters[] = (new filter(
281 text::class,
282 'idnumber',
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(
291 date::class,
292 'timecreated',
293 new lang_string('timecreated', 'core_reportbuilder'),
294 $this->get_entity_name(),
295 "{$tablealias}.timecreated"
297 ->add_joins($this->get_joins());
299 return $filters;