29
29
30
30
use core_reportbuilder \datasource ;
31
31
use core_reportbuilder \local \entities \course ;
32
+ use core_course \reportbuilder \local \entities \course_category ;
33
+ use core_course \reportbuilder \local \entities \enrolment ;
34
+ use core_enrol \reportbuilder \local \entities \enrol ;
32
35
use core_reportbuilder \local \entities \user ;
36
+ use core_role \reportbuilder \local \entities \role ;
33
37
use block_dedication \local \entities \dedication ;
38
+ use core_group \reportbuilder \local \entities \group ;
39
+ use core_cohort \reportbuilder \local \entities \cohort ;
40
+ use core_course \reportbuilder \local \entities \access ;
41
+ use core_course \reportbuilder \local \entities \completion ;
42
+ use core_reportbuilder \local \helpers \database ;
34
43
35
44
/**
36
45
* User dedication datasource.
@@ -56,28 +65,104 @@ protected function initialise(): void {
56
65
$ this ->add_entity ($ dedication );
57
66
58
67
// Add core user join.
59
- $ usercore = new user ();
60
- $ usercorealias = $ usercore ->get_table_alias ('user ' );
61
- $ usercorejoin = "JOIN {user} {$ usercorealias } ON {$ usercorealias }.id = {$ dedicationalias }.userid " ;
62
- $ this ->add_entity ($ usercore ->add_join ($ usercorejoin ));
63
-
64
- $ coursecore = new course ();
65
- $ coursecorealias = $ coursecore ->get_table_alias ('course ' );
66
- $ coursecorejoin = "JOIN {course} {$ coursecorealias } ON {$ coursecorealias }.id = {$ dedicationalias }.courseid " ;
67
- $ this ->add_entity ($ coursecore ->add_join ($ coursecorejoin ));
68
-
69
- $ this ->add_columns_from_entity ($ usercore ->get_entity_name ());
70
- $ this ->add_columns_from_entity ($ coursecore ->get_entity_name ());
71
- $ this ->add_columns_from_entity ($ dedication ->get_entity_name ());
72
-
73
- $ this ->add_filters_from_entity ($ usercore ->get_entity_name ());
74
- $ this ->add_filters_from_entity ($ coursecore ->get_entity_name ());
75
- $ this ->add_filters_from_entity ($ dedication ->get_entity_name ());
76
-
77
- $ this ->add_conditions_from_entity ($ usercore ->get_entity_name ());
78
- $ this ->add_conditions_from_entity ($ coursecore ->get_entity_name ());
79
- $ this ->add_conditions_from_entity ($ dedication ->get_entity_name ());
80
-
68
+ $ userentity = new user ();
69
+ $ user = $ userentity ->get_table_alias ('user ' );
70
+ $ usercorejoin = "JOIN {user} {$ user } ON {$ user }.id = {$ dedicationalias }.userid " ;
71
+ $ this ->add_entity ($ userentity ->add_join ($ usercorejoin ));
72
+
73
+ $ courseentity = new course ();
74
+ $ course = $ courseentity ->get_table_alias ('course ' );
75
+ $ context = $ courseentity ->get_table_alias ('context ' );
76
+ $ coursecorejoin = "JOIN {course} {$ course } ON {$ course }.id = {$ dedicationalias }.courseid " ;
77
+ $ this ->add_entity ($ courseentity ->add_join ($ coursecorejoin ));
78
+
79
+ // Join the course category entity.
80
+ $ coursecatentity = new course_category ();
81
+ $ categories = $ coursecatentity ->get_table_alias ('course_categories ' );
82
+ $ this ->add_entity ($ coursecatentity
83
+ ->add_join ("JOIN {course_categories} {$ categories } ON {$ categories }.id = {$ course }.category " ));
84
+
85
+ // Join the enrolment method entity.
86
+ $ enrolentity = new enrol ();
87
+ $ enrol = $ enrolentity ->get_table_alias ('enrol ' );
88
+ $ this ->add_entity ($ enrolentity
89
+ ->add_join ("LEFT JOIN {enrol} {$ enrol } ON {$ enrol }.courseid = {$ course }.id " ));
90
+
91
+ // Join the enrolments entity.
92
+ $ enrolmententity = (new enrolment ())
93
+ ->set_table_alias ('enrol ' , $ enrol );
94
+ $ userenrolment = $ enrolmententity ->get_table_alias ('user_enrolments ' );
95
+ $ this ->add_entity ($ enrolmententity
96
+ ->add_joins ($ enrolentity ->get_joins ())
97
+ ->add_join ("LEFT JOIN {user_enrolments} {$ userenrolment } ON {$ userenrolment }.enrolid = {$ enrol }.id AND {$ userenrolment }.userid = {$ user }.id " ));
98
+
99
+ // Join the role entity.
100
+ $ roleentity = (new role ())
101
+ ->set_table_alias ('context ' , $ context );
102
+ $ role = $ roleentity ->get_table_alias ('role ' );
103
+ $ this ->add_entity ($ roleentity
104
+ ->add_joins ($ userentity ->get_joins ())
105
+ ->add_join ($ courseentity ->get_context_join ())
106
+ ->add_join ("LEFT JOIN {role_assignments} ras ON ras.contextid = {$ context }.id AND ras.userid = {$ user }.id " )
107
+ ->add_join ("LEFT JOIN {role} {$ role } ON {$ role }.id = ras.roleid " )
108
+ );
109
+
110
+ // Join group entity.
111
+ $ groupentity = (new group ())
112
+ ->set_table_alias ('context ' , $ context );
113
+ $ groups = $ groupentity ->get_table_alias ('groups ' );
114
+
115
+ // Sub-select for all course group members.
116
+ $ groupsinnerselect = "
117
+ SELECT grs.*, grms.userid
118
+ FROM {groups} grs
119
+ JOIN {groups_members} grms ON grms.groupid = grs.id " ;
120
+
121
+ $ this ->add_entity ($ groupentity
122
+ ->add_join ($ courseentity ->get_context_join ())
123
+ ->add_joins ($ userentity ->get_joins ())
124
+ ->add_join ("
125
+ LEFT JOIN ( {$ groupsinnerselect }) {$ groups }
126
+ ON {$ groups }.courseid = {$ course }.id AND {$ groups }.userid = {$ user }.id " )
127
+ );
128
+
129
+ // Join cohort entity.
130
+ $ cohortentity = new cohort ();
131
+ $ cohortalias = $ cohortentity ->get_table_alias ('cohort ' );
132
+ $ cohortmemberalias = database::generate_alias ();
133
+ $ this ->add_entity ($ cohortentity
134
+ ->add_joins ($ userentity ->get_joins ())
135
+ ->add_joins ([
136
+ "LEFT JOIN {cohort_members} {$ cohortmemberalias } ON {$ cohortmemberalias }.userid = {$ user }.id " ,
137
+ "LEFT JOIN {cohort} {$ cohortalias } ON {$ cohortalias }.id = {$ cohortmemberalias }.cohortid " ,
138
+ ])
139
+ );
140
+
141
+ // Join completion entity.
142
+ $ completionentity = (new completion ())
143
+ ->set_table_aliases ([
144
+ 'course ' => $ course ,
145
+ 'user ' => $ user ,
146
+ ]);
147
+ $ completion = $ completionentity ->get_table_alias ('course_completion ' );
148
+ $ this ->add_entity ($ completionentity
149
+ ->add_joins ($ userentity ->get_joins ())
150
+ ->add_join ("
151
+ LEFT JOIN {course_completions} {$ completion }
152
+ ON {$ completion }.course = {$ course }.id AND {$ completion }.userid = {$ user }.id " )
153
+ );
154
+
155
+ // Join course access entity.
156
+ $ accessentity = (new access ())
157
+ ->set_table_alias ('user ' , $ user );
158
+ $ lastaccess = $ accessentity ->get_table_alias ('user_lastaccess ' );
159
+ $ this ->add_entity ($ accessentity
160
+ ->add_joins ($ userentity ->get_joins ())
161
+ ->add_join ("
162
+ LEFT JOIN {user_lastaccess} {$ lastaccess }
163
+ ON {$ lastaccess }.userid = {$ user }.id AND {$ lastaccess }.courseid = {$ course }.id " ));
164
+
165
+ $ this ->add_all_from_entities ();
81
166
}
82
167
83
168
0 commit comments