f1908eb6c290e4a52e47fb3c60e8f0cf946eb1dd
[moodle.git] / admin / webservice / forms.php
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.
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/>.
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  */
25 require_once $CFG->libdir . '/formslib.php';
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 {
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);
51     }
53 }
55 class external_service_form extends moodleform {
57     function definition() {
58         $mform = $this->_form;
59         $service = $this->_customdata;
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->addElement('advcheckbox', 'enabled', get_string('enabled', 'webservice'));
67         $mform->addElement('advcheckbox', 'restrictedusers',
68                 get_string('restrictedusers', 'webservice'));
69         $mform->addHelpButton('restrictedusers', 'restrictedusers', 'webservice');
71         //can users download files
72         $mform->addElement('advcheckbox', 'downloadfiles', get_string('downloadfiles', 'webservice'));
73         $mform->setAdvanced('downloadfiles');
74         $mform->addHelpButton('downloadfiles', 'downloadfiles', 'webservice');
76         /// needed to select automatically the 'No required capability" option
77         $currentcapabilityexist = false;
78         if (empty($service->requiredcapability)) {
79             $service->requiredcapability = "norequiredcapability";
80             $currentcapabilityexist = true;
81         }
83         // Prepare the list of capabilities to choose from
84         $systemcontext = get_context_instance(CONTEXT_SYSTEM);
85         $allcapabilities = fetch_context_capabilities($systemcontext);
86         $capabilitychoices = array();
87         $capabilitychoices['norequiredcapability'] = get_string('norequiredcapability',
88                         'webservice');
89         foreach ($allcapabilities as $cap) {
90             $capabilitychoices[$cap->name] = $cap->name . ': '
91                     . get_capability_string($cap->name);
92             if (!empty($service->requiredcapability)
93                     && $service->requiredcapability == $cap->name) {
94                 $currentcapabilityexist = true;
95             }
96         }
98         $mform->addElement('searchableselector', 'requiredcapability',
99                 get_string('requiredcapability', 'webservice'), $capabilitychoices);
100         $mform->addHelpButton('requiredcapability', 'requiredcapability', 'webservice');
101         $mform->setAdvanced('requiredcapability');
102 /// display notification error if the current requiredcapability doesn't exist anymore
103         if (empty($currentcapabilityexist)) {
104             global $OUTPUT;
105             $mform->addElement('static', 'capabilityerror', '',
106                     $OUTPUT->notification(get_string('selectedcapabilitydoesntexit',
107                                     'webservice', $service->requiredcapability)));
108             $service->requiredcapability = "norequiredcapability";
109         }
111         $mform->addElement('hidden', 'id');
112         $mform->setType('id', PARAM_INT);
114         if (!empty($service->id)) {
115             $buttonlabel = get_string('editaservice', 'webservice');
116         } else {
117             $buttonlabel = get_string('addaservice', 'webservice');
118         }
120         $this->add_action_buttons(true, $buttonlabel);
122         $this->set_data($service);
123     }
125     function definition_after_data() {
126         $mform = $this->_form;
127         $service = $this->_customdata;
129         if (!empty($service->component)) {
130             // built-in components must not be modified except the enabled flag!!
131             $mform->hardFreeze('name,requiredcapability,restrictedusers');
132         }
133     }
135     function validation($data, $files) {
136         $errors = parent::validation($data, $files);
137         return $errors;
138     }
142 class external_service_functions_form extends moodleform {
144     function definition() {
145         global $CFG;
147         $mform = $this->_form;
148         $data = $this->_customdata;
150         $mform->addElement('header', 'addfunction', get_string('addfunctions', 'webservice'));
152         require_once($CFG->dirroot . "/webservice/lib.php");
153         $webservicemanager = new webservice();
154         $functions = $webservicemanager->get_not_associated_external_functions($data['id']);
156         //we add the descriptions to the functions
157         foreach ($functions as $functionid => $functionname) {
158             //retrieve full function information (including the description)
159             $function = external_function_info($functionname);
160             $functions[$functionid] = $function->name . ':' . $function->description;
161         }
163         $mform->addElement('searchableselector', 'fid', get_string('name'),
164                 $functions, array('multiple'));
166         $mform->addElement('hidden', 'id');
167         $mform->setType('id', PARAM_INT);
169         $mform->addElement('hidden', 'action');
170         $mform->setType('action', PARAM_ACTION);
172         $this->add_action_buttons(true, get_string('addfunctions', 'webservice'));
174         $this->set_data($data);
175     }
179 class web_service_token_form extends moodleform {
181     function definition() {
182         global $USER, $DB, $CFG;
184         $mform = $this->_form;
185         $data = $this->_customdata;
187         $mform->addElement('header', 'token', get_string('token', 'webservice'));
189         if (empty($data->nouserselection)) {
190             //user searchable selector - get all users (admin and guest included)
191             //user must be confirmed, not deleted, not suspended, not guest
192             $sql = "SELECT u.id, u.firstname, u.lastname
193             FROM {user} u
194             WHERE u.deleted = 0 AND u.confirmed = 1 AND u.suspended = 0 AND u.id != ?
195             ORDER BY u.lastname";
196             $users = $DB->get_records_sql($sql, array($CFG->siteguest));
197             $options = array();
198             foreach ($users as $userid => $user) {
199                 $options[$userid] = $user->firstname . " " . $user->lastname;
200             }
201             $mform->addElement('searchableselector', 'user', get_string('user'), $options);
202             $mform->addRule('user', get_string('required'), 'required', null, 'client');
203         }
205         //service selector
206         $services = $DB->get_records('external_services');
207         $options = array();
208         $systemcontext = get_context_instance(CONTEXT_SYSTEM);
209         foreach ($services as $serviceid => $service) {
210             //check that the user has the required capability
211             //(only for generation by the profile page)
212             if (empty($data->nouserselection)
213                     || empty($service->requiredcapability)
214                     || has_capability($service->requiredcapability, $systemcontext, $USER->id)) {
215                 $options[$serviceid] = $service->name;
216             }
217         }
218         $mform->addElement('select', 'service', get_string('service', 'webservice'), $options);
219         $mform->addRule('service', get_string('required'), 'required', null, 'client');
222         $mform->addElement('text', 'iprestriction', get_string('iprestriction', 'webservice'));
224         $mform->addElement('date_selector', 'validuntil',
225                 get_string('validuntil', 'webservice'), array('optional' => true));
227         $mform->addElement('hidden', 'action');
228         $mform->setType('action', PARAM_ACTION);
230         $this->add_action_buttons(true);
232         $this->set_data($data);
233     }
235     function validation($data, $files) {
236         $errors = parent::validation($data, $files);
237         return $errors;
238     }