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