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