Skip to content

Commit

Permalink
MDL-75810 badges: tag support in badge custom report sources.
Browse files Browse the repository at this point in the history
  • Loading branch information
paulholden committed Jun 26, 2023
1 parent e8ae968 commit ccafa6e
Show file tree
Hide file tree
Showing 6 changed files with 370 additions and 50 deletions.
20 changes: 18 additions & 2 deletions badges/classes/reportbuilder/datasource/badges.php
Expand Up @@ -22,6 +22,7 @@
use core_reportbuilder\datasource;
use core_reportbuilder\local\entities\{course, user};
use core_badges\reportbuilder\local\entities\{badge, badge_issued};
use core_tag\reportbuilder\local\entities\tag;

/**
* Badges datasource
Expand Down Expand Up @@ -49,9 +50,15 @@ protected function initialise(): void {
$badgealias = $badgeentity->get_table_alias('badge');

$this->set_main_table('badge', $badgealias);

$this->add_entity($badgeentity);

// Join the tag entity.
$tagentity = (new tag())
->set_table_alias('tag', $badgeentity->get_table_alias('tag'))
->set_entity_title(new lang_string('badgetags', 'core_badges'));
$this->add_entity($tagentity
->add_joins($badgeentity->get_tag_joins()));

// Join the badge issued entity to the badge entity.
$badgeissuedentity = new badge_issued();
$badgeissuedalias = $badgeissuedentity->get_table_alias('badge_issued');
Expand Down Expand Up @@ -81,7 +88,16 @@ protected function initialise(): void {
);

// Add report elements from each of the entities we added to the report.
$this->add_all_from_entities();
$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($badgeissuedentity->get_entity_name());
$this->add_all_from_entity($userentity->get_entity_name());
$this->add_all_from_entity($courseentity->get_entity_name());
}

/**
Expand Down
26 changes: 22 additions & 4 deletions badges/classes/reportbuilder/datasource/users.php
Expand Up @@ -18,10 +18,12 @@

namespace core_badges\reportbuilder\datasource;

use lang_string;
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_tag\reportbuilder\local\entities\tag;

/**
* User badges datasource
Expand All @@ -48,17 +50,16 @@ protected function initialise(): void {
global $CFG;

$userentity = new user();

$useralias = $userentity->get_table_alias('user');

$this->set_main_table('user', $useralias);
$this->add_entity($userentity);

$paramguest = database::generate_param_name();
$this->add_base_condition_sql("{$useralias}.id != :{$paramguest} AND {$useralias}.deleted = 0", [
$paramguest => $CFG->siteguest,
]);

$this->add_entity($userentity);

// Join the badge issued entity to the user entity.
$badgeissuedentity = new badge_issued();
$badgeissuedalias = $badgeissuedentity->get_table_alias('badge_issued');
Expand All @@ -71,6 +72,14 @@ protected function initialise(): void {
->add_joins($badgeissuedentity->get_joins())
->add_join("LEFT JOIN {badge} {$badgealias} ON {$badgealias}.id = {$badgeissuedalias}.badgeid"));

// Join the tag entity.
$tagentity = (new tag())
->set_table_alias('tag', $badgeentity->get_table_alias('tag'))
->set_entity_title(new lang_string('badgetags', 'core_badges'));
$this->add_entity($tagentity
->add_joins($badgeentity->get_joins())
->add_joins($badgeentity->get_tag_joins()));

// Join the course entity to the badge entity, coalescing courseid with the siteid for site badges.
$courseentity = new course();
$coursealias = $courseentity->get_table_alias('course');
Expand All @@ -80,7 +89,16 @@ protected function initialise(): void {
CASE WHEN {$badgealias}.id IS NULL THEN 0 ELSE COALESCE({$badgealias}.courseid, 1) END"));

// Add report elements from each of the entities we added to the report.
$this->add_all_from_entities();
$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($courseentity->get_entity_name());
}

/**
Expand Down
11 changes: 11 additions & 0 deletions badges/classes/reportbuilder/local/entities/badge.php
Expand Up @@ -52,6 +52,8 @@ protected function get_default_table_aliases(): array {
return [
'badge' => 'b',
'context' => 'bctx',
'tag_instance' => 'bti',
'tag' => 'bt',
];
}

Expand Down Expand Up @@ -303,4 +305,13 @@ protected function get_all_filters(): array {

return $filters;
}

/**
* Return joins necessary for retrieving tags
*
* @return string[]
*/
public function get_tag_joins(): array {
return $this->get_tag_joins_for_entity('core_badges', 'badge', $this->get_table_alias('badge') . '.id');
}
}

0 comments on commit ccafa6e

Please sign in to comment.