2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
18 * Potential admin user selector.
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_potential_selector
extends user_selector_base
{
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()) {
41 $options['includecustomfields'] = true;
42 $options['exclude'] = explode(',', $CFG->siteadmins
);
43 parent
::__construct($name, $options);
46 public function find_users($search) {
49 [$wherecondition, $params] = $this->search_sql($search, 'u');
50 $params = array_merge($params, $this->userfieldsparams
);
52 $fields = 'SELECT u.id, ' . $this->userfieldsselects
;
53 $countfields = 'SELECT COUNT(1)';
55 $sql = " FROM {user} u
57 WHERE $wherecondition AND mnethostid = :localmnet";
59 // It could be dangerous to make remote users admins and also this could lead to other problems.
60 $params['localmnet'] = $CFG->mnet_localhost_id
;
62 [$sort, $sortparams] = users_order_by_sql('u', $search, $this->accesscontext
, $this->userfieldsmappings
);
63 $order = ' ORDER BY ' . $sort;
65 // Check to see if there are too many to show sensibly.
66 if (!$this->is_validating()) {
67 $potentialcount = $DB->count_records_sql($countfields . $sql, $params);
68 if ($potentialcount > $this->maxusersperpage
) {
69 return $this->too_many_results($search, $potentialcount);
73 $availableusers = $DB->get_records_sql($fields . $sql . $order, array_merge($params, $sortparams));
75 if (empty($availableusers)) {
80 $groupname = get_string('potusersmatching', 'core_role', $search);
82 $groupname = get_string('potusers', 'core_role');
85 return array($groupname => $availableusers);
88 protected function get_options() {
90 $options = parent
::get_options();
91 $options['file'] = $CFG->admin
. '/roles/lib.php';