Commit | Line | Data |
---|---|---|
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 | ||
27 | require_once("$CFG->libdir/externallib.php"); | |
28 | ||
29 | class 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 | ||
155 | public static function delete_users_parameters() { | |
156 | //TODO | |
157 | } | |
ef22c1b6 | 158 | public static function delete_users($params) { |
159 | //TODO | |
160 | } | |
d4e13355 | 161 | public static function delete_users_returns() { |
162 | //TODO | |
163 | } | |
ef22c1b6 | 164 | |
165 | ||
d4e13355 | 166 | public static function update_users_parameters() { |
167 | //TODO | |
168 | } | |
ef22c1b6 | 169 | public static function update_users($params) { |
170 | //TODO | |
171 | } | |
d4e13355 | 172 | public static function update_users_returns() { |
173 | //TODO | |
174 | } | |
175 | ||
7b472b32 PS |
176 | /** |
177 | * Returns description of method parameters | |
178 | * @return external_function_parameters | |
179 | */ | |
d4e13355 | 180 | public static function get_users_parameters() { |
71864f15 PS |
181 | return new external_function_parameters( |
182 | array( | |
183 | 'userids' => new external_multiple_structure(new external_value(PARAM_INT, 'user ID')), | |
184 | ) | |
185 | ); | |
d4e13355 | 186 | } |
7b472b32 | 187 | |
71864f15 PS |
188 | /** |
189 | * Get user information | |
190 | * | |
191 | * @param array $userids array of user ids | |
192 | * @return array An array of arrays describing users | |
193 | */ | |
194 | public static function get_users($userids) { | |
5de592b1 | 195 | $context = get_context_instance(CONTEXT_SYSTEM); |
196 | require_capability('moodle/user:viewdetails', $context); | |
197 | self::validate_context($context); | |
198 | ||
71864f15 | 199 | $params = self::validate_parameters(self::get_users_parameters(), array('userids'=>$userids)); |
5de592b1 | 200 | |
201 | //TODO: this search is probably useless for external systems because it is not exact | |
202 | // 1/ we should specify multiple search parameters including the mnet host id | |
d4e13355 | 203 | // 2/ custom profile fileds not included |
204 | ||
205 | $result = array(); | |
71864f15 | 206 | /* |
d4e13355 | 207 | $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'); |
208 | foreach ($users as $user) { | |
209 | $result[] = (array)$user; | |
71864f15 PS |
210 | }*/ |
211 | ||
212 | return $result; | |
d4e13355 | 213 | } |
7b472b32 PS |
214 | |
215 | /** | |
216 | * Returns description of method result value | |
217 | * @return external_description | |
218 | */ | |
d4e13355 | 219 | public static function get_users_returns() { |
71864f15 PS |
220 | return new external_multiple_structure( |
221 | new external_single_structure( | |
222 | array( | |
223 | 'username' => new external_value(PARAM_RAW, 'Username policy is defined in Moodle security config'), | |
224 | 'firstname' => new external_value(PARAM_NOTAGS, 'The first name(s) of the user'), | |
225 | 'lastname' => new external_value(PARAM_NOTAGS, 'The family name of the user'), | |
226 | 'email' => new external_value(PARAM_EMAIL, 'A valid and unique email address'), | |
227 | 'auth' => new external_value(PARAM_SAFEDIR, 'Auth plugins include manual, ldap, imap, etc', false), | |
228 | 'confirmed' => new external_value(PARAM_NUMBER, 'Active user: 1 if confirmed, 0 otherwise', false), | |
229 | 'idnumber' => new external_value(PARAM_RAW, 'An arbitrary ID code number perhaps from the institution', false), | |
230 | 'emailstop' => new external_value(PARAM_NUMBER, 'Email is blocked: 1 is blocked and 0 otherwise', false), | |
231 | 'lang' => new external_value(PARAM_SAFEDIR, 'Language code such as "en_utf8", must exist on server', false), | |
232 | 'theme' => new external_value(PARAM_SAFEDIR, 'Theme name such as "standard", must exist on server', false), | |
233 | 'timezone' => new external_value(PARAM_ALPHANUMEXT, 'Timezone code such as Australia/Perth, or 99 for default', false), | |
234 | 'mailformat' => new external_value(PARAM_INTEGER, 'Mail format code is 0 for plain text, 1 for HTML etc', false), | |
235 | 'description' => new external_value(PARAM_TEXT, 'User profile description, as HTML', false), | |
236 | 'city' => new external_value(PARAM_NOTAGS, 'Home city of the user', false), | |
237 | 'country' => new external_value(PARAM_ALPHA, 'Home country code of the user, such as AU or CZ', false), | |
238 | 'customfields' => new external_multiple_structure( | |
239 | new external_single_structure( | |
240 | array( | |
241 | 'type' => new external_value(PARAM_ALPHANUMEXT, 'The name of the custom field'), | |
242 | 'value' => new external_value(PARAM_RAW, 'The value of the custom field') | |
243 | ) | |
244 | ), 'User custom fields', false) | |
245 | ) | |
246 | ) | |
247 | ); | |
5de592b1 | 248 | } |
5de592b1 | 249 | } |