Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 401 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  
  26  /**
  27   * Badges datasource
  28   *
  29   * @package     core_badges
  30   * @copyright   2022 Paul Holden <paulh@moodle.com>
  31   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  32   */
  33  class badges extends datasource {
  34  
  35      /**
  36       * Return user friendly name of the report source
  37       *
  38       * @return string
  39       */
  40      public static function get_name(): string {
  41          return get_string('badges', 'core_badges');
  42      }
  43  
  44      /**
  45       * Initialise report
  46       */
  47      protected function initialise(): void {
  48          $badgeentity = new badge();
  49          $badgealias = $badgeentity->get_table_alias('badge');
  50  
  51          $this->set_main_table('badge', $badgealias);
  52  
  53          $this->add_entity($badgeentity);
  54  
  55          // Join the badge issued entity to the badge entity.
  56          $badgeissuedentity = new badge_issued();
  57          $badgeissuedalias = $badgeissuedentity->get_table_alias('badge_issued');
  58  
  59          $this->add_entity($badgeissuedentity
  60              ->add_join("LEFT JOIN {badge_issued} {$badgeissuedalias}
  61                  ON {$badgeissuedalias}.badgeid = {$badgealias}.id")
  62          );
  63  
  64          // Join the user entity to the badge issued entity.
  65          $userentity = new user();
  66          $useralias = $userentity->get_table_alias('user');
  67  
  68          $this->add_entity($userentity
  69              ->add_joins($badgeissuedentity->get_joins())
  70              ->add_join("LEFT JOIN {user} {$useralias}
  71                  ON {$useralias}.id = {$badgeissuedalias}.userid")
  72              ->set_entity_title(new lang_string('recipient', 'core_badges'))
  73          );
  74  
  75          // Join the course entity to the badge entity, coalescing courseid with the siteid for site badges.
  76          $courseentity = new course();
  77          $coursealias = $courseentity->get_table_alias('course');
  78          $this->add_entity($courseentity
  79              ->add_join("LEFT JOIN {course} {$coursealias}
  80                  ON {$coursealias}.id = COALESCE({$badgealias}.courseid, 1)")
  81          );
  82  
  83          // Add report elements from each of the entities we added to the report.
  84          $this->add_all_from_entities();
  85      }
  86  
  87      /**
  88       * Return the columns that will be added to the report upon creation
  89       *
  90       * @return string[]
  91       */
  92      public function get_default_columns(): array {
  93          return [
  94              'badge:name',
  95              'badge:description',
  96              'user:fullname',
  97              'badge_issued:issued',
  98          ];
  99      }
 100  
 101      /**
 102       * Return the filters that will be added to the report upon creation
 103       *
 104       * @return string[]
 105       */
 106      public function get_default_filters(): array {
 107          return [
 108              'badge:name',
 109              'user:fullname',
 110              'badge_issued:issued',
 111          ];
 112      }
 113  
 114      /**
 115       * Return the conditions that will be added to the report upon creation
 116       *
 117       * @return string[]
 118       */
 119      public function get_default_conditions(): array {
 120          return [
 121              'badge:type',
 122              'badge:name',
 123          ];
 124      }
 125  
 126      /**
 127       * Return the default sorting that will be added to the report once it is created
 128       *
 129       * @return array|int[]
 130       */
 131      public function get_default_column_sorting(): array {
 132          return [
 133              'badge:name' => SORT_ASC,
 134              'user:fullname' => SORT_ASC,
 135              'badge_issued:issued' => SORT_ASC,
 136          ];
 137      }
 138  }