]> git.mjollnir.org Git - moodle.git/commitdiff
webservice MDL-17135 create searchable select form element + use it in service form...
authorjerome mouneyrac <jerome@moodle.com>
Thu, 5 Nov 2009 07:06:09 +0000 (07:06 +0000)
committerjerome mouneyrac <jerome@moodle.com>
Thu, 5 Nov 2009 07:06:09 +0000 (07:06 +0000)
admin/webservice/forms.php
admin/webservice/service.php
lang/en_utf8/error.php
lang/en_utf8/form.php
lang/en_utf8/webservice.php
lib/form/searchableselector.js [new file with mode: 0644]
lib/form/searchableselector.php [new file with mode: 0644]
lib/formslib.php

index 9d77469cec118dd8fed235e296f71f74e78da535..db55be882a140be2592656228b84ed96ec738cc2 100644 (file)
@@ -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);
     }
 
index 94948f266850cc35b4fb064ca9518d3d487b2860..c231434434cab888694cf8b94c6e869972354902 100644 (file)
@@ -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);
 }
 
index d42363f9ac887ddc9a207e835f3196c388d05e1f..57cf7f5c62d2d2a5ff8d9dfaf83bca7268752f02 100644 (file)
@@ -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.<br />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';
index 0f46cdaca36d79cc4a99ca43ca2215498a8a586b..2473b043168734755564df694948d88cd145f758 100644 (file)
@@ -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.';
index 624fc9894410d6c4f724f5861bddbe1d9e30463d..e7a1ae9aac37bc6c185b5ebf0ad10f65553caa74 100644 (file)
@@ -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 (file)
index 0000000..85a2232
--- /dev/null
@@ -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 <http://www.gnu.org/licenses/>.
+
+/**
+ * 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 (file)
index 0000000..4a2656d
--- /dev/null
@@ -0,0 +1,63 @@
+<?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/>.
+
+/**
+ * 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;
+        }
+    }
+  
+}
index b7b38cf9cc90e9ee20b1102a63741b51db3e7528..5697154861b5608c2ff92bc93943d85c4522610d 100644 (file)
@@ -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');