Skip to content

Commit

Permalink
Merge branch 'wip-mdl-27837' of git://github.com/rajeshtaneja/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
samhemelryk committed Jul 18, 2011
2 parents c5fe313 + 3a70ccb commit cc4db9d
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 34 deletions.
14 changes: 13 additions & 1 deletion enrol/self/locallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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?
}
Expand Down
26 changes: 0 additions & 26 deletions lib/form/passwordunmask.js

This file was deleted.

12 changes: 7 additions & 5 deletions lib/form/passwordunmask.php
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
39 changes: 39 additions & 0 deletions lib/form/yui/passwordunmask/passwordunmask.js
Original file line number Diff line number Diff line change
@@ -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 changes: 15 additions & 2 deletions lib/formslib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -163,6 +164,18 @@ function moodleform($action=null, $customdata=null, $method='post', $target='',
$this->_process_submission($method);
}

/**
* 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.
*
Expand Down

0 comments on commit cc4db9d

Please sign in to comment.