1 |
efrain |
1 |
<?php
|
|
|
2 |
// This file is part of Moodle - https://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 <https://www.gnu.org/licenses/>.
|
|
|
16 |
|
|
|
17 |
namespace tool_courserating\reportbuilder\local\entities;
|
|
|
18 |
|
|
|
19 |
use core_reportbuilder\local\entities\base;
|
|
|
20 |
use core_reportbuilder\local\helpers\format;
|
|
|
21 |
use lang_string;
|
|
|
22 |
use core_reportbuilder\local\report\column;
|
|
|
23 |
use stdClass;
|
|
|
24 |
use core_reportbuilder\local\report\filter;
|
|
|
25 |
use core_reportbuilder\local\filters\number;
|
|
|
26 |
use core_reportbuilder\local\filters\select;
|
|
|
27 |
use tool_courserating\constants;
|
|
|
28 |
use tool_courserating\helper;
|
|
|
29 |
|
|
|
30 |
/**
|
|
|
31 |
* Reportbuilder entity representing table tool_courserating_summary.
|
|
|
32 |
*
|
|
|
33 |
* @package tool_courserating
|
|
|
34 |
* @copyright 2022 Marina Glancy
|
|
|
35 |
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
36 |
*/
|
|
|
37 |
class summary extends base {
|
|
|
38 |
/**
|
|
|
39 |
* Database tables that this entity uses and their default aliases
|
|
|
40 |
*
|
|
|
41 |
* @return array
|
|
|
42 |
*/
|
|
|
43 |
protected function get_default_table_aliases(): array {
|
|
|
44 |
return ['tool_courserating_summary' => 'tool_courserating_summary'];
|
|
|
45 |
}
|
|
|
46 |
|
|
|
47 |
/**
|
|
|
48 |
* Database tables that this entity uses
|
|
|
49 |
*
|
|
|
50 |
* @return string[]
|
|
|
51 |
*/
|
|
|
52 |
protected function get_default_tables(): array {
|
|
|
53 |
return array_keys($this->get_default_table_aliases());
|
|
|
54 |
}
|
|
|
55 |
|
|
|
56 |
/**
|
|
|
57 |
* The default title for this entity
|
|
|
58 |
*
|
|
|
59 |
* @return lang_string
|
|
|
60 |
*/
|
|
|
61 |
protected function get_default_entity_title(): lang_string {
|
|
|
62 |
return new lang_string('entity_summary', 'tool_courserating');
|
|
|
63 |
}
|
|
|
64 |
|
|
|
65 |
/**
|
|
|
66 |
* Initialise the entity
|
|
|
67 |
*
|
|
|
68 |
* @return base
|
|
|
69 |
*/
|
|
|
70 |
public function initialise(): base {
|
|
|
71 |
$columns = $this->get_all_columns();
|
|
|
72 |
foreach ($columns as $column) {
|
|
|
73 |
$this->add_column($column);
|
|
|
74 |
}
|
|
|
75 |
|
|
|
76 |
// All the filters defined by the entity can also be used as conditions.
|
|
|
77 |
$filters = $this->get_all_filters();
|
|
|
78 |
foreach ($filters as $filter) {
|
|
|
79 |
$this
|
|
|
80 |
->add_filter($filter)
|
|
|
81 |
->add_condition($filter);
|
|
|
82 |
}
|
|
|
83 |
|
|
|
84 |
return $this;
|
|
|
85 |
}
|
|
|
86 |
|
|
|
87 |
/**
|
|
|
88 |
* Returns list of all available columns
|
|
|
89 |
*
|
|
|
90 |
* @return column[]
|
|
|
91 |
*/
|
|
|
92 |
protected function get_all_columns(): array {
|
|
|
93 |
$tablealias = $this->get_table_alias('tool_courserating_summary');
|
|
|
94 |
$columns = [];
|
|
|
95 |
|
|
|
96 |
// Cntall column.
|
|
|
97 |
$columns[] = (new column(
|
|
|
98 |
'cntall',
|
|
|
99 |
new lang_string('summary_cntall', 'tool_courserating'),
|
|
|
100 |
$this->get_entity_name()
|
|
|
101 |
))
|
|
|
102 |
->add_joins($this->get_joins())
|
|
|
103 |
->set_type(column::TYPE_INTEGER)
|
|
|
104 |
->add_fields("{$tablealias}.cntall")
|
|
|
105 |
->set_is_sortable(true);
|
|
|
106 |
|
|
|
107 |
// Avgrating column.
|
|
|
108 |
$columns[] = (new column(
|
|
|
109 |
'avgrating',
|
|
|
110 |
new lang_string('summary_avgrating', 'tool_courserating'),
|
|
|
111 |
$this->get_entity_name()
|
|
|
112 |
))
|
|
|
113 |
->add_joins($this->get_joins())
|
|
|
114 |
->set_type(column::TYPE_FLOAT)
|
|
|
115 |
->add_fields("{$tablealias}.avgrating")
|
|
|
116 |
->set_is_sortable(true)
|
|
|
117 |
->add_callback(static function($value, stdClass $row): ?string {
|
|
|
118 |
return helper::format_avgrating($value);
|
|
|
119 |
});
|
|
|
120 |
|
|
|
121 |
$columns[] = (new column(
|
|
|
122 |
'stars',
|
|
|
123 |
new lang_string('ratingasstars', 'tool_courserating'),
|
|
|
124 |
$this->get_entity_name()
|
|
|
125 |
))
|
|
|
126 |
->add_joins($this->get_joins())
|
|
|
127 |
->set_type(column::TYPE_FLOAT)
|
|
|
128 |
->add_fields("{$tablealias}.avgrating")
|
|
|
129 |
->set_disabled_aggregation(['sum'])
|
|
|
130 |
->set_is_sortable(true)
|
|
|
131 |
->add_callback(static function($avgrating, $r) {
|
|
|
132 |
return helper::stars((float)$avgrating);
|
|
|
133 |
});
|
|
|
134 |
|
|
|
135 |
// Cntreviews column.
|
|
|
136 |
$columns[] = (new column(
|
|
|
137 |
'cntreviews',
|
|
|
138 |
new lang_string('summary_cntreviews', 'tool_courserating'),
|
|
|
139 |
$this->get_entity_name()
|
|
|
140 |
))
|
|
|
141 |
->add_joins($this->get_joins())
|
|
|
142 |
->set_type(column::TYPE_INTEGER)
|
|
|
143 |
->add_fields("{$tablealias}.cntreviews")
|
|
|
144 |
->set_is_sortable(true);
|
|
|
145 |
|
|
|
146 |
// Cnt01-Cnt05 columns.
|
|
|
147 |
for ($i = 1; $i <= 5; $i++) {
|
|
|
148 |
// phpcs:disable Squiz.PHP.CommentedOutCode.Found
|
|
|
149 |
// Mdlcode assume: $i ['1', '2', '3', '4', '5'].
|
|
|
150 |
$fld = "cnt0{$i}";
|
|
|
151 |
$columns[] = (new column(
|
|
|
152 |
$fld,
|
|
|
153 |
// phpcs:disable Squiz.PHP.CommentedOutCode.Found
|
|
|
154 |
// Mdlcode assume-next-line: $fld ['cnt01', 'cnt02', 'cnt03', 'cnt04', 'cnt05'] .
|
|
|
155 |
new lang_string('summary_'.$fld, 'tool_courserating'),
|
|
|
156 |
$this->get_entity_name()
|
|
|
157 |
))
|
|
|
158 |
->add_joins($this->get_joins())
|
|
|
159 |
->set_type(column::TYPE_FLOAT)
|
|
|
160 |
->add_field("CASE WHEN {$tablealias}.cntall > 0 THEN ".
|
|
|
161 |
"100.0*{$tablealias}.{$fld}/{$tablealias}.cntall ELSE NULL END", "p")
|
|
|
162 |
->set_is_sortable(true)
|
|
|
163 |
->set_groupby_sql("{$tablealias}.cntall, {$tablealias}.{$fld}")
|
|
|
164 |
->set_callback(function ($value, $row) {
|
|
|
165 |
return ($row->p === null) ? null : format::percent($value);
|
|
|
166 |
});
|
|
|
167 |
|
|
|
168 |
}
|
|
|
169 |
|
|
|
170 |
// Ratingmode column.
|
|
|
171 |
$columns[] = (new column(
|
|
|
172 |
'ratingmode',
|
|
|
173 |
new lang_string('summary_ratingmode', 'tool_courserating'),
|
|
|
174 |
$this->get_entity_name()
|
|
|
175 |
))
|
|
|
176 |
->add_joins($this->get_joins())
|
|
|
177 |
->set_type(column::TYPE_INTEGER)
|
|
|
178 |
->add_fields("{$tablealias}.ratingmode")
|
|
|
179 |
->set_is_sortable(true)
|
|
|
180 |
->set_callback(static function($v) {
|
|
|
181 |
return is_null($v) ? null : (constants::rated_courses_options()[$v] ?? null);
|
|
|
182 |
});
|
|
|
183 |
|
|
|
184 |
return $columns;
|
|
|
185 |
}
|
|
|
186 |
|
|
|
187 |
/**
|
|
|
188 |
* Return list of all available filters
|
|
|
189 |
*
|
|
|
190 |
* @return filter[]
|
|
|
191 |
*/
|
|
|
192 |
protected function get_all_filters(): array {
|
|
|
193 |
$tablealias = $this->get_table_alias('tool_courserating_summary');
|
|
|
194 |
$filters = [];
|
|
|
195 |
|
|
|
196 |
// Cntall filter.
|
|
|
197 |
$filters[] = (new filter(
|
|
|
198 |
number::class,
|
|
|
199 |
'cntall',
|
|
|
200 |
new lang_string('summary_cntall', 'tool_courserating'),
|
|
|
201 |
$this->get_entity_name(),
|
|
|
202 |
"{$tablealias}.cntall"
|
|
|
203 |
))
|
|
|
204 |
->add_joins($this->get_joins());
|
|
|
205 |
|
|
|
206 |
// Avgrating filter.
|
|
|
207 |
$filters[] = (new filter(
|
|
|
208 |
number::class,
|
|
|
209 |
'avgrating',
|
|
|
210 |
new lang_string('summary_avgrating', 'tool_courserating'),
|
|
|
211 |
$this->get_entity_name(),
|
|
|
212 |
"{$tablealias}.avgrating"
|
|
|
213 |
))
|
|
|
214 |
->add_joins($this->get_joins());
|
|
|
215 |
|
|
|
216 |
// Sumrating filter.
|
|
|
217 |
$filters[] = (new filter(
|
|
|
218 |
number::class,
|
|
|
219 |
'sumrating',
|
|
|
220 |
new lang_string('summary_sumrating', 'tool_courserating'),
|
|
|
221 |
$this->get_entity_name(),
|
|
|
222 |
"{$tablealias}.sumrating"
|
|
|
223 |
))
|
|
|
224 |
->add_joins($this->get_joins());
|
|
|
225 |
|
|
|
226 |
// Cntreviews filter.
|
|
|
227 |
$filters[] = (new filter(
|
|
|
228 |
number::class,
|
|
|
229 |
'cntreviews',
|
|
|
230 |
new lang_string('summary_cntreviews', 'tool_courserating'),
|
|
|
231 |
$this->get_entity_name(),
|
|
|
232 |
"{$tablealias}.cntreviews"
|
|
|
233 |
))
|
|
|
234 |
->add_joins($this->get_joins());
|
|
|
235 |
|
|
|
236 |
// Cnt01 filter.
|
|
|
237 |
$filters[] = (new filter(
|
|
|
238 |
number::class,
|
|
|
239 |
'cnt01',
|
|
|
240 |
new lang_string('summary_cnt01', 'tool_courserating'),
|
|
|
241 |
$this->get_entity_name(),
|
|
|
242 |
"CASE WHEN {$tablealias}.cntall > 0 THEN 100.0*{$tablealias}.cnt01/{$tablealias}.cntall ELSE NULL END"
|
|
|
243 |
))
|
|
|
244 |
->add_joins($this->get_joins());
|
|
|
245 |
|
|
|
246 |
// Cnt02 filter.
|
|
|
247 |
$filters[] = (new filter(
|
|
|
248 |
number::class,
|
|
|
249 |
'cnt02',
|
|
|
250 |
new lang_string('summary_cnt02', 'tool_courserating'),
|
|
|
251 |
$this->get_entity_name(),
|
|
|
252 |
"CASE WHEN {$tablealias}.cntall > 0 THEN 100.0*{$tablealias}.cnt02/{$tablealias}.cntall ELSE NULL END"
|
|
|
253 |
))
|
|
|
254 |
->add_joins($this->get_joins());
|
|
|
255 |
|
|
|
256 |
// Cnt03 filter.
|
|
|
257 |
$filters[] = (new filter(
|
|
|
258 |
number::class,
|
|
|
259 |
'cnt03',
|
|
|
260 |
new lang_string('summary_cnt03', 'tool_courserating'),
|
|
|
261 |
$this->get_entity_name(),
|
|
|
262 |
"CASE WHEN {$tablealias}.cntall > 0 THEN 100.0*{$tablealias}.cnt03/{$tablealias}.cntall ELSE NULL END"
|
|
|
263 |
))
|
|
|
264 |
->add_joins($this->get_joins());
|
|
|
265 |
|
|
|
266 |
// Cnt04 filter.
|
|
|
267 |
$filters[] = (new filter(
|
|
|
268 |
number::class,
|
|
|
269 |
'cnt04',
|
|
|
270 |
new lang_string('summary_cnt04', 'tool_courserating'),
|
|
|
271 |
$this->get_entity_name(),
|
|
|
272 |
"CASE WHEN {$tablealias}.cntall > 0 THEN 100.0*{$tablealias}.cnt04/{$tablealias}.cntall ELSE NULL END"
|
|
|
273 |
))
|
|
|
274 |
->add_joins($this->get_joins());
|
|
|
275 |
|
|
|
276 |
// Cnt05 filter.
|
|
|
277 |
$filters[] = (new filter(
|
|
|
278 |
number::class,
|
|
|
279 |
'cnt05',
|
|
|
280 |
new lang_string('summary_cnt05', 'tool_courserating'),
|
|
|
281 |
$this->get_entity_name(),
|
|
|
282 |
"CASE WHEN {$tablealias}.cntall > 0 THEN 100.0*{$tablealias}.cnt05/{$tablealias}.cntall ELSE NULL END"
|
|
|
283 |
))
|
|
|
284 |
->add_joins($this->get_joins());
|
|
|
285 |
|
|
|
286 |
// Ratingmode filter.
|
|
|
287 |
$filters[] = (new filter(
|
|
|
288 |
select::class,
|
|
|
289 |
'ratingmode',
|
|
|
290 |
new lang_string('summary_ratingmode', 'tool_courserating'),
|
|
|
291 |
$this->get_entity_name(),
|
|
|
292 |
"{$tablealias}.ratingmode"
|
|
|
293 |
))
|
|
|
294 |
->add_joins($this->get_joins())
|
|
|
295 |
->set_options_callback(static function(): array {
|
|
|
296 |
return constants::rated_courses_options();
|
|
|
297 |
});
|
|
|
298 |
|
|
|
299 |
return $filters;
|
|
|
300 |
}
|
|
|
301 |
}
|