2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
18 * Handles external (web service) function calls related to search.
20 * @package core_search
21 * @copyright 2017 The Open University
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 namespace core_search
;
27 use core_external\external_function_parameters
;
28 use core_external\external_multiple_structure
;
29 use core_external\external_single_structure
;
30 use core_external\external_value
;
31 use core_user\external\user_summary_exporter
;
34 * Handles external (web service) function calls related to search.
36 * @package core_search
37 * @copyright 2017 The Open University
38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
40 class external
extends \core_external\external_api
{
42 * Returns parameter types for get_relevant_users function.
44 * @return external_function_parameters Parameters
46 public static function get_relevant_users_parameters() {
47 return new external_function_parameters([
48 'query' => new external_value(
50 'Query string (full or partial user full name or other details)'
52 'courseid' => new external_value(PARAM_INT
, 'Course id (0 if none)'),
57 * Returns result type for get_relevant_users function.
59 * @return external_description Result type
61 public static function get_relevant_users_returns() {
62 return new external_multiple_structure(
63 new external_single_structure([
64 'id' => new external_value(PARAM_INT
, 'User id'),
65 'fullname' => new external_value(PARAM_RAW
, 'Full name as text'),
66 'profileimageurlsmall' => new external_value(PARAM_URL
, 'URL to small profile image')
72 * Searches for users given a query, taking into account the current user's permissions and
73 * possibly a course to check within.
75 * @param string $query Query text
76 * @param int $courseid Course id or 0 if no restriction
77 * @return array Defined return structure
79 public static function get_relevant_users($query, $courseid) {
82 // Validate parameter.
85 'courseid' => $courseid,
86 ] = self
::validate_parameters(self
::get_relevant_users_parameters(), [
88 'courseid' => $courseid,
91 // Validate the context (search page is always system context).
92 $systemcontext = \context_system
::instance();
93 self
::validate_context($systemcontext);
95 // Get course object too.
97 $coursecontext = \context_course
::instance($courseid);
99 $coursecontext = null;
102 // If not logged in, can't see anyone when forceloginforprofiles is on.
103 if (!empty($CFG->forceloginforprofiles
)) {
104 if (!isloggedin() ||
isguestuser()) {
109 $users = \core_user
::search($query, $coursecontext);
112 foreach ($users as $user) {
113 // Get a standard exported user object.
114 $fulldetails = (new user_summary_exporter($user))->export($PAGE->get_renderer('core'));
116 // To avoid leaking private data to students, only include the specific information we
117 // are going to display (and not the email, idnumber, etc).
118 $result[] = (object)['id' => $fulldetails->id
, 'fullname' => $fulldetails->fullname
,
119 'profileimageurlsmall' => $fulldetails->profileimageurlsmall
];