MDL-78835 group: include custom fields in related report entities.

This commit is contained in:
Paul Holden 2023-08-21 16:54:30 +01:00 committed by Sara Arjona
parent 67f225429b
commit dae1dcc8ae
No known key found for this signature in database
3 changed files with 59 additions and 11 deletions

View file

@ -26,7 +26,7 @@ use moodle_url;
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();
@ -70,13 +70,23 @@ class group extends base {
* @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)

View file

@ -24,11 +24,11 @@ use lang_string;
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 <paulh@moodle.com>
@ -63,13 +63,23 @@ class grouping extends base {
* @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)

View file

@ -18,6 +18,7 @@ declare(strict_types=1);
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};
@ -114,16 +115,37 @@ class groups_test extends core_reportbuilder_testcase {
*/
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 */
@ -142,6 +164,7 @@ class groups_test extends core_reportbuilder_testcase {
$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']);
@ -149,6 +172,7 @@ class groups_test extends core_reportbuilder_testcase {
$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']);
@ -170,11 +194,13 @@ class groups_test extends core_reportbuilder_testcase {
$grouppicture,
$grouptimecreated,
$grouptimemodified,
$groupcustomfield,
$groupingname,
$groupingidnumber,
$groupingdescription,
$groupingtimecreated,
$groupingtimemodified,
$groupingcustomfield,
$groupmembertimeadded,
$groupmemebercomponent,
$userusername,
@ -189,11 +215,13 @@ class groups_test extends core_reportbuilder_testcase {
$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);