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