Search moodle.org's
Developer Documentation

See Release Notes

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

Differences Between: [Versions 310 and 402] [Versions 311 and 402] [Versions 39 and 402]

   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, $this->userfieldsparams);
  43  
  44          if ($wherecondition) {
  45              $wherecondition = ' AND ' . $wherecondition;
  46          }
  47  
  48          $fields      = 'SELECT u.id, ' . $this->userfieldsselects;
  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                          $this->userfieldsjoin
  56                    WHERE ra.id IS NULL
  57                          $wherecondition";
  58          $params['contextid'] = $this->context->id;
  59          $params['roleid'] = $this->roleid;
  60  
  61          list($sort, $sortparams) = users_order_by_sql('u', $search, $this->accesscontext, $this->userfieldsmappings);
  62          $order = ' ORDER BY ' . $sort;
  63  
  64          // Check to see if there are too many to show sensibly.
  65          if (!$this->is_validating()) {
  66              $potentialmemberscount = $DB->count_records_sql($countfields . $sql, $params);
  67              if ($potentialmemberscount > $this->maxusersperpage) {
  68                  return $this->too_many_results($search, $potentialmemberscount);
  69              }
  70          }
  71  
  72          // If not, show them.
  73          $availableusers = $DB->get_records_sql($fields . $sql . $order, array_merge($params, $sortparams));
  74  
  75          if (empty($availableusers)) {
  76              return array();
  77          }
  78  
  79          if ($search) {
  80              $groupname = get_string('potusersmatching', 'core_role', $search);
  81          } else {
  82              $groupname = get_string('potusers', 'core_role');
  83          }
  84  
  85          return array($groupname => $availableusers);
  86      }
  87  }