MDL-12886 more external users api
[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 *
35b9a80a 81 * @param array $params An array of users to create. Each user is defined by $usertocreate above.
d4e13355 82 *
35b9a80a 83 * @return array An array of userids, one for each user that was created
5de592b1 84 */
7b472b32 85 public static function create_users($users) {
ef22c1b6 86 global $CFG, $DB;
7b472b32 87
5de592b1 88 // Ensure the current user is allowed to run this function
ef22c1b6 89 $context = get_context_instance(CONTEXT_SYSTEM);
5de592b1 90 require_capability('moodle/user:create', $context);
ef22c1b6 91 self::validate_context($context);
92
5de592b1 93 // Do basic automatic PARAM checks on incoming data, using params description
94 // This checks to make sure that:
95 // 1) No extra data was sent
d4e13355 96 // 2) All required items were sent
5de592b1 97 // 3) All data passes clean_param without changes (yes this is strict)
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
5de592b1 101
7b472b32 102 // TODO delegated transaction
5de592b1 103
ef22c1b6 104 $users = array();
7b472b32 105 foreach ($params['users'] as $user) {
5de592b1 106
107 // Empty or no auth is assumed to be manual
d4e13355 108 if (empty($user['auth'])) {
ef22c1b6 109 $user['auth'] = 'manual';
110 }
ef22c1b6 111
5de592b1 112 // Lang must be a real code, not empty string
113 if (isset($user['lang']) && empty($user['lang'])) {
ef22c1b6 114 unset($user['lang']);
115 }
116
5de592b1 117 // Make sure that the username doesn't already exist
7b472b32 118 if ($DB->record_exists('user', array('username'=>$user['username'], 'mnethostid'=>$CFG->mnet_localhost_id))) {
ef22c1b6 119 throw new invalid_parameter_exception($user['username']." username is already taken, sorry");
120 }
121
5de592b1 122 // Make sure that incoming data doesn't contain duplicate usernames
ef22c1b6 123 if (isset($users[$user['username']])) {
124 throw new invalid_parameter_exception("multiple users with the same username requested");
125 }
5de592b1 126
7b472b32 127 //TODO: validate username, auth, lang and theme
5de592b1 128
7b472b32 129 // finally create user
ef22c1b6 130 $record = create_user_record($user['username'], $user['password'], $user['auth']);
ef22c1b6 131
d4e13355 132 //TODO: preferences and custom fields
133
7b472b32 134 $users[] = array('id'=>$record->id, 'username'=>$record->username);
ef22c1b6 135 }
136
7b472b32 137 return $users;
ef22c1b6 138 }
139
7b472b32
PS
140 /**
141 * Returns description of method result value
142 * @return external_description
143 */
144 public static function create_users_returns() {
145 return new external_multiple_structure(
146 new external_single_structure(
147 array(
148 'id' => new external_value(PARAM_INT, 'user id'),
149 'username' => new external_value(PARAM_RAW, 'user name'),
150 )
151 )
152 );
d4e13355 153 }
154
155
156 public static function delete_users_parameters() {
157 //TODO
158 }
ef22c1b6 159 public static function delete_users($params) {
160 //TODO
161 }
d4e13355 162 public static function delete_users_returns() {
163 //TODO
164 }
ef22c1b6 165
166
d4e13355 167 public static function update_users_parameters() {
168 //TODO
169 }
ef22c1b6 170 public static function update_users($params) {
171 //TODO
172 }
d4e13355 173 public static function update_users_returns() {
174 //TODO
175 }
176
7b472b32
PS
177 /**
178 * Returns description of method parameters
179 * @return external_function_parameters
180 */
d4e13355 181 public static function get_users_parameters() {
5de592b1 182
d4e13355 183 }
7b472b32 184
5de592b1 185 public static function get_users($params) {
186 $context = get_context_instance(CONTEXT_SYSTEM);
187 require_capability('moodle/user:viewdetails', $context);
188 self::validate_context($context);
189
c9c5cc81 190 $params = self::validate_parameters(self::get_users_parameters(), $params);
5de592b1 191
192 //TODO: this search is probably useless for external systems because it is not exact
193 // 1/ we should specify multiple search parameters including the mnet host id
d4e13355 194 // 2/ custom profile fileds not included
195
196 $result = array();
197
198 $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');
199 foreach ($users as $user) {
200 $result[] = (array)$user;
201 }
202 }
7b472b32
PS
203
204 /**
205 * Returns description of method result value
206 * @return external_description
207 */
d4e13355 208 public static function get_users_returns() {
5de592b1 209
5de592b1 210 }
211
212}