diff --git a/lib/form/yui/checkboxcontroller/checkboxcontroller.js b/lib/form/yui/checkboxcontroller/checkboxcontroller.js new file mode 100644 index 00000000000..c59fd67dbe1 --- /dev/null +++ b/lib/form/yui/checkboxcontroller/checkboxcontroller.js @@ -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 . + + +/** + * 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 + * @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(''+linkname+''); + + // 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']}); \ No newline at end of file diff --git a/lib/formslib.php b/lib/formslib.php index 39537d8a1f5..2a497ea387d 100644 --- a/lib/formslib.php +++ b/lib/formslib.php @@ -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//\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); } /**