--- /dev/null
+<?php
+
+// This file is part of Moodle - http://moodle.org/
+//
+// Moodle is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// Moodle is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
+
+/**
+ * Web services admin UI forms
+ *
+ * @package webservice
+ * @copyright 2009 Moodle Pty Ltd (http://moodle.com)
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+require_once $CFG->libdir.'/formslib.php';
+
+class external_service_form extends moodleform {
+ function definition() {
+ global $CFG, $USER;
+
+ $mform = $this->_form;
+ $service = $this->_customdata;
+
+ $mform->addElement('header', 'extservice', get_string('externalservice', 'webservice'));
+
+ $mform->addElement('text', 'name', get_string('name'));
+ $mform->addRule('name', get_string('required'), 'required', null, 'client');
+ $mform->addElement('advcheckbox', 'enabled', get_string('enabled', 'webservice'));
+ $mform->addElement('text', 'requiredcapability', get_string('requiredcapability', 'webservice'));
+ // TODO: change to capability selection or even better if new forms element used,
+ // we also need to indicate if current capability does not exist in system!
+ $mform->addElement('advcheckbox', 'restrictedusers', get_string('restrictedusers', 'webservice'));
+
+ $mform->addElement('hidden', 'id');
+ $mform->setType('id', PARAM_INT);
+
+ $this->add_action_buttons(true);
+
+ $this->set_data($service);
+ }
+
+ function definition_after_data() {
+ $mform = $this->_form;
+ $service = $this->_customdata;
+
+ if (!empty($service->component)) {
+ // built-in components must not be modified except the enabled flag!!
+ $mform->hardFreeze('name,requiredcapability,restrictedusers');
+ }
+ }
+
+ function validation($data, $files) {
+ $errors = parent::validation($data, $files);
+
+ //TODO: better make sure the service name is unique
+
+ return $errors;
+ }
+}
\ No newline at end of file
require_once('../config.php');
require_once($CFG->libdir.'/adminlib.php');
+require_once('external_forms.php');
$id = required_param('id', PARAM_INT);
$action = optional_param('action', '', PARAM_ACTION);
$service = null;
}
-// delete a service
-if (!empty($action) and $action == 'delete' and confirm_sesskey() and $service and empty($service->component)) {
+if ($action == 'delete' and confirm_sesskey() and $service and empty($service->component)) {
if (!$confirm) {
admin_externalpage_print_header();
$optionsyes = array('id'=>$id, 'action'=>'delete', 'confirm'=>1, 'sesskey'=>sesskey());
redirect($returnurl);
}
-$clear = optional_param('clearbutton', false, PARAM_BOOL);
-$servicename = optional_param('servicename', '', PARAM_TEXT);
-$enableservice = optional_param('enableservice', 0, PARAM_BOOL);
-$restrictedusers = optional_param('restrictedusers', 0, PARAM_BOOL);
-$capability = optional_param('capability', '', PARAM_CAPABILITY);
-
-// clear the capability field
-if (!empty($clear)) {
- $service->name = $servicename;
- $service->enabled = $enableservice;
- $service->requiredcapability = "";
- $service->restrictedusers = $restrictedusers;
-} else {
-// add/update a service
- if ((!empty($action) and ($action == 'add' || $action == 'update') and confirm_sesskey())) {
-
- if (!empty($servicename)) {
- $tempservice = new object();
- $tempservice->name = $servicename;
- $tempservice->enabled = $enableservice;
- $tempservice->requiredcapability = $capability;
- $tempservice->restrictedusers = $restrictedusers;
-
- if ($action == 'add') {
- $DB->insert_record('external_services', $tempservice);
- }
- else {
- $tempservice->id = $service->id;
- $DB->update_record('external_services', $tempservice);
- }
-
- redirect($returnurl);
-
- }
- //administrator has omitted service name => display error message
- else {
- $service->name = $servicename;
- $service->enabled = $enableservice;
- $service->requiredcapability = $capability;
- $service->restrictedusers = $restrictedusers;
- $errormessage = get_string('emptyname', 'webservice');
- }
- }
-
-}
+$mform = new external_service_form(null, $service);
+if ($mform->is_cancelled()) {
+ redirect($returnurl);
-admin_externalpage_print_header();
-if (!empty($errormessage)) {
- echo $OUTPUT->notification($errormessage);
-}
-
+} else if ($data = $mform->get_data()) {
+ $data = (object)$data;
-// Prepare the list of capabilites to choose from
- $systemcontext = get_context_instance(CONTEXT_SYSTEM);
- $allcapabilities = fetch_context_capabilities($systemcontext);
- $capabilitychoices = array();
- foreach ($allcapabilities as $cap) {
- $capabilitychoices[$cap->name] = $cap->name . ': ' . get_capability_string($cap->name);
+ //TODO: add timecreated+modified and maybe logging too
+ if (empty($data->id)) {
+ $DB->insert_record('external_services', $data);
+ } else {
+ $DB->update_record('external_services', $data);
}
-// Javascript for the capability search/selection fields
- $PAGE->requires->yui_lib('event');
- $PAGE->requires->js('admin/webservice/script.js');
- $PAGE->requires->js_function_call('capability_service.cap_filter_init', array(get_string('search')));
-
-// UI
- $capability = optional_param('capability', '', PARAM_CAPABILITY);
- echo $OUTPUT->box_start('generalbox boxwidthwide boxaligncenter centerpara');
-
- $action = (empty($id))?'add':'update'; //if 'id' GET parameter = 0 we're adding a service, otherwise updating
-
- //the service form
- $form = new html_form();
- $form->url = new moodle_url('/admin/external_service.php', array('id' => $id, 'action' => $action)); // Required
- $form->button = new html_button();
- $form->button->id = 'settingssubmit';
- $form->button->text = get_string('saveservice', 'webservice'); // Required
- $form->button->disabled = false;
- $form->button->title = get_string('saveservice', 'webservice');
- $form->method = 'post';
- $form->id = 'settingsform';
-
- echo $OUTPUT->heading(get_string('externalservice', 'webservice'));
- //service name field
- $namefield = "<label>".get_string('servicename','webservice')." </label>";
- $nametextfield = new html_field();
- $nametextfield->name = 'servicename';
- $nametextfield->value = empty($service->name)?"":$service->name;
- $nametextfield->style = 'width: 30em;';
- $namefield .= $OUTPUT->textfield($nametextfield);
- $contents = $namefield;
- //enable field
- $servicecheckbox = new html_select_option();
- $servicecheckbox->value = true;
- $servicecheckbox->selected = empty($service->enabled)?false:true;
- $servicecheckbox->text = get_string('enabled', 'webservice');
- $servicecheckbox->label->text = get_string('enabled', 'webservice');
- $servicecheckbox->alt = get_string('enabled', 'webservice');
- $contents .= $OUTPUT->checkbox($servicecheckbox, 'enableservice');
- //help text
- $contents .= '<p id="intro">'. get_string('addservicehelp', 'webservice') . '</p>';
- //restricted users option
- $restricteduserscheckbox = new html_select_option();
- $restricteduserscheckbox->value = true;
- $restricteduserscheckbox->selected = empty($service->restrictedusers)?false:true;
- $restricteduserscheckbox->text = get_string('restrictedusers', 'webservice');
- $restricteduserscheckbox->label->text = get_string('restrictedusers', 'webservice');
- $restricteduserscheckbox->alt = get_string('restrictedusers', 'webservice');
- $contents .= $OUTPUT->checkbox($restricteduserscheckbox, 'restrictedusers');
- //capability section (search field + selection field)
- $contents .= '<p><label for="menucapability"> ' . get_string('requiredcapability', 'webservice') . '</label></p> ';
- $capabilityname = new html_field();
- $capabilityname->name = 'capabilityname';
- $capabilityname->id = 'capabilityname';
- $capabilityname->value = empty($service->requiredcapability)?"":$service->requiredcapability;
- $capabilityname->disabled = true;
- $capabilityname->style = 'width: 20em;';
- $capability = empty($service->requiredcapability)?"":$service->requiredcapability;
- $select = html_select::make($capabilitychoices, 'capability', $capability);
- $select->nothingvalue = '';
- $select->listbox = true;
- $select->tabindex = 0;
- $contents .= $OUTPUT->select($select);
- $contents .= '<br/><label for="menucapability"> ' . get_string('selectedcapability', 'webservice') . '</label> ';
- $contents .= $OUTPUT->textfield($capabilityname);
- $contents .= '<input type="submit" name="clearbutton" id="clearbutton" value="' . get_string('clear') . '" />';
- $contents .= "<br/><br/>";
- echo $OUTPUT->form($form, $contents);
-
- echo $OUTPUT->box_end();
+ redirect($returnurl);
+}
+admin_externalpage_print_header();
+$mform->display();
echo $OUTPUT->footer();
-capability_service = {
- select: null,
- input: null,
- button: null,
-
- cap_filter_init: function(strsearch) {
- // Find the form controls.
- capability_service.select = document.getElementById('menucapability');
- capability_service.button = document.getElementById('settingssubmit');
-
- // Create a div to hold the search UI.
- var div = document.createElement('div');
- div.id = 'capabilitysearchui';
-
- // Find the capability search input.
- var input = document.createElement('input');
- input.type = 'text';
- input.id = 'capabilitysearch';
- capability_service.input = input;
-
- // Create a label for the search input.
- var label = document.createElement('label');
- label.htmlFor = input.id;
- label.appendChild(document.createTextNode(strsearch + ' '));
-
- // Tie it all together
- div.appendChild(label);
- div.appendChild(input);
- capability_service.select.parentNode.insertBefore(div, capability_service.select);
- YAHOO.util.Event.addListener(input, 'keyup', capability_service.cap_filter_change);
- YAHOO.util.Event.addListener(capability_service.select, 'change', capability_service.validate);
- capability_service.select.options[0].style.display = 'none';
- capability_service.validate();
- },
-
- cap_filter_change: function() {
- var filtertext = capability_service.input.value;
- var options = capability_service.select.options;
- var onlycapability = -1;
- for (var i = 1; i < options.length; i++) {
- if (options[i].text.indexOf(filtertext) >= 0) {
- options[i].disabled = false;
- options[i].style.display = 'block';
- if (onlycapability == -1) {
- onlycapability = i;
- } else {
- onlycapability = -2;
- }
- } else {
- options[i].disabled = true;
- options[i].selected = false;
- options[i].style.display = 'none';
- }
- }
- if (onlycapability >= 0) {
- options[onlycapability].selected = true;
- }
- if (onlycapability == -1) {
- capability_service.input.className = "error";
- } else {
- capability_service.input.className = "";
- }
-
- capability_service.validate();
- },
-
- validate: function() {
- capabilityname = document.getElementById('capabilityname');
- capabilityname.value = capability_service.select.value;
-
-
- }
-}
/* This function disable the valid until field of a user into external_service_users.php*/
function disablevaliduntil(event, userid) {
$string['addfunction'] = 'Add function';
$string['addfunctionhelp'] = 'Select the function to add to the service.';
$string['addrequiredcapability'] = 'Assign/Unassign the required capability';
-$string['addservicehelp'] = 'Set settings for your service. You can add a unique required capability. In this case any
- user accessing to this service will need this capability. When you enable restricted users option, you will be able
- to select some restricted users from the services administration page. These users will be the only one able to
- access this service. Finally you can decide enable/disable the service at any time.';
$string['actwebserviceshhdr'] = 'Active web service protocols';
$string['configwebserviceplugins'] = 'For security reasons enable only protocols that are used.';
$string['deleteserviceconfirm'] = 'Do you really want to delete external service \"$a\"?';
$string['disabledwarning'] = 'All webs service protocols are disabled, the \Enable web services\" setting can be found in the \"Advanced features\" section.';
-$string['emptyname'] = 'The service name field cannot be empty.';
-$string['enabled'] = 'enabled';
+$string['enabled'] = 'Enabled';
$string['externalservices'] = 'External services';
$string['externalservice'] = 'External service';
$string['externalservicefunctions'] = 'External service functions';
$string['iprestriction'] = 'IP restriction';
$string['manageprotocols'] = 'Manage protocols';
$string['nofunctionselected'] = 'Please select a function to add';
-$string['nouserrestriction'] = 'No restriction';
-$string['potusers'] = 'Not authorized users';
-$string['potusersmatching'] = 'Not authorized users matching';
+$string['potusers'] = 'Not authorised users';
+$string['potusersmatching'] = 'Not authorised users matching';
$string['protocol'] = 'Protocol';
$string['removefunction'] = 'Remove';
$string['removefunctionconfirm'] = 'Do you really want to remove function \"$a->function\" from service \"$a->service\"?';
$string['requiredcapability'] = 'Required capability';
-$string['restrictedusers'] = 'Restricted users';
-$string['saveservice'] = 'Save service';
-$string['selectedcapability'] = 'Selected';
+$string['restrictedusers'] = 'Authorised users only';
$string['servicename'] = 'Service name';
$string['servicesbuiltin'] = 'Built-in services';
$string['servicescustom'] = 'Custom services';
-$string['serviceusers'] = 'Authorized users';
-$string['serviceusersmatching'] = 'Authorized users matching';
-$string['serviceuserssettings'] = 'Change settings for the Authorized users';
+$string['serviceusers'] = 'Authorised users';
+$string['serviceusersmatching'] = 'Authorised users matching';
+$string['serviceuserssettings'] = 'Change settings for the authorised users';
$string['test'] = 'Test';
$string['testclient'] = 'Web service test client';
$string['validuntil'] = 'Valid until';
$stradd = get_string('add');
$strfunctions = get_string('functions', 'webservice');
$strusers = get_string('restrictedusers', 'webservice');
+ $strserviceusers = get_string('serviceusers', 'webservice');
$esurl = "$CFG->wwwroot/$CFG->admin/external_service.php";
$efurl = "$CFG->wwwroot/$CFG->admin/external_service_functions.php";
$euurl = "$CFG->wwwroot/$CFG->admin/external_service_users.php";
// built in services
- $return = $OUTPUT->heading(get_string('servicesbuiltin', 'webservice'), 3, 'main', true);
+ $return = $OUTPUT->heading(get_string('servicesbuiltin', 'webservice'), 3, 'main');
$services = $DB->get_records_select('external_services', 'component IS NOT NULL', null, 'name');
$functions = "<a href=\"$efurl?id=$service->id\">$strfunctions</a>";
if ($service->restrictedusers) {
- $users = "<a href=\"$euurl?id=$service->id\">$strusers</a>";
+ $users = "<a href=\"$euurl?id=$service->id\">$strserviceusers</a>";
} else {
- $users = get_string('nouserrestriction','webservice');
+ $users = '-';
}
$edit = "<a href=\"$esurl?id=$service->id\">$stredit</a>";
$return .= $OUTPUT->table($table);
// Custom services
- $return .= $OUTPUT->heading(get_string('servicescustom', 'webservice'), 3, 'main', true);
+ $return .= $OUTPUT->heading(get_string('servicescustom', 'webservice'), 3, 'main');
$services = $DB->get_records_select('external_services', 'component IS NULL', null, 'name');
$table = new html_table();
$functions = "<a href=\"$efurl?id=$service->id\">$strfunctions</a>";
if ($service->restrictedusers) {
- $users = "<a href=\"$euurl?id=$service->id\">$strusers</a>";
+ $users = "<a href=\"$euurl?id=$service->id\">$strserviceusers</a>";
} else {
- $users = get_string('nouserrestriction','webservice');
+ $users = '-';
}
$edit = "<a href=\"$esurl?id=$service->id\">$stredit</a>";