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.

This commit is contained in:
nicolasconnault 2007-12-04 10:35:25 +00:00
parent 1d854cec5a
commit 6073a598a7
4 changed files with 96 additions and 12 deletions

View File

@ -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'));
}
}
?>

View File

@ -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)) {

18
lib/form/submitlink.php Normal file
View File

@ -0,0 +1,18 @@
<?php
global $CFG;
require_once("$CFG->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 "<noscript>" . parent::toHtml() . '</noscript><script type="text/javascript">' . $this->_js . "\n"
. 'document.write(\'<a href="#" onclick="' . $this->_onclick . '" onmouseover="' . $onmouseover . '" onmouseout="' . $onmouseout . '">'
. $text . "</a>');\n</script>";
}
}
?>

View File

@ -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//<![CDATA[\n";
if (!defined('HTML_QUICKFORM_CHECKBOXCONTROLLER_EXISTS')) {
$js .= <<<EOS
function html_quickform_toggle_checkboxes(group) {
var checkboxes = getElementsByClassName(document, 'input', '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;";
$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');