Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

Differences Between: [Versions 401 and 403] [Versions 402 and 403]

   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  declare(strict_types=1);
  18  
  19  namespace core_badges\reportbuilder\datasource;
  20  
  21  use lang_string;
  22  use core_reportbuilder\datasource;
  23  use core_reportbuilder\local\entities\{course, user};
  24  use core_badges\reportbuilder\local\entities\{badge, badge_issued};
  25  use core_tag\reportbuilder\local\entities\tag;
  26  
  27  /**
  28   * Badges datasource
  29   *
  30   * @package     core_badges
  31   * @copyright   2022 Paul Holden <paulh@moodle.com>
  32   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  33   */
  34  class badges extends datasource {
  35  
  36      /**
  37       * Return user friendly name of the report source
  38       *
  39       * @return string
  40       */
  41      public static function get_name(): string {
  42          return get_string('badges', 'core_badges');
  43      }
  44  
  45      /**
  46       * Initialise report
  47       */
  48      protected function initialise(): void {
  49          $badgeentity = new badge();
  50          $badgealias = $badgeentity->get_table_alias('badge');
  51  
  52          $this->set_main_table('badge', $badgealias);
  53          $this->add_entity($badgeentity);
  54  
  55          // Join the tag entity.
  56          $tagentity = (new tag())
  57              ->set_table_alias('tag', $badgeentity->get_table_alias('tag'))
  58              ->set_entity_title(new lang_string('badgetags', 'core_badges'));
  59          $this->add_entity($tagentity
  60              ->add_joins($badgeentity->get_tag_joins()));
  61  
  62          // Join the badge issued entity to the badge entity.
  63          $badgeissuedentity = new badge_issued();
  64          $badgeissuedalias = $badgeissuedentity->get_table_alias('badge_issued');
  65  
  66          $this->add_entity($badgeissuedentity
  67              ->add_join("LEFT JOIN {badge_issued} {$badgeissuedalias}
  68                  ON {$badgeissuedalias}.badgeid = {$badgealias}.id")
  69          );
  70  
  71          // Join the user entity to the badge issued entity.
  72          $userentity = new user();
  73          $useralias = $userentity->get_table_alias('user');
  74  
  75          $this->add_entity($userentity
  76              ->add_joins($badgeissuedentity->get_joins())
  77              ->add_join("LEFT JOIN {user} {$useralias}
  78                  ON {$useralias}.id = {$badgeissuedalias}.userid")
  79              ->set_entity_title(new lang_string('recipient', 'core_badges'))
  80          );
  81  
  82          // Join the course entity to the badge entity, coalescing courseid with the siteid for site badges.
  83          $courseentity = new course();
  84          $coursealias = $courseentity->get_table_alias('course');
  85          $this->add_entity($courseentity
  86              ->add_join("LEFT JOIN {course} {$coursealias}
  87                  ON {$coursealias}.id = COALESCE({$badgealias}.courseid, 1)")
  88          );
  89  
  90          // Add report elements from each of the entities we added to the report.
  91          $this->add_all_from_entity($badgeentity->get_entity_name());
  92  
  93          // Add specific tag entity elements.
  94          $this->add_columns_from_entity($tagentity->get_entity_name(), ['name', 'namewithlink']);
  95          $this->add_filter($tagentity->get_filter('name'));
  96          $this->add_condition($tagentity->get_condition('name'));
  97  
  98          $this->add_all_from_entity($badgeissuedentity->get_entity_name());
  99          $this->add_all_from_entity($userentity->get_entity_name());
 100          $this->add_all_from_entity($courseentity->get_entity_name());
 101      }
 102  
 103      /**
 104       * Return the columns that will be added to the report upon creation
 105       *
 106       * @return string[]
 107       */
 108      public function get_default_columns(): array {
 109          return [
 110              'badge:name',
 111              'badge:description',
 112              'user:fullname',
 113              'badge_issued:issued',
 114          ];
 115      }
 116  
 117      /**
 118       * Return the filters that will be added to the report upon creation
 119       *
 120       * @return string[]
 121       */
 122      public function get_default_filters(): array {
 123          return [
 124              'badge:name',
 125              'user:fullname',
 126              'badge_issued:issued',
 127          ];
 128      }
 129  
 130      /**
 131       * Return the conditions that will be added to the report upon creation
 132       *
 133       * @return string[]
 134       */
 135      public function get_default_conditions(): array {
 136          return [
 137              'badge:type',
 138              'badge:name',
 139          ];
 140      }
 141  
 142      /**
 143       * Return the default sorting that will be added to the report once it is created
 144       *
 145       * @return array|int[]
 146       */
 147      public function get_default_column_sorting(): array {
 148          return [
 149              'badge:name' => SORT_ASC,
 150              'user:fullname' => SORT_ASC,
 151              'badge_issued:issued' => SORT_ASC,
 152          ];
 153      }
 154  }