Skip to content

Commit

Permalink
MDL-73988 badges: implement badges datasource for custom reporting.
Browse files Browse the repository at this point in the history
Create two entities exposing reportable data on badges and their
recipients, via column and filter definitions.

Create report source bringing them together along with the user and
course entities to provide data for the reportbuilder editor.
  • Loading branch information
paulholden committed May 19, 2022
1 parent 40c574c commit 5274ee5
Show file tree
Hide file tree
Showing 5 changed files with 704 additions and 1 deletion.
125 changes: 125 additions & 0 deletions badges/classes/reportbuilder/datasource/badges.php
@@ -0,0 +1,125 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

declare(strict_types=1);

namespace core_badges\reportbuilder\datasource;

use lang_string;
use core_reportbuilder\datasource;
use core_reportbuilder\local\entities\{course, user};
use core_badges\reportbuilder\local\entities\{badge, badge_issued};

/**
* Badges datasource
*
* @package core_badges
* @copyright 2022 Paul Holden <paulh@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class badges extends datasource {

/**
* Return user friendly name of the report source
*
* @return string
*/
public static function get_name(): string {
return get_string('badges', 'core_badges');
}

/**
* Initialise report
*/
protected function initialise(): void {
$badgeentity = new badge();
$badgealias = $badgeentity->get_table_alias('badge');

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

$this->add_entity($badgeentity);

// Join the badge issued entity to the badge entity.
$badgeissuedentity = new badge_issued();
$badgeissuedalias = $badgeissuedentity->get_table_alias('badge_issued');

$this->add_entity($badgeissuedentity
->add_join("LEFT JOIN {badge_issued} {$badgeissuedalias}
ON {$badgeissuedalias}.badgeid = {$badgealias}.id")
);

// Join the user entity to the badge issued entity.
$userentity = new user();
$useralias = $userentity->get_table_alias('user');

$this->add_entity($userentity
->add_joins($badgeissuedentity->get_joins())
->add_join("LEFT JOIN {user} {$useralias}
ON {$useralias}.id = {$badgeissuedalias}.userid")
->set_entity_title(new lang_string('recipient', 'core_badges'))
);

// 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');
$this->add_entity($courseentity
->add_join("LEFT JOIN {course} {$coursealias}
ON {$coursealias}.id = COALESCE({$badgealias}.courseid, 1)")
);

// Add report elements from each of the entities we added to the report.
$this->add_all_from_entities();
}

/**
* Return the columns that will be added to the report upon creation
*
* @return string[]
*/
public function get_default_columns(): array {
return [
'badge:name',
'badge:description',
'user:fullname',
'badge_issued:issued',
];
}

/**
* Return the filters that will be added to the report upon creation
*
* @return string[]
*/
public function get_default_filters(): array {
return [
'badge:name',
'user:fullname',
'badge_issued:issued',
];
}

/**
* Return the conditions that will be added to the report upon creation
*
* @return string[]
*/
public function get_default_conditions(): array {
return [
'badge:type',
'badge:name',
];
}
}

0 comments on commit 5274ee5

Please sign in to comment.