Commit | Line | Data |
---|---|---|
86c252b4 | 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 | * Web services admin UI forms | |
20 | * | |
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 | |
24 | */ | |
9c954e88 | 25 | require_once $CFG->libdir . '/formslib.php'; |
86c252b4 | 26 | |
86dcc6f0 | 27 | /** |
28 | * Display the authorised user settings form | |
29 | * Including IP Restriction, Valid until and (TODO) capability | |
30 | */ | |
31 | class external_service_authorised_user_settings_form extends moodleform { | |
86dcc6f0 | 32 | |
9c954e88 | 33 | function definition() { |
86dcc6f0 | 34 | $mform = $this->_form; |
35 | $data = $this->_customdata; | |
36 | ||
9c954e88 | 37 | $mform->addElement('header', 'serviceusersettings', |
38 | get_string('serviceusersettings', 'webservice')); | |
86dcc6f0 | 39 | |
9c954e88 | 40 | $mform->addElement('text', 'iprestriction', |
41 | get_string('iprestriction', 'webservice')); | |
86dcc6f0 | 42 | $mform->addHelpButton('iprestriction', 'iprestriction', 'webservice'); |
43 | ||
9c954e88 | 44 | $mform->addElement('date_selector', 'validuntil', |
45 | get_string('validuntil', 'webservice'), array('optional' => true)); | |
86dcc6f0 | 46 | $mform->addHelpButton('validuntil', 'validuntil', 'webservice'); |
47 | ||
48 | $this->add_action_buttons(true, get_string('updateusersettings', 'webservice')); | |
49 | ||
50 | $this->set_data($data); | |
51 | } | |
86dcc6f0 | 52 | |
9c954e88 | 53 | } |
86dcc6f0 | 54 | |
86c252b4 | 55 | class external_service_form extends moodleform { |
86c252b4 | 56 | |
9c954e88 | 57 | function definition() { |
86c252b4 | 58 | $mform = $this->_form; |
75c386d4 | 59 | $service = isset($this->_customdata) ? $this->_customdata : new stdClass(); |
86c252b4 | 60 | |
9c954e88 | 61 | $mform->addElement('header', 'extservice', |
62 | get_string('externalservice', 'webservice')); | |
86c252b4 | 63 | |
64 | $mform->addElement('text', 'name', get_string('name')); | |
65 | $mform->addRule('name', get_string('required'), 'required', null, 'client'); | |
803ed741 | 66 | $mform->setType('name', PARAM_TEXT); |
86c252b4 | 67 | $mform->addElement('advcheckbox', 'enabled', get_string('enabled', 'webservice')); |
9c954e88 | 68 | $mform->addElement('advcheckbox', 'restrictedusers', |
69 | get_string('restrictedusers', 'webservice')); | |
9ef728d6 | 70 | $mform->addHelpButton('restrictedusers', 'restrictedusers', 'webservice'); |
cf9235d3 | 71 | |
af03513f JM |
72 | //can users download files |
73 | $mform->addElement('advcheckbox', 'downloadfiles', get_string('downloadfiles', 'webservice')); | |
74 | $mform->setAdvanced('downloadfiles'); | |
75 | $mform->addHelpButton('downloadfiles', 'downloadfiles', 'webservice'); | |
76 | ||
cf9235d3 | 77 | /// needed to select automatically the 'No required capability" option |
78 | $currentcapabilityexist = false; | |
9c954e88 | 79 | if (empty($service->requiredcapability)) { |
80 | $service->requiredcapability = "norequiredcapability"; | |
81 | $currentcapabilityexist = true; | |
cf9235d3 | 82 | } |
83 | ||
eab8ed9f | 84 | // Prepare the list of capabilities to choose from |
09179b78 | 85 | $systemcontext = get_context_instance(CONTEXT_SYSTEM); |
86 | $allcapabilities = fetch_context_capabilities($systemcontext); | |
87 | $capabilitychoices = array(); | |
9c954e88 | 88 | $capabilitychoices['norequiredcapability'] = get_string('norequiredcapability', |
89 | 'webservice'); | |
09179b78 | 90 | foreach ($allcapabilities as $cap) { |
9c954e88 | 91 | $capabilitychoices[$cap->name] = $cap->name . ': ' |
92 | . get_capability_string($cap->name); | |
93 | if (!empty($service->requiredcapability) | |
94 | && $service->requiredcapability == $cap->name) { | |
cf9235d3 | 95 | $currentcapabilityexist = true; |
96 | } | |
09179b78 | 97 | } |
98 | ||
9c954e88 | 99 | $mform->addElement('searchableselector', 'requiredcapability', |
100 | get_string('requiredcapability', 'webservice'), $capabilitychoices); | |
24a9a618 | 101 | $mform->addHelpButton('requiredcapability', 'requiredcapability', 'webservice'); |
64e2ed74 | 102 | $mform->setAdvanced('requiredcapability'); |
103 | /// display notification error if the current requiredcapability doesn't exist anymore | |
9c954e88 | 104 | if (empty($currentcapabilityexist)) { |
cf9235d3 | 105 | global $OUTPUT; |
9c954e88 | 106 | $mform->addElement('static', 'capabilityerror', '', |
107 | $OUTPUT->notification(get_string('selectedcapabilitydoesntexit', | |
108 | 'webservice', $service->requiredcapability))); | |
cf9235d3 | 109 | $service->requiredcapability = "norequiredcapability"; |
110 | } | |
9c954e88 | 111 | |
86c252b4 | 112 | $mform->addElement('hidden', 'id'); |
113 | $mform->setType('id', PARAM_INT); | |
114 | ||
c25662b0 | 115 | if (!empty($service->id)) { |
f38792ef | 116 | $buttonlabel = get_string('savechanges'); |
c25662b0 | 117 | } else { |
118 | $buttonlabel = get_string('addaservice', 'webservice'); | |
119 | } | |
120 | ||
121 | $this->add_action_buttons(true, $buttonlabel); | |
a591d387 | 122 | |
86c252b4 | 123 | $this->set_data($service); |
124 | } | |
125 | ||
126 | function definition_after_data() { | |
127 | $mform = $this->_form; | |
128 | $service = $this->_customdata; | |
129 | ||
130 | if (!empty($service->component)) { | |
131 | // built-in components must not be modified except the enabled flag!! | |
132 | $mform->hardFreeze('name,requiredcapability,restrictedusers'); | |
133 | } | |
134 | } | |
135 | ||
136 | function validation($data, $files) { | |
a591d387 | 137 | $errors = parent::validation($data, $files); |
86c252b4 | 138 | return $errors; |
139 | } | |
cd7a7891 | 140 | |
9c954e88 | 141 | } |
cd7a7891 | 142 | |
143 | class external_service_functions_form extends moodleform { | |
9c954e88 | 144 | |
cd7a7891 | 145 | function definition() { |
9c954e88 | 146 | global $CFG; |
cd7a7891 | 147 | |
148 | $mform = $this->_form; | |
149 | $data = $this->_customdata; | |
150 | ||
8399c714 | 151 | $mform->addElement('header', 'addfunction', get_string('addfunctions', 'webservice')); |
cd7a7891 | 152 | |
9c954e88 | 153 | require_once($CFG->dirroot . "/webservice/lib.php"); |
154 | $webservicemanager = new webservice(); | |
155 | $functions = $webservicemanager->get_not_associated_external_functions($data['id']); | |
cd7a7891 | 156 | |
44610991 | 157 | //we add the descriptions to the functions |
158 | foreach ($functions as $functionid => $functionname) { | |
9c954e88 | 159 | //retrieve full function information (including the description) |
160 | $function = external_function_info($functionname); | |
161 | $functions[$functionid] = $function->name . ':' . $function->description; | |
44610991 | 162 | } |
163 | ||
e1cf6b4b | 164 | $mform->addElement('searchableselector', 'fids', get_string('name'), |
206dd861 | 165 | $functions, array('multiple')); |
cd7a7891 | 166 | |
167 | $mform->addElement('hidden', 'id'); | |
168 | $mform->setType('id', PARAM_INT); | |
169 | ||
170 | $mform->addElement('hidden', 'action'); | |
171 | $mform->setType('action', PARAM_ACTION); | |
172 | ||
8399c714 | 173 | $this->add_action_buttons(true, get_string('addfunctions', 'webservice')); |
cd7a7891 | 174 | |
175 | $this->set_data($data); | |
176 | } | |
15e417fe | 177 | |
9c954e88 | 178 | } |
15e417fe | 179 | |
180 | class web_service_token_form extends moodleform { | |
9c954e88 | 181 | |
15e417fe | 182 | function definition() { |
94b9dad7 | 183 | global $USER, $DB, $CFG; |
15e417fe | 184 | |
185 | $mform = $this->_form; | |
186 | $data = $this->_customdata; | |
187 | ||
188 | $mform->addElement('header', 'token', get_string('token', 'webservice')); | |
189 | ||
5eacbd4b | 190 | if (empty($data->nouserselection)) { |
f1f31c1d JM |
191 | |
192 | //check if the number of user is reasonable to be displayed in a select box | |
193 | $usertotal = $DB->count_records('user', | |
194 | array('deleted' => 0, 'suspended' => 0, 'confirmed' => 1)); | |
195 | ||
196 | if ($usertotal < 500) { | |
197 | //user searchable selector - get all users (admin and guest included) | |
3f14cf7f AB |
198 | //user must be confirmed, not deleted, not suspended, not guest |
199 | $sql = "SELECT u.id, u.firstname, u.lastname | |
bbcde38b SH |
200 | FROM {user} u |
201 | WHERE u.deleted = 0 AND u.confirmed = 1 AND u.suspended = 0 AND u.id != ? | |
202 | ORDER BY u.lastname"; | |
3f14cf7f | 203 | $users = $DB->get_records_sql($sql, array($CFG->siteguest)); |
f1f31c1d JM |
204 | $options = array(); |
205 | foreach ($users as $userid => $user) { | |
206 | $options[$userid] = $user->firstname . " " . $user->lastname; | |
207 | } | |
208 | $mform->addElement('searchableselector', 'user', get_string('user'), $options); | |
209 | } else { | |
210 | //simple text box for username or user id (if two username exists, a form error is displayed) | |
211 | $mform->addElement('text', 'user', get_string('usernameorid', 'webservice')); | |
15e417fe | 212 | } |
5eacbd4b | 213 | $mform->addRule('user', get_string('required'), 'required', null, 'client'); |
15e417fe | 214 | } |
15e417fe | 215 | |
216 | //service selector | |
217 | $services = $DB->get_records('external_services'); | |
218 | $options = array(); | |
5eacbd4b | 219 | $systemcontext = get_context_instance(CONTEXT_SYSTEM); |
15e417fe | 220 | foreach ($services as $serviceid => $service) { |
9c954e88 | 221 | //check that the user has the required capability |
eab8ed9f | 222 | //(only for generation by the profile page) |
9c954e88 | 223 | if (empty($data->nouserselection) |
224 | || empty($service->requiredcapability) | |
225 | || has_capability($service->requiredcapability, $systemcontext, $USER->id)) { | |
5eacbd4b | 226 | $options[$serviceid] = $service->name; |
227 | } | |
15e417fe | 228 | } |
9c954e88 | 229 | $mform->addElement('select', 'service', get_string('service', 'webservice'), $options); |
15e417fe | 230 | $mform->addRule('service', get_string('required'), 'required', null, 'client'); |
9c954e88 | 231 | |
232 | ||
15e417fe | 233 | $mform->addElement('text', 'iprestriction', get_string('iprestriction', 'webservice')); |
234 | ||
9c954e88 | 235 | $mform->addElement('date_selector', 'validuntil', |
236 | get_string('validuntil', 'webservice'), array('optional' => true)); | |
15e417fe | 237 | |
238 | $mform->addElement('hidden', 'action'); | |
239 | $mform->setType('action', PARAM_ACTION); | |
240 | ||
241 | $this->add_action_buttons(true); | |
242 | ||
243 | $this->set_data($data); | |
244 | } | |
245 | ||
f1f31c1d JM |
246 | function get_data() { |
247 | global $DB; | |
248 | $data = parent::get_data(); | |
249 | ||
250 | if (!empty($data) && !is_numeric($data->user)) { | |
251 | //retrieve username | |
252 | $user = $DB->get_record('user', array('username' => $data->user), 'id'); | |
253 | $data->user = $user->id; | |
254 | } | |
255 | return $data; | |
256 | } | |
257 | ||
54352ac9 | 258 | function validation($data, $files) { |
f1f31c1d JM |
259 | global $DB; |
260 | ||
15e417fe | 261 | $errors = parent::validation($data, $files); |
f1f31c1d JM |
262 | |
263 | if (is_numeric($data['user'])) { | |
264 | $searchtype = 'id'; | |
265 | } else { | |
266 | $searchtype = 'username'; | |
267 | //check the username is valid | |
268 | if (clean_param($data['user'], PARAM_USERNAME) != $data['user']) { | |
269 | $errors['user'] = get_string('invalidusername'); | |
270 | } | |
271 | } | |
272 | ||
273 | if (!isset($errors['user'])) { | |
274 | $users = $DB->get_records('user', array($searchtype => $data['user']), '', 'id'); | |
275 | ||
276 | //check that the user exists in the database | |
277 | if (count($users) == 0) { | |
278 | $errors['user'] = get_string('usernameoridnousererror', 'webservice'); | |
279 | } else if (count($users) > 1) { //can only be a username search as id are unique | |
280 | $errors['user'] = get_string('usernameoridoccurenceerror', 'webservice'); | |
281 | } | |
282 | } | |
283 | ||
15e417fe | 284 | return $errors; |
285 | } | |
9c954e88 | 286 | |
15e417fe | 287 | } |