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

This commit is contained in:
skodak
2009-10-21 19:36:39 +00:00
parent f0dafb3c85
commit 86c252b47d
5 changed files with 99 additions and 221 deletions
+70
View File
@@ -0,0 +1,70 @@
<?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;
}
}
+15 -127
View File
@@ -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);
$mform = new external_service_form(null, $service);
// 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 ($mform->is_cancelled()) {
redirect($returnurl);
if ($action == 'add') {
$DB->insert_record('external_services', $tempservice);
}
else {
$tempservice->id = $service->id;
$DB->update_record('external_services', $tempservice);
}
} else if ($data = $mform->get_data()) {
$data = (object)$data;
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');
}
//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);
}
}
admin_externalpage_print_header();
if (!empty($errormessage)) {
echo $OUTPUT->notification($errormessage);
redirect($returnurl);
}
// 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);
}
// 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();
-73
View File
@@ -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) {