Skip to content

Commit

Permalink
MDL-78835 group: include custom fields in related report entities.
Browse files Browse the repository at this point in the history
  • Loading branch information
paulholden authored and sarjona committed Aug 28, 2023
1 parent 67f2254 commit dae1dcc
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 11 deletions.
16 changes: 13 additions & 3 deletions group/classes/reportbuilder/local/entities/group.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
use stdClass;
use core_reportbuilder\local\entities\base;
use core_reportbuilder\local\filters\{boolean_select, date, select, text};
use core_reportbuilder\local\helpers\format;
use core_reportbuilder\local\helpers\{custom_fields, format};
use core_reportbuilder\local\report\{column, filter};

defined('MOODLE_INTERNAL') || die();
Expand Down Expand Up @@ -70,13 +70,23 @@ protected function get_default_entity_title(): lang_string {
* @return base
*/
public function initialise(): base {
$columns = $this->get_all_columns();
$groupsalias = $this->get_table_alias('groups');

$customfields = (new custom_fields(
"{$groupsalias}.id",
$this->get_entity_name(),
'core_group',
'group',
))
->add_joins($this->get_joins());

$columns = array_merge($this->get_all_columns(), $customfields->get_columns());
foreach ($columns as $column) {
$this->add_column($column);
}

// All the filters defined by the entity can also be used as conditions.
$filters = $this->get_all_filters();
$filters = array_merge($this->get_all_filters(), $customfields->get_filters());
foreach ($filters as $filter) {
$this
->add_filter($filter)
Expand Down
18 changes: 14 additions & 4 deletions group/classes/reportbuilder/local/entities/grouping.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@
use stdClass;
use core_reportbuilder\local\entities\base;
use core_reportbuilder\local\filters\{date, text};
use core_reportbuilder\local\helpers\format;
use core_reportbuilder\local\helpers\{custom_fields, format};
use core_reportbuilder\local\report\{column, filter};

/**
* Group member entity
* Grouping entity
*
* @package core_group
* @copyright 2022 Paul Holden <[email protected]>
Expand Down Expand Up @@ -63,13 +63,23 @@ protected function get_default_entity_title(): lang_string {
* @return base
*/
public function initialise(): base {
$columns = $this->get_all_columns();
$groupingsalias = $this->get_table_alias('groupings');

$customfields = (new custom_fields(
"{$groupingsalias}.id",
$this->get_entity_name(),
'core_group',
'grouping',
))
->add_joins($this->get_joins());

$columns = array_merge($this->get_all_columns(), $customfields->get_columns());
foreach ($columns as $column) {
$this->add_column($column);
}

// All the filters defined by the entity can also be used as conditions.
$filters = $this->get_all_filters();
$filters = array_merge($this->get_all_filters(), $customfields->get_filters());
foreach ($filters as $filter) {
$this
->add_filter($filter)
Expand Down
36 changes: 32 additions & 4 deletions group/tests/reportbuilder/datasource/groups_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

namespace core_group\reportbuilder\datasource;

use core_customfield_generator;
use core_reportbuilder_generator;
use core_reportbuilder_testcase;
use core_reportbuilder\local\filters\{boolean_select, date, select, text};
Expand Down Expand Up @@ -114,16 +115,37 @@ public function test_datasource_groupings(): void {
*/
public function test_datasource_non_default_columns(): void {
$this->resetAfterTest();
$this->setAdminUser();

$course = $this->getDataGenerator()->create_course();
$user = $this->getDataGenerator()->create_and_enrol($course, 'student');

$group = $this->getDataGenerator()->create_group(['courseid' => $course->id, 'idnumber' => 'G101', 'enrolmentkey' => 'S',
'description' => 'My group']);
/** @var core_customfield_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_customfield');

// Create group with custom field, and single group member.
$groupfieldcategory = $generator->create_category(['component' => 'core_group', 'area' => 'group']);
$generator->create_field(['categoryid' => $groupfieldcategory->get('id'), 'shortname' => 'hi']);

$group = $this->getDataGenerator()->create_group([
'courseid' => $course->id,
'idnumber' => 'G101',
'enrolmentkey' => 'S',
'description' => 'My group',
'customfield_hi' => 'Hello',
]);
$this->getDataGenerator()->create_group_member(['userid' => $user->id, 'groupid' => $group->id]);

$grouping = $this->getDataGenerator()->create_grouping(['courseid' => $course->id, 'idnumber' => 'GR101',
'description' => 'My grouping']);
// Create grouping with custom field, and single group.
$groupingfieldcategory = $generator->create_category(['component' => 'core_group', 'area' => 'grouping']);
$generator->create_field(['categoryid' => $groupingfieldcategory->get('id'), 'shortname' => 'bye']);

$grouping = $this->getDataGenerator()->create_grouping([
'courseid' => $course->id,
'idnumber' => 'GR101',
'description' => 'My grouping',
'customfield_bye' => 'Goodbye',
]);
$this->getDataGenerator()->create_grouping_group(['groupingid' => $grouping->id, 'groupid' => $group->id]);

/** @var core_reportbuilder_generator $generator */
Expand All @@ -142,13 +164,15 @@ public function test_datasource_non_default_columns(): void {
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'group:picture']);
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'group:timecreated']);
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'group:timemodified']);
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'group:customfield_hi']);

// Grouping.
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'grouping:name']);
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'grouping:idnumber']);
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'grouping:description']);
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'grouping:timecreated']);
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'grouping:timemodified']);
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'grouping:customfield_bye']);

// Group member.
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'group_member:timeadded']);
Expand All @@ -170,11 +194,13 @@ public function test_datasource_non_default_columns(): void {
$grouppicture,
$grouptimecreated,
$grouptimemodified,
$groupcustomfield,
$groupingname,
$groupingidnumber,
$groupingdescription,
$groupingtimecreated,
$groupingtimemodified,
$groupingcustomfield,
$groupmembertimeadded,
$groupmemebercomponent,
$userusername,
Expand All @@ -189,11 +215,13 @@ public function test_datasource_non_default_columns(): void {
$this->assertEmpty($grouppicture);
$this->assertNotEmpty($grouptimecreated);
$this->assertNotEmpty($grouptimemodified);
$this->assertEquals('Hello', $groupcustomfield);
$this->assertEquals($grouping->name, $groupingname);
$this->assertEquals('GR101', $groupingidnumber);
$this->assertEquals(format_text($grouping->description), $groupingdescription);
$this->assertNotEmpty($groupingtimecreated);
$this->assertNotEmpty($groupingtimemodified);
$this->assertEquals('Goodbye', $groupingcustomfield);
$this->assertNotEmpty($groupmembertimeadded);
$this->assertEmpty($groupmemebercomponent);
$this->assertEquals($user->username, $userusername);
Expand Down

0 comments on commit dae1dcc

Please sign in to comment.