Merge branch 'MDL-81084-main' of https://github.com/andrewnicols/moodle
[moodle.git] / admin / roles / classes / admins_existing_selector.php
blob531887f0fb6807bb31b6c21f382cbec38e6d6559
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.
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/>.
17 /**
18 * Existing admin user selector.
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
25 defined('MOODLE_INTERNAL') || die();
27 require_once($CFG->dirroot.'/user/selector/lib.php');
29 class core_role_admins_existing_selector extends user_selector_base {
30 /**
31 * Create instance.
33 * @param string $name control name
34 * @param array $options should have two elements with keys groupid and courseid.
36 public function __construct($name = null, $options = array()) {
37 if (is_null($name)) {
38 $name = 'removeselect';
40 $options['includecustomfields'] = true;
41 parent::__construct($name, $options);
44 public function find_users($search) {
45 global $DB, $CFG;
47 [$wherecondition, $params] = $this->search_sql($search, 'u');
48 $params = array_merge($params, $this->userfieldsparams);
50 $fields = 'SELECT u.id, ' . $this->userfieldsselects;
52 if ($wherecondition) {
53 $wherecondition = "$wherecondition AND u.id IN ($CFG->siteadmins)";
54 } else {
55 $wherecondition = "u.id IN ($CFG->siteadmins)";
57 $sql = " FROM {user} u
58 $this->userfieldsjoin
59 WHERE $wherecondition";
61 [$sort, $sortparams] = users_order_by_sql('u', $search, $this->accesscontext, $this->userfieldsmappings);
62 $params = array_merge($params, $sortparams);
64 // Sort first by email domain and then by normal name order.
65 $order = " ORDER BY " . $DB->sql_substr('email', $DB->sql_position("'@'", 'email'),
66 $DB->sql_length('email') ) . ", $sort";
68 $availableusers = $DB->get_records_sql($fields . $sql . $order, $params);
70 if (empty($availableusers)) {
71 return array();
74 $mainadmin = array();
75 $mainadminuser = get_admin();
76 if ($mainadminuser && isset($availableusers[$mainadminuser->id])) {
77 $mainadmin = array($mainadminuser->id => $availableusers[$mainadminuser->id]);
78 unset($availableusers[$mainadminuser->id]);
81 $result = array();
82 if ($mainadmin) {
83 $result[get_string('mainadmin', 'core_role')] = $mainadmin;
86 if ($availableusers) {
87 if ($search) {
88 $groupname = get_string('extusersmatching', 'core_role', $search);
89 $result[$groupname] = $availableusers;
90 } else {
91 $groupnameprefix = get_string('extusers', 'core_role');
92 foreach ($availableusers as $user) {
93 if (isset($user->email)) {
94 $domain = substr($user->email, strpos($user->email, '@'));
95 $groupname = "$groupnameprefix $domain";
96 } else {
97 $groupname = $groupnameprefix;
99 $result[$groupname][] = $user;
104 return $result;
107 protected function get_options() {
108 global $CFG;
109 $options = parent::get_options();
110 $options['file'] = $CFG->admin . '/roles/lib.php';
111 return $options;