Merge branch 'wip-mdl-28972' of git://github.com/rajeshtaneja/moodle

This commit is contained in:
Sam Hemelryk
2012-03-14 14:40:07 +13:00
2 changed files with 150 additions and 43 deletions
+113
View File
@@ -0,0 +1,113 @@
// 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/>.
/**
* Group of date and time input element
*
* Contains class for a group of elements used to input a date and time.
*
* @package core_form
* @copyright 2012 Rajesh Taneja <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
YUI.add('moodle-form-checkboxcontroller', function(Y) {
var checkboxcontroller = function() {
checkboxcontroller.superclass.constructor.apply(this, arguments);
}
Y.extend(checkboxcontroller, Y.Base, {
_controllervaluenode : null,
_checkboxclass : null,
/*
* Initialize script if all params passed.
*
* @param object params values passed while initalizing script
*/
initializer : function(params) {
if (params && params.checkboxcontroller &&
params.controllerbutton &&
params.checkboxclass) {
// Id of controller node which keeps value in html.
this._controllervaluenode = '#id_'+params.checkboxcontroller;
// Checkboxes class name by which checkboxes will be selected
this._checkboxclass = '.'+params.checkboxclass;
// Replace submit button with link.
this.replaceButton('#id_'+params.controllerbutton);
}
},
/**
* Replace controller button with link and add event.
*
* @param string controllerbutton id of the controller button which needs to be replaced
*/
replaceButton : function(controllerbutton) {
var controllerbutton = Y.one(controllerbutton);
var linkname = controllerbutton.get('value');
// Link node which will replace controller button
var link = Y.Node.create('<a href="#" onmouseover="window.status=\''+linkname+'\';" onmouseout="window.status=\'\';">'+linkname+'</a>');
// Attach onclick event to link
link.on('click', this.onClick, this);
// Hide controller button
controllerbutton.hide();
// Insert link node
controllerbutton.get('parentNode').insert(link, controllerbutton.get('lastNode'));
},
/**
* Onclick event will be handled.
*
* @param Event e
*/
onClick : function(e) {
e.preventDefault();
this.switchGroupState();
},
/**
* Toggles checkboxes status belong to a group
*/
switchGroupState : function() {
if (this._checkboxclass) {
// Value which should be set on checkboxes
var newvalue = '';
// Get controller node which keeps value
var controllervaluenode = Y.one(this._controllervaluenode);
// Get all checkboxes with
var checkboxes = Y.all(this._checkboxclass);
// Toggle checkboxes in group, depending on conroller value
if (controllervaluenode.get('value') == 1) {
controllervaluenode.set('value', '0');
} else {
controllervaluenode.set('value', '1');
newvalue = 'checked';
}
checkboxes.set('checked', newvalue);
}
}
});
M.form = M.form || {};
M.form.checkboxcontroller = function(params) {
return new checkboxcontroller(params);
}
}, '@VERSION@', {requires:['base', 'node', 'util']});
+37 -43
View File
@@ -1058,7 +1058,12 @@ abstract class moodleform {
* @param int $originalValue The original general state of the checkboxes before the user first clicks this element
*/
function add_checkbox_controller($groupid, $text = null, $attributes = null, $originalValue = 0) {
global $CFG;
global $CFG, $PAGE;
// Name of the controller button
$checkboxcontrollername = 'nosubmit_checkbox_controller' . $groupid;
$checkboxcontrollerparam = 'checkbox_controller'. $groupid;
$checkboxgroupclass = 'checkboxgroup'.$groupid;
// Set the default text if none was specified
if (empty($text)) {
@@ -1066,54 +1071,43 @@ abstract class moodleform {
}
$mform = $this->_form;
$select_value = optional_param('checkbox_controller'. $groupid, null, PARAM_INT);
$select_value = optional_param($checkboxcontrollerparam, null, PARAM_INT);
$contollerbutton = optional_param($checkboxcontrollername, null, PARAM_ALPHAEXT);
if ($select_value == 0 || is_null($select_value)) {
$new_select_value = 1;
} else {
$new_select_value = 0;
$new_select_value = $select_value;
if (is_null($select_value)) {
$new_select_value = $originalValue;
} else if (!is_null($contollerbutton)) {
$new_select_value = (int) !$select_value;
}
// set checkbox state depending on orignal/submitted value by controoler button
if (!is_null($contollerbutton) || is_null($select_value)) {
foreach ($mform->_elements as $element) {
if (($element instanceof MoodleQuickForm_advcheckbox) &&
$element->getAttribute('class') == $checkboxgroupclass) {
$mform->setConstants(array($element->getName() => $new_select_value));
}
}
}
$mform->addElement('hidden', "checkbox_controller$groupid");
$mform->setType("checkbox_controller$groupid", PARAM_INT);
$mform->setConstants(array("checkbox_controller$groupid" => $new_select_value));
$mform->addElement('hidden', $checkboxcontrollerparam, $new_select_value, array('id' => "id_".$checkboxcontrollerparam));
$mform->setType($checkboxcontrollerparam, PARAM_INT);
$mform->setConstants(array($checkboxcontrollerparam => $new_select_value));
$checkbox_controller_name = 'nosubmit_checkbox_controller' . $groupid;
$mform->registerNoSubmitButton($checkbox_controller_name);
$PAGE->requires->yui_module('moodle-form-checkboxcontroller', 'M.form.checkboxcontroller',
array(
array('groupid' => $groupid,
'checkboxclass' => $checkboxgroupclass,
'checkboxcontroller' => $checkboxcontrollerparam,
'controllerbutton' => $checkboxcontrollername)
)
);
// Prepare Javascript for submit element
$js = "\n//<![CDATA[\n";
if (!defined('HTML_QUICKFORM_CHECKBOXCONTROLLER_EXISTS')) {
$js .= <<<EOS
function html_quickform_toggle_checkboxes(group) {
var checkboxes = document.getElementsByClassName('checkboxgroup' + group);
var newvalue = false;
var global = eval('html_quickform_checkboxgroup' + group + ';');
if (global == 1) {
eval('html_quickform_checkboxgroup' + group + ' = 0;');
newvalue = '';
} else {
eval('html_quickform_checkboxgroup' + group + ' = 1;');
newvalue = 'checked';
}
for (i = 0; i < checkboxes.length; i++) {
checkboxes[i].checked = newvalue;
}
}
EOS;
define('HTML_QUICKFORM_CHECKBOXCONTROLLER_EXISTS', true);
}
$js .= "\nvar html_quickform_checkboxgroup$groupid=$originalValue;\n";
$js .= "//]]>\n";
require_once("$CFG->libdir/form/submitlink.php");
$submitlink = new MoodleQuickForm_submitlink($checkbox_controller_name, $attributes);
$submitlink->_js = $js;
$submitlink->_onclick = "html_quickform_toggle_checkboxes($groupid); return false;";
require_once("$CFG->libdir/form/submit.php");
$submitlink = new MoodleQuickForm_submit($checkboxcontrollername, $attributes);
$mform->addElement($submitlink);
$mform->setDefault($checkbox_controller_name, $text);
$mform->registerNoSubmitButton($checkboxcontrollername);
$mform->setDefault($checkboxcontrollername, $text);
}
/**