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->addElement('advcheckbox', 'enabled', get_string('enabled', 'webservice'));
- $mform->addElement('text', 'requiredcapability', get_string('requiredcapability', 'webservice'));
- $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)) {
- // custom component
- $mform->hardFreeze('name,requiredcapability,restrictedusers');
- }
- }
-}
-
class external_service_functions_form extends moodleform {
function definition() {
global $CFG, $USER, $DB;
$service = null;
}
-if ($action == 'delete' and confirm_sesskey() and $service and empty($service->component)) {
+// delete a service
+if (!empty($action) and $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);
}
-$mform = new external_service_form(null, $service);
+$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');
+ }
+ }
-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;
- //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);
+// 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);
}
- redirect($returnurl);
-}
+// 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();
-admin_externalpage_print_header();
-$mform->display();
echo $OUTPUT->footer();
--- /dev/null
+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;
+
+
+ }
+}
\ No newline at end of file
$string['addfunction'] = 'Add function';
$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['enabled'] = 'Enabled';
+$string['emptyname'] = 'The service name field cannot be empty.';
+$string['enabled'] = 'enabled';
$string['externalservices'] = 'External services';
$string['externalservice'] = 'External service';
$string['externalservicefunctions'] = 'External service functions';
$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['servicename'] = 'Service name';
$string['servicesbuiltin'] = 'Built-in services';
$string['servicescustom'] = 'Custom services';
$string['serviceusers'] = 'Authorized users';