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.
   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\reportbuilder\audience;
  20  
  21  use context_system;
  22  use core_reportbuilder\local\audiences\base;
  23  use core_reportbuilder\local\helpers\database;
  24  use core_user;
  25  use MoodleQuickForm;
  26  
  27  /**
  28   * The backend class for Manually added users audience type
  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 manual extends base {
  35  
  36      /**
  37       * Adds audience's elements to the given mform
  38       *
  39       * @param MoodleQuickForm $mform The form to add elements to
  40       */
  41      public function get_config_form(MoodleQuickForm $mform): void {
  42          // Users selector.
  43          $options = [
  44              'ajax' => 'core_user/form_user_selector',
  45              'multiple' => true,
  46              'valuehtmlcallback' => function($userid) {
  47                  $user = core_user::get_user($userid);
  48                  return fullname($user, has_capability('moodle/site:viewfullnames', context_system::instance()));
  49              }
  50          ];
  51  
  52          $mform->addElement('autocomplete', 'users', get_string('addusers', 'core_reportbuilder'), [], $options);
  53          $mform->addRule('users', null, 'required', null, 'client');
  54      }
  55  
  56      /**
  57       * Helps to build SQL to retrieve users that matches the current report audience
  58       *
  59       * @param string $usertablealias
  60       * @return array array of three elements [$join, $where, $params]
  61       */
  62      public function get_sql(string $usertablealias): array {
  63          global $DB;
  64  
  65          $users = $this->get_configdata()['users'];
  66          $prefix = database::generate_param_name() . '_';
  67          [$insql, $inparams] = $DB->get_in_or_equal($users, SQL_PARAMS_NAMED, $prefix);
  68  
  69          return ['', "{$usertablealias}.id $insql", $inparams];
  70      }
  71  
  72      /**
  73       * Return user friendly name of this audience type
  74       *
  75       * @return string
  76       */
  77      public function get_name(): string {
  78          return get_string('manuallyaddedusers', 'core_reportbuilder');
  79      }
  80  
  81      /**
  82       * Return the description for the audience.
  83       *
  84       * @return string
  85       */
  86      public function get_description(): string {
  87          global $DB;
  88  
  89          $canviewfullnames = has_capability('moodle/site:viewfullnames', context_system::instance());
  90  
  91          $userslist = [];
  92  
  93          $userids = $this->get_configdata()['users'];
  94          [$sort] = users_order_by_sql();
  95          $users = $DB->get_records_list('user', 'id', $userids, $sort);
  96          foreach ($users as $user) {
  97              $userslist[] = fullname($user, $canviewfullnames);
  98          }
  99  
 100          return $this->format_description_for_multiselect($userslist);
 101      }
 102  
 103      /**
 104       * If the current user is able to add this audience.
 105       *
 106       * @return bool
 107       */
 108      public function user_can_add(): bool {
 109          return has_capability('moodle/user:viewalldetails', context_system::instance());
 110      }
 111  
 112      /**
 113       * If the current user is able to edit this audience.
 114       *
 115       * @return bool
 116       */
 117      public function user_can_edit(): bool {
 118          return has_capability('moodle/user:viewalldetails', context_system::instance());
 119      }
 120  }