web service MDL-21351 list of selectabl users should not include deleted user
[moodle.git] / admin / webservice / lib.php
CommitLineData
772db632 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 * Web services admin library
19 *
20 * @package webservice
21 * @copyright 2009 Moodle Pty Ltd (http://moodle.com)
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25require_once($CFG->dirroot . '/user/selector/lib.php');
26
da42e890 27/*
28* This class displays either all the Moodle users allowed to use a service,
29* either all the other Moodle users.
30 */
31class service_user_selector extends user_selector_base {
772db632 32 const MAX_USERS_PER_PAGE = 100;
33
34 protected $serviceid;
bb1dff21 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)
772db632 39
da42e890 40 public function __construct($name, $options) {
772db632 41 parent::__construct($name, $options);
42 if (!empty($options['serviceid'])) {
43 $this->serviceid = $options['serviceid'];
da42e890 44 }
45 else {
772db632 46 throw new moodle_exception('serviceidnotfound');
47 }
da42e890 48 $this->displayallowedusers = !empty($options['displayallowedusers']);
772db632 49 }
50
bb1dff21 51 /**
52 * Find allowed or not allowed users of a service (depend of $this->displayallowedusers)
53 * @global object $DB
54 * @param <type> $search
55 * @return array
56 */
772db632 57 public function find_users($search) {
58 global $DB;
da42e890 59 //by default wherecondition retrieves all users except the deleted, not
60 //confirmed and guest
0df0df23 61 list($wherecondition, $params) = $this->search_sql($search, 'u');
4f0c2d00 62 $params['serviceid'] = $this->serviceid;
772db632 63
da42e890 64
772db632 65 $fields = 'SELECT ' . $this->required_fields_sql('u');
66 $countfields = 'SELECT COUNT(1)';
67
da42e890 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
772db632 71 WHERE $wherecondition
cddd2527 72 AND u.deleted = 0
772db632 73 AND esu.userid = u.id
4f0c2d00 74 AND esu.externalserviceid = :serviceid";
772db632 75 }
76 else {
da42e890 77 ///the following SQL retrieve all users that are not allowed to the serviceid
cddd2527 78 $sql = " FROM {user} u WHERE $wherecondition AND u.deleted = 0
772db632 79 AND NOT EXISTS (SELECT esu.userid FROM {external_services_users} esu
4f0c2d00 80 WHERE esu.externalserviceid = :serviceid
772db632 81 AND esu.userid = u.id)";
da42e890 82 }
83
84 $order = ' ORDER BY u.lastname ASC, u.firstname ASC';
772db632 85
86 if (!$this->is_validating()) {
87 $potentialmemberscount = $DB->count_records_sql($countfields . $sql, $params);
da42e890 88 if ($potentialmemberscount > service_user_selector::MAX_USERS_PER_PAGE) {
772db632 89 return $this->too_many_results($search, $potentialmemberscount);
90 }
91 }
92
93 $availableusers = $DB->get_records_sql($fields . $sql . $order, $params);
94
95 if (empty($availableusers)) {
96 return array();
97 }
98
da42e890 99
772db632 100 if ($search) {
da42e890 101 $groupname = ($this->displayallowedusers) ?
102 get_string('serviceusersmatching', 'webservice', $search)
103 : get_string('potusersmatching', 'webservice', $search);
104 }
105 else {
106 $groupname = ($this->displayallowedusers) ?
107 get_string('serviceusers', 'webservice')
108 : get_string('potusers', 'webservice');
772db632 109 }
110
111 return array($groupname => $availableusers);
112 }
113
bb1dff21 114 /**
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
118 */
772db632 119 protected function get_options() {
120 global $CFG;
121 $options = parent::get_options();
72977b37 122 $options['file'] = $CFG->admin.'/webservice/lib.php'; //need to be set, otherwise
da42e890 123 // the /user/selector/search.php
772db632 124 //will fail to find this user_selector class
125 $options['serviceid'] = $this->serviceid;
da42e890 126 $options['displayallowedusers'] = $this->displayallowedusers;
772db632 127 return $options;
128 }
129}