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 | |
d4e13355 |
31 | public static function create_users_parameters() { |
35b9a80a |
32 | return new external_function_parameters( |
33 | array( |
34 | 'users' => new external_multiple_structure( |
35 | new external_single_structure( |
36 | array( |
37 | 'username' => new external_value(PARAM_USERNAME, 'Username policy is defined in Moodle security config'), |
38 | 'password' => new external_value(PARAM_RAW, 'Moodle passwords can consist of any character'), |
39 | 'firstname' => new external_value(PARAM_NOTAGS, 'The first name(s) of the user'), |
40 | 'lastname' => new external_value(PARAM_NOTAGS, 'The family name of the user'), |
41 | 'email' => new external_value(PARAM_EMAIL, 'A valid and unique email address'), |
42 | 'auth' => new external_value(PARAM_AUTH, 'Auth plugins include manual, ldap, imap, etc', false), |
43 | 'confirmed' => new external_value(PARAM_NUMBER, 'Active user: 1 if confirmed, 0 otherwise', false), |
44 | 'idnumber' => new external_value(PARAM_RAW, 'An arbitrary ID code number perhaps from the institution', false), |
45 | 'emailstop' => new external_value(PARAM_NUMBER, 'Email is blocked: 1 is blocked and 0 otherwise', false), |
46 | 'lang' => new external_value(PARAM_LANG, 'Language code such as "en_utf8", must exist on server', false), |
47 | 'theme' => new external_value(PARAM_THEME, 'Theme name such as "standard", must exist on server', false), |
48 | 'timezone' => new external_value(PARAM_ALPHANUMEXT, 'Timezone code such as Australia/Perth, or 99 for default', false), |
49 | 'mailformat' => new external_value(PARAM_INTEGER, 'Mail format code is 0 for plain text, 1 for HTML etc', false), |
50 | 'description' => new external_value(PARAM_TEXT, 'User profile description, as HTML', false), |
51 | 'city' => new external_value(PARAM_NOTAGS, 'Home city of the user', false), |
52 | 'country' => new external_value(PARAM_ALPHA, 'Home country code of the user, such as AU or CZ', false), |
53 | 'preferences' => new external_multiple_structure( |
54 | new external_single_structure( |
55 | array( |
56 | 'type' => new external_value(PARAM_ALPHANUMEXT, 'The name of the preference'), |
57 | 'value' => new external_value(PARAM_RAW, 'The value of the preference') |
58 | ) |
59 | ), 'User preferences', false), |
60 | 'customfields' => new external_multiple_structure( |
61 | new external_single_structure( |
62 | array( |
63 | 'type' => new external_value(PARAM_ALPHANUMEXT, 'The name of the custom field'), |
64 | 'value' => new external_value(PARAM_RAW, 'The value of the custom field') |
65 | ) |
66 | ), 'User custom fields', false) |
67 | ) |
68 | ) |
69 | ) |
70 | ) |
71 | ); |
625f0a24 |
72 | } |
73 | |
d4e13355 |
74 | /** |
5de592b1 |
75 | * Create one or more users |
76 | * |
35b9a80a |
77 | * @param array $params An array of users to create. Each user is defined by $usertocreate above. |
d4e13355 |
78 | * |
35b9a80a |
79 | * @return array An array of userids, one for each user that was created |
5de592b1 |
80 | */ |
35b9a80a |
81 | public static function create_users($userlist) { |
ef22c1b6 |
82 | global $CFG, $DB; |
35b9a80a |
83 | //varlog('I\'m in create_users()'); |
5de592b1 |
84 | // Ensure the current user is allowed to run this function |
ef22c1b6 |
85 | $context = get_context_instance(CONTEXT_SYSTEM); |
5de592b1 |
86 | require_capability('moodle/user:create', $context); |
ef22c1b6 |
87 | self::validate_context($context); |
88 | |
5de592b1 |
89 | // Do basic automatic PARAM checks on incoming data, using params description |
90 | // This checks to make sure that: |
91 | // 1) No extra data was sent |
d4e13355 |
92 | // 2) All required items were sent |
5de592b1 |
93 | // 3) All data passes clean_param without changes (yes this is strict) |
94 | // If any problems are found then exceptions are thrown with helpful error messages |
35b9a80a |
95 | $params = self::validate_parameters(self::create_users_parameters(), array('users'=>$userlist)); |
5de592b1 |
96 | |
97 | |
98 | // Perform further checks and build up a clean array of user data |
99 | // Nothing is actually performed until the whole dataset is checked |
ef22c1b6 |
100 | $users = array(); |
35b9a80a |
101 | foreach ($userlist as $user) { |
5de592b1 |
102 | |
103 | // Empty or no auth is assumed to be manual |
d4e13355 |
104 | if (empty($user['auth'])) { |
ef22c1b6 |
105 | $user['auth'] = 'manual'; |
106 | } |
ef22c1b6 |
107 | |
5de592b1 |
108 | // Lang must be a real code, not empty string |
109 | if (isset($user['lang']) && empty($user['lang'])) { |
ef22c1b6 |
110 | unset($user['lang']); |
111 | } |
112 | |
5de592b1 |
113 | // Make sure that the username doesn't already exist |
ef22c1b6 |
114 | if ($DB->get_record('user', array('username'=>$user['username'], 'mnethostid'=>$CFG->mnet_localhost_id))) { |
115 | throw new invalid_parameter_exception($user['username']." username is already taken, sorry"); |
116 | } |
117 | |
5de592b1 |
118 | // Make sure that incoming data doesn't contain duplicate usernames |
ef22c1b6 |
119 | if (isset($users[$user['username']])) { |
120 | throw new invalid_parameter_exception("multiple users with the same username requested"); |
121 | } |
5de592b1 |
122 | |
123 | // TODO: More checks here? |
124 | |
125 | $users[$user['username']] = $user; // Add this data to an array (mem overflows?) |
ef22c1b6 |
126 | } |
127 | |
128 | $result = array(); |
129 | |
5de592b1 |
130 | foreach ($users as $user) { // Actually create the user accounts now |
ef22c1b6 |
131 | $record = create_user_record($user['username'], $user['password'], $user['auth']); |
132 | unset($user['username']); |
133 | unset($user['password']); |
134 | unset($user['auth']); |
135 | |
136 | // now override the default (or external) values |
137 | foreach ($user as $key=>$value) { |
138 | $record->$key = $value; |
139 | } |
140 | $DB->update_record('user', $record); |
141 | |
d4e13355 |
142 | //TODO: preferences and custom fields |
143 | |
5de592b1 |
144 | $result[] = $record->id; |
145 | |
146 | // TODO: Save all the preferences and custom fields here |
147 | |
ef22c1b6 |
148 | } |
149 | |
150 | return $result; |
151 | } |
d4e13355 |
152 | public static function create_users_returns() { |
ef22c1b6 |
153 | |
d4e13355 |
154 | //TODO: the format of the description is not decided yet |
ef22c1b6 |
155 | |
d4e13355 |
156 | $createusersreturn = new object(); |
157 | $createusersreturn->userids = array('multiple' => PARAM_NUMBER); |
158 | |
159 | return $createusersreturn; |
160 | } |
161 | |
162 | |
163 | public static function delete_users_parameters() { |
164 | //TODO |
165 | } |
ef22c1b6 |
166 | public static function delete_users($params) { |
167 | //TODO |
168 | } |
d4e13355 |
169 | public static function delete_users_returns() { |
170 | //TODO |
171 | } |
ef22c1b6 |
172 | |
173 | |
d4e13355 |
174 | public static function update_users_parameters() { |
175 | //TODO |
176 | } |
ef22c1b6 |
177 | public static function update_users($params) { |
178 | //TODO |
179 | } |
d4e13355 |
180 | public static function update_users_returns() { |
181 | //TODO |
182 | } |
183 | |
184 | public static function get_users_parameters() { |
5de592b1 |
185 | |
d4e13355 |
186 | } |
5de592b1 |
187 | public static function get_users($params) { |
188 | $context = get_context_instance(CONTEXT_SYSTEM); |
189 | require_capability('moodle/user:viewdetails', $context); |
190 | self::validate_context($context); |
191 | |
c9c5cc81 |
192 | $params = self::validate_parameters(self::get_users_parameters(), $params); |
5de592b1 |
193 | |
194 | //TODO: this search is probably useless for external systems because it is not exact |
195 | // 1/ we should specify multiple search parameters including the mnet host id |
d4e13355 |
196 | // 2/ custom profile fileds not included |
197 | |
198 | $result = array(); |
199 | |
200 | $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'); |
201 | foreach ($users as $user) { |
202 | $result[] = (array)$user; |
203 | } |
204 | } |
205 | public static function get_users_returns() { |
5de592b1 |
206 | |
5de592b1 |
207 | } |
208 | |
209 | } |