Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

Differences Between: [Versions 400 and 401] [Versions 400 and 402] [Versions 400 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 core_reportbuilder\datasource;
  22  use core_reportbuilder\local\entities\user;
  23  use core_reportbuilder\local\helpers\database;
  24  use core_reportbuilder\manager;
  25  use core_reportbuilder\local\helpers\report;
  26  
  27  /**
  28   * Users datasource
  29   *
  30   * @package   core_reportbuilder
  31   * @copyright 2021 David Matamoros <davidmc@moodle.com>
  32   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  33   */
  34  class users extends datasource {
  35  
  36      /**
  37       * Return user friendly name of the datasource
  38       *
  39       * @return string
  40       */
  41      public static function get_name(): string {
  42          return get_string('users');
  43      }
  44  
  45      /**
  46       * Initialise report
  47       */
  48      protected function initialise(): void {
  49          global $CFG;
  50  
  51          $userentity = new user();
  52          $usertablealias = $userentity->get_table_alias('user');
  53  
  54          $this->set_main_table('user', $usertablealias);
  55  
  56          $userparamguest = database::generate_param_name();
  57          $this->add_base_condition_sql("{$usertablealias}.id != :{$userparamguest} AND {$usertablealias}.deleted = 0", [
  58              $userparamguest => $CFG->siteguest,
  59          ]);
  60  
  61          // Add all columns from entities to be available in custom reports.
  62          $this->add_entity($userentity);
  63  
  64          $userentityname = $userentity->get_entity_name();
  65          $this->add_columns_from_entity($userentityname);
  66          $this->add_filters_from_entity($userentityname);
  67          $this->add_conditions_from_entity($userentityname);
  68      }
  69  
  70      /**
  71       * Return the columns that will be added to the report once is created
  72       *
  73       * @return string[]
  74       */
  75      public function get_default_columns(): array {
  76          return ['user:fullname', 'user:username', 'user:email'];
  77      }
  78  
  79      /**
  80       * Return the filters that will be added to the report once is created
  81       *
  82       * @return string[]
  83       */
  84      public function get_default_filters(): array {
  85          return ['user:fullname', 'user:username', 'user:email'];
  86      }
  87  
  88      /**
  89       * Return the conditions that will be added to the report once is created
  90       *
  91       * @return string[]
  92       */
  93      public function get_default_conditions(): array {
  94          return ['user:fullname', 'user:username', 'user:email'];
  95      }
  96  
  97      /**
  98       * Set default columns and the sortorder
  99       */
 100      public function add_default_columns(): void {
 101          parent::add_default_columns();
 102  
 103          $persistent = $this->get_report_persistent();
 104          $report = manager::get_report_from_persistent($persistent);
 105          foreach ($report->get_active_columns() as $column) {
 106              if ($column->get_unique_identifier() === 'user:fullname') {
 107                  report::toggle_report_column_sorting($persistent->get('id'), $column->get_persistent()->get('id'), true);
 108              }
 109          }
 110      }
 111  }