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.

Differences Between: [Versions 310 and 401] [Versions 311 and 401] [Versions 39 and 401] [Versions 400 and 401] [Versions 401 and 402] [Versions 401 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  /**
  18   * Potential admin user selector.
  19   *
  20   * @package    core_role
  21   * @copyright  2010 Petr Skoda {@link http://skodak.org}
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  require_once($CFG->dirroot.'/user/selector/lib.php');
  28  
  29  class core_role_admins_potential_selector extends user_selector_base {
  30      /**
  31       * Create instance.
  32       *
  33       * @param string $name control name
  34       * @param array $options should have two elements with keys groupid and courseid.
  35       */
  36      public function __construct($name = null, $options = array()) {
  37          global $CFG;
  38          if (is_null($name)) {
  39              $name = 'addselect';
  40          }
  41          $options['multiselect'] = false;
  42          $options['includecustomfields'] = true;
  43          $options['exclude'] = explode(',', $CFG->siteadmins);
  44          parent::__construct($name, $options);
  45      }
  46  
  47      public function find_users($search) {
  48          global $CFG, $DB;
  49  
  50          [$wherecondition, $params] = $this->search_sql($search, 'u');
  51          $params = array_merge($params, $this->userfieldsparams);
  52  
  53          $fields = 'SELECT u.id, ' . $this->userfieldsselects;
  54          $countfields = 'SELECT COUNT(1)';
  55  
  56          $sql = " FROM {user} u
  57                        $this->userfieldsjoin
  58                  WHERE $wherecondition AND mnethostid = :localmnet";
  59  
  60          // It could be dangerous to make remote users admins and also this could lead to other problems.
  61          $params['localmnet'] = $CFG->mnet_localhost_id;
  62  
  63          [$sort, $sortparams] = users_order_by_sql('u', $search, $this->accesscontext, $this->userfieldsmappings);
  64          $order = ' ORDER BY ' . $sort;
  65  
  66          // Check to see if there are too many to show sensibly.
  67          if (!$this->is_validating()) {
  68              $potentialcount = $DB->count_records_sql($countfields . $sql, $params);
  69              if ($potentialcount > $this->maxusersperpage) {
  70                  return $this->too_many_results($search, $potentialcount);
  71              }
  72          }
  73  
  74          $availableusers = $DB->get_records_sql($fields . $sql . $order, array_merge($params, $sortparams));
  75  
  76          if (empty($availableusers)) {
  77              return array();
  78          }
  79  
  80          if ($search) {
  81              $groupname = get_string('potusersmatching', 'core_role', $search);
  82          } else {
  83              $groupname = get_string('potusers', 'core_role');
  84          }
  85  
  86          return array($groupname => $availableusers);
  87      }
  88  
  89      protected function get_options() {
  90          global $CFG;
  91          $options = parent::get_options();
  92          $options['file'] = $CFG->admin . '/roles/lib.php';
  93          return $options;
  94      }
  95  }