Skip to content

Commit

Permalink
MDL-76889 competency: add cohort data to competencies report source.
Browse files Browse the repository at this point in the history
  • Loading branch information
paulholden committed Aug 15, 2024
1 parent fd18c50 commit 8298abf
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 2 deletions.
29 changes: 28 additions & 1 deletion competency/classes/reportbuilder/datasource/competencies.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@
namespace core_competency\reportbuilder\datasource;

use core\reportbuilder\local\entities\context;
use core_cohort\reportbuilder\local\entities\cohort;
use core_competency\reportbuilder\local\entities\{competency, framework, usercompetency};
use core_reportbuilder\datasource;
use core_reportbuilder\local\entities\user;
use core_reportbuilder\local\filters\boolean_select;
use core_reportbuilder\local\helpers\database;

/**
* Competencies datasource
Expand Down Expand Up @@ -88,8 +90,33 @@ protected function initialise(): void {
->add_join("LEFT JOIN {user} {$useralias} ON {$useralias}.id = {$usercompetencyalias}.userid")
);

// Join cohort entity.
$cohortentity = new cohort();
$cohortalias = $cohortentity->get_table_alias('cohort');
$cohortmemberalias = database::generate_alias();
$this->add_entity($cohortentity
->add_joins($userentity->get_joins())
->add_joins([
"LEFT JOIN {cohort_members} {$cohortmemberalias} ON {$cohortmemberalias}.userid = {$useralias}.id",
"LEFT JOIN {cohort} {$cohortalias} ON {$cohortalias}.id = {$cohortmemberalias}.cohortid",
])
);

// Add report elements from each of the entities we added to the report.
$this->add_all_from_entities();
$this->add_all_from_entities([
$contextentity->get_entity_name(),
$frameworkentity->get_entity_name(),
$competencyentity->get_entity_name(),
$usercompetencyentity->get_entity_name(),
$userentity->get_entity_name(),
]);

$this->add_all_from_entity(
$cohortentity->get_entity_name(),
['name', 'idnumber', 'description', 'customfield*'],
['cohortselect', 'name', 'idnumber', 'customfield*'],
['cohortselect', 'name', 'idnumber', 'customfield*'],
);
}

/**
Expand Down
23 changes: 22 additions & 1 deletion competency/tests/reportbuilder/datasource/competencies_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ public function test_datasource_non_default_columns(): void {
$this->resetAfterTest();

$user = $this->getDataGenerator()->create_user();
$cohort = $this->getDataGenerator()->create_cohort(['name' => 'My cohort']);
cohort_add_member($cohort->id, $user->id);

$scale = $this->getDataGenerator()->create_scale(['name' => 'My scale', 'scale' => 'A,B,C,D']);

/** @var core_competency_generator $generator */
Expand Down Expand Up @@ -125,6 +128,9 @@ public function test_datasource_non_default_columns(): void {
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'usercompetency:status']);
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'usercompetency:rating']);

// Cohort.
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'cohort:name']);

$content = $this->get_custom_report_content($report->get('id'));
$this->assertCount(1, $content);

Expand All @@ -140,6 +146,7 @@ public function test_datasource_non_default_columns(): void {
$competencytimemodified,
$usercompetencystatus,
$usercompetencyrating,
$cohortname,
] = array_values($content[0]);

$this->assertEquals('So cool', $frameworkdescription);
Expand All @@ -153,6 +160,7 @@ public function test_datasource_non_default_columns(): void {
$this->assertNotEmpty($competencytimemodified);
$this->assertEquals('Idle', $usercompetencystatus);
$this->assertEquals('C', $usercompetencyrating);
$this->assertEquals('My cohort', $cohortname);
}

/**
Expand Down Expand Up @@ -269,6 +277,16 @@ public static function datasource_filters_provider(): array {
'user:username_operator' => text::IS_NOT_EQUAL_TO,
'user:username_value' => 'testuser',
], false],

// Cohort.
'Cohort name' => ['cohort:name', [
'cohort:name_operator' => text::IS_EQUAL_TO,
'cohort:name_value' => 'My cohort',
], true],
'Cohort name (no match)' => ['cohort:name', [
'cohort:name_operator' => text::IS_EQUAL_TO,
'cohort:name_value' => 'Another cohort',
], false],
];
}

Expand All @@ -289,7 +307,10 @@ public function test_datasource_filters(
$this->resetAfterTest();

$user = $this->getDataGenerator()->create_user(['username' => 'testuser']);
$scale = $this->getDataGenerator()->create_scale(['name' => 'My scale', 'scale' => 'A,B,C,D']);
$cohort = $this->getDataGenerator()->create_cohort(['name' => 'My cohort']);
cohort_add_member($cohort->id, $user->id);

$scale = $this->getDataGenerator()->create_scale([]);

/** @var core_competency_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_competency');
Expand Down

0 comments on commit 8298abf

Please sign in to comment.