From c14584f2066c2d24e32318a52f680eef32019297 Mon Sep 17 00:00:00 2001 From: Rajesh Taneja Date: Tue, 6 Mar 2012 12:06:59 +0800 Subject: [PATCH 1/2] MDL-28972 formslib: Checkbox controller will use yui to select and unselect checkboxes --- .../checkboxcontroller/checkboxcontroller.js | 113 ++++++++++++++++++ lib/formslib.php | 50 +++----- 2 files changed, 128 insertions(+), 35 deletions(-) create mode 100644 lib/form/yui/checkboxcontroller/checkboxcontroller.js 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..92aef3be3a1 100644 --- a/lib/formslib.php +++ b/lib/formslib.php @@ -1058,7 +1058,7 @@ 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; // Set the default text if none was specified if (empty($text)) { @@ -1068,50 +1068,30 @@ abstract class moodleform { $mform = $this->_form; $select_value = optional_param('checkbox_controller'. $groupid, null, PARAM_INT); - if ($select_value == 0 || is_null($select_value)) { - $new_select_value = 1; - } else { + if (is_null($select_value)) { $new_select_value = 0; + } else { + $new_select_value = (int) !$select_value; } - $mform->addElement('hidden', "checkbox_controller$groupid"); + $mform->addElement('hidden', "checkbox_controller".$groupid, null, array('id' => "id_checkbox_controller".$groupid)); $mform->setType("checkbox_controller$groupid", PARAM_INT); $mform->setConstants(array("checkbox_controller$groupid" => $new_select_value)); $checkbox_controller_name = 'nosubmit_checkbox_controller' . $groupid; $mform->registerNoSubmitButton($checkbox_controller_name); - // Prepare Javascript for submit element - $js = "\n//requires->yui_module('moodle-form-checkboxcontroller', 'M.form.checkboxcontroller', + array( + array('groupid' => $groupid, + 'checkboxclass' => 'checkboxgroup'.$groupid, + 'checkboxcontroller' => "checkbox_controller".$groupid, + 'controllerbutton' => $checkbox_controller_name) + ) + ); - 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($checkbox_controller_name, $attributes); $mform->addElement($submitlink); $mform->setDefault($checkbox_controller_name, $text); } From 7f850b23d7de9491a60b94583c77fc54fd6a235a Mon Sep 17 00:00:00 2001 From: Rajesh Taneja Date: Fri, 9 Mar 2012 14:15:02 +0800 Subject: [PATCH 2/2] MDL-28972 formslib: orignalvalue is considered and checkboxes are updated depending on orignalvalue of controller --- lib/formslib.php | 42 ++++++++++++++++++++++++++++-------------- 1 file changed, 28 insertions(+), 14 deletions(-) diff --git a/lib/formslib.php b/lib/formslib.php index 92aef3be3a1..2a497ea387d 100644 --- a/lib/formslib.php +++ b/lib/formslib.php @@ -1060,40 +1060,54 @@ abstract class moodleform { function add_checkbox_controller($groupid, $text = null, $attributes = null, $originalValue = 0) { 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)) { $text = get_string('selectallornone', 'form'); } $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); + $new_select_value = $select_value; if (is_null($select_value)) { - $new_select_value = 0; - } else { + $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, null, array('id' => "id_checkbox_controller".$groupid)); - $mform->setType("checkbox_controller$groupid", PARAM_INT); - $mform->setConstants(array("checkbox_controller$groupid" => $new_select_value)); - - $checkbox_controller_name = 'nosubmit_checkbox_controller' . $groupid; - $mform->registerNoSubmitButton($checkbox_controller_name); + $mform->addElement('hidden', $checkboxcontrollerparam, $new_select_value, array('id' => "id_".$checkboxcontrollerparam)); + $mform->setType($checkboxcontrollerparam, PARAM_INT); + $mform->setConstants(array($checkboxcontrollerparam => $new_select_value)); $PAGE->requires->yui_module('moodle-form-checkboxcontroller', 'M.form.checkboxcontroller', array( array('groupid' => $groupid, - 'checkboxclass' => 'checkboxgroup'.$groupid, - 'checkboxcontroller' => "checkbox_controller".$groupid, - 'controllerbutton' => $checkbox_controller_name) + 'checkboxclass' => $checkboxgroupclass, + 'checkboxcontroller' => $checkboxcontrollerparam, + 'controllerbutton' => $checkboxcontrollername) ) ); require_once("$CFG->libdir/form/submit.php"); - $submitlink = new MoodleQuickForm_submit($checkbox_controller_name, $attributes); + $submitlink = new MoodleQuickForm_submit($checkboxcontrollername, $attributes); $mform->addElement($submitlink); - $mform->setDefault($checkbox_controller_name, $text); + $mform->registerNoSubmitButton($checkboxcontrollername); + $mform->setDefault($checkboxcontrollername, $text); } /**