Repository navigation
Expand file tree
/
Copy pathget_users.php
More file actions
166 lines (144 loc) · 5.78 KB
/
Copy pathget_users.php
File metadata and controls
166 lines (144 loc) · 5.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace report_customsql\external;
use core_external\external_api;
use core_external\external_description;
use core_external\external_function_parameters;
use core_external\external_multiple_structure;
use core_external\external_single_structure;
use core_external\external_value;
/**
* Web service used by form autocomplete to get a list of users with a given capability.
*
* @package report_customsql
* @copyright 2020 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class get_users extends external_api {
/**
* Parameter declaration.
*
* @return external_function_parameters Parameters
*/
public static function execute_parameters(): external_function_parameters {
return new external_function_parameters([
'query' => new external_value(PARAM_RAW, 'Contents of the search box.'),
'capability' => new external_value(PARAM_CAPABILITY, 'Return only users with this capability in the system context.'),
]);
}
/**
* Get a list of users with the given capability at system level.
*
* @param string $query Content of the autocomplete search box.
* @param string $capability Return only users with this capability in the system context.
*
* @return array Of data about users.
*/
public static function execute(string $query, string $capability): array {
global $CFG, $DB;
[$query, $capability] = array_values(
self::validate_parameters(
self::execute_parameters(),
['query' => $query, 'capability' => $capability],
)
);
$context = \context_system::instance();
self::validate_context($context);
require_capability('report/customsql:definequeries', $context);
if (class_exists('\core_user\fields')) {
$extrafields = \core_user\fields::for_identity($context, false)->get_required_fields();
$fields = \core_user\fields::for_identity(
$context,
false
)->with_userpic()->get_sql(
'u',
false,
'',
'',
false,
)->selects;
} else {
$extrafields = get_extra_user_fields($context);
$fields = \user_picture::fields('u', $extrafields);
}
$withcapabilityjoin = get_with_capability_join($context, $capability, 'u.id');
[$wherecondition, $whereparams] = users_search_sql($query, 'u', true, $extrafields);
[$sort, $sortparams] = users_order_by_sql('u', $query, $context);
$users = $DB->get_records_sql("
SELECT $fields
FROM {user} u
JOIN (
SELECT id
FROM {user} u
$withcapabilityjoin->joins
WHERE $withcapabilityjoin->wheres
UNION
SELECT id
FROM {user} u
WHERE u.id IN ($CFG->siteadmins)
) userids ON userids.id = u.id
WHERE $wherecondition
ORDER BY $sort
", array_merge($whereparams, $withcapabilityjoin->params, $sortparams));
$results = [];
foreach ($users as $user) {
$results[] = self::prepare_result_object($user, $extrafields);
}
return $results;
}
/**
* Convert a user object into the form required to return.
*
* @param \stdClass $user data on a user.
* @param array $extrafields extra profile fields to show.
* @return \stdClass return object.
*/
public static function prepare_result_object(\stdClass $user, array $extrafields): \stdClass {
global $PAGE;
$userpicture = new \user_picture($user);
$userpicture->size = 0; // Size f2.
$identity = [];
foreach ($extrafields as $field) {
if ($user->$field) {
$identity[] = $user->$field;
}
}
$identity = implode(', ', $identity);
return (object) [
'id' => $user->id,
'fullname' => fullname($user),
'identity' => $identity,
'hasidentity' => (bool) $identity,
'profileimageurlsmall' => $userpicture->get_url($PAGE)->out(false),
];
}
/**
* Returns type for declaration.
*
* @return external_description Result type
*/
public static function execute_returns(): external_description {
return new external_multiple_structure(
new external_single_structure([
'id' => new external_value(PARAM_INT, 'User id.'),
'fullname' => new external_value(PARAM_RAW, 'User full name.'),
'identity' => new external_value(PARAM_RAW, 'Additional user identifying info.'),
'hasidentity' => new external_value(PARAM_BOOL, 'Whether identity is non-blank.'),
'profileimageurlsmall' => new external_value(PARAM_RAW, 'URL of the user profile image.'),
])
);
}
}