From: skodak Date: Wed, 21 Oct 2009 19:36:39 +0000 (+0000) Subject: MDL-17135 reverting recent change in service management UI - since 1.7 the right... X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=86c252b47db5aacc4f2bdad613ebfbca49aed532;p=moodle.git MDL-17135 reverting recent change in service management UI - since 1.7 the right way is to use formslib instead of hand written forms (which is roughly equiwalent to new outputlib stuff, sorry); this also fixes regression which incorrectly allowed editting of built-in services --- diff --git a/admin/external_forms.php b/admin/external_forms.php new file mode 100644 index 0000000000..9d77469cec --- /dev/null +++ b/admin/external_forms.php @@ -0,0 +1,70 @@ +. + +/** + * 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 diff --git a/admin/external_service.php b/admin/external_service.php index 5ed89b4b44..089ef3fada 100644 --- a/admin/external_service.php +++ b/admin/external_service.php @@ -25,6 +25,7 @@ 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); @@ -42,8 +43,7 @@ if ($id) { $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()); @@ -60,137 +60,25 @@ if (!empty($action) and $action == 'delete' and confirm_sesskey() and $service a 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 = ""; - $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 .= '

'. get_string('addservicehelp', 'webservice') . '

'; - //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 .= '

'; - $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 .= '
'; - $contents .= $OUTPUT->textfield($capabilityname); - $contents .= ''; - $contents .= "

"; - echo $OUTPUT->form($form, $contents); - - echo $OUTPUT->box_end(); + redirect($returnurl); +} +admin_externalpage_print_header(); +$mform->display(); echo $OUTPUT->footer(); diff --git a/admin/webservice/script.js b/admin/webservice/script.js index b604c24c1c..35a28abc97 100644 --- a/admin/webservice/script.js +++ b/admin/webservice/script.js @@ -1,76 +1,3 @@ -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) { diff --git a/lang/en_utf8/webservice.php b/lang/en_utf8/webservice.php index fadf7582fb..7d61457fb6 100644 --- a/lang/en_utf8/webservice.php +++ b/lang/en_utf8/webservice.php @@ -4,16 +4,11 @@ $string['accessexception'] = 'Access control exception'; $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'; @@ -23,22 +18,19 @@ $string['functions'] = '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'; diff --git a/lib/adminlib.php b/lib/adminlib.php index 0e58ff7adb..974dbd44c8 100644 --- a/lib/adminlib.php +++ b/lib/adminlib.php @@ -6161,13 +6161,14 @@ class admin_setting_manageexternalservices extends admin_setting { $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'); @@ -6194,9 +6195,9 @@ class admin_setting_manageexternalservices extends admin_setting { $functions = "id\">$strfunctions"; if ($service->restrictedusers) { - $users = "id\">$strusers"; + $users = "id\">$strserviceusers"; } else { - $users = get_string('nouserrestriction','webservice'); + $users = '-'; } $edit = "id\">$stredit"; @@ -6207,7 +6208,7 @@ class admin_setting_manageexternalservices extends admin_setting { $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(); @@ -6234,9 +6235,9 @@ class admin_setting_manageexternalservices extends admin_setting { $functions = "id\">$strfunctions"; if ($service->restrictedusers) { - $users = "id\">$strusers"; + $users = "id\">$strserviceusers"; } else { - $users = get_string('nouserrestriction','webservice'); + $users = '-'; } $edit = "id\">$stredit";