Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.
   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  
  25  require_once($CFG->dirroot . '/user/selector/lib.php');
  26  
  27  /*
  28   * This class displays either all the Moodle users allowed to use a service,
  29   * either all the other Moodle users.
  30   */
  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)
  37  
  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');
  44          }
  45          $this->displayallowedusers = !empty($options['displayallowedusers']);
  46      }
  47  
  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
  53       */
  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;
  60  
  61  
  62          $fields      = 'SELECT ' . $this->required_fields_sql('u');
  63          $countfields = 'SELECT COUNT(1)';
  64  
  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";
  72          }
  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)";
  79          }
  80  
  81          list($sort, $sortparams) = users_order_by_sql('u', $search, $this->accesscontext);
  82          $order = ' ORDER BY ' . $sort;
  83  
  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);
  88              }
  89          }
  90  
  91          $availableusers = $DB->get_records_sql($fields . $sql . $order, array_merge($params, $sortparams));
  92  
  93          if (empty($availableusers)) {
  94              return array();
  95          }
  96  
  97  
  98          if ($search) {
  99              $groupname = ($this->displayallowedusers) ?
 100                  get_string('serviceusersmatching', 'webservice', $search)
 101                  : get_string('potusersmatching', 'webservice', $search);
 102          }
 103          else {
 104              $groupname = ($this->displayallowedusers) ?
 105                  get_string('serviceusers', 'webservice')
 106                  : get_string('potusers', 'webservice');
 107          }
 108  
 109          return array($groupname => $availableusers);
 110      }
 111  
 112      /**
 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
 116       */
 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;
 126      }
 127  }