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.

Differences Between: [Versions 311 and 400] [Versions 311 and 401] [Versions 311 and 402] [Versions 311 and 403]

   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   * Existing user selector.
  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 users who already have the role in
  29   * question on the assign roles page.
  30   */
  31  class core_role_existing_role_holders extends core_role_assign_user_selector_base {
  32  
  33      public function find_users($search) {
  34          global $DB;
  35  
  36          list($wherecondition, $params) = $this->search_sql($search, 'u');
  37          list($ctxcondition, $ctxparams) = $DB->get_in_or_equal($this->context->get_parent_context_ids(true), SQL_PARAMS_NAMED, 'ctx');
  38          $params = array_merge($params, $ctxparams);
  39          $params['roleid'] = $this->roleid;
  40  
  41          list($sort, $sortparams) = users_order_by_sql('u', $search, $this->accesscontext);
  42          $params = array_merge($params, $sortparams);
  43  
  44          $fields = "SELECT ra.id AS raid," . $this->required_fields_sql('u') . ",ra.contextid,ra.component ";
  45          $countfields = "SELECT COUNT(1) ";
  46          $sql = "FROM {role_assignments} ra
  47                    JOIN {user} u ON u.id = ra.userid
  48                    JOIN {context} ctx ON ra.contextid = ctx.id
  49                   WHERE $wherecondition
  50                         AND ctx.id $ctxcondition
  51                         AND ra.roleid = :roleid";
  52           $order = " ORDER BY ctx.depth DESC, ra.component, $sort";
  53  
  54          if (!$this->is_validating()) {
  55              $existinguserscount = $DB->count_records_sql($countfields . $sql, $params);
  56              if ($existinguserscount > $this->maxusersperpage) {
  57                  return $this->too_many_results($search, $existinguserscount);
  58              }
  59          }
  60  
  61          $contextusers = $DB->get_records_sql($fields . $sql . $order, $params);
  62  
  63          // No users at all.
  64          if (empty($contextusers)) {
  65              return array();
  66          }
  67  
  68          // We have users. Out put them in groups by context depth.
  69          // To help the loop below, tack a dummy user on the end of the results
  70          // array, to trigger output of the last group.
  71          $dummyuser = new stdClass;
  72          $dummyuser->contextid = 0;
  73          $dummyuser->id = 0;
  74          $dummyuser->component = '';
  75          $contextusers[] = $dummyuser;
  76          $results = array(); // The results array we are building up.
  77          $doneusers = array(); // Ensures we only list each user at most once.
  78          $currentcontextid = $this->context->id;
  79          $currentgroup = array();
  80          foreach ($contextusers as $user) {
  81              if (isset($doneusers[$user->id])) {
  82                  continue;
  83              }
  84              $doneusers[$user->id] = 1;
  85              if ($user->contextid != $currentcontextid) {
  86                  // We have got to the end of the previous group. Add it to the results array.
  87                  if ($currentcontextid == $this->context->id) {
  88                      $groupname = $this->this_con_group_name($search, count($currentgroup));
  89                  } else {
  90                      $groupname = $this->parent_con_group_name($search, $currentcontextid);
  91                  }
  92                  $results[$groupname] = $currentgroup;
  93                  // Get ready for the next group.
  94                  $currentcontextid = $user->contextid;
  95                  $currentgroup = array();
  96              }
  97              // Add this user to the group we are building up.
  98              unset($user->contextid);
  99              if ($currentcontextid != $this->context->id) {
 100                  $user->disabled = true;
 101              }
 102              if ($user->component !== '') {
 103                  // Bad luck, you can tweak only manual role assignments.
 104                  $user->disabled = true;
 105              }
 106              unset($user->component);
 107              $currentgroup[$user->id] = $user;
 108          }
 109  
 110          return $results;
 111      }
 112  
 113      protected function this_con_group_name($search, $numusers) {
 114          if ($this->context->contextlevel == CONTEXT_SYSTEM) {
 115              // Special case in the System context.
 116              if ($search) {
 117                  return get_string('extusersmatching', 'core_role', $search);
 118              } else {
 119                  return get_string('extusers', 'core_role');
 120              }
 121          }
 122          $contexttype = context_helper::get_level_name($this->context->contextlevel);
 123          if ($search) {
 124              $a = new stdClass;
 125              $a->search = $search;
 126              $a->contexttype = $contexttype;
 127              if ($numusers) {
 128                  return get_string('usersinthisxmatching', 'core_role', $a);
 129              } else {
 130                  return get_string('noneinthisxmatching', 'core_role', $a);
 131              }
 132          } else {
 133              if ($numusers) {
 134                  return get_string('usersinthisx', 'core_role', $contexttype);
 135              } else {
 136                  return get_string('noneinthisx', 'core_role', $contexttype);
 137              }
 138          }
 139      }
 140  
 141      protected function parent_con_group_name($search, $contextid) {
 142          $context = context::instance_by_id($contextid);
 143          $contextname = $context->get_context_name(true, true);
 144          if ($search) {
 145              $a = new stdClass;
 146              $a->contextname = $contextname;
 147              $a->search = $search;
 148              return get_string('usersfrommatching', 'core_role', $a);
 149          } else {
 150              return get_string('usersfrom', 'core_role', $contextname);
 151          }
 152      }
 153  }