Skip to content

Commit

Permalink
MDL-76900 course: add enrolment/role entities to participants source.
Browse files Browse the repository at this point in the history
Deprecate columns/filters from current enrolment entity, to be
replaced by corresponding elements from each of the new entities.
  • Loading branch information
paulholden committed May 17, 2023
1 parent 8f7bff9 commit f500b2d
Show file tree
Hide file tree
Showing 5 changed files with 162 additions and 41 deletions.
37 changes: 28 additions & 9 deletions course/classes/reportbuilder/datasource/participants.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@
use core_course\reportbuilder\local\entities\access;
use core_course\reportbuilder\local\entities\completion;
use core_course\reportbuilder\local\entities\enrolment;
use core_enrol\reportbuilder\local\entities\enrol;
use core_group\reportbuilder\local\entities\group;
use core_reportbuilder\datasource;
use core_reportbuilder\local\entities\course;
use core_reportbuilder\local\entities\user;
use core_reportbuilder\local\helpers\database;
use core_role\reportbuilder\local\entities\role;

/**
* Course participants datasource
Expand All @@ -42,9 +44,10 @@ class participants extends datasource {
*/
protected function initialise(): void {
$courseentity = new course();
$course = $courseentity->get_table_alias('course');
$this->add_entity($courseentity);

$context = $courseentity->get_table_alias('context');
$course = $courseentity->get_table_alias('course');
$this->set_main_table('course', $course);

// Exclude site course.
Expand All @@ -57,14 +60,19 @@ protected function initialise(): void {
$this->add_entity($coursecatentity
->add_join("JOIN {course_categories} {$categories} ON {$categories}.id = {$course}.category"));

// Join the enrolment method entity.
$enrolentity = new enrol();
$enrol = $enrolentity->get_table_alias('enrol');
$this->add_entity($enrolentity
->add_join("LEFT JOIN {enrol} {$enrol} ON {$enrol}.courseid = {$course}.id"));

// Join the enrolments entity.
$enrolmententity = new enrolment();
$enrolmententity = (new enrolment())
->set_table_alias('enrol', $enrol);
$userenrolment = $enrolmententity->get_table_alias('user_enrolments');
$enrol = $enrolmententity->get_table_alias('enrol');
$enroljoin = "LEFT JOIN {enrol} {$enrol} ON {$enrol}.courseid = {$course}.id";
$userenrolmentjoin = " LEFT JOIN {user_enrolments} {$userenrolment} ON {$userenrolment}.enrolid = {$enrol}.id";
$enrolmententity->add_joins([$enroljoin, $userenrolmentjoin]);
$this->add_entity($enrolmententity);
$this->add_entity($enrolmententity
->add_joins($enrolentity->get_joins())
->add_join("LEFT JOIN {user_enrolments} {$userenrolment} ON {$userenrolment}.enrolid = {$enrol}.id"));

// Join user entity.
$userentity = new user();
Expand All @@ -73,9 +81,20 @@ protected function initialise(): void {
$userentity->add_join("LEFT JOIN {user} {$user} ON {$userenrolment}.userid = {$user}.id AND {$user}.deleted = 0");
$this->add_entity($userentity);

// Join the role entity.
$roleentity = (new role())
->set_table_alias('context', $context);
$role = $roleentity->get_table_alias('role');
$this->add_entity($roleentity
->add_joins($userentity->get_joins())
->add_join($courseentity->get_context_join())
->add_join("LEFT JOIN {role_assignments} ras ON ras.contextid = {$context}.id AND ras.userid = {$user}.id")
->add_join("LEFT JOIN {role} {$role} ON {$role}.id = ras.roleid")
);

// Join group entity.
$groupentity = (new group())
->set_table_alias('context', $courseentity->get_table_alias('context'));
->set_table_alias('context', $context);
$groups = $groupentity->get_table_alias('groups');

// Sub-select for all course group members.
Expand Down Expand Up @@ -135,7 +154,7 @@ public static function get_name(): string {
public function get_default_columns(): array {
return [
'course:coursefullnamewithlink',
'enrolment:method',
'enrol:name',
'user:fullnamewithlink',
];
}
Expand Down
9 changes: 6 additions & 3 deletions course/classes/reportbuilder/local/entities/enrolment.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ protected function get_all_columns(): array {
$userenrolments = $this->get_table_alias('user_enrolments');
$enrol = $this->get_table_alias('enrol');

// Enrolment method column.
// Enrolment method column (Deprecated since Moodle 4.3, to remove in MDL-78118).
$columns[] = (new column(
'method',
new lang_string('method', 'enrol'),
Expand All @@ -98,6 +98,7 @@ protected function get_all_columns(): array {
->set_type(column::TYPE_TEXT)
->add_fields("{$enrol}.enrol, {$enrol}.id")
->set_is_sortable(true)
->set_is_deprecated('See \'enrol:name\' for replacement')
->add_callback([enrolment_formatter::class, 'enrolment_name']);

// Enrolment time created.
Expand Down Expand Up @@ -153,7 +154,7 @@ protected function get_all_columns(): array {
->set_is_sortable(true)
->add_callback([enrolment_formatter::class, 'enrolment_status']);

// Role method column.
// Role column (Deprecated since Moodle 4.3, to remove in MDL-78118).
$ctx = database::generate_alias();
$ra = database::generate_alias();
$r = database::generate_alias();
Expand All @@ -171,6 +172,7 @@ protected function get_all_columns(): array {
->set_type(column::TYPE_TEXT)
->add_fields("{$r}.id, {$r}.name, {$r}.shortname, {$ctx}.instanceid")
->set_is_sortable(true, ["{$r}.shortname"])
->set_is_deprecated('See \'role:name\' for replacement')
->add_callback(static function(?string $value, stdClass $row): string {
if (!$row->id) {
return '';
Expand Down Expand Up @@ -213,7 +215,7 @@ protected function get_all_filters(): array {
$userenrolments = $this->get_table_alias('user_enrolments');
$enrol = $this->get_table_alias('enrol');

// Enrolment method.
// Enrolment method (Deprecated since Moodle 4.3, to remove in MDL-78118).
$enrolmentmethods = static function(): array {
return array_map(static function(enrol_plugin $plugin): string {
return get_string('pluginname', 'enrol_' . $plugin->get_name());
Expand All @@ -227,6 +229,7 @@ protected function get_all_filters(): array {
"{$enrol}.enrol"
))
->add_joins($this->get_joins())
->set_is_deprecated('See \'enrol:plugin\' for replacement')
->set_options_callback($enrolmentmethods);

// Enrolment time created.
Expand Down
2 changes: 2 additions & 0 deletions course/classes/reportbuilder/local/formatters/enrolment.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ class enrolment {
* @param string|null $value
* @param stdClass $row
* @return string
*
* @deprecated since Moodle 4.3 - please do not use this function any more (to remove in MDL-78118)
*/
public static function enrolment_name(?string $value, stdClass $row): string {
global $DB;
Expand Down
Loading

0 comments on commit f500b2d

Please sign in to comment.