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 400 and 403] [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_user\reportbuilder\datasource;
  20  
  21  use lang_string;
  22  use core_reportbuilder\datasource;
  23  use core_reportbuilder\local\entities\user;
  24  use core_reportbuilder\local\filters\boolean_select;
  25  use core_reportbuilder\local\helpers\database;
  26  use core_tag\reportbuilder\local\entities\tag;
  27  
  28  /**
  29   * Users datasource
  30   *
  31   * @package   core_user
  32   * @copyright 2021 David Matamoros <davidmc@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 datasource
  39       *
  40       * @return string
  41       */
  42      public static function get_name(): string {
  43          return get_string('users');
  44      }
  45  
  46      /**
  47       * Initialise report
  48       */
  49      protected function initialise(): void {
  50          global $CFG;
  51  
  52          $userentity = new user();
  53          $usertablealias = $userentity->get_table_alias('user');
  54  
  55          $this->set_main_table('user', $usertablealias);
  56  
  57          $userparamguest = database::generate_param_name();
  58          $this->add_base_condition_sql("{$usertablealias}.id != :{$userparamguest} AND {$usertablealias}.deleted = 0", [
  59              $userparamguest => $CFG->siteguest,
  60          ]);
  61  
  62          $this->add_entity($userentity);
  63  
  64          // Join the tag entity.
  65          $tagentity = (new tag())
  66              ->set_table_alias('tag', $userentity->get_table_alias('tag'))
  67              ->set_entity_title(new lang_string('interests'));
  68          $this->add_entity($tagentity
  69              ->add_joins($userentity->get_tag_joins()));
  70  
  71          // Add all columns/filters/conditions from entities to be available in custom reports.
  72          $this->add_all_from_entity($userentity->get_entity_name());
  73  
  74          // Add specific tag entity elements.
  75          $this->add_columns_from_entity($tagentity->get_entity_name(), ['name', 'namewithlink']);
  76          $this->add_filter($tagentity->get_filter('name'));
  77          $this->add_condition($tagentity->get_condition('name'));
  78      }
  79  
  80      /**
  81       * Return the columns that will be added to the report once is created
  82       *
  83       * @return string[]
  84       */
  85      public function get_default_columns(): array {
  86          return ['user:fullname', 'user:username', 'user:email'];
  87      }
  88  
  89      /**
  90       * Return the filters that will be added to the report once is created
  91       *
  92       * @return string[]
  93       */
  94      public function get_default_filters(): array {
  95          return ['user:fullname', 'user:username', 'user:email'];
  96      }
  97  
  98      /**
  99       * Return the conditions that will be added to the report once is created
 100       *
 101       * @return string[]
 102       */
 103      public function get_default_conditions(): array {
 104          return [
 105              'user:fullname',
 106              'user:username',
 107              'user:email',
 108              'user:suspended',
 109          ];
 110      }
 111  
 112      /**
 113       * Return the conditions values that will be added to the report once is created
 114       *
 115       * @return array
 116       */
 117      public function get_default_condition_values(): array {
 118          return [
 119              'user:suspended_operator' => boolean_select::NOT_CHECKED,
 120          ];
 121      }
 122  
 123      /**
 124       * Return the default sorting that will be added to the report once it is created
 125       *
 126       * @return array|int[]
 127       */
 128      public function get_default_column_sorting(): array {
 129          return [
 130              'user:fullname' => SORT_ASC,
 131          ];
 132      }
 133  }