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   * Existing 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_existing_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          if (is_null($name)) {
  38              $name = 'removeselect';
  39          }
  40          $options['multiselect'] = false;
  41          $options['includecustomfields'] = true;
  42          parent::__construct($name, $options);
  43      }
  44  
  45      public function find_users($search) {
  46          global $DB, $CFG;
  47  
  48          [$wherecondition, $params] = $this->search_sql($search, 'u');
  49          $params = array_merge($params, $this->userfieldsparams);
  50  
  51          $fields = 'SELECT u.id, ' . $this->userfieldsselects;
  52  
  53          if ($wherecondition) {
  54              $wherecondition = "$wherecondition AND u.id IN ($CFG->siteadmins)";
  55          } else {
  56              $wherecondition = "u.id IN ($CFG->siteadmins)";
  57          }
  58          $sql = " FROM {user} u
  59                        $this->userfieldsjoin
  60                  WHERE $wherecondition";
  61  
  62          [$sort, $sortparams] = users_order_by_sql('u', $search, $this->accesscontext, $this->userfieldsmappings);
  63          $params = array_merge($params, $sortparams);
  64  
  65          // Sort first by email domain and then by normal name order.
  66          $order = " ORDER BY " . $DB->sql_substr('email', $DB->sql_position("'@'", 'email'),
  67              $DB->sql_length('email') ) .  ", $sort";
  68  
  69          $availableusers = $DB->get_records_sql($fields . $sql . $order, $params);
  70  
  71          if (empty($availableusers)) {
  72              return array();
  73          }
  74  
  75          $mainadmin = array();
  76          $mainadminuser = get_admin();
  77          if ($mainadminuser && isset($availableusers[$mainadminuser->id])) {
  78              $mainadmin = array($mainadminuser->id => $availableusers[$mainadminuser->id]);
  79              unset($availableusers[$mainadminuser->id]);
  80          }
  81  
  82          $result = array();
  83          if ($mainadmin) {
  84              $result[get_string('mainadmin', 'core_role')] = $mainadmin;
  85          }
  86  
  87          if ($availableusers) {
  88              if ($search) {
  89                  $groupname = get_string('extusersmatching', 'core_role', $search);
  90                  $result[$groupname] = $availableusers;
  91              } else {
  92                  $groupnameprefix = get_string('extusers', 'core_role');
  93                  foreach ($availableusers as $user) {
  94                      if (isset($user->email)) {
  95                          $domain = substr($user->email, strpos($user->email, '@'));
  96                          $groupname = "$groupnameprefix $domain";
  97                      } else {
  98                          $groupname = $groupnameprefix;
  99                      }
 100                      $result[$groupname][] = $user;
 101                  }
 102              }
 103          }
 104  
 105          return $result;
 106      }
 107  
 108      protected function get_options() {
 109          global $CFG;
 110          $options = parent::get_options();
 111          $options['file'] = $CFG->admin . '/roles/lib.php';
 112          return $options;
 113      }
 114  }