Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 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          parent::__construct($name, $options);
  50  
  51          $coursecontext = $this->accesscontext->get_course_context(false);
  52          if ($coursecontext and $coursecontext->id != SITEID and !has_capability('moodle/role:manage', $coursecontext)) {
  53              // Prevent normal teachers from looking up all users.
  54              $this->onlyenrolled = true;
  55          } else {
  56              $this->onlyenrolled = false;
  57          }
  58      }
  59  
  60      public function find_users($search) {
  61          global $DB;
  62  
  63          list($wherecondition, $params) = $this->search_sql($search, 'u');
  64  
  65          $fields      = 'SELECT ' . $this->required_fields_sql('u');
  66          $countfields = 'SELECT COUNT(1)';
  67  
  68          $coursecontext = $this->accesscontext->get_course_context(false);
  69  
  70          if ($coursecontext and $coursecontext != SITEID) {
  71              $sql1 = " FROM {user} u
  72                        JOIN (SELECT DISTINCT subu.id
  73                                FROM {user} subu
  74                                JOIN {user_enrolments} ue ON (ue.userid = subu.id)
  75                                JOIN {enrol} e ON (e.id = ue.enrolid AND e.courseid = :courseid1)
  76                             ) subq ON subq.id = u.id
  77                       WHERE $wherecondition";
  78              $params['courseid1'] = $coursecontext->instanceid;
  79  
  80              if ($this->onlyenrolled) {
  81                  $sql2 = null;
  82              } else {
  83                  $sql2 = " FROM {user} u
  84                       LEFT JOIN ({user_enrolments} ue
  85                                  JOIN {enrol} e ON (e.id = ue.enrolid AND e.courseid = :courseid2)) ON (ue.userid = u.id)
  86                           WHERE $wherecondition
  87                                 AND ue.id IS NULL";
  88                  $params['courseid2'] = $coursecontext->instanceid;
  89              }
  90  
  91          } else {
  92              if ($this->onlyenrolled) {
  93                  // Bad luck, current user may not view only enrolled users.
  94                  return array();
  95              }
  96              $sql1 = null;
  97              $sql2 = " FROM {user} u
  98                       WHERE $wherecondition";
  99          }
 100  
 101          $params['contextid'] = $this->accesscontext->id;
 102  
 103          list($sort, $sortparams) = users_order_by_sql('u', $search, $this->accesscontext);
 104          $order = ' ORDER BY ' . $sort;
 105  
 106          $result = array();
 107  
 108          if ($search) {
 109              $groupname1 = get_string('enrolledusersmatching', 'enrol', $search);
 110              $groupname2 = get_string('potusersmatching', 'core_role', $search);
 111          } else {
 112              $groupname1 = get_string('enrolledusers', 'enrol');
 113              $groupname2 = get_string('potusers', 'core_role');
 114          }
 115  
 116          if ($sql1) {
 117              $enrolleduserscount = $DB->count_records_sql($countfields . $sql1, $params);
 118              if (!$this->is_validating() and $enrolleduserscount > $this->maxusersperpage) {
 119                  $result[$groupname1] = array();
 120                  $toomany = $this->too_many_results($search, $enrolleduserscount);
 121                  $result[implode(' - ', array_keys($toomany))] = array();
 122  
 123              } else {
 124                  $enrolledusers = $DB->get_records_sql($fields . $sql1 . $order, array_merge($params, $sortparams));
 125                  if ($enrolledusers) {
 126                      $result[$groupname1] = $enrolledusers;
 127                  }
 128              }
 129              if ($sql2) {
 130                  $result[''] = array();
 131              }
 132          }
 133          if ($sql2) {
 134              $otheruserscount = $DB->count_records_sql($countfields . $sql2, $params);
 135              if (!$this->is_validating() and $otheruserscount > $this->maxusersperpage) {
 136                  $result[$groupname2] = array();
 137                  $toomany = $this->too_many_results($search, $otheruserscount);
 138                  $result[implode(' - ', array_keys($toomany))] = array();
 139              } else {
 140                  $otherusers = $DB->get_records_sql($fields . $sql2 . $order, array_merge($params, $sortparams));
 141                  if ($otherusers) {
 142                      $result[$groupname2] = $otherusers;
 143                  }
 144              }
 145          }
 146  
 147          return $result;
 148      }
 149  
 150      protected function get_options() {
 151          global $CFG;
 152          $options = parent::get_options();
 153          $options['file'] = $CFG->admin . '/roles/lib.php';
 154          return $options;
 155      }
 156  }