Merge branch 'MDL-51922-master' of git://github.com/andrewnicols/moodle
[moodle.git] / user / selector / search.php
blob9125d0b6ea1495728a2bced9bf4cf54a59b22f33
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.
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/>.
17 /**
18 * Code to search for users in response to an ajax call from a user selector.
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
25 define('AJAX_SCRIPT', true);
27 require_once(dirname(__FILE__) . '/../../config.php');
28 require_once($CFG->dirroot . '/user/selector/lib.php');
30 $PAGE->set_context(context_system::instance());
31 $PAGE->set_url('/user/selector/search.php');
33 echo $OUTPUT->header();
35 // Check access.
36 require_login();
37 require_sesskey();
39 // Get the search parameter.
40 $search = required_param('search', PARAM_RAW);
42 // Get and validate the selectorid parameter.
43 $selectorhash = required_param('selectorid', PARAM_ALPHANUM);
44 if (!isset($USER->userselectors[$selectorhash])) {
45 print_error('unknownuserselector');
48 // Get the options.
49 $options = $USER->userselectors[$selectorhash];
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']);
60 $userselector = new $classname($name, $options);
62 // Do the search and output the results.
63 $results = $userselector->find_users($search);
64 $json = 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;
74 if (!empty($user->infobelow)) {
75 $output->infobelow = $user->infobelow;
77 $groupdata['users'][] = $output;
79 $json[] = $groupdata;
82 echo json_encode(array('results' => $json));