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] [Versions 400 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   * Code to search for users in response to an ajax call from a user selector.
  19   *
  20   * @package core_user
  21   * @copyright 1999 Martin Dougiamas  http://dougiamas.com
  22   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  define('AJAX_SCRIPT', true);
  26  
  27  require_once(__DIR__ . '/../../config.php');
  28  require_once($CFG->dirroot . '/user/selector/lib.php');
  29  
  30  $PAGE->set_context(context_system::instance());
  31  $PAGE->set_url('/user/selector/search.php');
  32  
  33  echo $OUTPUT->header();
  34  
  35  // Check access.
  36  require_login();
  37  require_sesskey();
  38  
  39  // Get the search parameter.
  40  $search = required_param('search', PARAM_RAW);
  41  
  42  // Get and validate the selectorid parameter.
  43  $selectorhash = required_param('selectorid', PARAM_ALPHANUM);
  44  if (!isset($USER->userselectors[$selectorhash])) {
  45      throw new \moodle_exception('unknownuserselector');
  46  }
  47  
  48  // Get the options.
  49  $options = $USER->userselectors[$selectorhash];
  50  
  51  // Create the appropriate userselector.
  52  $classname = $options['class'];
  53  unset($options['class']);
  54  $name = $options['name'];
  55  unset($options['name']);
  56  if (isset($options['file'])) {
  57      require_once($CFG->dirroot . '/' . $options['file']);
  58      unset($options['file']);
  59  }
  60  $userselector = new $classname($name, $options);
  61  
  62  // Do the search and output the results.
  63  $results = $userselector->find_users($search);
  64  $jsonresults = array();
  65  foreach ($results as $groupname => $users) {
  66      $groupdata = array('name' => $groupname, 'users' => array());
  67      foreach ($users as $user) {
  68          $output = new stdClass;
  69          $output->id = $user->id;
  70          $output->name = $userselector->output_user($user);
  71          if (!empty($user->disabled)) {
  72              $output->disabled = true;
  73          }
  74          if (!empty($user->infobelow)) {
  75              $output->infobelow = $user->infobelow;
  76          }
  77          $groupdata['users'][] = $output;
  78      }
  79      $jsonresults[] = $groupdata;
  80  }
  81  
  82  $json = array('results' => $jsonresults);
  83  
  84  // Also add users' group membership summaries, if possible.
  85  if (is_callable(array($userselector, 'get_user_summaries')) && isset($options['courseid'])) {
  86      $json['userSummaries'] = $userselector->get_user_summaries($options['courseid']);
  87  }
  88  
  89  echo json_encode($json);