Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.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 core_reportbuilder\datasource;
  22  use core_reportbuilder\local\entities\{course, user};
  23  use core_reportbuilder\local\helpers\database;
  24  use core_badges\reportbuilder\local\entities\{badge, badge_issued};
  25  
  26  /**
  27   * User badges datasource
  28   *
  29   * @package     core_badges
  30   * @copyright   2023 Paul Holden <paulh@moodle.com>
  31   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  32   */
  33  class users 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('userbadges', 'core_badges');
  42      }
  43  
  44      /**
  45       * Initialise report
  46       */
  47      protected function initialise(): void {
  48          global $CFG;
  49  
  50          $userentity = new user();
  51  
  52          $useralias = $userentity->get_table_alias('user');
  53          $this->set_main_table('user', $useralias);
  54  
  55          $paramguest = database::generate_param_name();
  56          $this->add_base_condition_sql("{$useralias}.id != :{$paramguest} AND {$useralias}.deleted = 0", [
  57              $paramguest => $CFG->siteguest,
  58          ]);
  59  
  60          $this->add_entity($userentity);
  61  
  62          // Join the badge issued entity to the user entity.
  63          $badgeissuedentity = new badge_issued();
  64          $badgeissuedalias = $badgeissuedentity->get_table_alias('badge_issued');
  65          $this->add_entity($badgeissuedentity
  66              ->add_join("LEFT JOIN {badge_issued} {$badgeissuedalias} ON {$badgeissuedalias}.userid = {$useralias}.id"));
  67  
  68          $badgeentity = new badge();
  69          $badgealias = $badgeentity->get_table_alias('badge');
  70          $this->add_entity($badgeentity
  71              ->add_joins($badgeissuedentity->get_joins())
  72              ->add_join("LEFT JOIN {badge} {$badgealias} ON {$badgealias}.id = {$badgeissuedalias}.badgeid"));
  73  
  74          // Join the course entity to the badge entity, coalescing courseid with the siteid for site badges.
  75          $courseentity = new course();
  76          $coursealias = $courseentity->get_table_alias('course');
  77          $this->add_entity($courseentity
  78              ->add_joins($badgeentity->get_joins())
  79              ->add_join("LEFT JOIN {course} {$coursealias} ON {$coursealias}.id =
  80                  CASE WHEN {$badgealias}.id IS NULL THEN 0 ELSE COALESCE({$badgealias}.courseid, 1) END"));
  81  
  82          // Add report elements from each of the entities we added to the report.
  83          $this->add_all_from_entities();
  84      }
  85  
  86      /**
  87       * Return the columns that will be added to the report upon creation
  88       *
  89       * @return string[]
  90       */
  91      public function get_default_columns(): array {
  92          return [
  93              'user:fullname',
  94              'badge:name',
  95              'badge:description',
  96              'badge_issued:issued',
  97          ];
  98      }
  99  
 100      /**
 101       * Return the column sorting that will be added to the report upon creation
 102       *
 103       * @return int[]
 104       */
 105      public function get_default_column_sorting(): array {
 106          return [
 107              'user:fullname' => SORT_ASC,
 108              'badge:name' => SORT_ASC,
 109              'badge_issued:issued' => SORT_ASC,
 110          ];
 111      }
 112  
 113      /**
 114       * Return the filters that will be added to the report upon creation
 115       *
 116       * @return string[]
 117       */
 118      public function get_default_filters(): array {
 119          return [
 120              'user:fullname',
 121              'badge:name',
 122              'badge_issued:issued',
 123          ];
 124      }
 125  
 126      /**
 127       * Return the conditions that will be added to the report upon creation
 128       *
 129       * @return string[]
 130       */
 131      public function get_default_conditions(): array {
 132          return [
 133              'badge:type',
 134              'badge:name',
 135          ];
 136      }
 137  }