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_reportbuilder\local\filters;
  20  
  21  use lang_string;
  22  use MoodleQuickForm;
  23  use core_reportbuilder\local\helpers\database;
  24  
  25  /**
  26   * User report filter
  27   *
  28   * This filter expects field SQL referring to a user ID (e.g. "{$tableuser}.id")
  29   *
  30   * @package     core_reportbuilder
  31   * @copyright   2021 Paul Holden <paulh@moodle.com>
  32   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  33   */
  34  class user extends base {
  35  
  36      /** @var int Filter for any user */
  37      public const USER_ANY = 0;
  38  
  39      /** @var int Filter for current user */
  40      public const USER_CURRENT = 1;
  41  
  42      /**
  43       * Return an array of operators available for this filter
  44       *
  45       * @return lang_string[]
  46       */
  47      private function get_operators(): array {
  48          $operators = [
  49              self::USER_ANY => new lang_string('userany', 'core_reportbuilder'),
  50              self::USER_CURRENT => new lang_string('usercurrent', 'core_reportbuilder'),
  51          ];
  52  
  53          return $this->filter->restrict_limited_operators($operators);
  54      }
  55  
  56      /**
  57       * Setup form
  58       *
  59       * @param MoodleQuickForm $mform
  60       */
  61      public function setup_form(MoodleQuickForm $mform): void {
  62          $operatorlabel = get_string('filterfieldoperator', 'core_reportbuilder', $this->get_header());
  63          $mform->addElement('select', "{$this->name}_operator", $operatorlabel, $this->get_operators())
  64              ->setHiddenLabel(true);
  65  
  66          $mform->setType("{$this->name}_operator", PARAM_INT);
  67          $mform->setDefault("{$this->name}_operator", self::USER_ANY);
  68      }
  69  
  70      /**
  71       * Return filter SQL
  72       *
  73       * @param array $values
  74       * @return array
  75       */
  76      public function get_sql_filter(array $values): array {
  77          global $USER;
  78  
  79          $fieldsql = $this->filter->get_field_sql();
  80          $params = $this->filter->get_field_params();
  81  
  82          $operator = $values["{$this->name}_operator"] ?? self::USER_ANY;
  83          switch ($operator) {
  84              case self::USER_CURRENT:
  85                  $paramuserid = database::generate_param_name();
  86                  $sql = "{$fieldsql} = :{$paramuserid}";
  87                  $params[$paramuserid] = $USER->id;
  88              break;
  89              default:
  90                  // Invalid or inactive filter.
  91                  return ['', []];
  92          }
  93  
  94          return [$sql, $params];
  95      }
  96  
  97      /**
  98       * Return sample filter values
  99       *
 100       * @return array
 101       */
 102      public function get_sample_values(): array {
 103          return [
 104              "{$this->name}_operator" => self::USER_CURRENT,
 105          ];
 106      }
 107  }