|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 // This file is part of Moodle - http://moodle.org/ 00003 // 00004 // Moodle is free software: you can redistribute it and/or modify 00005 // it under the terms of the GNU General Public License as published by 00006 // the Free Software Foundation, either version 3 of the License, or 00007 // (at your option) any later version. 00008 // 00009 // Moodle is distributed in the hope that it will be useful, 00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 // GNU General Public License for more details. 00013 // 00014 // You should have received a copy of the GNU General Public License 00015 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 00016 00025 require_once($CFG->dirroot . '/user/selector/lib.php'); 00026 00027 /* 00028 * This class displays either all the Moodle users allowed to use a service, 00029 * either all the other Moodle users. 00030 */ 00031 class service_user_selector extends user_selector_base { 00032 const MAX_USERS_PER_PAGE = 100; 00033 00034 protected $serviceid; 00035 protected $displayallowedusers; //set to true if the selector displays the 00036 //allowed users on this service 00037 //, set to false if the selector displays the 00038 // other users (false is the default default) 00039 00040 public function __construct($name, $options) { 00041 parent::__construct($name, $options); 00042 if (!empty($options['serviceid'])) { 00043 $this->serviceid = $options['serviceid']; 00044 } 00045 else { 00046 throw new moodle_exception('serviceidnotfound'); 00047 } 00048 $this->displayallowedusers = !empty($options['displayallowedusers']); 00049 } 00050 00057 public function find_users($search) { 00058 global $DB; 00059 //by default wherecondition retrieves all users except the deleted, not 00060 //confirmed and guest 00061 list($wherecondition, $params) = $this->search_sql($search, 'u'); 00062 $params['serviceid'] = $this->serviceid; 00063 00064 00065 $fields = 'SELECT ' . $this->required_fields_sql('u'); 00066 $countfields = 'SELECT COUNT(1)'; 00067 00068 if ($this->displayallowedusers) { 00070 $sql = " FROM {user} u, {external_services_users} esu 00071 WHERE $wherecondition 00072 AND u.deleted = 0 00073 AND esu.userid = u.id 00074 AND esu.externalserviceid = :serviceid"; 00075 } 00076 else { 00078 $sql = " FROM {user} u WHERE $wherecondition AND u.deleted = 0 00079 AND NOT EXISTS (SELECT esu.userid FROM {external_services_users} esu 00080 WHERE esu.externalserviceid = :serviceid 00081 AND esu.userid = u.id)"; 00082 } 00083 00084 $order = ' ORDER BY u.lastname ASC, u.firstname ASC'; 00085 00086 if (!$this->is_validating()) { 00087 $potentialmemberscount = $DB->count_records_sql($countfields . $sql, $params); 00088 if ($potentialmemberscount > service_user_selector::MAX_USERS_PER_PAGE) { 00089 return $this->too_many_results($search, $potentialmemberscount); 00090 } 00091 } 00092 00093 $availableusers = $DB->get_records_sql($fields . $sql . $order, $params); 00094 00095 if (empty($availableusers)) { 00096 return array(); 00097 } 00098 00099 00100 if ($search) { 00101 $groupname = ($this->displayallowedusers) ? 00102 get_string('serviceusersmatching', 'webservice', $search) 00103 : get_string('potusersmatching', 'webservice', $search); 00104 } 00105 else { 00106 $groupname = ($this->displayallowedusers) ? 00107 get_string('serviceusers', 'webservice') 00108 : get_string('potusers', 'webservice'); 00109 } 00110 00111 return array($groupname => $availableusers); 00112 } 00113 00119 protected function get_options() { 00120 global $CFG; 00121 $options = parent::get_options(); 00122 $options['file'] = $CFG->admin.'/webservice/lib.php'; //need to be set, otherwise 00123 // the /user/selector/search.php 00124 //will fail to find this user_selector class 00125 $options['serviceid'] = $this->serviceid; 00126 $options['displayallowedusers'] = $this->displayallowedusers; 00127 return $options; 00128 } 00129 }