From 09179b789bc48a0c457fe1fd5d695526685e8abd Mon Sep 17 00:00:00 2001 From: jerome mouneyrac Date: Thu, 5 Nov 2009 07:06:09 +0000 Subject: [PATCH] webservice MDL-17135 create searchable select form element + use it in service form for selecting a capability --- admin/webservice/forms.php | 18 ++++++- admin/webservice/service.php | 6 ++- lang/en_utf8/error.php | 1 + lang/en_utf8/form.php | 1 + lang/en_utf8/webservice.php | 2 + lib/form/searchableselector.js | 84 +++++++++++++++++++++++++++++++++ lib/form/searchableselector.php | 63 +++++++++++++++++++++++++ lib/formslib.php | 1 + 8 files changed, 174 insertions(+), 2 deletions(-) create mode 100644 lib/form/searchableselector.js create mode 100644 lib/form/searchableselector.php diff --git a/admin/webservice/forms.php b/admin/webservice/forms.php index 9d77469cec..db55be882a 100644 --- a/admin/webservice/forms.php +++ b/admin/webservice/forms.php @@ -37,7 +37,17 @@ class external_service_form extends moodleform { $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')); + + // Prepare the list of capabilites to choose from + $systemcontext = get_context_instance(CONTEXT_SYSTEM); + $allcapabilities = fetch_context_capabilities($systemcontext); + $capabilitychoices = array(); + $capabilitychoices['norequiredcapability'] = get_string('norequiredcapability', 'webservice'); + foreach ($allcapabilities as $cap) { + $capabilitychoices[$cap->name] = $cap->name . ': ' . get_capability_string($cap->name); + } + + $mform->addElement('searchableselector', 'requiredcapability', get_string('requiredcapability', 'webservice'),$capabilitychoices, array('size' => 12)); // 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')); @@ -47,6 +57,12 @@ class external_service_form extends moodleform { $this->add_action_buttons(true); + ///this three lines are needed to select automatically the 'No required capability" option + if (empty($service->requiredcapability)) + { + $service->requiredcapability = "norequiredcapability"; + } + $this->set_data($service); } diff --git a/admin/webservice/service.php b/admin/webservice/service.php index 94948f2668..c231434434 100644 --- a/admin/webservice/service.php +++ b/admin/webservice/service.php @@ -67,6 +67,10 @@ if ($mform->is_cancelled()) { } else if ($data = $mform->get_data()) { $data = (object)$data; + if (!empty($data->requiredcapability) && $data->requiredcapability == "norequiredcapability") + { + $data->requiredcapability = ""; + } //TODO: add timecreated+modified and maybe logging too if (empty($data->id)) { @@ -74,7 +78,7 @@ if ($mform->is_cancelled()) { } else { $DB->update_record('external_services', $data); } - + redirect($returnurl); } diff --git a/lang/en_utf8/error.php b/lang/en_utf8/error.php index d42363f9ac..57cf7f5c62 100644 --- a/lang/en_utf8/error.php +++ b/lang/en_utf8/error.php @@ -392,6 +392,7 @@ $string['reportnotavailable'] = 'This type of report is only available for the s $string['reverseproxyabused'] = 'Reverse proxy enabled, server can not be accessed directly, sorry.
Please contact server administrator.'; $string['rpcerror'] = 'RPC enrol/mnet/available_courses: ($a)'; $string['scheduledbackupsdisabled'] = 'Scheduled backups have been disabled by the server admin'; +$string['searchableselectorcannotbemultiple'] = 'A searchableselector form element cannot be multiple. Remove the \'multiple\' attribute from your call.'; $string['sectionnotexist'] = 'This section does not exist'; $string['secretalreadyused'] = 'Change password confirmation link was already used, password was not changed'; $string['sendmessage'] = 'Send message'; diff --git a/lang/en_utf8/form.php b/lang/en_utf8/form.php index 0f46cdaca3..2473b04316 100644 --- a/lang/en_utf8/form.php +++ b/lang/en_utf8/form.php @@ -29,6 +29,7 @@ $string['othersettings']='Other settings'; $string['requiredelement'] = 'Required field'; $string['revealpassword'] = 'Reveal'; $string['security'] = 'Security'; +$string['selected'] = 'Selected'; $string['selectallornone'] = 'Select all/none'; $string['showadvanced']='Show Advanced'; $string['somefieldsrequired'] = 'There are required fields in this form marked$a.'; diff --git a/lang/en_utf8/webservice.php b/lang/en_utf8/webservice.php index 624fc98944..e7a1ae9aac 100644 --- a/lang/en_utf8/webservice.php +++ b/lang/en_utf8/webservice.php @@ -19,6 +19,7 @@ $string['function'] = 'Function'; $string['functions'] = 'Functions'; $string['iprestriction'] = 'IP restriction'; $string['manageprotocols'] = 'Manage protocols'; +$string['norequiredcapability'] = 'No required capability'; $string['potusers'] = 'Not authorised users'; $string['potusersmatching'] = 'Not authorised users matching'; $string['protocol'] = 'Protocol'; @@ -26,6 +27,7 @@ $string['removefunction'] = 'Remove'; $string['removefunctionconfirm'] = 'Do you really want to remove function \"$a->function\" from service \"$a->service\"?'; $string['requiredcapability'] = 'Required capability'; $string['restrictedusers'] = 'Authorised users only'; +$string['selectedcapability'] = 'Selected'; $string['servicename'] = 'Service name'; $string['servicesbuiltin'] = 'Built-in services'; $string['servicescustom'] = 'Custom services'; diff --git a/lib/form/searchableselector.js b/lib/form/searchableselector.js new file mode 100644 index 0000000000..85a2232764 --- /dev/null +++ b/lib/form/searchableselector.js @@ -0,0 +1,84 @@ +// 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 . + +/** + * javascript for a searchable select type element + * + * @package formlib + * @copyright 2009 Jerome Mouneyrac + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +selector = { + select: null, + input: null, + button: null, + + filter_init: function(strsearch, selectinputid) { + // selector.id = selectinputid + selector.select = document.getElementById(selectinputid); + selector.button = document.getElementById('settingssubmit'); + + // Create a div to hold the search UI. + var div = document.createElement('div'); + div.id = 'searchui'; + + // Find the capability search input. + var input = document.createElement('input'); + input.type = 'text'; + input.id = selectinputid+'_search'; + selector.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); + selector.select.parentNode.insertBefore(div, selector.select); + YAHOO.util.Event.addListener(input, 'keyup', selector.filter_change); + }, + + filter_change: function() { + var searchtext = selector.input.value.toLowerCase(); + var options = selector.select.options; + var matchingoption = -1; + for (var i = 0; i < options.length; i++) { + var optiontext = options[i].text.toLowerCase(); + if (optiontext.indexOf(searchtext) >= 0) { //the option is matching the search text + options[i].disabled = false; //the option must be visible + options[i].style.display = 'block'; + if (matchingoption == -1) { //we found at least one + matchingoption = i; + } + } else { + options[i].disabled = true; + options[i].selected = false; + options[i].style.display = 'none'; + } + } + + if (matchingoption == -1) { //the search didn't find any matching, color the search text in red + selector.input.className = "error"; + } else { + selector.input.className = ""; + } + + } + +} + diff --git a/lib/form/searchableselector.php b/lib/form/searchableselector.php new file mode 100644 index 0000000000..4a2656d78f --- /dev/null +++ b/lib/form/searchableselector.php @@ -0,0 +1,63 @@ +. + +/** + * HTML class for a searchable select type element + * + * @package formlib + * @copyright 2009 Jerome Mouneyrac + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +require_once('select.php'); + +/** + * Display a select input with a search textfield input on the top + * The search textfield is created by the javascript file searchselector.js + * (so when javascript is not activated into the browser, the search field is not displayed) + * If ever the select can be reset/unselect/blank/nooption, you will have to add an option "noselected" + * and manage this special case when you get/set the form data (i.e. $mform->get_data()/$this->set_data($yourobject)). + */ +class MoodleQuickForm_searchableselector extends MoodleQuickForm_select{ + + + function MoodleQuickForm_searchableselector($elementName=null, $elementLabel=null, $options=null, $attributes=null) { + //return exception if the selector is set as multiple (TODO: implement multiple support) + if (!empty($attributes) && key_exists('multiple', $attributes)) { + throw new moodle_exception('searchableselectorcannotbemultiple'); + } + parent::MoodleQuickForm_select($elementName, $elementLabel, $options, $attributes); + } + + function toHtml(){ + global $OUTPUT; + if ($this->_hiddenLabel){ + return parent::toHtml(); + } else { + // Javascript for the search/selection fields + global $PAGE; + $PAGE->requires->yui_lib('event'); + $PAGE->requires->js('lib/form/searchableselector.js'); + $PAGE->requires->js_function_call('selector.filter_init', array(get_string('search'),$this->getAttribute('id'))); + + $strHtml = ''; + $strHtml .= parent::toHtml(); //the select input + return $strHtml; + } + } + +} diff --git a/lib/formslib.php b/lib/formslib.php index b7b38cf9cc..5697154861 100644 --- a/lib/formslib.php +++ b/lib/formslib.php @@ -2209,6 +2209,7 @@ $GLOBALS['_HTML_QuickForm_default_renderer'] = new MoodleQuickForm_Renderer(); MoodleQuickForm::registerElementType('advcheckbox', "$CFG->libdir/form/advcheckbox.php", 'MoodleQuickForm_advcheckbox'); MoodleQuickForm::registerElementType('button', "$CFG->libdir/form/button.php", 'MoodleQuickForm_button'); MoodleQuickForm::registerElementType('cancel', "$CFG->libdir/form/cancel.php", 'MoodleQuickForm_cancel'); +MoodleQuickForm::registerElementType('searchableselector', "$CFG->libdir/form/searchableselector.php", 'MoodleQuickForm_searchableselector'); MoodleQuickForm::registerElementType('checkbox', "$CFG->libdir/form/checkbox.php", 'MoodleQuickForm_checkbox'); MoodleQuickForm::registerElementType('choosecoursefile', "$CFG->libdir/form/choosecoursefile.php", 'MoodleQuickForm_choosecoursefile'); MoodleQuickForm::registerElementType('choosecoursefileorimsrepo', "$CFG->libdir/form/choosecoursefileorimsrepo.php", 'MoodleQuickForm_choosecoursefileorimsrepo'); -- 2.39.5