Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/lib/form/selectgroups.php
Go to the documentation of this file.
00001 <?php
00002 /* vim: set expandtab tabstop=4 shiftwidth=4: */
00003 // +----------------------------------------------------------------------+
00004 // | PHP version 4.0                                                      |
00005 // +----------------------------------------------------------------------+
00006 // | Copyright (c) 1997-2003 The PHP Group                                |
00007 // +----------------------------------------------------------------------+
00008 // | This source file is subject to version 2.0 of the PHP license,       |
00009 // | that is bundled with this package in the file LICENSE, and is        |
00010 // | available at through the world-wide-web at                           |
00011 // | http://www.php.net/license/2_02.txt.                                 |
00012 // | If you did not receive a copy of the PHP license and are unable to   |
00013 // | obtain it through the world-wide-web, please send a note to          |
00014 // | license@php.net so we can mail you a copy immediately.               |
00015 // +----------------------------------------------------------------------+
00016 // | Authors: Adam Daniel <adaniel1@eesus.jnj.com>                        |
00017 // |          Bertrand Mansion <bmansion@mamasam.com>                     |
00018 // +----------------------------------------------------------------------+
00019 
00020 require_once('HTML/QuickForm/element.php');
00021 
00031 class MoodleQuickForm_selectgroups extends HTML_QuickForm_element {
00032 
00033     // {{{ properties
00034 
00036     var $showchoose = false;
00037 
00045     var $_optGroups = array();
00046 
00054     var $_values = null;
00055 
00061     var $_helpbutton='';
00062     var $_hiddenLabel=false;
00063 
00078     function MoodleQuickForm_selectgroups($elementName=null, $elementLabel=null, $optgrps=null, $attributes=null, $showchoose=false)
00079     {
00080         $this->showchoose = $showchoose;
00081         HTML_QuickForm_element::HTML_QuickForm_element($elementName, $elementLabel, $attributes);
00082         $this->_persistantFreeze = true;
00083         $this->_type = 'selectgroups';
00084         if (isset($optgrps)) {
00085             $this->loadArrayOptGroups($optgrps);
00086         }
00087     } //end constructor
00088 
00089     // }}}
00090     // {{{ apiVersion()
00091 
00092 
00101     function setSelected($values)
00102     {
00103         if (is_string($values) && $this->getMultiple()) {
00104             $values = preg_split("/[ ]?,[ ]?/", $values);
00105         }
00106         if (is_array($values)) {
00107             $this->_values = array_values($values);
00108         } else {
00109             $this->_values = array($values);
00110         }
00111     } //end func setSelected
00112 
00113     // }}}
00114     // {{{ getSelected()
00115 
00123     function getSelected()
00124     {
00125         return $this->_values;
00126     } // end func getSelected
00127 
00128     // }}}
00129     // {{{ setName()
00130 
00139     function setName($name)
00140     {
00141         $this->updateAttributes(array('name' => $name));
00142     } //end func setName
00143 
00144     // }}}
00145     // {{{ getName()
00146 
00154     function getName()
00155     {
00156         return $this->getAttribute('name');
00157     } //end func getName
00158 
00159     // }}}
00160     // {{{ getPrivateName()
00161 
00169     function getPrivateName()
00170     {
00171         if ($this->getAttribute('multiple')) {
00172             return $this->getName() . '[]';
00173         } else {
00174             return $this->getName();
00175         }
00176     } //end func getPrivateName
00177 
00178     // }}}
00179     // {{{ setValue()
00180 
00189     function setValue($value)
00190     {
00191         $this->setSelected($value);
00192     } // end func setValue
00193 
00194     // }}}
00195     // {{{ getValue()
00196 
00204     function getValue()
00205     {
00206         return $this->_values;
00207     } // end func getValue
00208 
00209     // }}}
00210     // {{{ setSize()
00211 
00220     function setSize($size)
00221     {
00222         $this->updateAttributes(array('size' => $size));
00223     } //end func setSize
00224 
00225     // }}}
00226     // {{{ getSize()
00227 
00235     function getSize()
00236     {
00237         return $this->getAttribute('size');
00238     } //end func getSize
00239 
00240     // }}}
00241     // {{{ setMultiple()
00242 
00251     function setMultiple($multiple)
00252     {
00253         if ($multiple) {
00254             $this->updateAttributes(array('multiple' => 'multiple'));
00255         } else {
00256             $this->removeAttribute('multiple');
00257         }
00258     } //end func setMultiple
00259 
00260     // }}}
00261     // {{{ getMultiple()
00262 
00270     function getMultiple()
00271     {
00272         return (bool)$this->getAttribute('multiple');
00273     } //end func getMultiple
00274 
00285     function loadArrayOptGroups($arr, $values=null)
00286     {
00287         if (!is_array($arr)) {
00288             return PEAR::raiseError('Argument 1 of HTML_Select::loadArrayOptGroups is not a valid array');
00289         }
00290         if (isset($values)) {
00291             $this->setSelected($values);
00292         }
00293         foreach ($arr as $key => $val) {
00294             // Warning: new API since release 2.3
00295             $this->addOptGroup($key, $val);
00296         }
00297         return true;
00298     }
00310     function addOptGroup($text, $value, $attributes=null)
00311     {
00312         if (null === $attributes) {
00313             $attributes = array('label' => $text);
00314         } else {
00315             $attributes = $this->_parseAttributes($attributes);
00316             $this->_updateAttrArray($attributes, array('label' => $text));
00317         }
00318         $index = count($this->_optGroups);
00319         $this->_optGroups[$index] = array('attr' => $attributes);
00320         $this->loadArrayOptions($index, $value);
00321     }
00322 
00333     function loadArrayOptions($optgroup, $arr, $values=null)
00334     {
00335         if (!is_array($arr)) {
00336             return PEAR::raiseError('Argument 1 of HTML_Select::loadArray is not a valid array');
00337         }
00338         if (isset($values)) {
00339             $this->setSelected($values);
00340         }
00341         foreach ($arr as $key => $val) {
00342             // Warning: new API since release 2.3
00343             $this->addOption($optgroup, $val, $key);
00344         }
00345         return true;
00346     }
00347 
00359     function addOption($optgroup, $text, $value, $attributes=null)
00360     {
00361         if (null === $attributes) {
00362             $attributes = array('value' => $value);
00363         } else {
00364             $attributes = $this->_parseAttributes($attributes);
00365             if (isset($attributes['selected'])) {
00366                 // the 'selected' attribute will be set in toHtml()
00367                 $this->_removeAttr('selected', $attributes);
00368                 if (is_null($this->_values)) {
00369                     $this->_values = array($value);
00370                 } elseif (!in_array($value, $this->_values)) {
00371                     $this->_values[] = $value;
00372                 }
00373             }
00374             $this->_updateAttrArray($attributes, array('value' => $value));
00375         }
00376         $this->_optGroups[$optgroup]['options'][] = array('text' => $text, 'attr' => $attributes);
00377     }
00378 
00386     function toHtml()
00387     {
00388         if ($this->_flagFrozen) {
00389             return $this->getFrozenHtml();
00390         } else {
00391             $tabs    = $this->_getTabs();
00392             $strHtml = '';
00393 
00394             if ($this->getComment() != '') {
00395                 $strHtml .= $tabs . '<!-- ' . $this->getComment() . " //-->\n";
00396             }
00397 
00398             if (!$this->getMultiple()) {
00399                 $attrString = $this->_getAttrString($this->_attributes);
00400             } else {
00401                 $myName = $this->getName();
00402                 $this->setName($myName . '[]');
00403                 $attrString = $this->_getAttrString($this->_attributes);
00404                 $this->setName($myName);
00405             }
00406             $strHtml .= $tabs;
00407             if ($this->_hiddenLabel){
00408                 $this->_generateId();
00409                 $strHtml .= '<label class="accesshide" for="'.$this->getAttribute('id').'" >'.
00410                             $this->getLabel().'</label>';
00411             }
00412             $strHtml .=  '<select' . $attrString . ">\n";
00413             if ($this->showchoose) {
00414                 $strHtml .= $tabs . "\t\t<option value=\"\">" . get_string('choose') . "...</option>\n";
00415             }
00416             foreach ($this->_optGroups as $optGroup) {
00417                 if (empty($optGroup['options'])) {
00418                     //xhtml strict
00419                     continue;
00420                 }
00421                 $strHtml .= $tabs . "\t<optgroup" . ($this->_getAttrString($optGroup['attr'])) . '>';
00422                 foreach ($optGroup['options'] as $option){
00423                     if (is_array($this->_values) && in_array((string)$option['attr']['value'], $this->_values)) {
00424                         $this->_updateAttrArray($option['attr'], array('selected' => 'selected'));
00425                     }
00426                     $strHtml .= $tabs . "\t\t<option" . $this->_getAttrString($option['attr']) . '>' .
00427                                 $option['text'] . "</option>\n";
00428                 }
00429                 $strHtml .= $tabs . "\t</optgroup>\n";
00430             }
00431             return $strHtml . $tabs . '</select>';
00432         }
00433     } //end func toHtml
00434 
00435     // }}}
00436     // {{{ getFrozenHtml()
00437 
00445     function getFrozenHtml()
00446     {
00447         $value = array();
00448         if (is_array($this->_values)) {
00449             foreach ($this->_values as $key => $val) {
00450                 foreach ($this->_optGroups as $optGroup) {
00451                     for ($i = 0, $optCount = count($optGroup['options']); $i < $optCount; $i++) {
00452                         if ((string)$val == (string)$optGroup['options'][$i]['attr']['value']) {
00453                             $value[$key] = $optGroup['options'][$i]['text'];
00454                             break;
00455                         }
00456                     }
00457                 }
00458             }
00459         }
00460         $html = empty($value)? '&nbsp;': join('<br />', $value);
00461         if ($this->_persistantFreeze) {
00462             $name = $this->getPrivateName();
00463             // Only use id attribute if doing single hidden input
00464             if (1 == count($value)) {
00465                 $id     = $this->getAttribute('id');
00466                 $idAttr = isset($id)? array('id' => $id): array();
00467             } else {
00468                 $idAttr = array();
00469             }
00470             foreach ($value as $key => $item) {
00471                 $html .= '<input' . $this->_getAttrString(array(
00472                              'type'  => 'hidden',
00473                              'name'  => $name,
00474                              'value' => $this->_values[$key]
00475                          ) + $idAttr) . ' />';
00476             }
00477         }
00478         return $html;
00479     } //end func getFrozenHtml
00480 
00481     // }}}
00482     // {{{ exportValue()
00483 
00488     function exportValue(&$submitValues, $assoc = false)
00489     {
00490         if (empty($this->_optGroups)) {
00491             return $this->_prepareValue(null, $assoc);
00492         }
00493 
00494         $value = $this->_findValue($submitValues);
00495         if (is_null($value)) {
00496             $value = $this->getValue();
00497         }
00498         $value = (array)$value;
00499 
00500         $cleaned = array();
00501         foreach ($value as $v) {
00502             foreach ($this->_optGroups as $optGroup){
00503                 if (empty($optGroup['options'])) {
00504                     continue;
00505                 }
00506                 foreach ($optGroup['options'] as $option) {
00507                     if ((string)$option['attr']['value'] === (string)$v) {
00508                         $cleaned[] = (string)$option['attr']['value'];
00509                         break;
00510                     }
00511                 }
00512             }
00513         }
00514 
00515         if (empty($cleaned)) {
00516             return $this->_prepareValue(null, $assoc);
00517         }
00518         if ($this->getMultiple()) {
00519             return $this->_prepareValue($cleaned, $assoc);
00520         } else {
00521             return $this->_prepareValue($cleaned[0], $assoc);
00522         }
00523     }
00524     
00525     // }}}
00526     // {{{ onQuickFormEvent()
00527 
00528     function onQuickFormEvent($event, $arg, &$caller)
00529     {
00530         if ('updateValue' == $event) {
00531             $value = $this->_findValue($caller->_constantValues);
00532             if (null === $value) {
00533                 $value = $this->_findValue($caller->_submitValues);
00534                 // Fix for bug #4465 & #5269
00535                 // XXX: should we push this to element::onQuickFormEvent()?
00536                 if (null === $value && (!$caller->isSubmitted() || !$this->getMultiple())) {
00537                     $value = $this->_findValue($caller->_defaultValues);
00538                 }
00539             }
00540             if (null !== $value) {
00541                 $this->setValue($value);
00542             }
00543             return true;
00544         } else {
00545             return parent::onQuickFormEvent($event, $arg, $caller);
00546         }
00547     }
00548     function setHiddenLabel($hiddenLabel){
00549         $this->_hiddenLabel = $hiddenLabel;
00550     }
00558     function setHelpButton($helpbuttonargs, $function='helpbutton'){
00559         debugging('component setHelpButton() is not used any more, please use $mform->setHelpButton() instead');
00560     }
00567     function getHelpButton(){
00568         return $this->_helpbutton;
00569     }
00570 
00578     function getElementTemplateType(){
00579         if ($this->_flagFrozen){
00580             return 'static';
00581         } else {
00582             return 'default';
00583         }
00584     }
00585 }
 All Data Structures Namespaces Files Functions Variables Enumerations