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_course\reportbuilder\local\entities;
|
|
|
20 |
|
|
|
21 |
use context_coursecat;
|
|
|
22 |
use context_helper;
|
|
|
23 |
use html_writer;
|
|
|
24 |
use lang_string;
|
|
|
25 |
use moodle_url;
|
|
|
26 |
use stdClass;
|
|
|
27 |
use theme_config;
|
|
|
28 |
use core_course_category;
|
|
|
29 |
use core_reportbuilder\local\entities\base;
|
|
|
30 |
use core_reportbuilder\local\filters\{category, select, text};
|
|
|
31 |
use core_reportbuilder\local\report\column;
|
|
|
32 |
use core_reportbuilder\local\report\filter;
|
|
|
33 |
|
|
|
34 |
/**
|
|
|
35 |
* Course category entity
|
|
|
36 |
*
|
|
|
37 |
* @package core_course
|
|
|
38 |
* @copyright 2021 Paul Holden <paulh@moodle.com>
|
|
|
39 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
40 |
*/
|
|
|
41 |
class course_category extends base {
|
|
|
42 |
|
|
|
43 |
/**
|
|
|
44 |
* Database tables that this entity uses
|
|
|
45 |
*
|
|
|
46 |
* @return string[]
|
|
|
47 |
*/
|
|
|
48 |
protected function get_default_tables(): array {
|
|
|
49 |
return [
|
|
|
50 |
'context',
|
|
|
51 |
'course_categories',
|
|
|
52 |
];
|
|
|
53 |
}
|
|
|
54 |
|
|
|
55 |
/**
|
|
|
56 |
* The default title for this entity
|
|
|
57 |
*
|
|
|
58 |
* @return lang_string
|
|
|
59 |
*/
|
|
|
60 |
protected function get_default_entity_title(): lang_string {
|
|
|
61 |
return new lang_string('coursecategory');
|
|
|
62 |
}
|
|
|
63 |
|
|
|
64 |
/**
|
|
|
65 |
* Initialise the entity
|
|
|
66 |
*
|
|
|
67 |
* @return base
|
|
|
68 |
*/
|
|
|
69 |
public function initialise(): base {
|
|
|
70 |
$columns = $this->get_all_columns();
|
|
|
71 |
foreach ($columns as $column) {
|
|
|
72 |
$this->add_column($column);
|
|
|
73 |
}
|
|
|
74 |
|
|
|
75 |
// All the filters defined by the entity can also be used as conditions.
|
|
|
76 |
$filters = $this->get_all_filters();
|
|
|
77 |
foreach ($filters as $filter) {
|
|
|
78 |
$this
|
|
|
79 |
->add_filter($filter)
|
|
|
80 |
->add_condition($filter);
|
|
|
81 |
}
|
|
|
82 |
|
|
|
83 |
return $this;
|
|
|
84 |
}
|
|
|
85 |
|
|
|
86 |
/**
|
|
|
87 |
* Returns list of all available columns
|
|
|
88 |
*
|
|
|
89 |
* @return column[]
|
|
|
90 |
*/
|
|
|
91 |
protected function get_all_columns(): array {
|
|
|
92 |
global $DB;
|
|
|
93 |
|
|
|
94 |
$tablealias = $this->get_table_alias('course_categories');
|
|
|
95 |
$tablealiascontext = $this->get_table_alias('context');
|
|
|
96 |
|
|
|
97 |
// Name column.
|
|
|
98 |
$columns[] = (new column(
|
|
|
99 |
'name',
|
|
|
100 |
new lang_string('categoryname'),
|
|
|
101 |
$this->get_entity_name()
|
|
|
102 |
))
|
|
|
103 |
->add_joins($this->get_joins())
|
|
|
104 |
->add_join($this->get_context_join())
|
|
|
105 |
->set_type(column::TYPE_TEXT)
|
|
|
106 |
->add_fields("{$tablealias}.name, {$tablealias}.id")
|
|
|
107 |
->add_fields(context_helper::get_preload_record_columns_sql($tablealiascontext))
|
|
|
108 |
->add_callback(static function(?string $name, stdClass $category): string {
|
|
|
109 |
if (empty($category->id)) {
|
|
|
110 |
return '';
|
|
|
111 |
}
|
|
|
112 |
|
|
|
113 |
context_helper::preload_from_record($category);
|
|
|
114 |
$context = context_coursecat::instance($category->id);
|
|
|
115 |
|
|
|
116 |
return format_string($category->name, true, ['context' => $context]);
|
|
|
117 |
})
|
|
|
118 |
->set_is_sortable(true);
|
|
|
119 |
|
|
|
120 |
// Category name with link column.
|
|
|
121 |
$columns[] = (new column(
|
|
|
122 |
'namewithlink',
|
|
|
123 |
new lang_string('namewithlink', 'core_course'),
|
|
|
124 |
$this->get_entity_name()
|
|
|
125 |
))
|
|
|
126 |
->add_joins($this->get_joins())
|
|
|
127 |
->add_join($this->get_context_join())
|
|
|
128 |
->set_type(column::TYPE_TEXT)
|
|
|
129 |
->add_fields("{$tablealias}.name, {$tablealias}.id")
|
|
|
130 |
->add_fields(context_helper::get_preload_record_columns_sql($tablealiascontext))
|
|
|
131 |
->add_callback(static function(?string $name, stdClass $category): string {
|
|
|
132 |
if (empty($category->id)) {
|
|
|
133 |
return '';
|
|
|
134 |
}
|
|
|
135 |
context_helper::preload_from_record($category);
|
|
|
136 |
$context = context_coursecat::instance($category->id);
|
|
|
137 |
$url = new moodle_url('/course/management.php', ['categoryid' => $category->id]);
|
|
|
138 |
return html_writer::link($url,
|
|
|
139 |
format_string($category->name, true, ['context' => $context]));
|
|
|
140 |
})
|
|
|
141 |
->set_is_sortable(true);
|
|
|
142 |
|
|
|
143 |
// Path column.
|
|
|
144 |
$columns[] = (new column(
|
|
|
145 |
'path',
|
|
|
146 |
new lang_string('categorypath'),
|
|
|
147 |
$this->get_entity_name()
|
|
|
148 |
))
|
|
|
149 |
->add_joins($this->get_joins())
|
|
|
150 |
->set_type(column::TYPE_TEXT)
|
|
|
151 |
->add_fields("{$tablealias}.name, {$tablealias}.id")
|
|
|
152 |
->add_callback(static function(?string $name, stdClass $category): string {
|
|
|
153 |
return empty($category->id) ? '' :
|
|
|
154 |
core_course_category::get($category->id, MUST_EXIST, true)->get_nested_name(false);
|
|
|
155 |
})
|
|
|
156 |
->set_disabled_aggregation(['groupconcat', 'groupconcatdistinct'])
|
|
|
157 |
->set_is_sortable(true);
|
|
|
158 |
|
|
|
159 |
// ID number column.
|
|
|
160 |
$columns[] = (new column(
|
|
|
161 |
'idnumber',
|
|
|
162 |
new lang_string('idnumbercoursecategory'),
|
|
|
163 |
$this->get_entity_name()
|
|
|
164 |
))
|
|
|
165 |
->add_joins($this->get_joins())
|
|
|
166 |
->set_type(column::TYPE_TEXT)
|
|
|
167 |
->add_fields("{$tablealias}.idnumber")
|
|
|
168 |
->set_is_sortable(true);
|
|
|
169 |
|
|
|
170 |
// Description column (note we need to join/select from the context table in order to format the column).
|
|
|
171 |
$descriptionfieldsql = "{$tablealias}.description";
|
|
|
172 |
if ($DB->get_dbfamily() === 'oracle') {
|
|
|
173 |
$descriptionfieldsql = $DB->sql_order_by_text($descriptionfieldsql, 1024);
|
|
|
174 |
}
|
|
|
175 |
$columns[] = (new column(
|
|
|
176 |
'description',
|
|
|
177 |
new lang_string('description'),
|
|
|
178 |
$this->get_entity_name()
|
|
|
179 |
))
|
|
|
180 |
->add_joins($this->get_joins())
|
|
|
181 |
->add_join($this->get_context_join())
|
|
|
182 |
->set_type(column::TYPE_LONGTEXT)
|
|
|
183 |
->add_field($descriptionfieldsql, 'description')
|
|
|
184 |
->add_fields("{$tablealias}.descriptionformat, {$tablealias}.id")
|
|
|
185 |
->add_fields(context_helper::get_preload_record_columns_sql($tablealiascontext))
|
|
|
186 |
->add_callback(static function(?string $description, stdClass $category): string {
|
|
|
187 |
global $CFG;
|
|
|
188 |
require_once("{$CFG->libdir}/filelib.php");
|
|
|
189 |
|
|
|
190 |
if ($description === null) {
|
|
|
191 |
return '';
|
|
|
192 |
}
|
|
|
193 |
|
|
|
194 |
context_helper::preload_from_record($category);
|
|
|
195 |
$context = context_coursecat::instance($category->id);
|
|
|
196 |
|
|
|
197 |
$description = file_rewrite_pluginfile_urls($description, 'pluginfile.php', $context->id, 'coursecat',
|
|
|
198 |
'description', null);
|
|
|
199 |
|
|
|
200 |
return format_text($description, $category->descriptionformat, ['context' => $context->id]);
|
|
|
201 |
});
|
|
|
202 |
|
|
|
203 |
// Theme column.
|
|
|
204 |
$columns[] = (new column(
|
|
|
205 |
'theme',
|
|
|
206 |
new lang_string('theme'),
|
|
|
207 |
$this->get_entity_name()
|
|
|
208 |
))
|
|
|
209 |
->add_joins($this->get_joins())
|
|
|
210 |
->set_type(column::TYPE_TEXT)
|
|
|
211 |
->add_fields("{$tablealias}.theme")
|
|
|
212 |
->set_is_sortable(true)
|
|
|
213 |
->add_callback(static function (?string $theme): string {
|
|
|
214 |
if ((string) $theme === '') {
|
|
|
215 |
return '';
|
|
|
216 |
}
|
|
|
217 |
|
|
|
218 |
return get_string('pluginname', "theme_{$theme}");
|
|
|
219 |
});
|
|
|
220 |
|
|
|
221 |
// Course count column.
|
|
|
222 |
$columns[] = (new column(
|
|
|
223 |
'coursecount',
|
|
|
224 |
new lang_string('coursecount', 'core_course'),
|
|
|
225 |
$this->get_entity_name()
|
|
|
226 |
))
|
|
|
227 |
->add_joins($this->get_joins())
|
|
|
228 |
->set_type(column::TYPE_INTEGER)
|
|
|
229 |
->add_fields("{$tablealias}.coursecount")
|
|
|
230 |
->set_is_sortable(true);
|
|
|
231 |
|
|
|
232 |
return $columns;
|
|
|
233 |
}
|
|
|
234 |
|
|
|
235 |
/**
|
|
|
236 |
* Return list of all available filters
|
|
|
237 |
*
|
|
|
238 |
* @return filter[]
|
|
|
239 |
*/
|
|
|
240 |
protected function get_all_filters(): array {
|
|
|
241 |
$tablealias = $this->get_table_alias('course_categories');
|
|
|
242 |
|
|
|
243 |
// Select category filter.
|
|
|
244 |
$filters[] = (new filter(
|
|
|
245 |
category::class,
|
|
|
246 |
'name',
|
|
|
247 |
new lang_string('categoryselect', 'core_reportbuilder'),
|
|
|
248 |
$this->get_entity_name(),
|
|
|
249 |
"{$tablealias}.id"
|
|
|
250 |
))
|
|
|
251 |
->add_joins($this->get_joins())
|
|
|
252 |
->set_options([
|
|
|
253 |
'requiredcapabilities' => 'moodle/category:viewcourselist',
|
|
|
254 |
]);
|
|
|
255 |
|
|
|
256 |
// Name filter.
|
|
|
257 |
$filters[] = (new filter(
|
|
|
258 |
text::class,
|
|
|
259 |
'text',
|
|
|
260 |
new lang_string('categoryname'),
|
|
|
261 |
$this->get_entity_name(),
|
|
|
262 |
"{$tablealias}.name"
|
|
|
263 |
))
|
|
|
264 |
->add_joins($this->get_joins());
|
|
|
265 |
|
|
|
266 |
// ID number filter.
|
|
|
267 |
$filters[] = (new filter(
|
|
|
268 |
text::class,
|
|
|
269 |
'idnumber',
|
|
|
270 |
new lang_string('idnumbercoursecategory'),
|
|
|
271 |
$this->get_entity_name(),
|
|
|
272 |
"{$tablealias}.idnumber"
|
|
|
273 |
))
|
|
|
274 |
->add_joins($this->get_joins());
|
|
|
275 |
|
|
|
276 |
// Theme filter.
|
|
|
277 |
$filters[] = (new filter(
|
|
|
278 |
select::class,
|
|
|
279 |
'theme',
|
|
|
280 |
new lang_string('theme'),
|
|
|
281 |
$this->get_entity_name(),
|
|
|
282 |
"{$tablealias}.theme",
|
|
|
283 |
))
|
|
|
284 |
->set_options_callback(static function(): array {
|
|
|
285 |
return array_map(
|
|
|
286 |
fn(theme_config $theme) => $theme->get_theme_name(),
|
|
|
287 |
get_list_of_themes(),
|
|
|
288 |
);
|
|
|
289 |
})
|
|
|
290 |
->add_joins($this->get_joins());
|
|
|
291 |
|
|
|
292 |
return $filters;
|
|
|
293 |
}
|
|
|
294 |
|
|
|
295 |
/**
|
|
|
296 |
* Return context join used by columns
|
|
|
297 |
*
|
|
|
298 |
* @return string
|
|
|
299 |
*/
|
|
|
300 |
public function get_context_join(): string {
|
|
|
301 |
$coursecategories = $this->get_table_alias('course_categories');
|
|
|
302 |
$context = $this->get_table_alias('context');
|
|
|
303 |
return "LEFT JOIN {context} {$context} ON {$context}.instanceid = {$coursecategories}.id
|
|
|
304 |
AND {$context}.contextlevel = " . CONTEXT_COURSECAT;
|
|
|
305 |
}
|
|
|
306 |
}
|