Skip to content

Commit 6d7ed53

Browse files
committed
Fix #111 Improve the fields available in dedication report source.
1 parent 17a24ab commit 6d7ed53

File tree

2 files changed

+109
-24
lines changed

2 files changed

+109
-24
lines changed

classes/reportbuilder/datasource/user_dedication.php

Lines changed: 107 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,17 @@
2929

3030
use core_reportbuilder\datasource;
3131
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;
3235
use core_reportbuilder\local\entities\user;
36+
use core_role\reportbuilder\local\entities\role;
3337
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;
3443

3544
/**
3645
* User dedication datasource.
@@ -56,28 +65,104 @@ protected function initialise(): void {
5665
$this->add_entity($dedication);
5766

5867
// 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();
81166
}
82167

83168

lang/en/block_dedication.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
defined('MOODLE_INTERNAL') || die();
2525

2626

27-
$string['admin_filter_courseid'] = 'Course Name';
27+
$string['admin_filter_courseid'] = 'Course name';
2828
$string['admin_filter_courseid_help'] = 'Filter the report by the course name';
2929
$string['admin_filter_form'] = 'Course dedication configuration';
3030
$string['admin_filter_form_help'] = 'Time is estimated based in the concepts of Session and Session duration applied to log entries.
@@ -61,7 +61,7 @@
6161
$string['privacy:metadata:block_dedication:timestart'] = 'The start time of the data collected';
6262
$string['report_dedication'] = 'Tool Dedication Report';
6363
$string['report_timespent'] = 'Report Timespent';
64-
$string['user_dedication_datasource'] = 'Users Dedication';
64+
$string['user_dedication_datasource'] = 'User dedication';
6565
$string['dedication:addinstance'] = 'Allow to add dedication block';
6666
$string['dedication:myaddinstance'] = 'Allow to add dedication block to dashboard page';
6767
$string['dedication:viewreports'] = 'Allow to view dedication reports';

0 commit comments

Comments
 (0)