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   * 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 below the course level. (CONTEXT_MODULE and
  30   * some CONTEXT_BLOCK).
  31   *
  32   * This returns only enrolled users in this context.
  33   */
  34  class core_role_potential_assignees_below_course extends core_role_assign_user_selector_base {
  35      public function find_users($search) {
  36          global $DB;
  37  
  38          list($enrolsql, $eparams) = get_enrolled_sql($this->context);
  39  
  40          // Now we have to go to the database.
  41          list($wherecondition, $params) = $this->search_sql($search, 'u');
  42          $params = array_merge($params, $eparams);
  43  
  44          if ($wherecondition) {
  45              $wherecondition = ' AND ' . $wherecondition;
  46          }
  47  
  48          $fields      = 'SELECT ' . $this->required_fields_sql('u');
  49          $countfields = 'SELECT COUNT(u.id)';
  50  
  51          $sql   = " FROM ($enrolsql) enrolled_users_view
  52                     JOIN {user} u ON u.id = enrolled_users_view.id
  53                LEFT JOIN {role_assignments} ra ON (ra.userid = enrolled_users_view.id AND
  54                                              ra.roleid = :roleid AND ra.contextid = :contextid)
  55                    WHERE ra.id IS NULL
  56                          $wherecondition";
  57          $params['contextid'] = $this->context->id;
  58          $params['roleid'] = $this->roleid;
  59  
  60          list($sort, $sortparams) = users_order_by_sql('u', $search, $this->accesscontext);
  61          $order = ' ORDER BY ' . $sort;
  62  
  63          // Check to see if there are too many to show sensibly.
  64          if (!$this->is_validating()) {
  65              $potentialmemberscount = $DB->count_records_sql($countfields . $sql, $params);
  66              if ($potentialmemberscount > $this->maxusersperpage) {
  67                  return $this->too_many_results($search, $potentialmemberscount);
  68              }
  69          }
  70  
  71          // If not, show them.
  72          $availableusers = $DB->get_records_sql($fields . $sql . $order, array_merge($params, $sortparams));
  73  
  74          if (empty($availableusers)) {
  75              return array();
  76          }
  77  
  78          if ($search) {
  79              $groupname = get_string('potusersmatching', 'core_role', $search);
  80          } else {
  81              $groupname = get_string('potusers', 'core_role');
  82          }
  83  
  84          return array($groupname => $availableusers);
  85      }
  86  }