[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/admin/roles/classes/ -> check_users_selector.php (source)

   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 {user_enrolments} ue ON (ue.userid = u.id)
  73                        JOIN {enrol} e ON (e.id = ue.enrolid AND e.courseid = :courseid1)
  74                       WHERE $wherecondition";
  75              $params['courseid1'] = $coursecontext->instanceid;
  76  
  77              if ($this->onlyenrolled) {
  78                  $sql2 = null;
  79              } else {
  80                  $sql2 = " FROM {user} u
  81                       LEFT JOIN ({user_enrolments} ue
  82                                  JOIN {enrol} e ON (e.id = ue.enrolid AND e.courseid = :courseid2)) ON (ue.userid = u.id)
  83                           WHERE $wherecondition
  84                                 AND ue.id IS NULL";
  85                  $params['courseid2'] = $coursecontext->instanceid;
  86              }
  87  
  88          } else {
  89              if ($this->onlyenrolled) {
  90                  // Bad luck, current user may not view only enrolled users.
  91                  return array();
  92              }
  93              $sql1 = null;
  94              $sql2 = " FROM {user} u
  95                       WHERE $wherecondition";
  96          }
  97  
  98          $params['contextid'] = $this->accesscontext->id;
  99  
 100          list($sort, $sortparams) = users_order_by_sql('u', $search, $this->accesscontext);
 101          $order = ' ORDER BY ' . $sort;
 102  
 103          $result = array();
 104  
 105          if ($search) {
 106              $groupname1 = get_string('enrolledusersmatching', 'enrol', $search);
 107              $groupname2 = get_string('potusersmatching', 'core_role', $search);
 108          } else {
 109              $groupname1 = get_string('enrolledusers', 'enrol');
 110              $groupname2 = get_string('potusers', 'core_role');
 111          }
 112  
 113          if ($sql1) {
 114              $enrolleduserscount = $DB->count_records_sql($countfields . $sql1, $params);
 115              if (!$this->is_validating() and $enrolleduserscount > $this->maxusersperpage) {
 116                  $result[$groupname1] = array();
 117                  $toomany = $this->too_many_results($search, $enrolleduserscount);
 118                  $result[implode(' - ', array_keys($toomany))] = array();
 119  
 120              } else {
 121                  $enrolledusers = $DB->get_records_sql($fields . $sql1 . $order, array_merge($params, $sortparams));
 122                  if ($enrolledusers) {
 123                      $result[$groupname1] = $enrolledusers;
 124                  }
 125              }
 126              if ($sql2) {
 127                  $result[''] = array();
 128              }
 129          }
 130          if ($sql2) {
 131              $otheruserscount = $DB->count_records_sql($countfields . $sql2, $params);
 132              if (!$this->is_validating() and $otheruserscount > $this->maxusersperpage) {
 133                  $result[$groupname2] = array();
 134                  $toomany = $this->too_many_results($search, $otheruserscount);
 135                  $result[implode(' - ', array_keys($toomany))] = array();
 136              } else {
 137                  $otherusers = $DB->get_records_sql($fields . $sql2 . $order, array_merge($params, $sortparams));
 138                  if ($otherusers) {
 139                      $result[$groupname2] = $otherusers;
 140                  }
 141              }
 142          }
 143  
 144          return $result;
 145      }
 146  
 147      protected function get_options() {
 148          global $CFG;
 149          $options = parent::get_options();
 150          $options['file'] = $CFG->admin . '/roles/lib.php';
 151          return $options;
 152      }
 153  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1