forum MDL-23145 switched forum digests over to just use email instead of being chanel...
[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),
610a447e 49 'idnumber' => new external_value(PARAM_RAW, 'An arbitrary ID code number perhaps from the institution', VALUE_DEFAULT, ''),
fb79269b 50 'emailstop' => new external_value(PARAM_NUMBER, 'Email is blocked: 1 is blocked and 0 otherwise', VALUE_DEFAULT, 0),
3a915b06 51 'lang' => new external_value(PARAM_SAFEDIR, 'Language code such as "en", must exist on server', VALUE_DEFAULT, $CFG->lang, NULL_NOT_ALLOWED),
fb79269b 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");
30a4fb1b 88 require_once($CFG->dirroot."/user/profile/lib.php"); //required for customfields related function
89 //TODO: move the functions somewhere else as
90 //they are "user" related
7b472b32 91
5de592b1 92 // Ensure the current user is allowed to run this function
ef22c1b6 93 $context = get_context_instance(CONTEXT_SYSTEM);
ef22c1b6 94 self::validate_context($context);
fb79269b 95 require_capability('moodle/user:create', $context);
96
5de592b1 97 // Do basic automatic PARAM checks on incoming data, using params description
5de592b1 98 // If any problems are found then exceptions are thrown with helpful error messages
7b472b32
PS
99 $params = self::validate_parameters(self::create_users_parameters(), array('users'=>$users));
100
667b496a
PS
101 $availableauths = get_plugin_list('auth');
102 unset($availableauths['mnet']); // these would need mnethostid too
103 unset($availableauths['webservice']); // we do not want new webservice users for now
104
105 $availablethemes = get_plugin_list('theme');
1f96e907 106 $availablelangs = get_string_manager()->get_list_of_translations();
5de592b1 107
38b76f3c 108 $transaction = $DB->start_delegated_transaction();
5de592b1 109
fb79269b 110 $userids = array();
7b472b32 111 foreach ($params['users'] as $user) {
667b496a
PS
112 // Make sure that the username doesn't already exist
113 if ($DB->record_exists('user', array('username'=>$user['username'], 'mnethostid'=>$CFG->mnet_localhost_id))) {
114 throw new invalid_parameter_exception('Username already exists: '.$user['username']);
ef22c1b6 115 }
ef22c1b6 116
667b496a
PS
117 // Make sure auth is valid
118 if (empty($availableauths[$user['auth']])) {
119 throw new invalid_parameter_exception('Invalid authentication type: '.$user['auth']);
ef22c1b6 120 }
121
667b496a
PS
122 // Make sure lang is valid
123 if (empty($availablelangs[$user['lang']])) {
124 throw new invalid_parameter_exception('Invalid language code: '.$user['lang']);
ef22c1b6 125 }
126
667b496a 127 // Make sure lang is valid
fb79269b 128 if (!empty($user['theme']) && empty($availablethemes[$user['theme']])) { //theme is VALUE_OPTIONAL,
129 // so no default value.
130 // We need to test if the client sent it
131 // => !empty($user['theme'])
667b496a 132 throw new invalid_parameter_exception('Invalid theme: '.$user['theme']);
ef22c1b6 133 }
5de592b1 134
38b76f3c
PS
135 // make sure there is no data loss during truncation
136 $truncated = truncate_userinfo($user);
137 foreach ($truncated as $key=>$value) {
610a447e 138 if ($truncated[$key] !== $user[$key]) {
139 throw new invalid_parameter_exception('Property: '.$key.' is too long: '.$user[$key]);
140 }
38b76f3c 141 }
5de592b1 142
fb79269b 143 $user['confirmed'] = true;
a1988186 144 $user['mnethostid'] = $CFG->mnet_localhost_id;
30a4fb1b 145 $user['id'] = user_create_user($user);
146
147 // custom fields
148 if(!empty($user['customfields'])) {
149 foreach($user['customfields'] as $customfield) {
150 $user["profile_field_".$customfield['type']] = $customfield['value']; //profile_save_data() saves profile file
151 //it's expecting a user with the correct id,
152 //and custom field to be named profile_field_"shortname"
153 }
154 profile_save_data((object) $user);
155 }
667b496a 156
30a4fb1b 157 //TODO: preferences
d4e13355 158
c4c352dd 159 $userids[] = array('id'=>$user['id'], 'username'=>$user['username']);
ef22c1b6 160 }
161
38b76f3c 162 $transaction->allow_commit();
667b496a 163
fb79269b 164 return $userids;
ef22c1b6 165 }
166
7b472b32
PS
167 /**
168 * Returns description of method result value
169 * @return external_description
170 */
171 public static function create_users_returns() {
172 return new external_multiple_structure(
173 new external_single_structure(
174 array(
175 'id' => new external_value(PARAM_INT, 'user id'),
176 'username' => new external_value(PARAM_RAW, 'user name'),
177 )
178 )
179 );
d4e13355 180 }
181
182
930680cb
PS
183 /**
184 * Returns description of method parameters
185 * @return external_function_parameters
186 */
d4e13355 187 public static function delete_users_parameters() {
930680cb
PS
188 return new external_function_parameters(
189 array(
190 'userids' => new external_multiple_structure(new external_value(PARAM_INT, 'user ID')),
191 )
192 );
d4e13355 193 }
930680cb 194
38b76f3c
PS
195 public static function delete_users($userids) {
196 global $CFG, $DB;
fb79269b 197 require_once($CFG->dirroot."/user/lib.php");
38b76f3c
PS
198
199 // Ensure the current user is allowed to run this function
200 $context = get_context_instance(CONTEXT_SYSTEM);
201 require_capability('moodle/user:delete', $context);
202 self::validate_context($context);
203
fb79269b 204 $params = self::validate_parameters(self::delete_users_parameters(), array('userids'=>$userids));
38b76f3c
PS
205
206 $transaction = $DB->start_delegated_transaction();
fb79269b 207 // TODO: this is problematic because the DB rollback does not handle rollbacking of deleted user images!
38b76f3c
PS
208
209 foreach ($params['userids'] as $userid) {
210 $user = $DB->get_record('user', array('id'=>$userid, 'deleted'=>0), '*', MUST_EXIST);
fb79269b 211 user_delete_user($user);
38b76f3c
PS
212 }
213
214 $transaction->allow_commit();
215
216 return null;
ef22c1b6 217 }
930680cb
PS
218
219 /**
220 * Returns description of method result value
221 * @return external_description
222 */
d4e13355 223 public static function delete_users_returns() {
930680cb 224 return null;
d4e13355 225 }
ef22c1b6 226
227
930680cb
PS
228 /**
229 * Returns description of method parameters
230 * @return external_function_parameters
231 */
d4e13355 232 public static function update_users_parameters() {
fb79269b 233 global $CFG;
234 return new external_function_parameters(
235 array(
236 'users' => new external_multiple_structure(
237 new external_single_structure(
238 array(
239 'id' => new external_value(PARAM_NUMBER, 'ID of the user'),
240 'username' => new external_value(PARAM_RAW, 'Username policy is defined in Moodle security config', VALUE_OPTIONAL, '',NULL_NOT_ALLOWED),
241 'password' => new external_value(PARAM_RAW, 'Plain text password consisting of any characters', VALUE_OPTIONAL, '',NULL_NOT_ALLOWED),
242 'firstname' => new external_value(PARAM_NOTAGS, 'The first name(s) of the user', VALUE_OPTIONAL, '',NULL_NOT_ALLOWED),
243 'lastname' => new external_value(PARAM_NOTAGS, 'The family name of the user', VALUE_OPTIONAL),
244 'email' => new external_value(PARAM_EMAIL, 'A valid and unique email address', VALUE_OPTIONAL, '',NULL_NOT_ALLOWED),
245 'auth' => new external_value(PARAM_SAFEDIR, 'Auth plugins include manual, ldap, imap, etc', VALUE_OPTIONAL, '', NULL_NOT_ALLOWED),
246 'idnumber' => new external_value(PARAM_RAW, 'An arbitrary ID code number perhaps from the institution', VALUE_OPTIONAL),
247 'emailstop' => new external_value(PARAM_NUMBER, 'Email is blocked: 1 is blocked and 0 otherwise', VALUE_OPTIONAL),
3a915b06 248 'lang' => new external_value(PARAM_SAFEDIR, 'Language code such as "en", must exist on server', VALUE_OPTIONAL, '', NULL_NOT_ALLOWED),
fb79269b 249 'theme' => new external_value(PARAM_SAFEDIR, 'Theme name such as "standard", must exist on server', VALUE_OPTIONAL),
250 'timezone' => new external_value(PARAM_ALPHANUMEXT, 'Timezone code such as Australia/Perth, or 99 for default', VALUE_OPTIONAL),
251 'mailformat' => new external_value(PARAM_INTEGER, 'Mail format code is 0 for plain text, 1 for HTML etc', VALUE_OPTIONAL),
252 'description' => new external_value(PARAM_TEXT, 'User profile description, as HTML', VALUE_OPTIONAL),
253 'city' => new external_value(PARAM_NOTAGS, 'Home city of the user', VALUE_OPTIONAL),
254 'country' => new external_value(PARAM_ALPHA, 'Home country code of the user, such as AU or CZ', VALUE_OPTIONAL),
fb79269b 255 'customfields' => new external_multiple_structure(
256 new external_single_structure(
257 array(
258 'type' => new external_value(PARAM_ALPHANUMEXT, 'The name of the custom field'),
259 'value' => new external_value(PARAM_RAW, 'The value of the custom field')
260 )
261 ), 'User custom fields', VALUE_OPTIONAL)
262 )
263 )
264 )
265 )
266 );
d4e13355 267 }
38b76f3c
PS
268
269 public static function update_users($users) {
270 global $CFG, $DB;
fb79269b 271 require_once($CFG->dirroot."/user/lib.php");
9baf3a7b 272 require_once($CFG->dirroot."/user/profile/lib.php"); //required for customfields related function
273 //TODO: move the functions somewhere else as
274 //they are "user" related
38b76f3c
PS
275
276 // Ensure the current user is allowed to run this function
277 $context = get_context_instance(CONTEXT_SYSTEM);
278 require_capability('moodle/user:update', $context);
279 self::validate_context($context);
280
281 $params = self::validate_parameters(self::update_users_parameters(), array('users'=>$users));
282
283 $transaction = $DB->start_delegated_transaction();
284
285 foreach ($params['users'] as $user) {
fb79269b 286 user_update_user($user);
9baf3a7b 287 //update user custom fields
288 if(!empty($user['customfields'])) {
289
290 foreach($user['customfields'] as $customfield) {
291 $user["profile_field_".$customfield['type']] = $customfield['value']; //profile_save_data() saves profile file
292 //it's expecting a user with the correct id,
293 //and custom field to be named profile_field_"shortname"
294 }
295 profile_save_data((object) $user);
296 }
38b76f3c
PS
297 }
298
9baf3a7b 299
300
301
38b76f3c
PS
302 $transaction->allow_commit();
303
304 return null;
ef22c1b6 305 }
930680cb
PS
306
307 /**
308 * Returns description of method result value
309 * @return external_description
310 */
d4e13355 311 public static function update_users_returns() {
930680cb 312 return null;
d4e13355 313 }
314
7b472b32
PS
315 /**
316 * Returns description of method parameters
317 * @return external_function_parameters
318 */
fb79269b 319 public static function get_users_by_id_parameters() {
71864f15
PS
320 return new external_function_parameters(
321 array(
322 'userids' => new external_multiple_structure(new external_value(PARAM_INT, 'user ID')),
fb79269b 323 )
71864f15 324 );
d4e13355 325 }
7b472b32 326
930680cb 327
71864f15
PS
328 /**
329 * Get user information
330 *
331 * @param array $userids array of user ids
332 * @return array An array of arrays describing users
333 */
fb79269b 334 public static function get_users_by_id($userids) {
335 global $CFG;
336 require_once($CFG->dirroot."/user/lib.php");
337 require_once($CFG->dirroot."/user/profile/lib.php"); //required for customfields related function
338 //TODO: move the functions somewhere else as
339 //they are "user" related
340
5de592b1 341 $context = get_context_instance(CONTEXT_SYSTEM);
342 require_capability('moodle/user:viewdetails', $context);
343 self::validate_context($context);
344
fb79269b 345 $params = self::validate_parameters(self::get_users_by_id_parameters(), array('userids'=>$userids));
5de592b1 346
fb79269b 347 //TODO: check if there is any performance issue: we do one DB request to retrieve all user,
348 // then for each user the profile_load_data does at least two DB requests
d4e13355 349
fb79269b 350 $users = user_get_users_by_id($params['userids']);
351 $result =array();
d4e13355 352 foreach ($users as $user) {
fb79269b 353 if (empty($user->deleted)) {
354
355 $userarray = (array) $user; //we want to return an array not an object
356 /// now we transfert all profile_field_xxx into the customfields external_multiple_structure required by description
0f31850a 357 $userarray['customfields'] = array();
fb79269b 358 $customfields = profile_user_record($user->id);
359 $customfields = (array) $customfields;
360 foreach ($customfields as $key => $value) {
361 $userarray['customfields'][] = array('type' => $key, 'value' => $value);
362 }
363
364 $result[] = $userarray;
365 }
366
367 }
71864f15
PS
368
369 return $result;
d4e13355 370 }
7b472b32
PS
371
372 /**
373 * Returns description of method result value
374 * @return external_description
375 */
fb79269b 376 public static function get_users_by_id_returns() {
71864f15
PS
377 return new external_multiple_structure(
378 new external_single_structure(
379 array(
fb79269b 380 'id' => new external_value(PARAM_NUMBER, 'ID of the user'),
71864f15
PS
381 'username' => new external_value(PARAM_RAW, 'Username policy is defined in Moodle security config'),
382 'firstname' => new external_value(PARAM_NOTAGS, 'The first name(s) of the user'),
383 'lastname' => new external_value(PARAM_NOTAGS, 'The family name of the user'),
384 'email' => new external_value(PARAM_EMAIL, 'A valid and unique email address'),
40e85c92
PS
385 'auth' => new external_value(PARAM_SAFEDIR, 'Auth plugins include manual, ldap, imap, etc'),
386 'confirmed' => new external_value(PARAM_NUMBER, 'Active user: 1 if confirmed, 0 otherwise'),
387 'idnumber' => new external_value(PARAM_RAW, 'An arbitrary ID code number perhaps from the institution'),
388 'emailstop' => new external_value(PARAM_NUMBER, 'Email is blocked: 1 is blocked and 0 otherwise'),
3a915b06 389 'lang' => new external_value(PARAM_SAFEDIR, 'Language code such as "en", must exist on server'),
40e85c92
PS
390 'theme' => new external_value(PARAM_SAFEDIR, 'Theme name such as "standard", must exist on server'),
391 'timezone' => new external_value(PARAM_ALPHANUMEXT, 'Timezone code such as Australia/Perth, or 99 for default'),
392 'mailformat' => new external_value(PARAM_INTEGER, 'Mail format code is 0 for plain text, 1 for HTML etc'),
393 'description' => new external_value(PARAM_TEXT, 'User profile description, as HTML'),
394 'city' => new external_value(PARAM_NOTAGS, 'Home city of the user'),
395 'country' => new external_value(PARAM_ALPHA, 'Home country code of the user, such as AU or CZ'),
71864f15
PS
396 'customfields' => new external_multiple_structure(
397 new external_single_structure(
398 array(
399 'type' => new external_value(PARAM_ALPHANUMEXT, 'The name of the custom field'),
400 'value' => new external_value(PARAM_RAW, 'The value of the custom field')
401 )
40e85c92 402 ), 'User custom fields')
71864f15
PS
403 )
404 )
405 );
5de592b1 406 }
5de592b1 407}