|
Moodle
2.2.1
http://www.collinsharper.com
|
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 require_once($CFG->libdir . '/form/text.php'); 00030 00037 class MoodleQuickForm_duration extends MoodleQuickForm_group { 00042 protected $_options = array('optional' => false, 'defaultunit' => 60); 00043 00044 private $_units = null; 00045 00057 function MoodleQuickForm_duration($elementName = null, $elementLabel = null, $options = array(), $attributes = null) { 00058 $this->HTML_QuickForm_element($elementName, $elementLabel, $attributes); 00059 $this->_persistantFreeze = true; 00060 $this->_appendName = true; 00061 $this->_type = 'duration'; 00062 00063 // Set the options, do not bother setting bogus ones 00064 if (!is_array($options)) { 00065 $options = array(); 00066 } 00067 $this->_options['optional'] = !empty($options['optional']); 00068 if (isset($options['defaultunit'])) { 00069 if (!array_key_exists($options['defaultunit'], $this->get_units())) { 00070 throw new coding_exception($options['defaultunit'] . 00071 ' is not a recognised unit in MoodleQuickForm_duration.'); 00072 } 00073 $this->_options['defaultunit'] = $options['defaultunit']; 00074 } 00075 } 00076 00080 public function get_units() { 00081 if (is_null($this->_units)) { 00082 $this->_units = array( 00083 86400 => get_string('days'), 00084 3600 => get_string('hours'), 00085 60 => get_string('minutes'), 00086 1 => get_string('seconds'), 00087 ); 00088 } 00089 return $this->_units; 00090 } 00091 00097 public function seconds_to_unit($seconds) { 00098 if ($seconds == 0) { 00099 return array(0, $this->_options['defaultunit']); 00100 } 00101 foreach ($this->get_units() as $unit => $notused) { 00102 if (fmod($seconds, $unit) == 0) { 00103 return array($seconds / $unit, $unit); 00104 } 00105 } 00106 return array($seconds, 1); 00107 } 00108 00109 // Override of standard quickforms method. 00110 function _createElements() { 00111 $attributes = $this->getAttributes(); 00112 if (is_null($attributes)) { 00113 $attributes = array(); 00114 } 00115 if (!isset($attributes['size'])) { 00116 $attributes['size'] = 3; 00117 } 00118 $this->_elements = array(); 00119 $this->_elements[] = MoodleQuickForm::createElement('text', 'number', get_string('time', 'form'), $attributes, true); 00120 unset($attributes['size']); 00121 $this->_elements[] = MoodleQuickForm::createElement('select', 'timeunit', get_string('timeunit', 'form'), $this->get_units(), $attributes, true); 00122 // If optional we add a checkbox which the user can use to turn if on 00123 if($this->_options['optional']) { 00124 $this->_elements[] = MoodleQuickForm::createElement('checkbox', 'enabled', null, get_string('enable'), $this->getAttributes(), true); 00125 } 00126 foreach ($this->_elements as $element){ 00127 if (method_exists($element, 'setHiddenLabel')){ 00128 $element->setHiddenLabel(true); 00129 } 00130 } 00131 } 00132 00133 // Override of standard quickforms method. 00134 function onQuickFormEvent($event, $arg, $caller) { 00135 switch ($event) { 00136 case 'updateValue': 00137 // constant values override both default and submitted ones 00138 // default values are overriden by submitted 00139 $value = $this->_findValue($caller->_constantValues); 00140 if (null === $value) { 00141 // if no boxes were checked, then there is no value in the array 00142 // yet we don't want to display default value in this case 00143 if ($caller->isSubmitted()) { 00144 $value = $this->_findValue($caller->_submitValues); 00145 } else { 00146 $value = $this->_findValue($caller->_defaultValues); 00147 } 00148 } 00149 if (!is_array($value)) { 00150 list($number, $unit) = $this->seconds_to_unit($value); 00151 $value = array('number' => $number, 'timeunit' => $unit); 00152 // If optional, default to off, unless a date was provided 00153 if ($this->_options['optional']) { 00154 $value['enabled'] = $number != 0; 00155 } 00156 } else { 00157 $value['enabled'] = isset($value['enabled']); 00158 } 00159 if (null !== $value){ 00160 $this->setValue($value); 00161 } 00162 break; 00163 00164 case 'createElement': 00165 if ($arg[2]['optional']) { 00166 $caller->disabledIf($arg[0], $arg[0] . '[enabled]'); 00167 } 00168 $caller->setType($arg[0] . '[number]', PARAM_NUMBER); 00169 return parent::onQuickFormEvent($event, $arg, $caller); 00170 break; 00171 00172 default: 00173 return parent::onQuickFormEvent($event, $arg, $caller); 00174 } 00175 } 00176 00177 // Override of standard quickforms method. 00178 function toHtml() { 00179 include_once('HTML/QuickForm/Renderer/Default.php'); 00180 $renderer = new HTML_QuickForm_Renderer_Default(); 00181 $renderer->setElementTemplate('{element}'); 00182 parent::accept($renderer); 00183 return $renderer->toHtml(); 00184 } 00185 00186 // Override of standard quickforms method. 00187 function accept($renderer, $required = false, $error = null) { 00188 $renderer->renderElement($this, $required, $error); 00189 } 00190 00199 function exportValue($submitValues, $notused = false) { 00200 // Get the values from all the child elements. 00201 $valuearray = array(); 00202 foreach ($this->_elements as $element) { 00203 $thisexport = $element->exportValue($submitValues[$this->getName()], true); 00204 if (!is_null($thisexport)) { 00205 $valuearray += $thisexport; 00206 } 00207 } 00208 00209 // Convert the value to an integer number of seconds. 00210 if (empty($valuearray)) { 00211 return null; 00212 } 00213 if ($this->_options['optional'] && empty($valuearray['enabled'])) { 00214 return array($this->getName() => 0); 00215 } 00216 return array($this->getName() => $valuearray['number'] * $valuearray['timeunit']); 00217 } 00218 }