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 400 and 402]

   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 context_system;
  22  use core_user;
  23  use lang_string;
  24  use MoodleQuickForm;
  25  use core_reportbuilder\local\helpers\database;
  26  
  27  /**
  28   * User report filter
  29   *
  30   * This filter expects field SQL referring to a user ID (e.g. "{$tableuser}.id")
  31   *
  32   * @package     core_reportbuilder
  33   * @copyright   2021 Paul Holden <paulh@moodle.com>
  34   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class user extends base {
  37  
  38      /** @var int Filter for any user */
  39      public const USER_ANY = 0;
  40  
  41      /** @var int Filter for current user */
  42      public const USER_CURRENT = 1;
  43  
  44      /** @var int Filter for selected user */
  45      public const USER_SELECT = 2;
  46  
  47      /**
  48       * Return an array of operators available for this filter
  49       *
  50       * @return lang_string[]
  51       */
  52      private function get_operators(): array {
  53          $operators = [
  54              self::USER_ANY => new lang_string('userany', 'core_reportbuilder'),
  55              self::USER_CURRENT => new lang_string('usercurrent', 'core_reportbuilder'),
  56              self::USER_SELECT => new lang_string('select'),
  57          ];
  58  
  59          return $this->filter->restrict_limited_operators($operators);
  60      }
  61  
  62      /**
  63       * Setup form
  64       *
  65       * @param MoodleQuickForm $mform
  66       */
  67      public function setup_form(MoodleQuickForm $mform): void {
  68          $operatorlabel = get_string('filterfieldoperator', 'core_reportbuilder', $this->get_header());
  69          $mform->addElement('select', "{$this->name}_operator", $operatorlabel, $this->get_operators())
  70              ->setHiddenLabel(true);
  71  
  72          $mform->setType("{$this->name}_operator", PARAM_INT);
  73          $mform->setDefault("{$this->name}_operator", self::USER_ANY);
  74  
  75          $options = [
  76              'ajax' => 'core_user/form_user_selector',
  77              'multiple' => true,
  78              'valuehtmlcallback' => static function($userid): string {
  79                  $user = core_user::get_user($userid);
  80                  return fullname($user, has_capability('moodle/site:viewfullnames', context_system::instance()));
  81              }
  82          ];
  83          $mform->addElement('autocomplete', "{$this->name}_value", get_string('user'), [], $options)
  84              ->setHiddenLabel(true);
  85          $mform->hideIf("{$this->name}_value", "{$this->name}_operator", 'neq', self::USER_SELECT);
  86      }
  87  
  88      /**
  89       * Return filter SQL
  90       *
  91       * @param array $values
  92       * @return array
  93       */
  94      public function get_sql_filter(array $values): array {
  95          global $DB, $USER;
  96  
  97          $fieldsql = $this->filter->get_field_sql();
  98          $params = $this->filter->get_field_params();
  99  
 100          $operator = $values["{$this->name}_operator"] ?? self::USER_ANY;
 101          $userids = $values["{$this->name}_value"] ?? [];
 102  
 103          switch ($operator) {
 104              case self::USER_CURRENT:
 105                  $paramuserid = database::generate_param_name();
 106                  $sql = "{$fieldsql} = :{$paramuserid}";
 107                  $params[$paramuserid] = $USER->id;
 108              break;
 109              case self::USER_SELECT:
 110                  $paramuserid = database::generate_param_name();
 111                  [$useridselect, $useridparams] = $DB->get_in_or_equal($userids, SQL_PARAMS_NAMED, "{$paramuserid}_", true, null);
 112                  $sql = "{$fieldsql} {$useridselect}";
 113                  $params = array_merge($params, $useridparams);
 114              break;
 115              default:
 116                  // Invalid or inactive filter.
 117                  return ['', []];
 118          }
 119  
 120          return [$sql, $params];
 121      }
 122  
 123      /**
 124       * Return sample filter values
 125       *
 126       * @return array
 127       */
 128      public function get_sample_values(): array {
 129          return [
 130              "{$this->name}_operator" => self::USER_SELECT,
 131              "{$this->name}_value" => [1],
 132          ];
 133      }
 134  }