"MDL-13766, fixed a bug in move_to_filepool and moving code block"
[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() {
35b9a80a 36 return new external_function_parameters(
37 array(
38 'users' => new external_multiple_structure(
39 new external_single_structure(
40 array(
7b472b32
PS
41 'username' => new external_value(PARAM_RAW, 'Username policy is defined in Moodle security config'),
42 'password' => new external_value(PARAM_RAW, 'Moodle passwords can consist of any character'),
43 'firstname' => new external_value(PARAM_NOTAGS, 'The first name(s) of the user'),
44 'lastname' => new external_value(PARAM_NOTAGS, 'The family name of the user'),
45 'email' => new external_value(PARAM_EMAIL, 'A valid and unique email address'),
46 'auth' => new external_value(PARAM_SAFEDIR, 'Auth plugins include manual, ldap, imap, etc', false),
47 'confirmed' => new external_value(PARAM_NUMBER, 'Active user: 1 if confirmed, 0 otherwise', false),
48 'idnumber' => new external_value(PARAM_RAW, 'An arbitrary ID code number perhaps from the institution', false),
49 'emailstop' => new external_value(PARAM_NUMBER, 'Email is blocked: 1 is blocked and 0 otherwise', false),
50 'lang' => new external_value(PARAM_SAFEDIR, 'Language code such as "en_utf8", must exist on server', false),
51 'theme' => new external_value(PARAM_SAFEDIR, 'Theme name such as "standard", must exist on server', false),
52 'timezone' => new external_value(PARAM_ALPHANUMEXT, 'Timezone code such as Australia/Perth, or 99 for default', false),
53 'mailformat' => new external_value(PARAM_INTEGER, 'Mail format code is 0 for plain text, 1 for HTML etc', false),
35b9a80a 54 'description' => new external_value(PARAM_TEXT, 'User profile description, as HTML', false),
7b472b32
PS
55 'city' => new external_value(PARAM_NOTAGS, 'Home city of the user', false),
56 'country' => new external_value(PARAM_ALPHA, 'Home country code of the user, such as AU or CZ', false),
35b9a80a 57 'preferences' => new external_multiple_structure(
58 new external_single_structure(
59 array(
7b472b32 60 'type' => new external_value(PARAM_ALPHANUMEXT, 'The name of the preference'),
35b9a80a 61 'value' => new external_value(PARAM_RAW, 'The value of the preference')
62 )
63 ), 'User preferences', false),
64 'customfields' => new external_multiple_structure(
65 new external_single_structure(
66 array(
7b472b32 67 'type' => new external_value(PARAM_ALPHANUMEXT, 'The name of the custom field'),
35b9a80a 68 'value' => new external_value(PARAM_RAW, 'The value of the custom field')
69 )
70 ), 'User custom fields', false)
71 )
72 )
73 )
74 )
75 );
625f0a24 76 }
77
d4e13355 78 /**
5de592b1 79 * Create one or more users
80 *
71864f15
PS
81 * @param array $users An array of users to create.
82 * @return array An array of arrays
5de592b1 83 */
7b472b32 84 public static function create_users($users) {
ef22c1b6 85 global $CFG, $DB;
7b472b32 86
5de592b1 87 // Ensure the current user is allowed to run this function
ef22c1b6 88 $context = get_context_instance(CONTEXT_SYSTEM);
5de592b1 89 require_capability('moodle/user:create', $context);
ef22c1b6 90 self::validate_context($context);
91
5de592b1 92 // Do basic automatic PARAM checks on incoming data, using params description
93 // This checks to make sure that:
94 // 1) No extra data was sent
d4e13355 95 // 2) All required items were sent
5de592b1 96 // 3) All data passes clean_param without changes (yes this is strict)
97 // If any problems are found then exceptions are thrown with helpful error messages
7b472b32
PS
98 $params = self::validate_parameters(self::create_users_parameters(), array('users'=>$users));
99
5de592b1 100
7b472b32 101 // TODO delegated transaction
5de592b1 102
ef22c1b6 103 $users = array();
7b472b32 104 foreach ($params['users'] as $user) {
5de592b1 105
106 // Empty or no auth is assumed to be manual
d4e13355 107 if (empty($user['auth'])) {
ef22c1b6 108 $user['auth'] = 'manual';
109 }
ef22c1b6 110
5de592b1 111 // Lang must be a real code, not empty string
112 if (isset($user['lang']) && empty($user['lang'])) {
ef22c1b6 113 unset($user['lang']);
114 }
115
5de592b1 116 // Make sure that the username doesn't already exist
7b472b32 117 if ($DB->record_exists('user', array('username'=>$user['username'], 'mnethostid'=>$CFG->mnet_localhost_id))) {
ef22c1b6 118 throw new invalid_parameter_exception($user['username']." username is already taken, sorry");
119 }
120
5de592b1 121 // Make sure that incoming data doesn't contain duplicate usernames
ef22c1b6 122 if (isset($users[$user['username']])) {
123 throw new invalid_parameter_exception("multiple users with the same username requested");
124 }
5de592b1 125
7b472b32 126 //TODO: validate username, auth, lang and theme
5de592b1 127
7b472b32 128 // finally create user
ef22c1b6 129 $record = create_user_record($user['username'], $user['password'], $user['auth']);
ef22c1b6 130
d4e13355 131 //TODO: preferences and custom fields
132
7b472b32 133 $users[] = array('id'=>$record->id, 'username'=>$record->username);
ef22c1b6 134 }
135
7b472b32 136 return $users;
ef22c1b6 137 }
138
7b472b32
PS
139 /**
140 * Returns description of method result value
141 * @return external_description
142 */
143 public static function create_users_returns() {
144 return new external_multiple_structure(
145 new external_single_structure(
146 array(
147 'id' => new external_value(PARAM_INT, 'user id'),
148 'username' => new external_value(PARAM_RAW, 'user name'),
149 )
150 )
151 );
d4e13355 152 }
153
154
930680cb
PS
155 /**
156 * Returns description of method parameters
157 * @return external_function_parameters
158 */
d4e13355 159 public static function delete_users_parameters() {
930680cb
PS
160 return new external_function_parameters(
161 array(
162 'userids' => new external_multiple_structure(new external_value(PARAM_INT, 'user ID')),
163 )
164 );
d4e13355 165 }
930680cb 166
ef22c1b6 167 public static function delete_users($params) {
168 //TODO
169 }
930680cb
PS
170
171 /**
172 * Returns description of method result value
173 * @return external_description
174 */
d4e13355 175 public static function delete_users_returns() {
930680cb 176 return null;
d4e13355 177 }
ef22c1b6 178
179
930680cb
PS
180 /**
181 * Returns description of method parameters
182 * @return external_function_parameters
183 */
d4e13355 184 public static function update_users_parameters() {
185 //TODO
186 }
ef22c1b6 187 public static function update_users($params) {
188 //TODO
189 }
930680cb
PS
190
191 /**
192 * Returns description of method result value
193 * @return external_description
194 */
d4e13355 195 public static function update_users_returns() {
930680cb 196 return null;
d4e13355 197 }
198
7b472b32
PS
199 /**
200 * Returns description of method parameters
201 * @return external_function_parameters
202 */
d4e13355 203 public static function get_users_parameters() {
71864f15
PS
204 return new external_function_parameters(
205 array(
206 'userids' => new external_multiple_structure(new external_value(PARAM_INT, 'user ID')),
207 )
208 );
d4e13355 209 }
7b472b32 210
930680cb 211
71864f15
PS
212 /**
213 * Get user information
214 *
215 * @param array $userids array of user ids
216 * @return array An array of arrays describing users
217 */
218 public static function get_users($userids) {
5de592b1 219 $context = get_context_instance(CONTEXT_SYSTEM);
220 require_capability('moodle/user:viewdetails', $context);
221 self::validate_context($context);
222
71864f15 223 $params = self::validate_parameters(self::get_users_parameters(), array('userids'=>$userids));
5de592b1 224
225 //TODO: this search is probably useless for external systems because it is not exact
226 // 1/ we should specify multiple search parameters including the mnet host id
d4e13355 227 // 2/ custom profile fileds not included
228
229 $result = array();
71864f15 230/*
d4e13355 231 $users = get_users(true, $params['search'], false, null, 'firstname ASC','', '', '', 1000, 'id, mnethostid, auth, confirmed, username, idnumber, firstname, lastname, email, emailstop, lang, theme, timezone, mailformat, city, description, country');
232 foreach ($users as $user) {
233 $result[] = (array)$user;
71864f15
PS
234 }*/
235
236 return $result;
d4e13355 237 }
7b472b32
PS
238
239 /**
240 * Returns description of method result value
241 * @return external_description
242 */
d4e13355 243 public static function get_users_returns() {
71864f15
PS
244 return new external_multiple_structure(
245 new external_single_structure(
246 array(
247 'username' => new external_value(PARAM_RAW, 'Username policy is defined in Moodle security config'),
248 'firstname' => new external_value(PARAM_NOTAGS, 'The first name(s) of the user'),
249 'lastname' => new external_value(PARAM_NOTAGS, 'The family name of the user'),
250 'email' => new external_value(PARAM_EMAIL, 'A valid and unique email address'),
251 'auth' => new external_value(PARAM_SAFEDIR, 'Auth plugins include manual, ldap, imap, etc', false),
252 'confirmed' => new external_value(PARAM_NUMBER, 'Active user: 1 if confirmed, 0 otherwise', false),
253 'idnumber' => new external_value(PARAM_RAW, 'An arbitrary ID code number perhaps from the institution', false),
254 'emailstop' => new external_value(PARAM_NUMBER, 'Email is blocked: 1 is blocked and 0 otherwise', false),
255 'lang' => new external_value(PARAM_SAFEDIR, 'Language code such as "en_utf8", must exist on server', false),
256 'theme' => new external_value(PARAM_SAFEDIR, 'Theme name such as "standard", must exist on server', false),
257 'timezone' => new external_value(PARAM_ALPHANUMEXT, 'Timezone code such as Australia/Perth, or 99 for default', false),
258 'mailformat' => new external_value(PARAM_INTEGER, 'Mail format code is 0 for plain text, 1 for HTML etc', false),
259 'description' => new external_value(PARAM_TEXT, 'User profile description, as HTML', false),
260 'city' => new external_value(PARAM_NOTAGS, 'Home city of the user', false),
261 'country' => new external_value(PARAM_ALPHA, 'Home country code of the user, such as AU or CZ', false),
262 'customfields' => new external_multiple_structure(
263 new external_single_structure(
264 array(
265 'type' => new external_value(PARAM_ALPHANUMEXT, 'The name of the custom field'),
266 'value' => new external_value(PARAM_RAW, 'The value of the custom field')
267 )
268 ), 'User custom fields', false)
269 )
270 )
271 );
5de592b1 272 }
5de592b1 273}