Merge branch 'MDL-38700-24' of git://github.com/danpoltawski/moodle into MOODLE_24_STABLE
[moodle.git] / admin / webservice / forms.php
blob745e9e186b4952f6bf541f90498aa3e0c2afceae
1 <?php
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.
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/>.
18 /**
19 * Web services admin UI forms
21 * @package webservice
22 * @copyright 2009 Moodle Pty Ltd (http://moodle.com)
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 require_once $CFG->libdir . '/formslib.php';
27 /**
28 * Display the authorised user settings form
29 * Including IP Restriction, Valid until and (TODO) capability
31 class external_service_authorised_user_settings_form extends moodleform {
33 function definition() {
34 $mform = $this->_form;
35 $data = $this->_customdata;
37 $mform->addElement('header', 'serviceusersettings',
38 get_string('serviceusersettings', 'webservice'));
40 $mform->addElement('text', 'iprestriction',
41 get_string('iprestriction', 'webservice'));
42 $mform->addHelpButton('iprestriction', 'iprestriction', 'webservice');
44 $mform->addElement('date_selector', 'validuntil',
45 get_string('validuntil', 'webservice'), array('optional' => true));
46 $mform->addHelpButton('validuntil', 'validuntil', 'webservice');
48 $this->add_action_buttons(true, get_string('updateusersettings', 'webservice'));
50 $this->set_data($data);
55 class external_service_form extends moodleform {
57 function definition() {
58 $mform = $this->_form;
59 $service = isset($this->_customdata) ? $this->_customdata : new stdClass();
61 $mform->addElement('header', 'extservice',
62 get_string('externalservice', 'webservice'));
64 $mform->addElement('text', 'name', get_string('name'));
65 $mform->addRule('name', get_string('required'), 'required', null, 'client');
66 $mform->setType('name', PARAM_TEXT);
67 $mform->addElement('advcheckbox', 'enabled', get_string('enabled', 'webservice'));
68 $mform->addElement('advcheckbox', 'restrictedusers',
69 get_string('restrictedusers', 'webservice'));
70 $mform->addHelpButton('restrictedusers', 'restrictedusers', 'webservice');
72 //can users download files
73 $mform->addElement('advcheckbox', 'downloadfiles', get_string('downloadfiles', 'webservice'));
74 $mform->setAdvanced('downloadfiles');
75 $mform->addHelpButton('downloadfiles', 'downloadfiles', 'webservice');
77 /// needed to select automatically the 'No required capability" option
78 $currentcapabilityexist = false;
79 if (empty($service->requiredcapability)) {
80 $service->requiredcapability = "norequiredcapability";
81 $currentcapabilityexist = true;
84 // Prepare the list of capabilities to choose from
85 $systemcontext = context_system::instance();
86 $allcapabilities = fetch_context_capabilities($systemcontext);
87 $capabilitychoices = array();
88 $capabilitychoices['norequiredcapability'] = get_string('norequiredcapability',
89 'webservice');
90 foreach ($allcapabilities as $cap) {
91 $capabilitychoices[$cap->name] = $cap->name . ': '
92 . get_capability_string($cap->name);
93 if (!empty($service->requiredcapability)
94 && $service->requiredcapability == $cap->name) {
95 $currentcapabilityexist = true;
99 $mform->addElement('searchableselector', 'requiredcapability',
100 get_string('requiredcapability', 'webservice'), $capabilitychoices);
101 $mform->addHelpButton('requiredcapability', 'requiredcapability', 'webservice');
102 $mform->setAdvanced('requiredcapability');
103 /// display notification error if the current requiredcapability doesn't exist anymore
104 if (empty($currentcapabilityexist)) {
105 global $OUTPUT;
106 $mform->addElement('static', 'capabilityerror', '',
107 $OUTPUT->notification(get_string('selectedcapabilitydoesntexit',
108 'webservice', $service->requiredcapability)));
109 $service->requiredcapability = "norequiredcapability";
112 $mform->addElement('hidden', 'id');
113 $mform->setType('id', PARAM_INT);
115 if (!empty($service->id)) {
116 $buttonlabel = get_string('savechanges');
117 } else {
118 $buttonlabel = get_string('addaservice', 'webservice');
121 $this->add_action_buttons(true, $buttonlabel);
123 $this->set_data($service);
126 function definition_after_data() {
127 $mform = $this->_form;
128 $service = $this->_customdata;
130 if (!empty($service->component)) {
131 // built-in components must not be modified except the enabled flag!!
132 $mform->hardFreeze('name,requiredcapability,restrictedusers');
136 function validation($data, $files) {
137 $errors = parent::validation($data, $files);
138 return $errors;
143 class external_service_functions_form extends moodleform {
145 function definition() {
146 global $CFG;
148 $mform = $this->_form;
149 $data = $this->_customdata;
151 $mform->addElement('header', 'addfunction', get_string('addfunctions', 'webservice'));
153 require_once($CFG->dirroot . "/webservice/lib.php");
154 $webservicemanager = new webservice();
155 $functions = $webservicemanager->get_not_associated_external_functions($data['id']);
157 //we add the descriptions to the functions
158 foreach ($functions as $functionid => $functionname) {
159 //retrieve full function information (including the description)
160 $function = external_function_info($functionname);
161 $functions[$functionid] = $function->name . ':' . $function->description;
164 $mform->addElement('searchableselector', 'fids', get_string('name'),
165 $functions, array('multiple'));
166 $mform->addRule('fids', get_string('required'), 'required', null, 'client');
168 $mform->addElement('hidden', 'id');
169 $mform->setType('id', PARAM_INT);
171 $mform->addElement('hidden', 'action');
172 $mform->setType('action', PARAM_ALPHANUMEXT);
174 $this->add_action_buttons(true, get_string('addfunctions', 'webservice'));
176 $this->set_data($data);
181 class web_service_token_form extends moodleform {
183 function definition() {
184 global $USER, $DB, $CFG;
186 $mform = $this->_form;
187 $data = $this->_customdata;
189 $mform->addElement('header', 'token', get_string('token', 'webservice'));
191 if (empty($data->nouserselection)) {
193 //check if the number of user is reasonable to be displayed in a select box
194 $usertotal = $DB->count_records('user',
195 array('deleted' => 0, 'suspended' => 0, 'confirmed' => 1));
197 if ($usertotal < 500) {
198 list($sort, $params) = users_order_by_sql('u');
199 //user searchable selector - get all users (admin and guest included)
200 //user must be confirmed, not deleted, not suspended, not guest
201 $sql = "SELECT u.id, u.firstname, u.lastname
202 FROM {user} u
203 WHERE u.deleted = 0 AND u.confirmed = 1 AND u.suspended = 0 AND u.id != :siteguestid
204 ORDER BY $sort";
205 $params['siteguestid'] = $CFG->siteguest;
206 $users = $DB->get_records_sql($sql, $params);
207 $options = array();
208 foreach ($users as $userid => $user) {
209 $options[$userid] = fullname($user);
211 $mform->addElement('searchableselector', 'user', get_string('user'), $options);
212 } else {
213 //simple text box for username or user id (if two username exists, a form error is displayed)
214 $mform->addElement('text', 'user', get_string('usernameorid', 'webservice'));
216 $mform->addRule('user', get_string('required'), 'required', null, 'client');
219 //service selector
220 $services = $DB->get_records('external_services');
221 $options = array();
222 $systemcontext = context_system::instance();
223 foreach ($services as $serviceid => $service) {
224 //check that the user has the required capability
225 //(only for generation by the profile page)
226 if (empty($data->nouserselection)
227 || empty($service->requiredcapability)
228 || has_capability($service->requiredcapability, $systemcontext, $USER->id)) {
229 $options[$serviceid] = $service->name;
232 $mform->addElement('select', 'service', get_string('service', 'webservice'), $options);
233 $mform->addRule('service', get_string('required'), 'required', null, 'client');
236 $mform->addElement('text', 'iprestriction', get_string('iprestriction', 'webservice'));
238 $mform->addElement('date_selector', 'validuntil',
239 get_string('validuntil', 'webservice'), array('optional' => true));
241 $mform->addElement('hidden', 'action');
242 $mform->setType('action', PARAM_ALPHANUMEXT);
244 $this->add_action_buttons(true);
246 $this->set_data($data);
249 function get_data() {
250 global $DB;
251 $data = parent::get_data();
253 if (!empty($data) && !is_numeric($data->user)) {
254 //retrieve username
255 $user = $DB->get_record('user', array('username' => $data->user), 'id');
256 $data->user = $user->id;
258 return $data;
261 function validation($data, $files) {
262 global $DB;
264 $errors = parent::validation($data, $files);
266 if (is_numeric($data['user'])) {
267 $searchtype = 'id';
268 } else {
269 $searchtype = 'username';
270 //check the username is valid
271 if (clean_param($data['user'], PARAM_USERNAME) != $data['user']) {
272 $errors['user'] = get_string('invalidusername');
276 if (!isset($errors['user'])) {
277 $users = $DB->get_records('user', array($searchtype => $data['user']), '', 'id');
279 //check that the user exists in the database
280 if (count($users) == 0) {
281 $errors['user'] = get_string('usernameoridnousererror', 'webservice');
282 } else if (count($users) > 1) { //can only be a username search as id are unique
283 $errors['user'] = get_string('usernameoridoccurenceerror', 'webservice');
287 return $errors;