Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

MDL-27837 enrol - Fixed checkboxes for multiple self enrolment

  • Loading branch information...
commit 3012b215f6be15fd8df97aed91e2669b437a5c6b 1 parent 93d82f4
authored July 08, 2011
14  enrol/self/locallib.php
@@ -31,6 +31,16 @@
31 31
 class enrol_self_enrol_form extends moodleform {
32 32
     protected $instance;
33 33
 
  34
+    /**
  35
+     * Overriding this function to get unique form id for multiple self enrolments
  36
+     *
  37
+     * @return string form identifier
  38
+     */
  39
+    protected function get_form_identifier() {
  40
+        $formid = $this->_customdata->id.'_'.get_class($this);
  41
+        return $formid;
  42
+    }
  43
+
34 44
     public function definition() {
35 45
         $mform = $this->_form;
36 46
         $instance = $this->_customdata;
@@ -40,7 +50,9 @@ public function definition() {
40 50
         if ($instance->password) {
41 51
             $heading = $plugin->get_instance_name($instance);
42 52
             $mform->addElement('header', 'selfheader', $heading);
43  
-            $mform->addElement('passwordunmask', 'enrolpassword', get_string('password', 'enrol_self'));
  53
+            //change the id of self enrolment key input as there can be multiple self enrolment methods
  54
+            $mform->addElement('passwordunmask', 'enrolpassword', get_string('password', 'enrol_self'),
  55
+                    array('id' => $instance->id."_enrolpassword"));
44 56
         } else {
45 57
             // nothing?
46 58
         }
26  lib/form/passwordunmask.js
... ...
@@ -1,26 +0,0 @@
1  
-var is_ie = (navigator.userAgent.toLowerCase().indexOf("msie") != -1);
2  
-
3  
-document.getElementById(punmask.id).setAttribute("autocomplete", "off");
4  
-
5  
-var unmaskdiv = document.getElementById(punmask.id+"unmaskdiv");
6  
-
7  
-var unmaskchb = document.createElement("input");
8  
-unmaskchb.setAttribute("type", "checkbox");
9  
-unmaskchb.setAttribute("id", punmask.id+"unmask");
10  
-unmaskchb.onchange = function() {unmaskPassword(punmask.id);};
11  
-unmaskdiv.appendChild(unmaskchb);
12  
-
13  
-var unmasklbl = document.createElement("label");
14  
-unmasklbl.innerHTML = punmask.unmaskstr;
15  
-if (is_ie) {
16  
-  unmasklbl.setAttribute("htmlFor", punmask.id+"unmask");
17  
-} else {
18  
-  unmasklbl.setAttribute("for", punmask.id+"unmask");
19  
-}
20  
-unmaskdiv.appendChild(unmasklbl);
21  
-
22  
-if (is_ie) {
23  
-  // ugly hack to work around the famous onchange IE bug
24  
-  unmaskchb.onclick = function() {this.blur();};
25  
-  unmaskdiv.onclick = function() {this.blur();};
26  
-}
12  lib/form/passwordunmask.php
@@ -19,15 +19,17 @@ function MoodleQuickForm_passwordunmask($elementName=null, $elementLabel=null, $
19 19
     }
20 20
 
21 21
     function toHtml() {
22  
-        global $CFG;
  22
+        global $PAGE;
  23
+
23 24
         if ($this->_flagFrozen) {
24 25
             return $this->getFrozenHtml();
25 26
         } else {
26  
-            $id = $this->getAttribute('id');
27 27
             $unmask = get_string('unmaskpassword', 'form');
28  
-            $unmaskjs = html_writer::script(js_writer::set_variable('punmask', array('id'=>$id, 'unmaskstr'=>$unmask)));
29  
-            $unmaskjs .= html_writer::script('', $CFG->httpswwwroot.'/lib/form/passwordunmask.js');
30  
-            return $this->_getTabs() . '<input' . $this->_getAttrString($this->_attributes) . ' /><div class="unmask" id="'.$id.'unmaskdiv"></div>'.$unmaskjs;
  28
+            $this->updateAttributes(array('autocomplete' => 'off'));
  29
+            //Pass id of the element, so that unmask checkbox can be attached.
  30
+            $PAGE->requires->yui_module('moodle-form-passwordunmask', 'M.form.passwordunmask',
  31
+                    array(array('formid' => $this->getAttribute('id'), 'checkboxname' => $unmask)));
  32
+            return $this->_getTabs() . '<input' . $this->_getAttrString($this->_attributes) . ' />';
31 33
         }
32 34
     } //end func toHtml
33 35
 
39  lib/form/yui/passwordunmask/passwordunmask.js
... ...
@@ -0,0 +1,39 @@
  1
+YUI.add('moodle-form-passwordunmask', function(Y) {
  2
+    var PASSWORDUNMASK = function() {
  3
+        PASSWORDUNMASK.superclass.constructor.apply(this, arguments);
  4
+    }
  5
+
  6
+    Y.extend(PASSWORDUNMASK, Y.Base, {
  7
+        //Initialize checkbox if id is passed
  8
+        initializer : function(params) {
  9
+            if (params && params.formid) {
  10
+               this.add_checkbox(params.formid, params.checkboxname);
  11
+            }
  12
+        },
  13
+        //Create checkbox for unmasking password
  14
+        add_checkbox : function(elementid, checkboxlabel) {
  15
+            var node = Y.one('#'+elementid);
  16
+
  17
+            //retaining unmask div from previous implementation.
  18
+            var unmaskdiv = Y.Node.create('<div id="'+elementid+'unmaskdiv" class="unmask"></div>');
  19
+
  20
+            //Add checkbox for unmasking to unmaskdiv
  21
+            var unmaskchb = Y.Node.create('<input id="'+elementid+'unmask" type="checkbox">');
  22
+            unmaskdiv.appendChild(unmaskchb);
  23
+            //Attach event using static javascript function for unmasking password.
  24
+            unmaskchb.on('click', function() {unmaskPassword(elementid);});
  25
+
  26
+            //Add label for checkbox to unmaskdiv
  27
+            var unmasklabel = Y.Node.create('<label for="'+elementid+'unmask">'+checkboxlabel+'</label>');
  28
+            unmaskdiv.appendChild(unmasklabel);
  29
+
  30
+            //Insert unmask div in the same div as password input.
  31
+            node.get('parentNode').insert(unmaskdiv, node.get('lastNode'));
  32
+        }
  33
+    });
  34
+
  35
+    M.form = M.form || {};
  36
+    M.form.passwordunmask = function(params) {
  37
+        return new PASSWORDUNMASK(params);
  38
+    }
  39
+}, '@VERSION@', {requires:['base', 'node']});
17  lib/formslib.php
@@ -141,9 +141,10 @@ function moodleform($action=null, $customdata=null, $method='post', $target='',
141 141
         if (empty($action)){
142 142
             $action = strip_querystring(qualified_me());
143 143
         }
144  
-
145  
-        $this->_formname = get_class($this); // '_form' suffix kept in order to prevent collisions of form id and other element
  144
+        // Assign custom data first, so that get_form_identifier can use it.
146 145
         $this->_customdata = $customdata;
  146
+        $this->_formname = $this->get_form_identifier();
  147
+
147 148
         $this->_form = new MoodleQuickForm($this->_formname, $method, $action, $target, $attributes);
148 149
         if (!$editable){
149 150
             $this->_form->hardFreeze();
@@ -164,6 +165,18 @@ function moodleform($action=null, $customdata=null, $method='post', $target='',
164 165
     }
165 166
 
166 167
     /**
  168
+     * It should returns unique identifier for the form.
  169
+     * Currently it will return class name, but in case two same forms have to be
  170
+     * rendered on same page then override function to get unique form identifier.
  171
+     * e.g This is used on multiple self enrollments page.
  172
+     *
  173
+     * @return string form identifier.
  174
+     */
  175
+    protected function get_form_identifier() {
  176
+        return get_class($this);
  177
+    }
  178
+
  179
+    /**
167 180
      * To autofocus on first form element or first element with error.
168 181
      *
169 182
      * @param string $name if this is set then the focus is forced to a field with this name

0 notes on commit 3012b21

Please sign in to comment.
Something went wrong with that request. Please try again.