|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 00003 // This file is part of Moodle - http://moodle.org/ 00004 // 00005 // Moodle is free software: you can redistribute it and/or modify 00006 // it under the terms of the GNU General Public License as published by 00007 // the Free Software Foundation, either version 3 of the License, or 00008 // (at your option) any later version. 00009 // 00010 // Moodle is distributed in the hope that it will be useful, 00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 // GNU General Public License for more details. 00014 // 00015 // You should have received a copy of the GNU General Public License 00016 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 00017 00026 require_once(dirname(__FILE__) . '/../../config.php'); 00027 require_once($CFG->dirroot . '/user/selector/lib.php'); 00028 00029 $PAGE->set_context(get_context_instance(CONTEXT_SYSTEM)); 00030 $PAGE->set_url('/user/selector/search.php'); 00031 00032 // In developer debug mode, when there is a debug=1 in the URL send as plain text 00033 // for easier debugging. 00034 if (debugging('', DEBUG_DEVELOPER) && optional_param('debug', false, PARAM_BOOL)) { 00035 header('Content-type: text/plain; charset=UTF-8'); 00036 $debugmode = true; 00037 } else { 00038 header('Content-type: application/json; charset=utf-8'); 00039 $debugmode = false; 00040 } 00041 00042 // Check access. 00043 if (!isloggedin()) {; 00044 print_error('mustbeloggedin'); 00045 } 00046 if (!confirm_sesskey()) { 00047 print_error('invalidsesskey'); 00048 } 00049 00050 // Get the search parameter. 00051 $search = required_param('search', PARAM_RAW); 00052 00053 // Get and validate the selectorid parameter. 00054 $selectorhash = required_param('selectorid', PARAM_ALPHANUM); 00055 if (!isset($USER->userselectors[$selectorhash])) { 00056 print_error('unknownuserselector'); 00057 } 00058 00059 // Get the options. 00060 $options = $USER->userselectors[$selectorhash]; 00061 00062 if ($debugmode) { 00063 echo 'Search string: ', $search, "\n"; 00064 echo 'Options: '; 00065 print_r($options); 00066 echo "\n"; 00067 } 00068 00069 // Create the appropriate userselector. 00070 $classname = $options['class']; 00071 unset($options['class']); 00072 $name = $options['name']; 00073 unset($options['name']); 00074 if (isset($options['file'])) { 00075 require_once($CFG->dirroot . '/' . $options['file']); 00076 unset($options['file']); 00077 } 00078 $userselector = new $classname($name, $options); 00079 00080 // Do the search and output the results. 00081 $users = $userselector->find_users($search); 00082 foreach ($users as &$group) { 00083 foreach ($group as $user) { 00084 $output = new stdClass; 00085 $output->id = $user->id; 00086 $output->name = $userselector->output_user($user); 00087 if (!empty($user->disabled)) { 00088 $output->disabled = true; 00089 } 00090 $group[$user->id] = $output; 00091 } 00092 } 00093 00094 echo json_encode(array('results' => $users));