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 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_reportbuilder\local\helpers\database;
  25  use core_badges\reportbuilder\local\entities\{badge, badge_issued};
  26  use core_tag\reportbuilder\local\entities\tag;
  27  
  28  /**
  29   * User badges datasource
  30   *
  31   * @package     core_badges
  32   * @copyright   2023 Paul Holden <paulh@moodle.com>
  33   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  34   */
  35  class users extends datasource {
  36  
  37      /**
  38       * Return user friendly name of the report source
  39       *
  40       * @return string
  41       */
  42      public static function get_name(): string {
  43          return get_string('userbadges', 'core_badges');
  44      }
  45  
  46      /**
  47       * Initialise report
  48       */
  49      protected function initialise(): void {
  50          global $CFG;
  51  
  52          $userentity = new user();
  53          $useralias = $userentity->get_table_alias('user');
  54  
  55          $this->set_main_table('user', $useralias);
  56          $this->add_entity($userentity);
  57  
  58          $paramguest = database::generate_param_name();
  59          $this->add_base_condition_sql("{$useralias}.id != :{$paramguest} AND {$useralias}.deleted = 0", [
  60              $paramguest => $CFG->siteguest,
  61          ]);
  62  
  63          // Join the badge issued entity to the user entity.
  64          $badgeissuedentity = new badge_issued();
  65          $badgeissuedalias = $badgeissuedentity->get_table_alias('badge_issued');
  66          $this->add_entity($badgeissuedentity
  67              ->add_join("LEFT JOIN {badge_issued} {$badgeissuedalias} ON {$badgeissuedalias}.userid = {$useralias}.id"));
  68  
  69          $badgeentity = new badge();
  70          $badgealias = $badgeentity->get_table_alias('badge');
  71          $this->add_entity($badgeentity
  72              ->add_joins($badgeissuedentity->get_joins())
  73              ->add_join("LEFT JOIN {badge} {$badgealias} ON {$badgealias}.id = {$badgeissuedalias}.badgeid"));
  74  
  75          // Join the tag entity.
  76          $tagentity = (new tag())
  77              ->set_table_alias('tag', $badgeentity->get_table_alias('tag'))
  78              ->set_entity_title(new lang_string('badgetags', 'core_badges'));
  79          $this->add_entity($tagentity
  80              ->add_joins($badgeentity->get_joins())
  81              ->add_joins($badgeentity->get_tag_joins()));
  82  
  83          // Join the course entity to the badge entity, coalescing courseid with the siteid for site badges.
  84          $courseentity = new course();
  85          $coursealias = $courseentity->get_table_alias('course');
  86          $this->add_entity($courseentity
  87              ->add_joins($badgeentity->get_joins())
  88              ->add_join("LEFT JOIN {course} {$coursealias} ON {$coursealias}.id =
  89                  CASE WHEN {$badgealias}.id IS NULL THEN 0 ELSE COALESCE({$badgealias}.courseid, 1) END"));
  90  
  91          // Add report elements from each of the entities we added to the report.
  92          $this->add_all_from_entity($userentity->get_entity_name());
  93          $this->add_all_from_entity($badgeissuedentity->get_entity_name());
  94          $this->add_all_from_entity($badgeentity->get_entity_name());
  95  
  96          // Add specific tag entity elements.
  97          $this->add_columns_from_entity($tagentity->get_entity_name(), ['name', 'namewithlink']);
  98          $this->add_filter($tagentity->get_filter('name'));
  99          $this->add_condition($tagentity->get_condition('name'));
 100  
 101          $this->add_all_from_entity($courseentity->get_entity_name());
 102      }
 103  
 104      /**
 105       * Return the columns that will be added to the report upon creation
 106       *
 107       * @return string[]
 108       */
 109      public function get_default_columns(): array {
 110          return [
 111              'user:fullname',
 112              'badge:name',
 113              'badge:description',
 114              'badge_issued:issued',
 115          ];
 116      }
 117  
 118      /**
 119       * Return the column sorting that will be added to the report upon creation
 120       *
 121       * @return int[]
 122       */
 123      public function get_default_column_sorting(): array {
 124          return [
 125              'user:fullname' => SORT_ASC,
 126              'badge:name' => SORT_ASC,
 127              'badge_issued:issued' => SORT_ASC,
 128          ];
 129      }
 130  
 131      /**
 132       * Return the filters that will be added to the report upon creation
 133       *
 134       * @return string[]
 135       */
 136      public function get_default_filters(): array {
 137          return [
 138              'user:fullname',
 139              'badge:name',
 140              'badge_issued:issued',
 141          ];
 142      }
 143  
 144      /**
 145       * Return the conditions that will be added to the report upon creation
 146       *
 147       * @return string[]
 148       */
 149      public function get_default_conditions(): array {
 150          return [
 151              'badge:type',
 152              'badge:name',
 153          ];
 154      }
 155  }