Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
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\datasource;
18
 
19
use core_reportbuilder\datasource;
20
use core_reportbuilder\local\entities\course;
21
use core_reportbuilder\local\filters\select;
22
use core_reportbuilder\local\helpers\database;
23
use core_reportbuilder\local\helpers\report;
24
use core_reportbuilder\manager;
25
use tool_courserating\reportbuilder\local\entities\summary;
26
use tool_courserating\reportbuilder\local\entities\rating;
27
use core_reportbuilder\local\entities\user;
28
 
29
/**
30
 * Reportbuilder datasource courseratings.
31
 *
32
 * @package     tool_courserating
33
 * @copyright   2022 Marina Glancy
34
 * @license     https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35
 */
36
class courseratings extends datasource {
37
    /**
38
     * Return user friendly name of the datasource
39
     *
40
     * @return string
41
     */
42
    public static function get_name(): string {
43
        return get_string('datasource_courseratings', 'tool_courserating');
44
    }
45
 
46
    /**
47
     * Initialise report
48
     */
49
    protected function initialise(): void {
50
        global $CFG;
51
        $courseentity = new course();
52
        $coursetablealias = $courseentity->get_table_alias('course');
53
        $this->set_main_table('course', $coursetablealias);
54
        $this->add_entity($courseentity);
55
        $paramsiteid = database::generate_param_name();
56
        $this->add_base_condition_sql("{$coursetablealias}.id != :{$paramsiteid}", [$paramsiteid => SITEID]);
57
 
58
        // Join the coursecategory entity.
59
        if ($CFG->version > 2022110000) {
60
            // Course category entity was renamed in 4.1.
61
            $coursecategoryentity = new \core_course\reportbuilder\local\entities\course_category();
62
        } else {
63
            $coursecategoryentity = new \core_course\local\entities\course_category();
64
        }
65
        $coursecategorytablealias = $coursecategoryentity->get_table_alias('course_categories');
66
        $coursecategoryjoin = "LEFT JOIN {course_categories} {$coursecategorytablealias}
67
                               ON {$coursecategorytablealias}.id = {$coursetablealias}.category";
68
        $this->add_entity($coursecategoryentity->add_join($coursecategoryjoin));
69
 
70
        // Join the summary entity.
71
        $summaryentity = new summary();
72
        $summarytablealias = $summaryentity->get_table_alias('tool_courserating_summary');
73
        $summaryjoin = "LEFT JOIN {tool_courserating_summary} {$summarytablealias}
74
                               ON {$summarytablealias}.courseid = {$coursetablealias}.id";
75
        $this->add_entity($summaryentity->add_join($summaryjoin));
76
 
77
        // Join the rating entity.
78
        $ratingentity = new rating();
79
        $ratingtablealias = $ratingentity->get_table_alias('tool_courserating_rating');
80
        $ratingjoin = "LEFT JOIN {tool_courserating_rating} {$ratingtablealias}
81
                               ON {$ratingtablealias}.courseid = {$coursetablealias}.id";
82
        $this->add_entity($ratingentity->add_join($ratingjoin));
83
 
84
        // Join the user entity.
85
        $userentity = new user();
86
        $usertablealias = $userentity->get_table_alias('user');
87
        $userjoin = "LEFT JOIN {user} {$usertablealias}
88
                               ON {$usertablealias}.id = {$ratingtablealias}.userid";
89
        $this->add_entity($userentity->add_join($ratingjoin)->add_join($userjoin));
90
 
91
        // Add all columns from entities to be available in custom reports.
92
        $this->add_columns_from_entity($courseentity->get_entity_name());
93
        $this->add_columns_from_entity($coursecategoryentity->get_entity_name());
94
        $this->add_columns_from_entity($summaryentity->get_entity_name());
95
        $this->add_columns_from_entity($ratingentity->get_entity_name());
96
        $this->add_columns_from_entity($userentity->get_entity_name());
97
 
98
        // Add all filters from entities to be available in custom reports.
99
        $this->add_filters_from_entity($courseentity->get_entity_name());
100
        $this->add_filters_from_entity($coursecategoryentity->get_entity_name());
101
        $this->add_filters_from_entity($summaryentity->get_entity_name());
102
        $this->add_filters_from_entity($ratingentity->get_entity_name());
103
        $this->add_filters_from_entity($userentity->get_entity_name());
104
 
105
        // Add all conditions from entities to be available in custom reports.
106
        $this->add_conditions_from_entity($courseentity->get_entity_name());
107
        $this->add_conditions_from_entity($coursecategoryentity->get_entity_name());
108
        $this->add_conditions_from_entity($summaryentity->get_entity_name());
109
        $this->add_conditions_from_entity($ratingentity->get_entity_name());
110
        $this->add_conditions_from_entity($userentity->get_entity_name());
111
    }
112
 
113
    /**
114
     * Return the columns that will be added to the report as part of default setup
115
     *
116
     * @return string[]
117
     */
118
    public function get_default_columns(): array {
119
        return [
120
            'course:coursefullnamewithlink',
121
            'course_category:name',
122
            'summary:avgrating',
123
            'summary:stars',
124
        ];
125
    }
126
 
127
    /**
128
     * Return the filters that will be added to the report once is created
129
     *
130
     * @return string[]
131
     */
132
    public function get_default_filters(): array {
133
        return [
134
            'course:courseselector',
135
        ];
136
    }
137
 
138
    /**
139
     * Return the conditions that will be added to the report once is created
140
     *
141
     * @return string[]
142
     */
143
    public function get_default_conditions(): array {
144
        return [
145
            'summary:ratingmode',
146
        ];
147
    }
148
 
149
    /**
150
     * Set default columns and the sortorder
151
     */
152
    public function add_default_columns(): void {
153
        parent::add_default_columns();
154
 
155
        $persistent = $this->get_report_persistent();
156
        $report = manager::get_report_from_persistent($persistent);
157
        foreach ($report->get_active_columns() as $column) {
158
            if ($column->get_unique_identifier() === 'course:coursefullnamewithlink') {
159
                report::toggle_report_column_sorting($persistent->get('id'), $column->get_persistent()->get('id'), true);
160
            }
161
        }
162
    }
163
 
164
    /**
165
     * Add default conditions and their values
166
     */
167
    public function add_default_conditions(): void {
168
        parent::add_default_conditions();
169
        $this->set_condition_values([
170
            'summary:ratingmode_operator' => select::NOT_EQUAL_TO,
171
            'summary:ratingmode_value' => 0,
172
        ]);
173
    }
174
}