From: nicolasconnault Date: Tue, 4 Dec 2007 10:35:25 +0000 (+0000) Subject: MDL-12389 Removed the selectallornone element in favour of a more generic submitlink... X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=6073a598a751dd8a46e146c26b2d206619620b2d;p=moodle.git MDL-12389 Removed the selectallornone element in favour of a more generic submitlink element. The checkbox controller is now added to a form using moodleform::add_checkbox_controller. --- diff --git a/grade/export/grade_export_form.php b/grade/export/grade_export_form.php index 13811f05a0..515f52d93b 100755 --- a/grade/export/grade_export_form.php +++ b/grade/export/grade_export_form.php @@ -138,13 +138,13 @@ class grade_export_form extends moodleform { } if ($needs_multiselect) { - $mform->addElement('selectallornone', 1, null, null, 1); // 2nd argument is group name, 3rd is link text, 4th is attributes and 5th is original value + $this->add_checkbox_controller(1, null, null, 1); // 1st argument is group name, 2nd is link text, 3rd is attributes and 4th is original value } } $mform->addElement('hidden', 'id', $COURSE->id); - $this->add_action_buttons(false, get_string('submit')); + } } ?> diff --git a/lib/form/advcheckbox.php b/lib/form/advcheckbox.php index ebf43f8690..63938e7072 100644 --- a/lib/form/advcheckbox.php +++ b/lib/form/advcheckbox.php @@ -46,13 +46,6 @@ class MoodleQuickForm_advcheckbox extends HTML_QuickForm_advcheckbox{ if (!is_null($attributes['group'])) { - $select_value = optional_param('select'. $attributes['group'], null, PARAM_INT); - if ($select_value == 1) { - $this->setValue(1); - } elseif ($select_value == 0) { - $this->setValue(0); - } - $this->_group = 'checkboxgroup' . $attributes['group']; unset($attributes['group']); if (is_null($attributes)) { diff --git a/lib/form/submitlink.php b/lib/form/submitlink.php new file mode 100644 index 0000000000..9b5c6b015c --- /dev/null +++ b/lib/form/submitlink.php @@ -0,0 +1,18 @@ +libdir/form/submit.php"); +class MoodleQuickForm_submitlink extends MoodleQuickForm_submit { + var $_js; + var $_onclick; + + function toHtml() { + $text = $this->_attributes['value']; + $onmouseover = "window.status=\'" . $text . "\';"; + $onmouseout = "window.status=\'\';"; + + return ""; + } +} +?> diff --git a/lib/formslib.php b/lib/formslib.php index 8c3eacb35b..b2c3809368 100644 --- a/lib/formslib.php +++ b/lib/formslib.php @@ -49,7 +49,7 @@ if ($CFG->debug >= DEBUG_ALL){ /** * Moodle specific wrapper that separates quickforms syntax from moodle code. You won't directly - * use this class you should write a class defintion which extends this class or a more specific + * use this class you should write a class definition which extends this class or a more specific * subclass such a moodleform_mod for each form you want to display and/or process with formslib. * * You will write your own definition() method which performs the form set up. @@ -618,6 +618,79 @@ class moodleform { return $repeats; } + + /** + * Adds a link/button that controls the checked state of a group of checkboxes. + * @param int $groupid The id of the group of advcheckboxes this element controls + * @param string $text The text of the link. Defaults to "select all/none" + * @param array $attributes associative array of HTML attributes + * @param int $originalValue The original general state of the checkboxes before the user first clicks this element + */ + function add_checkbox_controller($groupid, $buttontext, $attributes, $originalValue = 0) { + global $CFG; + if (empty($text)) { + $text = get_string('selectallornone', 'form'); + } + + $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 { + $new_select_value = 0; + } + + $mform->addElement('hidden', "checkbox_controller$groupid"); + $mform->setConstants(array("checkbox_controller$groupid" => $new_select_value)); + + // Locate all checkboxes for this group and set their value, IF the optional param was given + if (!is_null($select_value)) { + foreach ($this->_form->_elements as $element) { + if ($element->getAttribute('class') == "checkboxgroup$groupid") { + $mform->setConstants(array($element->getAttribute('name') => $select_value)); + } + } + } + + $checkbox_controller_name = 'nosubmit_checkbox_controller' . $groupid; + $mform->registerNoSubmitButton($checkbox_controller_name); + + // 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;"; + $mform->addElement($submitlink); + $mform->setDefault($checkbox_controller_name, $text); + } + /** * Use this method to a cancel and submit button to the end of your form. Pass a param of false * if you don't want a cancel button in your form. If you have a cancel button make sure you @@ -649,7 +722,7 @@ class moodleform { /** * You never extend this class directly. The class methods of this class are available from - * the private $this->_form property on moodleform and it's children. You generally only + * the private $this->_form property on moodleform and its children. You generally only * call methods on this class from within abstract methods that you override on moodleform such * as definition and definition_after_data * @@ -1786,7 +1859,7 @@ MoodleQuickForm::registerElementType('passwordunmask', "$CFG->libdir/form/passwo MoodleQuickForm::registerElementType('radio', "$CFG->libdir/form/radio.php", 'MoodleQuickForm_radio'); MoodleQuickForm::registerElementType('select', "$CFG->libdir/form/select.php", 'MoodleQuickForm_select'); MoodleQuickForm::registerElementType('selectgroups', "$CFG->libdir/form/selectgroups.php", 'MoodleQuickForm_selectgroups'); -MoodleQuickForm::registerElementType('selectallornone', "$CFG->libdir/form/selectallornone.php", 'MoodleQuickForm_selectallornone'); +MoodleQuickForm::registerElementType('submitlink', "$CFG->libdir/form/submitlink.php", 'MoodleQuickForm_submitlink'); MoodleQuickForm::registerElementType('text', "$CFG->libdir/form/text.php", 'MoodleQuickForm_text'); MoodleQuickForm::registerElementType('textarea', "$CFG->libdir/form/textarea.php", 'MoodleQuickForm_textarea'); MoodleQuickForm::registerElementType('date_selector', "$CFG->libdir/form/dateselector.php", 'MoodleQuickForm_date_selector');