MDL-52378 Singleview: Exclude Grade Attribute Permissions
[moodle.git] / admin / webservice / lib.php
blobb135d0f9bb33c7746d91bb1b819938ee2504f309
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 * Web services admin library
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
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 protected $serviceid;
33 protected $displayallowedusers; //set to true if the selector displays the
34 //allowed users on this service
35 //, set to false if the selector displays the
36 // other users (false is the default default)
38 public function __construct($name, $options) {
39 parent::__construct($name, $options);
40 if (!empty($options['serviceid'])) {
41 $this->serviceid = $options['serviceid'];
42 } else {
43 throw new moodle_exception('serviceidnotfound');
45 $this->displayallowedusers = !empty($options['displayallowedusers']);
48 /**
49 * Find allowed or not allowed users of a service (depend of $this->displayallowedusers)
50 * @global object $DB
51 * @param <type> $search
52 * @return array
54 public function find_users($search) {
55 global $DB;
56 //by default wherecondition retrieves all users except the deleted, not
57 //confirmed and guest
58 list($wherecondition, $params) = $this->search_sql($search, 'u');
59 $params['serviceid'] = $this->serviceid;
62 $fields = 'SELECT ' . $this->required_fields_sql('u');
63 $countfields = 'SELECT COUNT(1)';
65 if ($this->displayallowedusers) {
66 ///the following SQL retrieve all users that are allowed to the serviceid
67 $sql = " FROM {user} u, {external_services_users} esu
68 WHERE $wherecondition
69 AND u.deleted = 0
70 AND esu.userid = u.id
71 AND esu.externalserviceid = :serviceid";
73 else {
74 ///the following SQL retrieve all users that are not allowed to the serviceid
75 $sql = " FROM {user} u WHERE $wherecondition AND u.deleted = 0
76 AND NOT EXISTS (SELECT esu.userid FROM {external_services_users} esu
77 WHERE esu.externalserviceid = :serviceid
78 AND esu.userid = u.id)";
81 list($sort, $sortparams) = users_order_by_sql('u', $search, $this->accesscontext);
82 $order = ' ORDER BY ' . $sort;
84 if (!$this->is_validating()) {
85 $potentialmemberscount = $DB->count_records_sql($countfields . $sql, $params);
86 if ($potentialmemberscount > $this->maxusersperpage) {
87 return $this->too_many_results($search, $potentialmemberscount);
91 $availableusers = $DB->get_records_sql($fields . $sql . $order, array_merge($params, $sortparams));
93 if (empty($availableusers)) {
94 return array();
98 if ($search) {
99 $groupname = ($this->displayallowedusers) ?
100 get_string('serviceusersmatching', 'webservice', $search)
101 : get_string('potusersmatching', 'webservice', $search);
103 else {
104 $groupname = ($this->displayallowedusers) ?
105 get_string('serviceusers', 'webservice')
106 : get_string('potusers', 'webservice');
109 return array($groupname => $availableusers);
113 * This options are automatically used by the AJAX search
114 * @global object $CFG
115 * @return object options pass to the constructor when AJAX search call a new selector
117 protected function get_options() {
118 global $CFG;
119 $options = parent::get_options();
120 $options['file'] = $CFG->admin.'/webservice/lib.php'; //need to be set, otherwise
121 // the /user/selector/search.php
122 //will fail to find this user_selector class
123 $options['serviceid'] = $this->serviceid;
124 $options['displayallowedusers'] = $this->displayallowedusers;
125 return $options;