Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

MDL-27837 enrol - Fixed checkboxes for multiple self enrolment

  • Loading branch information...
commit 3012b215f6be15fd8df97aed91e2669b437a5c6b 1 parent 93d82f4
Rajesh Taneja authored
14 enrol/self/locallib.php
View
@@ -31,6 +31,16 @@
class enrol_self_enrol_form extends moodleform {
protected $instance;
+ /**
+ * Overriding this function to get unique form id for multiple self enrolments
+ *
+ * @return string form identifier
+ */
+ protected function get_form_identifier() {
+ $formid = $this->_customdata->id.'_'.get_class($this);
+ return $formid;
+ }
+
public function definition() {
$mform = $this->_form;
$instance = $this->_customdata;
@@ -40,7 +50,9 @@ public function definition() {
if ($instance->password) {
$heading = $plugin->get_instance_name($instance);
$mform->addElement('header', 'selfheader', $heading);
- $mform->addElement('passwordunmask', 'enrolpassword', get_string('password', 'enrol_self'));
+ //change the id of self enrolment key input as there can be multiple self enrolment methods
+ $mform->addElement('passwordunmask', 'enrolpassword', get_string('password', 'enrol_self'),
+ array('id' => $instance->id."_enrolpassword"));
} else {
// nothing?
}
26 lib/form/passwordunmask.js
View
@@ -1,26 +0,0 @@
-var is_ie = (navigator.userAgent.toLowerCase().indexOf("msie") != -1);
-
-document.getElementById(punmask.id).setAttribute("autocomplete", "off");
-
-var unmaskdiv = document.getElementById(punmask.id+"unmaskdiv");
-
-var unmaskchb = document.createElement("input");
-unmaskchb.setAttribute("type", "checkbox");
-unmaskchb.setAttribute("id", punmask.id+"unmask");
-unmaskchb.onchange = function() {unmaskPassword(punmask.id);};
-unmaskdiv.appendChild(unmaskchb);
-
-var unmasklbl = document.createElement("label");
-unmasklbl.innerHTML = punmask.unmaskstr;
-if (is_ie) {
- unmasklbl.setAttribute("htmlFor", punmask.id+"unmask");
-} else {
- unmasklbl.setAttribute("for", punmask.id+"unmask");
-}
-unmaskdiv.appendChild(unmasklbl);
-
-if (is_ie) {
- // ugly hack to work around the famous onchange IE bug
- unmaskchb.onclick = function() {this.blur();};
- unmaskdiv.onclick = function() {this.blur();};
-}
12 lib/form/passwordunmask.php
View
@@ -19,15 +19,17 @@ function MoodleQuickForm_passwordunmask($elementName=null, $elementLabel=null, $
}
function toHtml() {
- global $CFG;
+ global $PAGE;
+
if ($this->_flagFrozen) {
return $this->getFrozenHtml();
} else {
- $id = $this->getAttribute('id');
$unmask = get_string('unmaskpassword', 'form');
- $unmaskjs = html_writer::script(js_writer::set_variable('punmask', array('id'=>$id, 'unmaskstr'=>$unmask)));
- $unmaskjs .= html_writer::script('', $CFG->httpswwwroot.'/lib/form/passwordunmask.js');
- return $this->_getTabs() . '<input' . $this->_getAttrString($this->_attributes) . ' /><div class="unmask" id="'.$id.'unmaskdiv"></div>'.$unmaskjs;
+ $this->updateAttributes(array('autocomplete' => 'off'));
+ //Pass id of the element, so that unmask checkbox can be attached.
+ $PAGE->requires->yui_module('moodle-form-passwordunmask', 'M.form.passwordunmask',
+ array(array('formid' => $this->getAttribute('id'), 'checkboxname' => $unmask)));
+ return $this->_getTabs() . '<input' . $this->_getAttrString($this->_attributes) . ' />';
}
} //end func toHtml
39 lib/form/yui/passwordunmask/passwordunmask.js
View
@@ -0,0 +1,39 @@
+YUI.add('moodle-form-passwordunmask', function(Y) {
+ var PASSWORDUNMASK = function() {
+ PASSWORDUNMASK.superclass.constructor.apply(this, arguments);
+ }
+
+ Y.extend(PASSWORDUNMASK, Y.Base, {
+ //Initialize checkbox if id is passed
+ initializer : function(params) {
+ if (params && params.formid) {
+ this.add_checkbox(params.formid, params.checkboxname);
+ }
+ },
+ //Create checkbox for unmasking password
+ add_checkbox : function(elementid, checkboxlabel) {
+ var node = Y.one('#'+elementid);
+
+ //retaining unmask div from previous implementation.
+ var unmaskdiv = Y.Node.create('<div id="'+elementid+'unmaskdiv" class="unmask"></div>');
+
+ //Add checkbox for unmasking to unmaskdiv
+ var unmaskchb = Y.Node.create('<input id="'+elementid+'unmask" type="checkbox">');
+ unmaskdiv.appendChild(unmaskchb);
+ //Attach event using static javascript function for unmasking password.
+ unmaskchb.on('click', function() {unmaskPassword(elementid);});
+
+ //Add label for checkbox to unmaskdiv
+ var unmasklabel = Y.Node.create('<label for="'+elementid+'unmask">'+checkboxlabel+'</label>');
+ unmaskdiv.appendChild(unmasklabel);
+
+ //Insert unmask div in the same div as password input.
+ node.get('parentNode').insert(unmaskdiv, node.get('lastNode'));
+ }
+ });
+
+ M.form = M.form || {};
+ M.form.passwordunmask = function(params) {
+ return new PASSWORDUNMASK(params);
+ }
+}, '@VERSION@', {requires:['base', 'node']});
17 lib/formslib.php
View
@@ -141,9 +141,10 @@ function moodleform($action=null, $customdata=null, $method='post', $target='',
if (empty($action)){
$action = strip_querystring(qualified_me());
}
-
- $this->_formname = get_class($this); // '_form' suffix kept in order to prevent collisions of form id and other element
+ // Assign custom data first, so that get_form_identifier can use it.
$this->_customdata = $customdata;
+ $this->_formname = $this->get_form_identifier();
+
$this->_form = new MoodleQuickForm($this->_formname, $method, $action, $target, $attributes);
if (!$editable){
$this->_form->hardFreeze();
@@ -164,6 +165,18 @@ function moodleform($action=null, $customdata=null, $method='post', $target='',
}
/**
+ * It should returns unique identifier for the form.
+ * Currently it will return class name, but in case two same forms have to be
+ * rendered on same page then override function to get unique form identifier.
+ * e.g This is used on multiple self enrollments page.
+ *
+ * @return string form identifier.
+ */
+ protected function get_form_identifier() {
+ return get_class($this);
+ }
+
+ /**
* To autofocus on first form element or first element with error.
*
* @param string $name if this is set then the focus is forced to a field with this name
Please sign in to comment.
Something went wrong with that request. Please try again.