Skip to content

Commit

Permalink
MDL-12389 Added a new getElementsByClassName function in lib/javascri…
Browse files Browse the repository at this point in the history
…pt.php, added the selectallornone formslib element, modified the advcheckbox element to support the group variable, and implemented the new element in grade_export_form
  • Loading branch information
nicolasconnault committed Dec 3, 2007
1 parent 166db28 commit 8f9607d
Show file tree
Hide file tree
Showing 6 changed files with 61,807 additions and 13,333 deletions.
10 changes: 7 additions & 3 deletions grade/export/grade_export_form.php
Expand Up @@ -125,16 +125,20 @@ function definition() {
$total = $grade_items[1];
unset($grade_items[1]);
$grade_items[] = $total;

$needs_multiselect = false;
foreach ($grade_items as $grade_item) {
if (!empty($features['idnumberrequired']) and empty($grade_item->idnumber)) {
$mform->addElement('advcheckbox', 'itemids['.$grade_item->id.']', $grade_item->get_name(), get_string('noidnumber', 'grades'));
$mform->hardFreeze('itemids['.$grade_item->id.']');

} else {
$mform->addElement('advcheckbox', 'itemids['.$grade_item->id.']', $grade_item->get_name());
$mform->addElement('advcheckbox', 'itemids['.$grade_item->id.']', $grade_item->get_name(), null, array('group' => 1));
$mform->setDefault('itemids['.$grade_item->id.']', 1);
$needs_multiselect = true;
}
}

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
}
}

Expand Down
35 changes: 35 additions & 0 deletions lib/form/advcheckbox.php
Expand Up @@ -17,6 +17,13 @@ class MoodleQuickForm_advcheckbox extends HTML_QuickForm_advcheckbox{
* @var string
*/
var $_helpbutton='';

/**
* Group to which this checkbox belongs (for select all/select none button)
* @var string $_group
*/
var $_group;

/**
* Class constructor
*
Expand All @@ -36,6 +43,34 @@ function MoodleQuickForm_advcheckbox($elementName=null, $elementLabel=null, $tex
if ($values === null){
$values = array(0, 1);
}

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)) {
$attributes = array();
$attributes['class'] .= " $this->_group";
} elseif (is_array($attributes)) {
if (isset($attributes['class'])) {
$attributes['class'] .= " $this->_group";
} else {
$attributes['class'] = $this->_group;
}
} elseif ($strpos = stripos($attributes, 'class="')) {
$attributes = str_ireplace('class="', 'class="' . $this->_group . ' ', $attributes);
} else {
$attributes .= ' class="' . $this->_group . '"';
}
}

parent::HTML_QuickForm_advcheckbox($elementName, $elementLabel, $text, $attributes, $values);
} //end constructor

Expand Down
126 changes: 126 additions & 0 deletions lib/form/selectallornone.php
@@ -0,0 +1,126 @@
<?php // $Id$

///////////////////////////////////////////////////////////////////////////
// //
// NOTICE OF COPYRIGHT //
// //
// Moodle - Modular Object-Oriented Dynamic Learning Environment //
// http://moodle.com //
// //
// Copyright (C) 1999 onwards Martin Dougiamas http://dougiamas.com //
// //
// This program 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 2 of the License, or //
// (at your option) any later version. //
// //
// This program 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: //
// //
// http://www.gnu.org/copyleft/gpl.html //
// //
///////////////////////////////////////////////////////////////////////////

require_once("HTML/QuickForm/link.php");

/**
* HTML class for a multiple checkboxes state controller
*
* @author Nicolas Connault <nicolasconnault@gmail.com>
* @version 1.0
* @since PHP4.04pl1
* @access public
*/
class MoodleQuickForm_selectallornone extends HTML_QuickForm_link {
/**
* The original state of the checkboxes controlled by this element. This determines whether the first click of this element will switch them all to
* checked or to unchecked. It doesn't change the checked state of the original elements (there could be a mixed of checked/unchecked there), but
* there has to be a decision as to which action will be taken by clicking "select all/select none" the first time.
* @var int $originalValue
*/
var $_originalValue = 0;

/**
* Constructor
* @param string $elementName The name 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 MoodleQuickForm_selectallornone($elementName=null, $text=null, $attributes=null, $originalValue=0) {
if (is_null($originalValue)) {
$originalValue = 0;
}

global $FULLME;
$this->_originalValue = $originalValue;

if (is_null($elementName)) {
return;
}
$elementLabel = '&nbsp;';
$strselectallornone = get_string('selectallornone', 'form');
$attributes['onmouseover'] = "window.status='" . $strselectallornone . "';";
$attributes['onmouseout'] = "window.status='';";
$attributes['onclick'] = "html_quickform_toggle_checkboxes($elementName); return false;";
$select_value = optional_param('select'. $elementName, $originalValue, PARAM_INT);

if ($select_value == 0) {
$new_select_value = 1;
} else {
$new_select_value = 0;
}

$old_selectstr = "&select$elementName=$select_value";
$new_selectstr = "&select$elementName=$new_select_value";
$new_fullme = str_replace($old_selectstr, '', $FULLME);

$href = "$new_fullme$new_selectstr";

if (empty($text)) {
$text = $strselectallornone;
}
$this->HTML_QuickForm_link($elementName, $elementLabel, $href, $text, $attributes);
}

function toHtml() {
if (is_null($this->_originalValue)) {
return false;
}

$group = $this->_attributes['name'];
if ($this->_flagFrozen) {
$js = '';
} else {
$js = "<script type=\"text/javascript\">\n//<![CDATA[\n";
if (!defined('HTML_QUICKFORM_SELECTALLORNONE_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_SELECTALLORNONE_EXISTS', true);
}
$js .= "var html_quickform_checkboxgroup$group=$this->_originalValue;";

$js .= "//]]>\n</script>";
}
return $js . parent::toHtml();
}
}
?>
1 change: 1 addition & 0 deletions lib/formslib.php
Expand Up @@ -1786,6 +1786,7 @@ function getStopFieldsetElements(){
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('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');
Expand Down
29 changes: 29 additions & 0 deletions lib/javascript.php
Expand Up @@ -87,5 +87,34 @@ function inserttext(text) {
$focus=false; // Prevent themes from adding it to body tag which breaks addonload(), MDL-10249
} ?>

function getElementsByClassName(oElm, strTagName, oClassNames){
var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
var arrReturnElements = new Array();
var arrRegExpClassNames = new Array();
if(typeof oClassNames == "object"){
for(var i=0; i<oClassNames.length; i++){
arrRegExpClassNames.push(new RegExp("(^|\\s)" + oClassNames[i].replace(/\-/g, "\\-") + "(\\s|$)"));
}
}
else{
arrRegExpClassNames.push(new RegExp("(^|\\s)" + oClassNames.replace(/\-/g, "\\-") + "(\\s|$)"));
}
var oElement;
var bMatchesAll;
for(var j=0; j<arrElements.length; j++){
oElement = arrElements[j];
bMatchesAll = true;
for(var k=0; k<arrRegExpClassNames.length; k++){
if(!arrRegExpClassNames[k].test(oElement.className)){
bMatchesAll = false;
break;
}
}
if(bMatchesAll){
arrReturnElements.push(oElement);
}
}
return (arrReturnElements)
}
//]]>
</script>

0 comments on commit 8f9607d

Please sign in to comment.