Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
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\datasource;
20
 
1441 ariadna 21
use core_cohort\reportbuilder\local\entities\cohort;
1 efrain 22
use core_course\reportbuilder\local\entities\course_category;
23
use core_course\reportbuilder\local\entities\access;
24
use core_course\reportbuilder\local\entities\completion;
25
use core_course\reportbuilder\local\entities\enrolment;
26
use core_enrol\reportbuilder\local\entities\enrol;
27
use core_group\reportbuilder\local\entities\group;
28
use core_reportbuilder\datasource;
29
use core_reportbuilder\local\entities\course;
30
use core_reportbuilder\local\entities\user;
31
use core_reportbuilder\local\filters\select;
32
use core_reportbuilder\local\helpers\database;
33
use core_role\reportbuilder\local\entities\role;
34
use core_user\output\status_field;
35
 
36
/**
37
 * Course participants datasource
38
 *
39
 * @package     core_course
40
 * @copyright   2022 David Matamoros <davidmc@moodle.com>
41
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
42
 */
43
class participants extends datasource {
44
 
45
    /**
46
     * Initialise report
47
     */
48
    protected function initialise(): void {
49
        $courseentity = new course();
50
        $this->add_entity($courseentity);
51
 
52
        $context = $courseentity->get_table_alias('context');
53
        $course = $courseentity->get_table_alias('course');
54
        $this->set_main_table('course', $course);
55
 
56
        // Exclude site course.
57
        $paramsiteid = database::generate_param_name();
58
        $this->add_base_condition_sql("{$course}.id != :{$paramsiteid}", [$paramsiteid => SITEID]);
59
 
60
        // Join the course category entity.
61
        $coursecatentity = new course_category();
62
        $categories = $coursecatentity->get_table_alias('course_categories');
63
        $this->add_entity($coursecatentity
64
            ->add_join("JOIN {course_categories} {$categories} ON {$categories}.id = {$course}.category"));
65
 
66
        // Join the enrolment method entity.
67
        $enrolentity = new enrol();
68
        $enrol = $enrolentity->get_table_alias('enrol');
69
        $this->add_entity($enrolentity
70
            ->add_join("LEFT JOIN {enrol} {$enrol} ON {$enrol}.courseid = {$course}.id"));
71
 
72
        // Join the enrolments entity.
73
        $enrolmententity = (new enrolment())
74
            ->set_table_alias('enrol', $enrol);
75
        $userenrolment = $enrolmententity->get_table_alias('user_enrolments');
76
        $this->add_entity($enrolmententity
77
            ->add_joins($enrolentity->get_joins())
78
            ->add_join("LEFT JOIN {user_enrolments} {$userenrolment} ON {$userenrolment}.enrolid = {$enrol}.id"));
79
 
80
        // Join user entity.
81
        $userentity = new user();
82
        $user = $userentity->get_table_alias('user');
83
        $this->add_entity($userentity
84
            ->add_joins($enrolmententity->get_joins())
85
            ->add_join("LEFT JOIN {user} {$user} ON {$user}.id = {$userenrolment}.userid AND {$user}.deleted = 0"));
86
 
87
        // Join the role entity.
88
        $roleentity = (new role())
89
            ->set_table_alias('context', $context);
90
        $role = $roleentity->get_table_alias('role');
91
        $this->add_entity($roleentity
92
            ->add_joins($userentity->get_joins())
93
            ->add_join($courseentity->get_context_join())
94
            ->add_join("LEFT JOIN {role_assignments} ras ON ras.contextid = {$context}.id AND ras.userid = {$user}.id")
95
            ->add_join("LEFT JOIN {role} {$role} ON {$role}.id = ras.roleid")
96
        );
97
 
98
        // Join group entity.
99
        $groupentity = (new group())
100
            ->set_table_alias('context', $context);
101
        $groups = $groupentity->get_table_alias('groups');
102
 
103
        // Sub-select for all course group members.
104
        $groupsinnerselect = "
105
            SELECT grs.*, grms.userid
106
              FROM {groups} grs
107
              JOIN {groups_members} grms ON grms.groupid = grs.id";
108
 
109
        $this->add_entity($groupentity
110
            ->add_join($courseentity->get_context_join())
111
            ->add_joins($userentity->get_joins())
112
            ->add_join("
113
                LEFT JOIN ({$groupsinnerselect}) {$groups}
114
                       ON {$groups}.courseid = {$course}.id AND {$groups}.userid = {$user}.id")
115
        );
116
 
1441 ariadna 117
        // Join cohort entity.
118
        $cohortentity = new cohort();
119
        $cohortalias = $cohortentity->get_table_alias('cohort');
120
        $cohortmemberalias = database::generate_alias();
121
        $this->add_entity($cohortentity
122
            ->add_joins($userentity->get_joins())
123
            ->add_joins([
124
                "LEFT JOIN {cohort_members} {$cohortmemberalias} ON {$cohortmemberalias}.userid = {$user}.id",
125
                "LEFT JOIN {cohort} {$cohortalias} ON {$cohortalias}.id = {$cohortmemberalias}.cohortid",
126
            ])
127
        );
128
 
1 efrain 129
        // Join completion entity.
130
        $completionentity = (new completion())
131
            ->set_table_aliases([
132
                'course' => $course,
133
                'user' => $user,
134
            ]);
1441 ariadna 135
        $completion = $completionentity->get_table_alias('course_completions');
1 efrain 136
        $this->add_entity($completionentity
137
            ->add_joins($userentity->get_joins())
138
            ->add_join("
139
                LEFT JOIN {course_completions} {$completion}
140
                       ON {$completion}.course = {$course}.id AND {$completion}.userid = {$user}.id")
141
        );
142
 
143
        // Join course access entity.
144
        $accessentity = (new access())
145
            ->set_table_alias('user', $user);
146
        $lastaccess = $accessentity->get_table_alias('user_lastaccess');
147
        $this->add_entity($accessentity
148
            ->add_joins($userentity->get_joins())
149
            ->add_join("
150
                LEFT JOIN {user_lastaccess} {$lastaccess}
151
                       ON {$lastaccess}.userid = {$user}.id AND {$lastaccess}.courseid = {$course}.id"));
152
 
153
        // Add all entities columns/filters/conditions.
1441 ariadna 154
        $this->add_all_from_entities([
155
            $courseentity->get_entity_name(),
156
            $coursecatentity->get_entity_name(),
157
            $enrolmententity->get_entity_name(),
158
            $enrolentity->get_entity_name(),
159
            $userentity->get_entity_name(),
160
            $roleentity->get_entity_name(),
161
            $groupentity->get_entity_name(),
162
        ]);
163
 
164
        $this->add_all_from_entity($cohortentity->get_entity_name(), ['name', 'idnumber', 'description', 'customfield*'],
165
            ['cohortselect', 'name', 'idnumber', 'customfield*'], ['cohortselect', 'name', 'idnumber', 'customfield*']);
166
 
167
        $this->add_all_from_entities([
168
            $completionentity->get_entity_name(),
169
            $accessentity->get_entity_name(),
170
        ]);
1 efrain 171
    }
172
 
173
    /**
174
     * Return user friendly name of the datasource
175
     *
176
     * @return string
177
     */
178
    public static function get_name(): string {
179
        return get_string('courseparticipants', 'course');
180
    }
181
 
182
    /**
183
     * Return the columns that will be added to the report as part of default setup
184
     *
185
     * @return string[]
186
     */
187
    public function get_default_columns(): array {
188
        return [
189
            'course:coursefullnamewithlink',
190
            'user:fullnamewithlink',
191
            'enrol:name',
192
        ];
193
    }
194
 
195
    /**
196
     * Return the column sorting that will be added to the report upon creation
197
     *
198
     * @return int[]
199
     */
200
    public function get_default_column_sorting(): array {
201
        return [
202
            'course:coursefullnamewithlink' => SORT_ASC,
203
            'user:fullnamewithlink' => SORT_ASC,
204
            'enrol:name' => SORT_ASC,
205
        ];
206
    }
207
 
208
    /**
209
     * Return the filters that will be added to the report once is created
210
     *
211
     * @return string[]
212
     */
213
    public function get_default_filters(): array {
214
        return [
215
            'user:suspended',
216
            'user:confirmed',
217
        ];
218
    }
219
 
220
    /**
221
     * Return the conditions that will be added to the report once is created
222
     *
223
     * @return string[]
224
     */
225
    public function get_default_conditions(): array {
226
        return [
227
            'enrolment:status',
228
            'user:suspended',
229
            'user:confirmed',
230
        ];
231
    }
232
 
233
    /**
234
     * Return the condition values that will be set for the report upon creation
235
     *
236
     * @return array
237
     */
238
    public function get_default_condition_values(): array {
239
        return [
240
            'enrolment:status_operator' => select::EQUAL_TO,
241
            'enrolment:status_value' => status_field::STATUS_ACTIVE,
242
        ];
243
    }
244
}