[ Index ] |
PHP Cross Reference of Unnamed Project |
[Summary view] [Print] [Text view]
1 <?php 2 // This file is part of Moodle - http://moodle.org/ 3 // 4 // Moodle is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // Moodle is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU General Public License for more details. 13 // 14 // You should have received a copy of the GNU General Public License 15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 16 17 18 /** 19 * Duration form element 20 * 21 * Contains class to create length of time for element. 22 * 23 * @package core_form 24 * @copyright 2009 Tim Hunt 25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 26 */ 27 28 global $CFG; 29 require_once($CFG->libdir . '/form/group.php'); 30 require_once($CFG->libdir . '/formslib.php'); 31 require_once($CFG->libdir . '/form/text.php'); 32 33 /** 34 * Duration element 35 * 36 * HTML class for a length of time. For example, 30 minutes of 4 days. The 37 * values returned to PHP is the duration in seconds. 38 * 39 * @package core_form 40 * @category form 41 * @copyright 2009 Tim Hunt 42 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 43 */ 44 class MoodleQuickForm_duration extends MoodleQuickForm_group { 45 /** 46 * Control the fieldnames for form elements 47 * optional => if true, show a checkbox beside the element to turn it on (or off) 48 * @var array 49 */ 50 protected $_options = array('optional' => false, 'defaultunit' => 60); 51 52 /** @var array associative array of time units (days, hours, minutes, seconds) */ 53 private $_units = null; 54 55 /** 56 * constructor 57 * 58 * @param string $elementName Element's name 59 * @param mixed $elementLabel Label(s) for an element 60 * @param array $options Options to control the element's display. Recognised values are 61 * 'optional' => true/false - whether to display an 'enabled' checkbox next to the element. 62 * 'defaultunit' => 1|60|3600|86400|604800 - the default unit to display when the time is blank. 63 * If not specified, minutes is used. 64 * @param mixed $attributes Either a typical HTML attribute string or an associative array 65 */ 66 public function __construct($elementName = null, $elementLabel = null, $options = array(), $attributes = null) { 67 // TODO MDL-52313 Replace with the call to parent::__construct(). 68 HTML_QuickForm_element::__construct($elementName, $elementLabel, $attributes); 69 $this->_persistantFreeze = true; 70 $this->_appendName = true; 71 $this->_type = 'duration'; 72 73 // Set the options, do not bother setting bogus ones 74 if (!is_array($options)) { 75 $options = array(); 76 } 77 $this->_options['optional'] = !empty($options['optional']); 78 if (isset($options['defaultunit'])) { 79 if (!array_key_exists($options['defaultunit'], $this->get_units())) { 80 throw new coding_exception($options['defaultunit'] . 81 ' is not a recognised unit in MoodleQuickForm_duration.'); 82 } 83 $this->_options['defaultunit'] = $options['defaultunit']; 84 } 85 } 86 87 /** 88 * Old syntax of class constructor. Deprecated in PHP7. 89 * 90 * @deprecated since Moodle 3.1 91 */ 92 public function MoodleQuickForm_duration($elementName = null, $elementLabel = null, $options = array(), $attributes = null) { 93 debugging('Use of class name as constructor is deprecated', DEBUG_DEVELOPER); 94 self::__construct($elementName, $elementLabel, $options, $attributes); 95 } 96 97 /** 98 * Returns time associative array of unit length. 99 * 100 * @return array unit length in seconds => string unit name. 101 */ 102 public function get_units() { 103 if (is_null($this->_units)) { 104 $this->_units = array( 105 604800 => get_string('weeks'), 106 86400 => get_string('days'), 107 3600 => get_string('hours'), 108 60 => get_string('minutes'), 109 1 => get_string('seconds'), 110 ); 111 } 112 return $this->_units; 113 } 114 115 /** 116 * Converts seconds to the best possible time unit. for example 117 * 1800 -> array(30, 60) = 30 minutes. 118 * 119 * @param int $seconds an amout of time in seconds. 120 * @return array associative array ($number => $unit) 121 */ 122 public function seconds_to_unit($seconds) { 123 if ($seconds == 0) { 124 return array(0, $this->_options['defaultunit']); 125 } 126 foreach ($this->get_units() as $unit => $notused) { 127 if (fmod($seconds, $unit) == 0) { 128 return array($seconds / $unit, $unit); 129 } 130 } 131 return array($seconds, 1); 132 } 133 134 /** 135 * Override of standard quickforms method to create this element. 136 */ 137 function _createElements() { 138 $attributes = $this->getAttributes(); 139 if (is_null($attributes)) { 140 $attributes = array(); 141 } 142 if (!isset($attributes['size'])) { 143 $attributes['size'] = 3; 144 } 145 $this->_elements = array(); 146 // E_STRICT creating elements without forms is nasty because it internally uses $this 147 $this->_elements[] = @MoodleQuickForm::createElement('text', 'number', get_string('time', 'form'), $attributes, true); 148 unset($attributes['size']); 149 $this->_elements[] = @MoodleQuickForm::createElement('select', 'timeunit', get_string('timeunit', 'form'), $this->get_units(), $attributes, true); 150 // If optional we add a checkbox which the user can use to turn if on 151 if($this->_options['optional']) { 152 $this->_elements[] = @MoodleQuickForm::createElement('checkbox', 'enabled', null, get_string('enable'), $this->getAttributes(), true); 153 } 154 foreach ($this->_elements as $element){ 155 if (method_exists($element, 'setHiddenLabel')){ 156 $element->setHiddenLabel(true); 157 } 158 } 159 } 160 161 /** 162 * Called by HTML_QuickForm whenever form event is made on this element 163 * 164 * @param string $event Name of event 165 * @param mixed $arg event arguments 166 * @param object $caller calling object 167 * @return bool 168 */ 169 function onQuickFormEvent($event, $arg, &$caller) { 170 switch ($event) { 171 case 'updateValue': 172 // constant values override both default and submitted ones 173 // default values are overriden by submitted 174 $value = $this->_findValue($caller->_constantValues); 175 if (null === $value) { 176 // if no boxes were checked, then there is no value in the array 177 // yet we don't want to display default value in this case 178 if ($caller->isSubmitted()) { 179 $value = $this->_findValue($caller->_submitValues); 180 } else { 181 $value = $this->_findValue($caller->_defaultValues); 182 } 183 } 184 if (!is_array($value)) { 185 list($number, $unit) = $this->seconds_to_unit($value); 186 $value = array('number' => $number, 'timeunit' => $unit); 187 // If optional, default to off, unless a date was provided 188 if ($this->_options['optional']) { 189 $value['enabled'] = $number != 0; 190 } 191 } else { 192 $value['enabled'] = isset($value['enabled']); 193 } 194 if (null !== $value){ 195 $this->setValue($value); 196 } 197 break; 198 199 case 'createElement': 200 if ($arg[2]['optional']) { 201 $caller->disabledIf($arg[0], $arg[0] . '[enabled]'); 202 } 203 $caller->setType($arg[0] . '[number]', PARAM_FLOAT); 204 return parent::onQuickFormEvent($event, $arg, $caller); 205 break; 206 207 default: 208 return parent::onQuickFormEvent($event, $arg, $caller); 209 } 210 } 211 212 /** 213 * Returns HTML for advchecbox form element. 214 * 215 * @return string 216 */ 217 function toHtml() { 218 include_once('HTML/QuickForm/Renderer/Default.php'); 219 $renderer = new HTML_QuickForm_Renderer_Default(); 220 $renderer->setElementTemplate('{element}'); 221 parent::accept($renderer); 222 return $renderer->toHtml(); 223 } 224 225 /** 226 * Accepts a renderer 227 * 228 * @param HTML_QuickForm_Renderer $renderer An HTML_QuickForm_Renderer object 229 * @param bool $required Whether a group is required 230 * @param string $error An error message associated with a group 231 */ 232 function accept(&$renderer, $required = false, $error = null) { 233 $renderer->renderElement($this, $required, $error); 234 } 235 236 /** 237 * Output a timestamp. Give it the name of the group. 238 * Override of standard quickforms method. 239 * 240 * @param array $submitValues 241 * @param bool $notused Not used. 242 * @return array field name => value. The value is the time interval in seconds. 243 */ 244 function exportValue(&$submitValues, $notused = false) { 245 // Get the values from all the child elements. 246 $valuearray = array(); 247 foreach ($this->_elements as $element) { 248 $thisexport = $element->exportValue($submitValues[$this->getName()], true); 249 if (!is_null($thisexport)) { 250 $valuearray += $thisexport; 251 } 252 } 253 254 // Convert the value to an integer number of seconds. 255 if (empty($valuearray)) { 256 return null; 257 } 258 if ($this->_options['optional'] && empty($valuearray['enabled'])) { 259 return array($this->getName() => 0); 260 } 261 return array($this->getName() => $valuearray['number'] * $valuearray['timeunit']); 262 } 263 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu Aug 11 10:00:09 2016 | Cross-referenced by PHPXref 0.7.1 |