3 // This file is part of Moodle - http://moodle.org/
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
19 * Code to search for users in response to an ajax call from a user selector.
21 * @copyright 1999 Martin Dougiamas http://dougiamas.com
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 require_once(dirname(__FILE__) . '/../../config.php');
27 require_once($CFG->dirroot . '/user/selector/lib.php');
29 $PAGE->set_context(get_context_instance(CONTEXT_SYSTEM));
30 $PAGE->set_url('/user/selector/search.php');
32 // In developer debug mode, when there is a debug=1 in the URL send as plain text
33 // for easier debugging.
34 if (debugging('', DEBUG_DEVELOPER) && optional_param('debug', false, PARAM_BOOL)) {
35 header('Content-type: text/plain; charset=UTF-8');
38 header('Content-type: application/json');
44 print_error('mustbeloggedin');
46 if (!confirm_sesskey()) {
47 print_error('invalidsesskey');
50 // Get the search parameter.
51 $search = required_param('search', PARAM_RAW);
53 // Get and validate the selectorid parameter.
54 $selectorhash = required_param('selectorid', PARAM_ALPHANUM);
55 if (!isset($USER->userselectors[$selectorhash])) {
56 print_error('unknownuserselector');
60 $options = $USER->userselectors[$selectorhash];
63 echo 'Search string: ', $search, "\n";
69 // Create the appropriate userselector.
70 $classname = $options['class'];
71 unset($options['class']);
72 $name = $options['name'];
73 unset($options['name']);
74 if (isset($options['file'])) {
75 require_once($CFG->dirroot . '/' . $options['file']);
76 unset($options['file']);
78 $userselector = new $classname($name, $options);
80 // Do the search and output the results.
81 $users = $userselector->find_users($search);
82 foreach ($users as &$group) {
83 foreach ($group as $user) {
84 $output = new stdClass;
85 $output->id = $user->id;
86 $output->name = $userselector->output_user($user);
87 if (!empty($user->disabled)) {
88 $output->disabled = true;
90 $group[$user->id] = $output;
94 echo json_encode(array('results' => $users));