Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [Versions 311 and 401] [Versions 39 and 401]

   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   * Library code used by the roles administration interfaces.
  19   *
  20   * @package    core_role
  21   * @copyright  1999 onwards Martin Dougiamas (http://dougiamas.com)
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  /**
  28   * User selector subclass for the list of potential users on the assign roles page,
  29   * when we are assigning in a context at or above the course level. In this case we
  30   * show all the users in the system who do not already have the role.
  31   */
  32  class core_role_potential_assignees_course_and_above extends core_role_assign_user_selector_base {
  33      public function find_users($search) {
  34          global $DB;
  35  
  36          list($wherecondition, $params) = $this->search_sql($search, 'u');
  37          $params = array_merge($params, $this->userfieldsparams);
  38  
  39          $fields      = 'SELECT u.id, ' . $this->userfieldsselects;
  40          $countfields = 'SELECT COUNT(1)';
  41  
  42          $sql = " FROM {user} u
  43                        $this->userfieldsjoin
  44                  WHERE $wherecondition
  45                        AND u.id NOT IN (
  46                           SELECT r.userid
  47                             FROM {role_assignments} r
  48                            WHERE r.contextid = :contextid
  49                                  AND r.roleid = :roleid)";
  50  
  51          list($sort, $sortparams) = users_order_by_sql('', $search, $this->accesscontext, $this->userfieldsmappings);
  52          $order = ' ORDER BY ' . $sort;
  53  
  54          $params['contextid'] = $this->context->id;
  55          $params['roleid'] = $this->roleid;
  56  
  57          if (!$this->is_validating()) {
  58              $potentialmemberscount = $DB->count_records_sql($countfields . $sql, $params);
  59              if ($potentialmemberscount > $this->maxusersperpage) {
  60                  return $this->too_many_results($search, $potentialmemberscount);
  61              }
  62          }
  63  
  64          $availableusers = $DB->get_records_sql($fields . $sql . $order, array_merge($params, $sortparams));
  65  
  66          if (empty($availableusers)) {
  67              return array();
  68          }
  69  
  70          if ($search) {
  71              $groupname = get_string('potusersmatching', 'core_role', $search);
  72          } else {
  73              $groupname = get_string('potusers', 'core_role');
  74          }
  75  
  76          return array($groupname => $availableusers);
  77      }
  78  }