mod-chat MDL-21534 Added documentation, revised namespace, fixed non-namespaced funct...
[moodle.git] / user / externallib.php
CommitLineData
ef22c1b6 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/>.
17
18/**
19 * External user API
20 *
21 * @package moodlecore
22 * @subpackage webservice
551f4420 23 * @copyright 2009 Moodle Pty Ltd (http://moodle.com)
ef22c1b6 24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 */
26
27require_once("$CFG->libdir/externallib.php");
28
29class moodle_user_external extends external_api {
30
7b472b32
PS
31 /**
32 * Returns description of method parameters
33 * @return external_function_parameters
34 */
d4e13355 35 public static function create_users_parameters() {
667b496a
PS
36 global $CFG;
37
35b9a80a 38 return new external_function_parameters(
39 array(
40 'users' => new external_multiple_structure(
41 new external_single_structure(
42 array(
7b472b32 43 'username' => new external_value(PARAM_RAW, 'Username policy is defined in Moodle security config'),
667b496a 44 'password' => new external_value(PARAM_RAW, 'Plain text password consisting of any characters'),
7b472b32
PS
45 'firstname' => new external_value(PARAM_NOTAGS, 'The first name(s) of the user'),
46 'lastname' => new external_value(PARAM_NOTAGS, 'The family name of the user'),
47 'email' => new external_value(PARAM_EMAIL, 'A valid and unique email address'),
fb79269b 48 'auth' => new external_value(PARAM_SAFEDIR, 'Auth plugins include manual, ldap, imap, etc', VALUE_DEFAULT, 'manual', NULL_NOT_ALLOWED),
49 'idnumber' => new external_value(PARAM_RAW, 'An arbitrary ID code number perhaps from the institution', VALUE_DEFAULT, null),
50 'emailstop' => new external_value(PARAM_NUMBER, 'Email is blocked: 1 is blocked and 0 otherwise', VALUE_DEFAULT, 0),
51 'lang' => new external_value(PARAM_SAFEDIR, 'Language code such as "en_utf8", must exist on server', VALUE_DEFAULT, $CFG->lang, NULL_NOT_ALLOWED),
52 'theme' => new external_value(PARAM_SAFEDIR, 'Theme name such as "standard", must exist on server', VALUE_OPTIONAL),
53 'timezone' => new external_value(PARAM_ALPHANUMEXT, 'Timezone code such as Australia/Perth, or 99 for default', VALUE_OPTIONAL),
54 'mailformat' => new external_value(PARAM_INTEGER, 'Mail format code is 0 for plain text, 1 for HTML etc', VALUE_OPTIONAL),
55 'description' => new external_value(PARAM_TEXT, 'User profile description, as HTML', VALUE_OPTIONAL),
56 'city' => new external_value(PARAM_NOTAGS, 'Home city of the user', VALUE_OPTIONAL),
57 'country' => new external_value(PARAM_ALPHA, 'Home country code of the user, such as AU or CZ', VALUE_OPTIONAL),
35b9a80a 58 'preferences' => new external_multiple_structure(
59 new external_single_structure(
60 array(
7b472b32 61 'type' => new external_value(PARAM_ALPHANUMEXT, 'The name of the preference'),
35b9a80a 62 'value' => new external_value(PARAM_RAW, 'The value of the preference')
63 )
fb79269b 64 ), 'User preferences', VALUE_OPTIONAL),
35b9a80a 65 'customfields' => new external_multiple_structure(
66 new external_single_structure(
67 array(
7b472b32 68 'type' => new external_value(PARAM_ALPHANUMEXT, 'The name of the custom field'),
35b9a80a 69 'value' => new external_value(PARAM_RAW, 'The value of the custom field')
70 )
fb79269b 71 ), 'User custom fields', VALUE_OPTIONAL)
35b9a80a 72 )
73 )
74 )
75 )
76 );
625f0a24 77 }
78
d4e13355 79 /**
5de592b1 80 * Create one or more users
81 *
71864f15
PS
82 * @param array $users An array of users to create.
83 * @return array An array of arrays
5de592b1 84 */
7b472b32 85 public static function create_users($users) {
ef22c1b6 86 global $CFG, $DB;
fb79269b 87 require_once($CFG->dirroot."/user/lib.php");
7b472b32 88
5de592b1 89 // Ensure the current user is allowed to run this function
ef22c1b6 90 $context = get_context_instance(CONTEXT_SYSTEM);
ef22c1b6 91 self::validate_context($context);
fb79269b 92 require_capability('moodle/user:create', $context);
93
5de592b1 94 // Do basic automatic PARAM checks on incoming data, using params description
5de592b1 95 // If any problems are found then exceptions are thrown with helpful error messages
7b472b32
PS
96 $params = self::validate_parameters(self::create_users_parameters(), array('users'=>$users));
97
667b496a
PS
98 $availableauths = get_plugin_list('auth');
99 unset($availableauths['mnet']); // these would need mnethostid too
100 unset($availableauths['webservice']); // we do not want new webservice users for now
101
102 $availablethemes = get_plugin_list('theme');
103 $availablelangs = get_list_of_languages();
5de592b1 104
38b76f3c 105 $transaction = $DB->start_delegated_transaction();
5de592b1 106
fb79269b 107 $userids = array();
7b472b32 108 foreach ($params['users'] as $user) {
667b496a
PS
109 // Make sure that the username doesn't already exist
110 if ($DB->record_exists('user', array('username'=>$user['username'], 'mnethostid'=>$CFG->mnet_localhost_id))) {
111 throw new invalid_parameter_exception('Username already exists: '.$user['username']);
ef22c1b6 112 }
ef22c1b6 113
667b496a
PS
114 // Make sure auth is valid
115 if (empty($availableauths[$user['auth']])) {
116 throw new invalid_parameter_exception('Invalid authentication type: '.$user['auth']);
ef22c1b6 117 }
118
667b496a
PS
119 // Make sure lang is valid
120 if (empty($availablelangs[$user['lang']])) {
121 throw new invalid_parameter_exception('Invalid language code: '.$user['lang']);
ef22c1b6 122 }
123
667b496a 124 // Make sure lang is valid
fb79269b 125 if (!empty($user['theme']) && empty($availablethemes[$user['theme']])) { //theme is VALUE_OPTIONAL,
126 // so no default value.
127 // We need to test if the client sent it
128 // => !empty($user['theme'])
667b496a 129 throw new invalid_parameter_exception('Invalid theme: '.$user['theme']);
ef22c1b6 130 }
5de592b1 131
38b76f3c
PS
132 // make sure there is no data loss during truncation
133 $truncated = truncate_userinfo($user);
134 foreach ($truncated as $key=>$value) {
135 if ($truncated[$key] !== $user[$key]) {
136 throw new invalid_parameter_exception('Property: '.$key.' is too long: '.$user[$key]);
137 }
138 }
5de592b1 139
fb79269b 140 $user['confirmed'] = true;
141 $newuserid = user_create_user($user);
667b496a 142
d4e13355 143 //TODO: preferences and custom fields
144
fb79269b 145 $userids[] = array('id'=>$newuserid, 'username'=>$user['username']);
ef22c1b6 146 }
147
38b76f3c 148 $transaction->allow_commit();
667b496a 149
fb79269b 150 return $userids;
ef22c1b6 151 }
152
7b472b32
PS
153 /**
154 * Returns description of method result value
155 * @return external_description
156 */
157 public static function create_users_returns() {
158 return new external_multiple_structure(
159 new external_single_structure(
160 array(
161 'id' => new external_value(PARAM_INT, 'user id'),
162 'username' => new external_value(PARAM_RAW, 'user name'),
163 )
164 )
165 );
d4e13355 166 }
167
168
930680cb
PS
169 /**
170 * Returns description of method parameters
171 * @return external_function_parameters
172 */
d4e13355 173 public static function delete_users_parameters() {
930680cb
PS
174 return new external_function_parameters(
175 array(
176 'userids' => new external_multiple_structure(new external_value(PARAM_INT, 'user ID')),
177 )
178 );
d4e13355 179 }
930680cb 180
38b76f3c
PS
181 public static function delete_users($userids) {
182 global $CFG, $DB;
fb79269b 183 require_once($CFG->dirroot."/user/lib.php");
38b76f3c
PS
184
185 // Ensure the current user is allowed to run this function
186 $context = get_context_instance(CONTEXT_SYSTEM);
187 require_capability('moodle/user:delete', $context);
188 self::validate_context($context);
189
fb79269b 190 $params = self::validate_parameters(self::delete_users_parameters(), array('userids'=>$userids));
38b76f3c
PS
191
192 $transaction = $DB->start_delegated_transaction();
fb79269b 193 // TODO: this is problematic because the DB rollback does not handle rollbacking of deleted user images!
38b76f3c
PS
194
195 foreach ($params['userids'] as $userid) {
196 $user = $DB->get_record('user', array('id'=>$userid, 'deleted'=>0), '*', MUST_EXIST);
fb79269b 197 user_delete_user($user);
38b76f3c
PS
198 }
199
200 $transaction->allow_commit();
201
202 return null;
ef22c1b6 203 }
930680cb
PS
204
205 /**
206 * Returns description of method result value
207 * @return external_description
208 */
d4e13355 209 public static function delete_users_returns() {
930680cb 210 return null;
d4e13355 211 }
ef22c1b6 212
213
930680cb
PS
214 /**
215 * Returns description of method parameters
216 * @return external_function_parameters
217 */
d4e13355 218 public static function update_users_parameters() {
fb79269b 219 global $CFG;
220 return new external_function_parameters(
221 array(
222 'users' => new external_multiple_structure(
223 new external_single_structure(
224 array(
225 'id' => new external_value(PARAM_NUMBER, 'ID of the user'),
226 'username' => new external_value(PARAM_RAW, 'Username policy is defined in Moodle security config', VALUE_OPTIONAL, '',NULL_NOT_ALLOWED),
227 'password' => new external_value(PARAM_RAW, 'Plain text password consisting of any characters', VALUE_OPTIONAL, '',NULL_NOT_ALLOWED),
228 'firstname' => new external_value(PARAM_NOTAGS, 'The first name(s) of the user', VALUE_OPTIONAL, '',NULL_NOT_ALLOWED),
229 'lastname' => new external_value(PARAM_NOTAGS, 'The family name of the user', VALUE_OPTIONAL),
230 'email' => new external_value(PARAM_EMAIL, 'A valid and unique email address', VALUE_OPTIONAL, '',NULL_NOT_ALLOWED),
231 'auth' => new external_value(PARAM_SAFEDIR, 'Auth plugins include manual, ldap, imap, etc', VALUE_OPTIONAL, '', NULL_NOT_ALLOWED),
232 'idnumber' => new external_value(PARAM_RAW, 'An arbitrary ID code number perhaps from the institution', VALUE_OPTIONAL),
233 'emailstop' => new external_value(PARAM_NUMBER, 'Email is blocked: 1 is blocked and 0 otherwise', VALUE_OPTIONAL),
234 'lang' => new external_value(PARAM_SAFEDIR, 'Language code such as "en_utf8", must exist on server', VALUE_OPTIONAL, '', NULL_NOT_ALLOWED),
235 'theme' => new external_value(PARAM_SAFEDIR, 'Theme name such as "standard", must exist on server', VALUE_OPTIONAL),
236 'timezone' => new external_value(PARAM_ALPHANUMEXT, 'Timezone code such as Australia/Perth, or 99 for default', VALUE_OPTIONAL),
237 'mailformat' => new external_value(PARAM_INTEGER, 'Mail format code is 0 for plain text, 1 for HTML etc', VALUE_OPTIONAL),
238 'description' => new external_value(PARAM_TEXT, 'User profile description, as HTML', VALUE_OPTIONAL),
239 'city' => new external_value(PARAM_NOTAGS, 'Home city of the user', VALUE_OPTIONAL),
240 'country' => new external_value(PARAM_ALPHA, 'Home country code of the user, such as AU or CZ', VALUE_OPTIONAL),
241 'preferences' => new external_multiple_structure(
242 new external_single_structure(
243 array(
244 'type' => new external_value(PARAM_ALPHANUMEXT, 'The name of the preference'),
245 'value' => new external_value(PARAM_RAW, 'The value of the preference')
246 )
247 ), 'User preferences', VALUE_OPTIONAL),
248 'customfields' => new external_multiple_structure(
249 new external_single_structure(
250 array(
251 'type' => new external_value(PARAM_ALPHANUMEXT, 'The name of the custom field'),
252 'value' => new external_value(PARAM_RAW, 'The value of the custom field')
253 )
254 ), 'User custom fields', VALUE_OPTIONAL)
255 )
256 )
257 )
258 )
259 );
d4e13355 260 }
38b76f3c
PS
261
262 public static function update_users($users) {
263 global $CFG, $DB;
fb79269b 264 require_once($CFG->dirroot."/user/lib.php");
38b76f3c
PS
265
266 // Ensure the current user is allowed to run this function
267 $context = get_context_instance(CONTEXT_SYSTEM);
268 require_capability('moodle/user:update', $context);
269 self::validate_context($context);
270
271 $params = self::validate_parameters(self::update_users_parameters(), array('users'=>$users));
272
273 $transaction = $DB->start_delegated_transaction();
274
275 foreach ($params['users'] as $user) {
fb79269b 276 user_update_user($user);
38b76f3c
PS
277 }
278
279 $transaction->allow_commit();
280
281 return null;
ef22c1b6 282 }
930680cb
PS
283
284 /**
285 * Returns description of method result value
286 * @return external_description
287 */
d4e13355 288 public static function update_users_returns() {
930680cb 289 return null;
d4e13355 290 }
291
7b472b32
PS
292 /**
293 * Returns description of method parameters
294 * @return external_function_parameters
295 */
fb79269b 296 public static function get_users_by_id_parameters() {
71864f15
PS
297 return new external_function_parameters(
298 array(
299 'userids' => new external_multiple_structure(new external_value(PARAM_INT, 'user ID')),
fb79269b 300 )
71864f15 301 );
d4e13355 302 }
7b472b32 303
930680cb 304
71864f15
PS
305 /**
306 * Get user information
307 *
308 * @param array $userids array of user ids
309 * @return array An array of arrays describing users
310 */
fb79269b 311 public static function get_users_by_id($userids) {
312 global $CFG;
313 require_once($CFG->dirroot."/user/lib.php");
314 require_once($CFG->dirroot."/user/profile/lib.php"); //required for customfields related function
315 //TODO: move the functions somewhere else as
316 //they are "user" related
317
5de592b1 318 $context = get_context_instance(CONTEXT_SYSTEM);
319 require_capability('moodle/user:viewdetails', $context);
320 self::validate_context($context);
321
fb79269b 322 $params = self::validate_parameters(self::get_users_by_id_parameters(), array('userids'=>$userids));
5de592b1 323
fb79269b 324 //TODO: check if there is any performance issue: we do one DB request to retrieve all user,
325 // then for each user the profile_load_data does at least two DB requests
d4e13355 326
fb79269b 327 $users = user_get_users_by_id($params['userids']);
328 $result =array();
d4e13355 329 foreach ($users as $user) {
fb79269b 330 if (empty($user->deleted)) {
331
332 $userarray = (array) $user; //we want to return an array not an object
333 /// now we transfert all profile_field_xxx into the customfields external_multiple_structure required by description
334 $userarray['customfields'] = null;
335 $customfields = profile_user_record($user->id);
336 $customfields = (array) $customfields;
337 foreach ($customfields as $key => $value) {
338 $userarray['customfields'][] = array('type' => $key, 'value' => $value);
339 }
340
341 $result[] = $userarray;
342 }
343
344 }
71864f15
PS
345
346 return $result;
d4e13355 347 }
7b472b32
PS
348
349 /**
350 * Returns description of method result value
351 * @return external_description
352 */
fb79269b 353 public static function get_users_by_id_returns() {
71864f15
PS
354 return new external_multiple_structure(
355 new external_single_structure(
356 array(
fb79269b 357 'id' => new external_value(PARAM_NUMBER, 'ID of the user'),
71864f15
PS
358 'username' => new external_value(PARAM_RAW, 'Username policy is defined in Moodle security config'),
359 'firstname' => new external_value(PARAM_NOTAGS, 'The first name(s) of the user'),
360 'lastname' => new external_value(PARAM_NOTAGS, 'The family name of the user'),
361 'email' => new external_value(PARAM_EMAIL, 'A valid and unique email address'),
40e85c92
PS
362 'auth' => new external_value(PARAM_SAFEDIR, 'Auth plugins include manual, ldap, imap, etc'),
363 'confirmed' => new external_value(PARAM_NUMBER, 'Active user: 1 if confirmed, 0 otherwise'),
364 'idnumber' => new external_value(PARAM_RAW, 'An arbitrary ID code number perhaps from the institution'),
365 'emailstop' => new external_value(PARAM_NUMBER, 'Email is blocked: 1 is blocked and 0 otherwise'),
366 'lang' => new external_value(PARAM_SAFEDIR, 'Language code such as "en_utf8", must exist on server'),
367 'theme' => new external_value(PARAM_SAFEDIR, 'Theme name such as "standard", must exist on server'),
368 'timezone' => new external_value(PARAM_ALPHANUMEXT, 'Timezone code such as Australia/Perth, or 99 for default'),
369 'mailformat' => new external_value(PARAM_INTEGER, 'Mail format code is 0 for plain text, 1 for HTML etc'),
370 'description' => new external_value(PARAM_TEXT, 'User profile description, as HTML'),
371 'city' => new external_value(PARAM_NOTAGS, 'Home city of the user'),
372 'country' => new external_value(PARAM_ALPHA, 'Home country code of the user, such as AU or CZ'),
71864f15
PS
373 'customfields' => new external_multiple_structure(
374 new external_single_structure(
375 array(
376 'type' => new external_value(PARAM_ALPHANUMEXT, 'The name of the custom field'),
377 'value' => new external_value(PARAM_RAW, 'The value of the custom field')
378 )
40e85c92 379 ), 'User custom fields')
71864f15
PS
380 )
381 )
382 );
5de592b1 383 }
5de592b1 384}