Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/lib/form/datetimeselector.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_time_selector extends MoodleQuickForm_group{
00049     var $_options = array('startyear' => 1970, 'stopyear' => 2020, 'defaulttime' => 0,
00050                     'timezone' => 99, 'applydst' => true, 'step' => 5, 'optional' => false);
00051 
00057     var $_wrap = array('', '');
00058 
00068     function MoodleQuickForm_date_time_selector($elementName = null, $elementLabel = null, $options = array(), $attributes = null)
00069     {
00070         $this->HTML_QuickForm_element($elementName, $elementLabel, $attributes);
00071         $this->_persistantFreeze = true;
00072         $this->_appendName = true;
00073         $this->_type = 'date_time_selector';
00074         // set the options, do not bother setting bogus ones
00075         if (is_array($options)) {
00076             foreach ($options as $name => $value) {
00077                 if (isset($this->_options[$name])) {
00078                     if (is_array($value) && is_array($this->_options[$name])) {
00079                         $this->_options[$name] = @array_merge($this->_options[$name], $value);
00080                     } else {
00081                         $this->_options[$name] = $value;
00082                     }
00083                 }
00084             }
00085         }
00086         form_init_date_js();
00087     }
00088 
00089     // }}}
00090     // {{{ _createElements()
00091 
00092     function _createElements()
00093     {
00094         $this->_elements = array();
00095         for ($i=1; $i<=31; $i++) {
00096             $days[$i] = $i;
00097         }
00098         for ($i=1; $i<=12; $i++) {
00099             $months[$i] = userdate(gmmktime(12,0,0,$i,15,2000), "%B");
00100         }
00101         for ($i=$this->_options['startyear']; $i<=$this->_options['stopyear']; $i++) {
00102             $years[$i] = $i;
00103         }
00104         for ($i=0; $i<=23; $i++) {
00105             $hours[$i] = sprintf("%02d",$i);
00106         }
00107         for ($i=0; $i<60; $i+=$this->_options['step']) {
00108             $minutes[$i] = sprintf("%02d",$i);
00109         }
00110         $this->_elements[] =& MoodleQuickForm::createElement('select', 'day', get_string('day', 'form'), $days, $this->getAttributes(), true);
00111         $this->_elements[] =& MoodleQuickForm::createElement('select', 'month', get_string('month', 'form'), $months, $this->getAttributes(), true);
00112         $this->_elements[] =& MoodleQuickForm::createElement('select', 'year', get_string('year', 'form'), $years, $this->getAttributes(), true);
00113         if (right_to_left()) {   // Switch order of elements for Right-to-Left
00114             $this->_elements[] =& MoodleQuickForm::createElement('select', 'minute', get_string('minute', 'form'), $minutes, $this->getAttributes(), true);
00115             $this->_elements[] =& MoodleQuickForm::createElement('select', 'hour', get_string('hour', 'form'), $hours, $this->getAttributes(), true);
00116         } else {
00117             $this->_elements[] =& MoodleQuickForm::createElement('select', 'hour', get_string('hour', 'form'), $hours, $this->getAttributes(), true);
00118             $this->_elements[] =& MoodleQuickForm::createElement('select', 'minute', get_string('minute', 'form'), $minutes, $this->getAttributes(), true);
00119         }
00120         // If optional we add a checkbox which the user can use to turn if on
00121         if($this->_options['optional']) {
00122             $this->_elements[] =& MoodleQuickForm::createElement('checkbox', 'enabled', null, get_string('enable'), $this->getAttributes(), true);
00123         }
00124         foreach ($this->_elements as $element){
00125             if (method_exists($element, 'setHiddenLabel')){
00126                 $element->setHiddenLabel(true);
00127             }
00128         }
00129 
00130     }
00131 
00132     // }}}
00133     // {{{ onQuickFormEvent()
00134 
00145     function onQuickFormEvent($event, $arg, &$caller)
00146     {
00147         switch ($event) {
00148             case 'updateValue':
00149                 // constant values override both default and submitted ones
00150                 // default values are overriden by submitted
00151                 $value = $this->_findValue($caller->_constantValues);
00152                 if (null === $value) {
00153                     // if no boxes were checked, then there is no value in the array
00154                     // yet we don't want to display default value in this case
00155                     if ($caller->isSubmitted()) {
00156                         $value = $this->_findValue($caller->_submitValues);
00157                     } else {
00158                         $value = $this->_findValue($caller->_defaultValues);
00159                     }
00160                 }
00161                 $requestvalue=$value;
00162                 if ($value == 0) {
00163                     $value = $this->_options['defaulttime'];
00164                     if (!$value) {
00165                         $value = time();
00166                     }
00167                 }
00168                 if (!is_array($value)) {
00169                     $currentdate = usergetdate($value);
00170                     // Round minutes to the previous multiple of step.
00171                     $currentdate['minutes'] -= $currentdate['minutes'] % $this->_options['step'];
00172                     $value = array(
00173                         'minute' => $currentdate['minutes'],
00174                         'hour' => $currentdate['hours'],
00175                         'day' => $currentdate['mday'],
00176                         'month' => $currentdate['mon'],
00177                         'year' => $currentdate['year']);
00178                     // If optional, default to off, unless a date was provided
00179                     if($this->_options['optional']) {
00180                         $value['enabled'] = $requestvalue != 0;
00181                     }
00182                 } else {
00183                     $value['enabled'] = isset($value['enabled']);
00184                 }
00185                 if (null !== $value){
00186                     $this->setValue($value);
00187                 }
00188                 break;
00189             case 'createElement':
00190                 if($arg[2]['optional']) {
00191                     $caller->disabledIf($arg[0], $arg[0].'[enabled]');
00192                 }
00193                 return parent::onQuickFormEvent($event, $arg, $caller);
00194                 break;
00195             default:
00196                 return parent::onQuickFormEvent($event, $arg, $caller);
00197         }
00198     }
00199 
00200     // }}}
00201     // {{{ toHtml()
00202 
00203     function toHtml()
00204     {
00205         include_once('HTML/QuickForm/Renderer/Default.php');
00206         $renderer = new HTML_QuickForm_Renderer_Default();
00207         $renderer->setElementTemplate('{element}');
00208         parent::accept($renderer);
00209         return $this->_wrap[0] . $renderer->toHtml() . $this->_wrap[1];
00210     }
00211 
00212     // }}}
00213     // {{{ accept()
00214 
00215     function accept(&$renderer, $required = false, $error = null)
00216     {
00217         $renderer->renderElement($this, $required, $error);
00218     }
00219 
00220     // }}}
00221 
00229     function exportValue(&$submitValues, $assoc = false)
00230     {
00231         $value = null;
00232         $valuearray = array();
00233         foreach ($this->_elements as $element){
00234             $thisexport = $element->exportValue($submitValues[$this->getName()], true);
00235             if ($thisexport!=null){
00236                 $valuearray += $thisexport;
00237             }
00238         }
00239         if (count($valuearray)){
00240             if($this->_options['optional']) {
00241                 // If checkbox is on, the value is zero, so go no further
00242                 if(empty($valuearray['enabled'])) {
00243                     $value[$this->getName()] = 0;
00244                     return $value;
00245                 }
00246             }
00247             $valuearray=$valuearray + array('year' => 1970, 'month' => 1, 'day' => 1, 'hour' => 0, 'minute' => 0);
00248             $value[$this->getName()] = make_timestamp(
00249                                    $valuearray['year'],
00250                                    $valuearray['month'],
00251                                    $valuearray['day'],
00252                                    $valuearray['hour'],
00253                                    $valuearray['minute'],
00254                                    0,
00255                                    $this->_options['timezone'],
00256                                    $this->_options['applydst']);
00257 
00258             return $value;
00259         } else {
00260 
00261             return null;
00262         }
00263     }
00264 
00265     // }}}
00266 }
 All Data Structures Namespaces Files Functions Variables Enumerations