MDL-25973 add missing charset
[moodle.git] / user / selector / search.php
CommitLineData
ce221eb5 1<?php
2
3// This file is part of Moodle - http://moodle.org/
4//
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.
9//
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.
14//
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/>.
f05c20fb 17
18/**
19 * Code to search for users in response to an ajax call from a user selector.
20 *
ce221eb5 21 * @copyright 1999 Martin Dougiamas http://dougiamas.com
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 * @package user
24 */
f05c20fb 25
26require_once(dirname(__FILE__) . '/../../config.php');
27require_once($CFG->dirroot . '/user/selector/lib.php');
28
0e638ff4 29$PAGE->set_context(get_context_instance(CONTEXT_SYSTEM));
a6855934 30$PAGE->set_url('/user/selector/search.php');
ce221eb5 31
6cab02ac 32// In developer debug mode, when there is a debug=1 in the URL send as plain text
33// for easier debugging.
34if (debugging('', DEBUG_DEVELOPER) && optional_param('debug', false, PARAM_BOOL)) {
35 header('Content-type: text/plain; charset=UTF-8');
36 $debugmode = true;
37} else {
8a7703ce 38 header('Content-type: application/json; charset=utf-8');
6cab02ac 39 $debugmode = false;
40}
41
f05c20fb 42// Check access.
d56f9e65 43if (!isloggedin()) {;
44 print_error('mustbeloggedin');
45}
f05c20fb 46if (!confirm_sesskey()) {
47 print_error('invalidsesskey');
48}
49
50// Get the search parameter.
51$search = required_param('search', PARAM_RAW);
52
53// Get and validate the selectorid parameter.
54$selectorhash = required_param('selectorid', PARAM_ALPHANUM);
55if (!isset($USER->userselectors[$selectorhash])) {
56 print_error('unknownuserselector');
57}
58
6cab02ac 59// Get the options.
f05c20fb 60$options = $USER->userselectors[$selectorhash];
6cab02ac 61
62if ($debugmode) {
63 echo 'Search string: ', $search, "\n";
64 echo 'Options: ';
65 print_r($options);
66 echo "\n";
67}
68
69// Create the appropriate userselector.
f05c20fb 70$classname = $options['class'];
71unset($options['class']);
72$name = $options['name'];
73unset($options['name']);
74if (isset($options['file'])) {
75 require_once($CFG->dirroot . '/' . $options['file']);
76 unset($options['file']);
77}
78$userselector = new $classname($name, $options);
79
80// Do the search and output the results.
81$users = $userselector->find_users($search);
d56f9e65 82foreach ($users as &$group) {
6cab02ac 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;
89 }
90 $group[$user->id] = $output;
d56f9e65 91 }
92}
93
f05c20fb 94echo json_encode(array('results' => $users));