Skip to content

Instantly share code, notes, and snippets.

@marinaglancy
Created October 25, 2022 08:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marinaglancy/4d73664dc907a56db33cd95e0811b82a to your computer and use it in GitHub Desktop.
Save marinaglancy/4d73664dc907a56db33cd95e0811b82a to your computer and use it in GitHub Desktop.
User badges datasource
<?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 userbadges extends datasource {
/**
* Return user friendly name of the report source
*
* @return string
*/
public static function get_name(): string {
return 'User badges';
}
/**
* Initialise report
*/
protected function initialise(): void {
$userentity = new user();
$useralias = $userentity->get_table_alias('user');
$badgeissuedentity = new badge_issued();
$badgeissuedalias = $badgeissuedentity->get_table_alias('badge_issued');
$badgeissuedentity->add_join("LEFT JOIN {badge_issued} {$badgeissuedalias}
ON {$badgeissuedalias}.userid = {$useralias}.id");
$badgeentity = new badge();
$badgealias = $badgeentity->get_table_alias('badge');
$badgeentity
->add_joins($badgeissuedentity->get_joins())
->add_join("LEFT JOIN {badge} {$badgealias}
ON {$badgeissuedalias}.badgeid = {$badgealias}.id");
$this->set_main_table('user', $useralias);
$this->add_entity($userentity);
$this->add_entity($badgeissuedentity);
$this->add_entity($badgeentity);
$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 ['user:fullname'];
}
/**
* Return the filters that will be added to the report upon creation
*
* @return string[]
*/
public function get_default_filters(): array {
return [];
}
/**
* Return the conditions that will be added to the report upon creation
*
* @return string[]
*/
public function get_default_conditions(): array {
return [];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment