f05c20fb |
1 | <?php // $Id$ |
2 | |
3 | /////////////////////////////////////////////////////////////////////////// |
4 | // // |
5 | // NOTICE OF COPYRIGHT // |
6 | // // |
7 | // Moodle - Modular Object-Oriented Dynamic Learning Environment // |
8 | // http://moodle.org // |
9 | // // |
10 | // Copyright (C) 1999 onwards Martin Dougiamas http://dougiamas.com // |
11 | // // |
12 | // This program is free software; you can redistribute it and/or modify // |
13 | // it under the terms of the GNU General Public License as published by // |
14 | // the Free Software Foundation; either version 2 of the License, or // |
15 | // (at your option) any later version. // |
16 | // // |
17 | // This program is distributed in the hope that it will be useful, // |
18 | // but WITHOUT ANY WARRANTY; without even the implied warranty of // |
19 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // |
20 | // GNU General Public License for more details: // |
21 | // // |
22 | // http://www.gnu.org/copyleft/gpl.html // |
23 | // // |
24 | /////////////////////////////////////////////////////////////////////////// |
25 | |
26 | /** |
27 | * Code to search for users in response to an ajax call from a user selector. |
28 | * |
29 | * @license http://www.gnu.org/copyleft/gpl.html GNU Public License |
30 | * @package userselector |
1e8e4687 |
31 | *//** */ |
f05c20fb |
32 | |
33 | require_once(dirname(__FILE__) . '/../../config.php'); |
34 | require_once($CFG->dirroot . '/user/selector/lib.php'); |
35 | |
6cab02ac |
36 | // In developer debug mode, when there is a debug=1 in the URL send as plain text |
37 | // for easier debugging. |
38 | if (debugging('', DEBUG_DEVELOPER) && optional_param('debug', false, PARAM_BOOL)) { |
39 | header('Content-type: text/plain; charset=UTF-8'); |
40 | $debugmode = true; |
41 | } else { |
42 | header('Content-type: application/json'); |
43 | $debugmode = false; |
44 | } |
45 | |
f05c20fb |
46 | // Check access. |
d56f9e65 |
47 | if (!isloggedin()) {; |
48 | print_error('mustbeloggedin'); |
49 | } |
f05c20fb |
50 | if (!confirm_sesskey()) { |
51 | print_error('invalidsesskey'); |
52 | } |
53 | |
54 | // Get the search parameter. |
55 | $search = required_param('search', PARAM_RAW); |
56 | |
57 | // Get and validate the selectorid parameter. |
58 | $selectorhash = required_param('selectorid', PARAM_ALPHANUM); |
59 | if (!isset($USER->userselectors[$selectorhash])) { |
60 | print_error('unknownuserselector'); |
61 | } |
62 | |
6cab02ac |
63 | // Get the options. |
f05c20fb |
64 | $options = $USER->userselectors[$selectorhash]; |
6cab02ac |
65 | |
66 | if ($debugmode) { |
67 | echo 'Search string: ', $search, "\n"; |
68 | echo 'Options: '; |
69 | print_r($options); |
70 | echo "\n"; |
71 | } |
72 | |
73 | // Create the appropriate userselector. |
f05c20fb |
74 | $classname = $options['class']; |
75 | unset($options['class']); |
76 | $name = $options['name']; |
77 | unset($options['name']); |
78 | if (isset($options['file'])) { |
79 | require_once($CFG->dirroot . '/' . $options['file']); |
80 | unset($options['file']); |
81 | } |
82 | $userselector = new $classname($name, $options); |
83 | |
84 | // Do the search and output the results. |
85 | $users = $userselector->find_users($search); |
d56f9e65 |
86 | foreach ($users as &$group) { |
6cab02ac |
87 | foreach ($group as $user) { |
88 | $output = new stdClass; |
89 | $output->id = $user->id; |
90 | $output->name = $userselector->output_user($user); |
91 | if (!empty($user->disabled)) { |
92 | $output->disabled = true; |
93 | } |
94 | $group[$user->id] = $output; |
d56f9e65 |
95 | } |
96 | } |
97 | |
f05c20fb |
98 | echo json_encode(array('results' => $users)); |
99 | ?> |