MDL-78962 core/loadingicon: remove jQuery requirement in the API
[moodle.git] / admin / webservice / forms.php
blob4f15173f35c2f67e1ba3a78e6f31232d89e9a44c
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');
43 $mform->setType('iprestriction', PARAM_RAW_TRIMMED);
45 $mform->addElement('date_selector', 'validuntil',
46 get_string('validuntil', 'webservice'), array('optional' => true));
47 $mform->addHelpButton('validuntil', 'validuntil', 'webservice');
48 $mform->setType('validuntil', PARAM_INT);
50 $this->add_action_buttons(true, get_string('updateusersettings', 'webservice'));
52 $this->set_data($data);
57 class external_service_form extends moodleform {
59 function definition() {
60 $mform = $this->_form;
61 $service = isset($this->_customdata) ? $this->_customdata : new stdClass();
63 $mform->addElement('header', 'extservice',
64 get_string('externalservice', 'webservice'));
66 $mform->addElement('text', 'name', get_string('name'));
67 $mform->addRule('name', get_string('required'), 'required', null, 'client');
68 $mform->setType('name', PARAM_TEXT);
70 $mform->addElement('text', 'shortname', get_string('shortname'), 'maxlength="255" size="20"');
71 $mform->setType('shortname', PARAM_TEXT);
72 if (!empty($service->id)) {
73 $mform->hardFreeze('shortname');
74 $mform->setConstants('shortname', $service->shortname);
77 $mform->addElement('advcheckbox', 'enabled', get_string('enabled', 'webservice'));
78 $mform->setType('enabled', PARAM_BOOL);
79 $mform->addElement('advcheckbox', 'restrictedusers',
80 get_string('restrictedusers', 'webservice'));
81 $mform->addHelpButton('restrictedusers', 'restrictedusers', 'webservice');
82 $mform->setType('restrictedusers', PARAM_BOOL);
84 // Can users download files?
85 $mform->addElement('advcheckbox', 'downloadfiles', get_string('downloadfiles', 'webservice'));
86 $mform->setAdvanced('downloadfiles');
87 $mform->addHelpButton('downloadfiles', 'downloadfiles', 'webservice');
88 $mform->setType('downloadfiles', PARAM_BOOL);
90 // Can users upload files?
91 $mform->addElement('advcheckbox', 'uploadfiles', get_string('uploadfiles', 'webservice'));
92 $mform->setAdvanced('uploadfiles');
93 $mform->addHelpButton('uploadfiles', 'uploadfiles', 'webservice');
95 /// needed to select automatically the 'No required capability" option
96 $currentcapabilityexist = false;
97 if (empty($service->requiredcapability)) {
98 $service->requiredcapability = "norequiredcapability";
99 $currentcapabilityexist = true;
102 // Prepare the list of capabilities to choose from
103 $systemcontext = context_system::instance();
104 $allcapabilities = $systemcontext->get_capabilities();
105 $capabilitychoices = array();
106 $capabilitychoices['norequiredcapability'] = get_string('norequiredcapability',
107 'webservice');
108 foreach ($allcapabilities as $cap) {
109 $capabilitychoices[$cap->name] = $cap->name . ': '
110 . get_capability_string($cap->name);
111 if (!empty($service->requiredcapability)
112 && $service->requiredcapability == $cap->name) {
113 $currentcapabilityexist = true;
117 $mform->addElement('searchableselector', 'requiredcapability',
118 get_string('requiredcapability', 'webservice'), $capabilitychoices);
119 $mform->addHelpButton('requiredcapability', 'requiredcapability', 'webservice');
120 $mform->setAdvanced('requiredcapability');
121 $mform->setType('requiredcapability', PARAM_RAW);
122 /// display notification error if the current requiredcapability doesn't exist anymore
123 if (empty($currentcapabilityexist)) {
124 global $OUTPUT;
125 $mform->addElement('static', 'capabilityerror', '',
126 $OUTPUT->notification(get_string('selectedcapabilitydoesntexit',
127 'webservice', $service->requiredcapability)));
128 $service->requiredcapability = "norequiredcapability";
131 $mform->addElement('hidden', 'id');
132 $mform->setType('id', PARAM_INT);
134 if (!empty($service->id)) {
135 $buttonlabel = get_string('savechanges');
136 } else {
137 $buttonlabel = get_string('addaservice', 'webservice');
140 $this->add_action_buttons(true, $buttonlabel);
142 $this->set_data($service);
145 function definition_after_data() {
146 $mform = $this->_form;
147 $service = $this->_customdata;
149 if (!empty($service->component)) {
150 // built-in components must not be modified except the enabled flag!!
151 $mform->hardFreeze('name,requiredcapability,restrictedusers');
155 function validation($data, $files) {
156 global $DB;
158 $errors = parent::validation($data, $files);
160 // Add field validation check for duplicate name.
161 if ($webservice = $DB->get_record('external_services', array('name' => $data['name']))) {
162 if (empty($data['id']) || $webservice->id != $data['id']) {
163 $errors['name'] = get_string('nameexists', 'webservice');
167 // Add field validation check for duplicate shortname.
168 // Allow duplicated "empty" shortnames.
169 if (!empty($data['shortname'])) {
170 if ($service = $DB->get_record('external_services', array('shortname' => $data['shortname']), '*', IGNORE_MULTIPLE)) {
171 if (empty($data['id']) || $service->id != $data['id']) {
172 $errors['shortname'] = get_string('shortnametaken', 'webservice', $service->name);
177 return $errors;
182 class external_service_functions_form extends moodleform {
184 function definition() {
185 global $CFG;
187 $mform = $this->_form;
188 $data = $this->_customdata;
190 $mform->addElement('header', 'addfunction', get_string('addfunctions', 'webservice'));
192 require_once($CFG->dirroot . "/webservice/lib.php");
193 $webservicemanager = new webservice();
194 $functions = $webservicemanager->get_not_associated_external_functions($data['id']);
196 //we add the descriptions to the functions
197 foreach ($functions as $functionid => $functionname) {
198 //retrieve full function information (including the description)
199 $function = \core_external\external_api::external_function_info($functionname);
200 if (empty($function->deprecated)) {
201 $functions[$functionid] = $function->name . ':' . $function->description;
202 } else {
203 // Exclude the deprecated ones.
204 unset($functions[$functionid]);
208 $mform->addElement('searchableselector', 'fids', get_string('name'),
209 $functions, array('multiple'));
210 $mform->addRule('fids', get_string('required'), 'required', null, 'client');
212 $mform->addElement('hidden', 'id');
213 $mform->setType('id', PARAM_INT);
215 $mform->addElement('hidden', 'action');
216 $mform->setType('action', PARAM_ALPHANUMEXT);
218 $this->add_action_buttons(true, get_string('addfunctions', 'webservice'));
220 $this->set_data($data);