Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

Differences Between: [Versions 310 and 403] [Versions 311 and 403] [Versions 39 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   * 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  require_once($CFG->dirroot.'/user/selector/lib.php');
  28  
  29  /**
  30   * User selector subclass for the selection of users in the check permissions page.
  31   *
  32   * @copyright 2012 Petr Skoda {@link http://skodak.org}
  33   */
  34  class core_role_check_users_selector extends user_selector_base {
  35      /** @var bool limit listing of users to enrolled only */
  36      protected $onlyenrolled;
  37  
  38      /**
  39       * Constructor.
  40       *
  41       * @param string $name the control name/id for use in the HTML.
  42       * @param array $options other options needed to construct this selector.
  43       * You must be able to clone a userselector by doing new get_class($us)($us->get_name(), $us->get_options());
  44       */
  45      public function __construct($name, $options) {
  46          if (!isset($options['multiselect'])) {
  47              $options['multiselect'] = false;
  48          }
  49          $options['includecustomfields'] = true;
  50          parent::__construct($name, $options);
  51  
  52          $coursecontext = $this->accesscontext->get_course_context(false);
  53          if ($coursecontext and $coursecontext->id != SITEID and !has_capability('moodle/role:manage', $coursecontext)) {
  54              // Prevent normal teachers from looking up all users.
  55              $this->onlyenrolled = true;
  56          } else {
  57              $this->onlyenrolled = false;
  58          }
  59      }
  60  
  61      public function find_users($search) {
  62          global $DB;
  63  
  64          list($wherecondition, $params) = $this->search_sql($search, 'u');
  65          $params = array_merge($params, $this->userfieldsparams);
  66  
  67          $fields      = 'SELECT u.id, ' . $this->userfieldsselects;
  68          $countfields = 'SELECT COUNT(1)';
  69  
  70          $coursecontext = $this->accesscontext->get_course_context(false);
  71  
  72          if ($coursecontext and $coursecontext != SITEID) {
  73              $sql1 = " FROM {user} u
  74                        JOIN (SELECT DISTINCT subu.id
  75                                FROM {user} subu
  76                                JOIN {user_enrolments} ue ON (ue.userid = subu.id)
  77                                JOIN {enrol} e ON (e.id = ue.enrolid AND e.courseid = :courseid1)
  78                             ) subq ON subq.id = u.id
  79                             $this->userfieldsjoin
  80                       WHERE $wherecondition";
  81              $params['courseid1'] = $coursecontext->instanceid;
  82  
  83              if ($this->onlyenrolled) {
  84                  $sql2 = null;
  85              } else {
  86                  $sql2 = " FROM {user} u
  87                       LEFT JOIN ({user_enrolments} ue
  88                                  JOIN {enrol} e ON (e.id = ue.enrolid AND e.courseid = :courseid2)) ON (ue.userid = u.id)
  89                                 $this->userfieldsjoin
  90                           WHERE $wherecondition
  91                                 AND ue.id IS NULL";
  92                  $params['courseid2'] = $coursecontext->instanceid;
  93              }
  94  
  95          } else {
  96              if ($this->onlyenrolled) {
  97                  // Bad luck, current user may not view only enrolled users.
  98                  return array();
  99              }
 100              $sql1 = null;
 101              $sql2 = " FROM {user} u
 102                             $this->userfieldsjoin
 103                       WHERE $wherecondition";
 104          }
 105  
 106          $params['contextid'] = $this->accesscontext->id;
 107  
 108          list($sort, $sortparams) = users_order_by_sql('u', $search, $this->accesscontext, $this->userfieldsmappings);
 109          $order = ' ORDER BY ' . $sort;
 110  
 111          $result = array();
 112  
 113          if ($search) {
 114              $groupname1 = get_string('enrolledusersmatching', 'enrol', $search);
 115              $groupname2 = get_string('potusersmatching', 'core_role', $search);
 116          } else {
 117              $groupname1 = get_string('enrolledusers', 'enrol');
 118              $groupname2 = get_string('potusers', 'core_role');
 119          }
 120  
 121          if ($sql1) {
 122              $enrolleduserscount = $DB->count_records_sql($countfields . $sql1, $params);
 123              if (!$this->is_validating() and $enrolleduserscount > $this->maxusersperpage) {
 124                  $result[$groupname1] = array();
 125                  $toomany = $this->too_many_results($search, $enrolleduserscount);
 126                  $result[implode(' - ', array_keys($toomany))] = array();
 127  
 128              } else {
 129                  $enrolledusers = $DB->get_records_sql($fields . $sql1 . $order, array_merge($params, $sortparams));
 130                  if ($enrolledusers) {
 131                      $result[$groupname1] = $enrolledusers;
 132                  }
 133              }
 134              if ($sql2) {
 135                  $result[''] = array();
 136              }
 137          }
 138          if ($sql2) {
 139              $otheruserscount = $DB->count_records_sql($countfields . $sql2, $params);
 140              if (!$this->is_validating() and $otheruserscount > $this->maxusersperpage) {
 141                  $result[$groupname2] = array();
 142                  $toomany = $this->too_many_results($search, $otheruserscount);
 143                  $result[implode(' - ', array_keys($toomany))] = array();
 144              } else {
 145                  $otherusers = $DB->get_records_sql($fields . $sql2 . $order, array_merge($params, $sortparams));
 146                  if ($otherusers) {
 147                      $result[$groupname2] = $otherusers;
 148                  }
 149              }
 150          }
 151  
 152          return $result;
 153      }
 154  
 155      protected function get_options() {
 156          global $CFG;
 157          $options = parent::get_options();
 158          $options['file'] = $CFG->admin . '/roles/lib.php';
 159          return $options;
 160      }
 161  }