| 1 | 
           efrain | 
           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 | 
              | 
        
        
           | 1441 | 
           ariadna | 
           75 | 
                   // Specific user selection.
  | 
        
        
            | 
            | 
           76 | 
                   $valuelabel = get_string('filterfieldvalue', 'core_reportbuilder', $this->get_header());
  | 
        
        
           | 1 | 
           efrain | 
           77 | 
                   $options = [
  | 
        
        
            | 
            | 
           78 | 
                       'ajax' => 'core_user/form_user_selector',
  | 
        
        
            | 
            | 
           79 | 
                       'multiple' => true,
  | 
        
        
            | 
            | 
           80 | 
                       'valuehtmlcallback' => static function($userid): string {
  | 
        
        
            | 
            | 
           81 | 
                           $user = core_user::get_user($userid);
  | 
        
        
            | 
            | 
           82 | 
                           return fullname($user, has_capability('moodle/site:viewfullnames', context_system::instance()));
  | 
        
        
            | 
            | 
           83 | 
                       }
  | 
        
        
            | 
            | 
           84 | 
                   ];
  | 
        
        
           | 1441 | 
           ariadna | 
           85 | 
                   $mform->addElement('autocomplete', "{$this->name}_value", $valuelabel, [], $options)
  | 
        
        
           | 1 | 
           efrain | 
           86 | 
                       ->setHiddenLabel(true);
  | 
        
        
            | 
            | 
           87 | 
                   $mform->hideIf("{$this->name}_value", "{$this->name}_operator", 'neq', self::USER_SELECT);
  | 
        
        
            | 
            | 
           88 | 
               }
  | 
        
        
            | 
            | 
           89 | 
              | 
        
        
            | 
            | 
           90 | 
               /**
  | 
        
        
            | 
            | 
           91 | 
                * Return filter SQL
  | 
        
        
            | 
            | 
           92 | 
                *
  | 
        
        
            | 
            | 
           93 | 
                * @param array $values
  | 
        
        
            | 
            | 
           94 | 
                * @return array
  | 
        
        
            | 
            | 
           95 | 
                */
  | 
        
        
            | 
            | 
           96 | 
               public function get_sql_filter(array $values): array {
  | 
        
        
            | 
            | 
           97 | 
                   global $DB, $USER;
  | 
        
        
            | 
            | 
           98 | 
              | 
        
        
            | 
            | 
           99 | 
                   $fieldsql = $this->filter->get_field_sql();
  | 
        
        
            | 
            | 
           100 | 
                   $params = $this->filter->get_field_params();
  | 
        
        
            | 
            | 
           101 | 
              | 
        
        
            | 
            | 
           102 | 
                   $operator = $values["{$this->name}_operator"] ?? self::USER_ANY;
  | 
        
        
            | 
            | 
           103 | 
                   $userids = $values["{$this->name}_value"] ?? [];
  | 
        
        
            | 
            | 
           104 | 
              | 
        
        
            | 
            | 
           105 | 
                   switch ($operator) {
  | 
        
        
            | 
            | 
           106 | 
                       case self::USER_CURRENT:
  | 
        
        
            | 
            | 
           107 | 
                           $paramuserid = database::generate_param_name();
  | 
        
        
            | 
            | 
           108 | 
                           $sql = "{$fieldsql} = :{$paramuserid}";
  | 
        
        
            | 
            | 
           109 | 
                           $params[$paramuserid] = $USER->id;
  | 
        
        
            | 
            | 
           110 | 
                       break;
  | 
        
        
            | 
            | 
           111 | 
                       case self::USER_SELECT:
  | 
        
        
            | 
            | 
           112 | 
                           [$useridselect, $useridparams] = $DB->get_in_or_equal(
  | 
        
        
            | 
            | 
           113 | 
                               $userids,
  | 
        
        
            | 
            | 
           114 | 
                               SQL_PARAMS_NAMED,
  | 
        
        
            | 
            | 
           115 | 
                               database::generate_param_name('_'),
  | 
        
        
            | 
            | 
           116 | 
                               true,
  | 
        
        
            | 
            | 
           117 | 
                               null,
  | 
        
        
            | 
            | 
           118 | 
                           );
  | 
        
        
            | 
            | 
           119 | 
              | 
        
        
            | 
            | 
           120 | 
                           $sql = "{$fieldsql} {$useridselect}";
  | 
        
        
            | 
            | 
           121 | 
                           $params = array_merge($params, $useridparams);
  | 
        
        
            | 
            | 
           122 | 
                       break;
  | 
        
        
            | 
            | 
           123 | 
                       default:
  | 
        
        
            | 
            | 
           124 | 
                           // Invalid or inactive filter.
  | 
        
        
            | 
            | 
           125 | 
                           return ['', []];
  | 
        
        
            | 
            | 
           126 | 
                   }
  | 
        
        
            | 
            | 
           127 | 
              | 
        
        
            | 
            | 
           128 | 
                   return [$sql, $params];
  | 
        
        
            | 
            | 
           129 | 
               }
  | 
        
        
            | 
            | 
           130 | 
              | 
        
        
            | 
            | 
           131 | 
               /**
  | 
        
        
            | 
            | 
           132 | 
                * Return sample filter values
  | 
        
        
            | 
            | 
           133 | 
                *
  | 
        
        
            | 
            | 
           134 | 
                * @return array
  | 
        
        
            | 
            | 
           135 | 
                */
  | 
        
        
            | 
            | 
           136 | 
               public function get_sample_values(): array {
  | 
        
        
            | 
            | 
           137 | 
                   return [
  | 
        
        
            | 
            | 
           138 | 
                       "{$this->name}_operator" => self::USER_SELECT,
  | 
        
        
            | 
            | 
           139 | 
                       "{$this->name}_value" => [1],
  | 
        
        
            | 
            | 
           140 | 
                   ];
  | 
        
        
            | 
            | 
           141 | 
               }
  | 
        
        
            | 
            | 
           142 | 
           }
  |