mirror of
https://github.com/moodle/moodle.git
synced 2025-01-17 05:28:30 +01:00
MDL-79552 cohort: add cohort data to user-focused custom reports.
This commit is contained in:
parent
bdbc9e0128
commit
4fb106506d
@ -23,6 +23,7 @@ use core_reportbuilder\datasource;
|
||||
use core_reportbuilder\local\entities\{course, user};
|
||||
use core_reportbuilder\local\helpers\database;
|
||||
use core_badges\reportbuilder\local\entities\{badge, badge_issued};
|
||||
use core_cohort\reportbuilder\local\entities\cohort;
|
||||
use core_tag\reportbuilder\local\entities\tag;
|
||||
|
||||
/**
|
||||
@ -88,17 +89,23 @@ class users extends datasource {
|
||||
->add_join("LEFT JOIN {course} {$coursealias} ON {$coursealias}.id =
|
||||
CASE WHEN {$badgealias}.id IS NULL THEN 0 ELSE COALESCE({$badgealias}.courseid, 1) END"));
|
||||
|
||||
// Join the cohort entity.
|
||||
$cohortentity = new cohort();
|
||||
$cohortalias = $cohortentity->get_table_alias('cohort');
|
||||
$cohortmemberalias = database::generate_alias();
|
||||
$this->add_entity($cohortentity->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_entity($userentity->get_entity_name());
|
||||
$this->add_all_from_entity($badgeissuedentity->get_entity_name());
|
||||
$this->add_all_from_entity($badgeentity->get_entity_name());
|
||||
|
||||
// Add specific tag entity elements.
|
||||
$this->add_columns_from_entity($tagentity->get_entity_name(), ['name', 'namewithlink']);
|
||||
$this->add_filter($tagentity->get_filter('name'));
|
||||
$this->add_condition($tagentity->get_condition('name'));
|
||||
|
||||
$this->add_all_from_entity($tagentity->get_entity_name(), ['name', 'namewithlink'], ['name'], ['name']);
|
||||
$this->add_all_from_entity($courseentity->get_entity_name());
|
||||
$this->add_all_from_entity($cohortentity->get_entity_name(), ['name', 'idnumber', 'description', 'customfield*'],
|
||||
['name', 'idnumber', 'customfield*'], ['name', 'idnumber', 'customfield*']);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -92,6 +92,9 @@ class users_test extends core_reportbuilder_testcase {
|
||||
$course = $this->getDataGenerator()->create_course();
|
||||
$user = $this->getDataGenerator()->create_and_enrol($course, 'student', ['firstname' => 'Zoe', 'lastname' => 'Zebra']);
|
||||
|
||||
$cohort = $this->getDataGenerator()->create_cohort(['name' => 'My cohort']);
|
||||
cohort_add_member($cohort->id, $user->id);
|
||||
|
||||
/** @var core_badges_generator $generator */
|
||||
$generator = $this->getDataGenerator()->get_plugin_generator('core_badges');
|
||||
|
||||
@ -137,14 +140,17 @@ class users_test extends core_reportbuilder_testcase {
|
||||
|
||||
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'course:fullname']);
|
||||
|
||||
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'cohort:name']);
|
||||
|
||||
$content = $this->get_custom_report_content($report->get('id'));
|
||||
$this->assertCount(3, $content);
|
||||
|
||||
// Admin user, no badge issued.
|
||||
[$fullname, $badgename, $criteria, $image, $language, $version, $status, $expiry, $tag, $expires, $visible, $coursename]
|
||||
= array_values($content[0]);
|
||||
[$fullname, $badgename, $namewithlink, $criteria, $image, $language, $version, $status, $expiry, $tag, $expires,
|
||||
$visible, $coursename, $cohortname] = array_values($content[0]);
|
||||
$this->assertEquals('Admin User', $fullname);
|
||||
$this->assertEmpty($badgename);
|
||||
$this->assertEmpty($namewithlink);
|
||||
$this->assertEmpty($criteria);
|
||||
$this->assertEmpty($image);
|
||||
$this->assertEmpty($language);
|
||||
@ -155,13 +161,14 @@ class users_test extends core_reportbuilder_testcase {
|
||||
$this->assertEmpty($expires);
|
||||
$this->assertEmpty($visible);
|
||||
$this->assertEmpty($coursename);
|
||||
$this->assertEmpty($cohortname);
|
||||
|
||||
$expectedbadgesitelink = \html_writer::link(new \moodle_url('/badges/overview.php',
|
||||
['id' => $badgesite->id]), ($badgesite->name));
|
||||
|
||||
// User issued site badge.
|
||||
[$fullname, $badgename, $namewithlink, $criteria, $image, $language, $version, $status, $expiry, $tag, $expires,
|
||||
$visible, $coursename] = array_values($content[1]);
|
||||
$visible, $coursename, $cohortname] = array_values($content[1]);
|
||||
$this->assertEquals(fullname($user), $fullname);
|
||||
$this->assertEquals($badgesite->name, $badgename);
|
||||
$this->assertEquals($expectedbadgesitelink, $namewithlink);
|
||||
@ -175,13 +182,14 @@ class users_test extends core_reportbuilder_testcase {
|
||||
$this->assertNotEmpty($expires);
|
||||
$this->assertEquals('Yes', $visible);
|
||||
$this->assertEquals('PHPUnit test site', $coursename);
|
||||
$this->assertEquals($cohort->name, $cohortname);
|
||||
|
||||
$expectedbadgecourselink = \html_writer::link(new \moodle_url('/badges/overview.php',
|
||||
['id' => $badgecourse->id]), ($badgecourse->name));
|
||||
|
||||
// User issued course badge.
|
||||
[$fullname, $badgename, $namewithlink, $criteria, $image, $language, $version, $status, $expiry, $tag, $expires,
|
||||
$visible, $coursename] = array_values($content[2]);
|
||||
$visible, $coursename, $cohortname] = array_values($content[2]);
|
||||
$this->assertEquals(fullname($user), $fullname);
|
||||
$this->assertEquals($badgecourse->name, $badgename);
|
||||
$this->assertEquals($expectedbadgecourselink, $namewithlink);
|
||||
@ -195,6 +203,7 @@ class users_test extends core_reportbuilder_testcase {
|
||||
$this->assertEmpty($expires);
|
||||
$this->assertEquals('Yes', $visible);
|
||||
$this->assertEquals($course->fullname, $coursename);
|
||||
$this->assertEquals($cohort->name, $cohortname);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -282,6 +291,16 @@ class users_test extends core_reportbuilder_testcase {
|
||||
'course:fullname_operator' => text::IS_EQUAL_TO,
|
||||
'course:fullname_value' => 'Course 2',
|
||||
], false],
|
||||
|
||||
// Cohort.
|
||||
'Filter cohort name' => ['cohort:name', [
|
||||
'cohort:name_operator' => text::IS_EQUAL_TO,
|
||||
'cohort:name_value' => 'My cohort',
|
||||
], true],
|
||||
'Filter cohort name (no match)' => ['cohort:name', [
|
||||
'cohort:name_operator' => text::IS_EQUAL_TO,
|
||||
'cohort:name_value' => 'Not my cohort',
|
||||
], false],
|
||||
];
|
||||
}
|
||||
|
||||
@ -300,6 +319,9 @@ class users_test extends core_reportbuilder_testcase {
|
||||
$course = $this->getDataGenerator()->create_course(['fullname' => 'Course 1']);
|
||||
$user = $this->getDataGenerator()->create_and_enrol($course, 'student', ['firstname' => 'Zoe', 'lastname' => 'Zebra']);
|
||||
|
||||
$cohort = $this->getDataGenerator()->create_cohort(['name' => 'My cohort']);
|
||||
cohort_add_member($cohort->id, $user->id);
|
||||
|
||||
/** @var core_badges_generator $generator */
|
||||
$generator = $this->getDataGenerator()->get_plugin_generator('core_badges');
|
||||
$badge = $generator->create_badge([
|
||||
|
@ -19,6 +19,7 @@ declare(strict_types=1);
|
||||
namespace core_user\reportbuilder\datasource;
|
||||
|
||||
use lang_string;
|
||||
use core_cohort\reportbuilder\local\entities\cohort;
|
||||
use core_reportbuilder\datasource;
|
||||
use core_reportbuilder\local\entities\user;
|
||||
use core_reportbuilder\local\filters\boolean_select;
|
||||
@ -50,17 +51,16 @@ class users extends datasource {
|
||||
global $CFG;
|
||||
|
||||
$userentity = new user();
|
||||
$usertablealias = $userentity->get_table_alias('user');
|
||||
$useralias = $userentity->get_table_alias('user');
|
||||
|
||||
$this->set_main_table('user', $usertablealias);
|
||||
$this->set_main_table('user', $useralias);
|
||||
$this->add_entity($userentity);
|
||||
|
||||
$userparamguest = database::generate_param_name();
|
||||
$this->add_base_condition_sql("{$usertablealias}.id != :{$userparamguest} AND {$usertablealias}.deleted = 0", [
|
||||
$this->add_base_condition_sql("{$useralias}.id != :{$userparamguest} AND {$useralias}.deleted = 0", [
|
||||
$userparamguest => $CFG->siteguest,
|
||||
]);
|
||||
|
||||
$this->add_entity($userentity);
|
||||
|
||||
// Join the tag entity.
|
||||
$tagentity = (new tag())
|
||||
->set_table_alias('tag', $userentity->get_table_alias('tag'))
|
||||
@ -68,13 +68,20 @@ class users extends datasource {
|
||||
$this->add_entity($tagentity
|
||||
->add_joins($userentity->get_tag_joins()));
|
||||
|
||||
// Join the cohort entity.
|
||||
$cohortentity = new cohort();
|
||||
$cohortalias = $cohortentity->get_table_alias('cohort');
|
||||
$cohortmemberalias = database::generate_alias();
|
||||
$this->add_entity($cohortentity->add_joins([
|
||||
"LEFT JOIN {cohort_members} {$cohortmemberalias} ON {$cohortmemberalias}.userid = {$useralias}.id",
|
||||
"LEFT JOIN {cohort} {$cohortalias} ON {$cohortalias}.id = {$cohortmemberalias}.cohortid",
|
||||
]));
|
||||
|
||||
// Add all columns/filters/conditions from entities to be available in custom reports.
|
||||
$this->add_all_from_entity($userentity->get_entity_name());
|
||||
|
||||
// Add specific tag entity elements.
|
||||
$this->add_columns_from_entity($tagentity->get_entity_name(), ['name', 'namewithlink']);
|
||||
$this->add_filter($tagentity->get_filter('name'));
|
||||
$this->add_condition($tagentity->get_condition('name'));
|
||||
$this->add_all_from_entity($tagentity->get_entity_name(), ['name', 'namewithlink'], ['name'], ['name']);
|
||||
$this->add_all_from_entity($cohortentity->get_entity_name(), ['name', 'idnumber', 'description', 'customfield*'],
|
||||
['name', 'idnumber', 'customfield*'], ['name', 'idnumber', 'customfield*']);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -123,7 +130,7 @@ class users extends datasource {
|
||||
/**
|
||||
* Return the default sorting that will be added to the report once it is created
|
||||
*
|
||||
* @return array|int[]
|
||||
* @return int[]
|
||||
*/
|
||||
public function get_default_column_sorting(): array {
|
||||
return [
|
||||
|
@ -80,6 +80,9 @@ class users_test extends core_reportbuilder_testcase {
|
||||
'interests' => ['Horses'],
|
||||
]);
|
||||
|
||||
$cohort = $this->getDataGenerator()->create_cohort(['name' => 'My cohort']);
|
||||
cohort_add_member($cohort->id, $user->id);
|
||||
|
||||
/** @var core_reportbuilder_generator $generator */
|
||||
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
|
||||
$report = $generator->create_report(['name' => 'Users', 'source' => users::class, 'default' => 0]);
|
||||
@ -116,6 +119,9 @@ class users_test extends core_reportbuilder_testcase {
|
||||
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'tag:name']);
|
||||
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'tag:namewithlink']);
|
||||
|
||||
// Cohort.
|
||||
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'cohort:name']);
|
||||
|
||||
$content = $this->get_custom_report_content($report->get('id'));
|
||||
$this->assertCount(2, $content);
|
||||
|
||||
@ -155,6 +161,7 @@ class users_test extends core_reportbuilder_testcase {
|
||||
$this->assertEquals('0.0.0.0', $userrow[24]);
|
||||
$this->assertEquals('Horses', $userrow[25]);
|
||||
$this->assertStringContainsString('Horses', $userrow[26]);
|
||||
$this->assertEquals($cohort->name, $userrow[27]);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -257,7 +264,6 @@ class users_test extends core_reportbuilder_testcase {
|
||||
'user:address_operator' => text::IS_EQUAL_TO,
|
||||
'user:address_value' => 'Small Farm',
|
||||
], false],
|
||||
|
||||
'Filter city' => ['user:city', [
|
||||
'user:city_operator' => text::IS_EQUAL_TO,
|
||||
'user:city_value' => 'Barcelona',
|
||||
@ -376,6 +382,16 @@ class users_test extends core_reportbuilder_testcase {
|
||||
'Filter tag name not empty' => ['tag:name', [
|
||||
'tag:name_operator' => tags::NOT_EMPTY,
|
||||
], true],
|
||||
|
||||
// Cohort.
|
||||
'Filter cohort name' => ['cohort:name', [
|
||||
'cohort:name_operator' => text::IS_EQUAL_TO,
|
||||
'cohort:name_value' => 'My cohort',
|
||||
], true],
|
||||
'Filter cohort name (no match)' => ['cohort:name', [
|
||||
'cohort:name_operator' => text::IS_EQUAL_TO,
|
||||
'cohort:name_value' => 'Not my cohort',
|
||||
], false],
|
||||
];
|
||||
}
|
||||
|
||||
@ -414,6 +430,9 @@ class users_test extends core_reportbuilder_testcase {
|
||||
'lastip' => '0.0.0.0',
|
||||
]);
|
||||
|
||||
$cohort = $this->getDataGenerator()->create_cohort(['name' => 'My cohort']);
|
||||
cohort_add_member($cohort->id, $user->id);
|
||||
|
||||
/** @var core_reportbuilder_generator $generator */
|
||||
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user