Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/lib/form/dateselector.php
Go to the documentation of this file.
00001 <?php
00002 
00004 //                                                                       //
00005 // NOTICE OF COPYRIGHT                                                   //
00006 //                                                                       //
00007 // Moodle - Modular Object-Oriented Dynamic Learning Environment         //
00008 //          http://moodle.org                                            //
00009 //                                                                       //
00010 // Copyright (C) 1999 onwards Martin Dougiamas  http://dougiamas.com     //
00011 //                                                                       //
00012 // This program is free software; you can redistribute it and/or modify  //
00013 // it under the terms of the GNU General Public License as published by  //
00014 // the Free Software Foundation; either version 2 of the License, or     //
00015 // (at your option) any later version.                                   //
00016 //                                                                       //
00017 // This program is distributed in the hope that it will be useful,       //
00018 // but WITHOUT ANY WARRANTY; without even the implied warranty of        //
00019 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         //
00020 // GNU General Public License for more details:                          //
00021 //                                                                       //
00022 //          http://www.gnu.org/copyleft/gpl.html                         //
00023 //                                                                       //
00025 
00026 global $CFG;
00027 require_once($CFG->libdir . '/form/group.php');
00028 require_once($CFG->libdir . '/formslib.php');
00029 
00037 class MoodleQuickForm_date_selector extends MoodleQuickForm_group
00038 {
00048     protected $_options = array('startyear' => 1970, 'stopyear' => 2020,
00049             'timezone' => 99, 'applydst' => true, 'optional' => false);
00050 
00056     protected $_wrap = array('', '');
00057 
00067     function MoodleQuickForm_date_selector($elementName = null, $elementLabel = null, $options = array(), $attributes = null)
00068     {
00069         $this->HTML_QuickForm_element($elementName, $elementLabel, $attributes);
00070         $this->_persistantFreeze = true;
00071         $this->_appendName = true;
00072         $this->_type = 'date_selector';
00073         // set the options, do not bother setting bogus ones
00074         if (is_array($options)) {
00075             foreach ($options as $name => $value) {
00076                 if (isset($this->_options[$name])) {
00077                     if (is_array($value) && is_array($this->_options[$name])) {
00078                         $this->_options[$name] = @array_merge($this->_options[$name], $value);
00079                     } else {
00080                         $this->_options[$name] = $value;
00081                     }
00082                 }
00083             }
00084         }
00085         form_init_date_js();
00086     }
00087 
00088     // }}}
00089     // {{{ _createElements()
00090 
00091     function _createElements()
00092     {
00093         $this->_elements = array();
00094         for ($i=1; $i<=31; $i++) {
00095             $days[$i] = $i;
00096         }
00097         for ($i=1; $i<=12; $i++) {
00098             $months[$i] = userdate(gmmktime(12,0,0,$i,15,2000), "%B");
00099         }
00100         for ($i=$this->_options['startyear']; $i<=$this->_options['stopyear']; $i++) {
00101             $years[$i] = $i;
00102         }
00103         $this->_elements[] =& MoodleQuickForm::createElement('select', 'day', get_string('day', 'form'), $days, $this->getAttributes(), true);
00104         $this->_elements[] =& MoodleQuickForm::createElement('select', 'month', get_string('month', 'form'), $months, $this->getAttributes(), true);
00105         $this->_elements[] =& MoodleQuickForm::createElement('select', 'year', get_string('year', 'form'), $years, $this->getAttributes(), true);
00106         // If optional we add a checkbox which the user can use to turn if on
00107         if($this->_options['optional']) {
00108             $this->_elements[] =& MoodleQuickForm::createElement('checkbox', 'enabled', null, get_string('enable'), $this->getAttributes(), true);
00109         }
00110         foreach ($this->_elements as $element){
00111             if (method_exists($element, 'setHiddenLabel')){
00112                 $element->setHiddenLabel(true);
00113             }
00114         }
00115 
00116     }
00117 
00118     // }}}
00119     // {{{ onQuickFormEvent()
00120 
00131     function onQuickFormEvent($event, $arg, &$caller)
00132     {
00133         switch ($event) {
00134             case 'updateValue':
00135                 // constant values override both default and submitted ones
00136                 // default values are overriden by submitted
00137                 $value = $this->_findValue($caller->_constantValues);
00138                 if (null === $value) {
00139                     // if no boxes were checked, then there is no value in the array
00140                     // yet we don't want to display default value in this case
00141                     if ($caller->isSubmitted()) {
00142                         $value = $this->_findValue($caller->_submitValues);
00143                     } else {
00144                         $value = $this->_findValue($caller->_defaultValues);
00145                     }
00146                 }
00147                 $requestvalue=$value;
00148                 if ($value == 0) {
00149                     $value = time();
00150                 }
00151                 if (!is_array($value)) {
00152                     $currentdate = usergetdate($value);
00153                     $value = array(
00154                         'day' => $currentdate['mday'],
00155                         'month' => $currentdate['mon'],
00156                         'year' => $currentdate['year']);
00157                     // If optional, default to off, unless a date was provided
00158                      if($this->_options['optional']) {
00159                         $value['enabled'] = $requestvalue != 0;
00160                     }
00161                 } else {
00162                     $value['enabled'] = isset($value['enabled']);
00163                 }
00164                 if (null !== $value){
00165                     $this->setValue($value);
00166                 }
00167                 break;
00168             case 'createElement':
00169                 // Optional is an optional param, if its set we need to add a disabledIf rule.
00170                 // If its empty or not specified then its not an optional dateselector.
00171                 if (!empty($arg[2]['optional']) && !empty($arg[0])) {
00172                     $caller->disabledIf($arg[0], $arg[0].'[enabled]');
00173                 }
00174                 return parent::onQuickFormEvent($event, $arg, $caller);
00175                 break;
00176             default:
00177                 return parent::onQuickFormEvent($event, $arg, $caller);
00178         }
00179     } // end func onQuickFormEvent
00180 
00181     // {{{ toHtml()
00182 
00183     function toHtml()
00184     {
00185         include_once('HTML/QuickForm/Renderer/Default.php');
00186         $renderer = new HTML_QuickForm_Renderer_Default();
00187         $renderer->setElementTemplate('{element}');
00188         parent::accept($renderer);
00189         return $this->_wrap[0] . $renderer->toHtml() . $this->_wrap[1];
00190     }
00191 
00192     // }}}
00193     // {{{ accept()
00194 
00195     function accept(&$renderer, $required = false, $error = null)
00196     {
00197         $renderer->renderElement($this, $required, $error);
00198     }
00199 
00200     // }}}
00201 
00209     function exportValue(&$submitValues, $assoc = false)
00210     {
00211         $value = null;
00212         $valuearray = array();
00213         foreach ($this->_elements as $element){
00214             $thisexport = $element->exportValue($submitValues[$this->getName()], true);
00215             if ($thisexport!=null){
00216                 $valuearray += $thisexport;
00217             }
00218         }
00219         if (count($valuearray)){
00220             if($this->_options['optional']) {
00221                 // If checkbox is on, the value is zero, so go no further
00222                 if(empty($valuearray['enabled'])) {
00223                     $value[$this->getName()] = 0;
00224                     return $value;
00225                 }
00226             }
00227 
00228             $value[$this->getName()] = make_timestamp($valuearray['year'],
00229                                    $valuearray['month'],
00230                                    $valuearray['day'],
00231                                    0, 0, 0,
00232                                    $this->_options['timezone'],
00233                                    $this->_options['applydst']);
00234 
00235             return $value;
00236         } else {
00237             return null;
00238         }
00239     }
00240 
00241     // }}}
00242 }
 All Data Structures Namespaces Files Functions Variables Enumerations