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 |
namespace tool_courserating\reportbuilder\local\systemreports;
|
|
|
18 |
|
|
|
19 |
use context_system;
|
|
|
20 |
use core_reportbuilder\local\entities\user;
|
|
|
21 |
use core_reportbuilder\local\helpers\database;
|
|
|
22 |
use core_reportbuilder\system_report;
|
|
|
23 |
use stdClass;
|
|
|
24 |
use tool_courserating\helper;
|
|
|
25 |
use tool_courserating\permission;
|
|
|
26 |
use tool_courserating\reportbuilder\local\entities\rating;
|
|
|
27 |
|
|
|
28 |
/**
|
|
|
29 |
* Config changes system report class implementation
|
|
|
30 |
*
|
|
|
31 |
* @package tool_courserating
|
|
|
32 |
* @copyright 2022 Marina Glancy
|
|
|
33 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
34 |
*/
|
|
|
35 |
class course_ratings_report extends system_report {
|
|
|
36 |
|
|
|
37 |
/**
|
|
|
38 |
* Get course id
|
|
|
39 |
*
|
|
|
40 |
* @return mixed
|
|
|
41 |
*/
|
|
|
42 |
protected function get_course_id() {
|
|
|
43 |
return $this->get_context()->instanceid;
|
|
|
44 |
}
|
|
|
45 |
|
|
|
46 |
/**
|
|
|
47 |
* Initialise report, we need to set the main table, load our entities and set columns/filters
|
|
|
48 |
*/
|
|
|
49 |
protected function initialise(): void {
|
|
|
50 |
// Our main entity, it contains all of the column definitions that we need.
|
|
|
51 |
$ratingentity = new rating();
|
|
|
52 |
$ratingtablealias = $ratingentity->get_table_alias('tool_courserating_rating');
|
|
|
53 |
$paramcourseid = database::generate_param_name();
|
|
|
54 |
$this->add_base_condition_sql("{$ratingtablealias}.courseid = :{$paramcourseid}",
|
|
|
55 |
[$paramcourseid => $this->get_course_id()]);
|
|
|
56 |
|
|
|
57 |
$this->set_main_table('tool_courserating_rating', $ratingtablealias);
|
|
|
58 |
$this->add_entity($ratingentity);
|
|
|
59 |
|
|
|
60 |
// We can join the "user" entity to our "main" entity using standard SQL JOIN.
|
|
|
61 |
$entityuser = new user();
|
|
|
62 |
$entityuseralias = $entityuser->get_table_alias('user');
|
|
|
63 |
$this->add_entity($entityuser
|
|
|
64 |
->add_join("JOIN {user} {$entityuseralias} ON {$entityuseralias}.id = {$ratingtablealias}.userid")
|
|
|
65 |
);
|
|
|
66 |
|
|
|
67 |
// Now we can call our helper methods to add the content we want to include in the report.
|
|
|
68 |
$this->add_columns();
|
|
|
69 |
$this->add_filters();
|
|
|
70 |
|
|
|
71 |
// Set if report can be downloaded.
|
|
|
72 |
$this->set_downloadable(true, get_string('pluginname', 'tool_courserating'));
|
|
|
73 |
}
|
|
|
74 |
|
|
|
75 |
/**
|
|
|
76 |
* Validates access to view this report
|
|
|
77 |
*
|
|
|
78 |
* @return bool
|
|
|
79 |
*/
|
|
|
80 |
protected function can_view(): bool {
|
|
|
81 |
return permission::can_view_report($this->get_course_id());
|
|
|
82 |
}
|
|
|
83 |
|
|
|
84 |
/**
|
|
|
85 |
* Adds the columns we want to display in the report
|
|
|
86 |
*
|
|
|
87 |
* They are all provided by the entities we previously added in the {@see initialise} method, referencing each by their
|
|
|
88 |
* unique identifier
|
|
|
89 |
*/
|
|
|
90 |
protected function add_columns(): void {
|
|
|
91 |
$columns = [
|
|
|
92 |
'user:fullnamewithpicturelink',
|
|
|
93 |
'rating:timemodified',
|
|
|
94 |
'rating:rating',
|
|
|
95 |
'rating:review',
|
|
|
96 |
'rating:flags',
|
|
|
97 |
];
|
|
|
98 |
if (permission::can_delete_rating(0, $this->get_course_id())) {
|
|
|
99 |
$columns[] = 'rating:actions';
|
|
|
100 |
}
|
|
|
101 |
|
|
|
102 |
$this->add_columns_from_entities($columns);
|
|
|
103 |
|
|
|
104 |
// Default sorting.
|
|
|
105 |
$this->set_initial_sort_column('rating:timemodified', SORT_DESC);
|
|
|
106 |
|
|
|
107 |
// Custom callbacks.
|
|
|
108 |
if ($column = $this->get_column('rating:rating')) {
|
|
|
109 |
$column->set_callback([helper::class, 'format_rating_in_course_report']);
|
|
|
110 |
}
|
|
|
111 |
if ($column = $this->get_column('rating:flags')) {
|
|
|
112 |
$column->set_callback([helper::class, 'format_flags_in_course_report']);
|
|
|
113 |
}
|
|
|
114 |
|
|
|
115 |
}
|
|
|
116 |
|
|
|
117 |
/**
|
|
|
118 |
* Adds the filters we want to display in the report
|
|
|
119 |
*
|
|
|
120 |
* They are all provided by the entities we previously added in the {@see initialise} method, referencing each by their
|
|
|
121 |
* unique identifier
|
|
|
122 |
*/
|
|
|
123 |
protected function add_filters(): void {
|
|
|
124 |
$filters = [
|
|
|
125 |
'user:fullname',
|
|
|
126 |
'rating:rating',
|
|
|
127 |
'rating:hasreview',
|
|
|
128 |
'rating:review',
|
|
|
129 |
'rating:flags',
|
|
|
130 |
];
|
|
|
131 |
|
|
|
132 |
$this->add_filters_from_entities($filters);
|
|
|
133 |
}
|
|
|
134 |
}
|