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 * Web services admin library
21 * @copyright 2009 Moodle Pty Ltd (http://moodle.com)
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 require_once($CFG->dirroot
. '/user/selector/lib.php');
28 * This class displays either all the Moodle users allowed to use a service,
29 * either all the other Moodle users.
31 class service_user_selector
extends user_selector_base
{
32 const MAX_USERS_PER_PAGE
= 100;
35 protected $displayallowedusers; //set to true if the selector displays the
36 //allowed users on this service
37 //, set to false if the selector displays the
38 // other users (false is the default default)
40 public function __construct($name, $options) {
41 parent
::__construct($name, $options);
42 if (!empty($options['serviceid'])) {
43 $this->serviceid
= $options['serviceid'];
46 throw new moodle_exception('serviceidnotfound');
48 $this->displayallowedusers
= !empty($options['displayallowedusers']);
52 * Find allowed or not allowed users of a service (depend of $this->displayallowedusers)
54 * @param <type> $search
57 public function find_users($search) {
59 //by default wherecondition retrieves all users except the deleted, not
61 list($wherecondition, $params) = $this->search_sql($search, 'u');
62 $params['serviceid'] = $this->serviceid
;
65 $fields = 'SELECT ' . $this->required_fields_sql('u');
66 $countfields = 'SELECT COUNT(1)';
68 if ($this->displayallowedusers
) {
69 ///the following SQL retrieve all users that are allowed to the serviceid
70 $sql = " FROM {user} u, {external_services_users} esu
74 AND esu.externalserviceid = :serviceid";
77 ///the following SQL retrieve all users that are not allowed to the serviceid
78 $sql = " FROM {user} u WHERE $wherecondition AND u.deleted = 0
79 AND NOT EXISTS (SELECT esu.userid FROM {external_services_users} esu
80 WHERE esu.externalserviceid = :serviceid
81 AND esu.userid = u.id)";
84 $order = ' ORDER BY u.lastname ASC, u.firstname ASC';
86 if (!$this->is_validating()) {
87 $potentialmemberscount = $DB->count_records_sql($countfields . $sql, $params);
88 if ($potentialmemberscount > service_user_selector
::MAX_USERS_PER_PAGE
) {
89 return $this->too_many_results($search, $potentialmemberscount);
93 $availableusers = $DB->get_records_sql($fields . $sql . $order, $params);
95 if (empty($availableusers)) {
101 $groupname = ($this->displayallowedusers
) ?
102 get_string('serviceusersmatching', 'webservice', $search)
103 : get_string('potusersmatching', 'webservice', $search);
106 $groupname = ($this->displayallowedusers
) ?
107 get_string('serviceusers', 'webservice')
108 : get_string('potusers', 'webservice');
111 return array($groupname => $availableusers);
115 * This options are automatically used by the AJAX search
116 * @global object $CFG
117 * @return object options pass to the constructor when AJAX search call a new selector
119 protected function get_options() {
121 $options = parent
::get_options();
122 $options['file'] = $CFG->admin
.'/webservice/lib.php'; //need to be set, otherwise
123 // the /user/selector/search.php
124 //will fail to find this user_selector class
125 $options['serviceid'] = $this->serviceid
;
126 $options['displayallowedusers'] = $this->displayallowedusers
;