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