[ Index ] |
PHP Cross Reference of moodle-2.8 |
[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 * Group of date and time input element 20 * 21 * Contains class for a group of elements used to input a date and time. 22 * 23 * @package core_form 24 * @copyright 2006 Jamie Pratt <[email protected]> 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 32 /** 33 * Element used to input a date and time. 34 * 35 * Class for a group of elements used to input a date and time. 36 * 37 * @package core_form 38 * @category form 39 * @copyright 2006 Jamie Pratt <[email protected]> 40 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 41 */ 42 class MoodleQuickForm_date_time_selector extends MoodleQuickForm_group { 43 44 /** 45 * Options for the element. 46 * 47 * startyear => int start of range of years that can be selected 48 * stopyear => int last year that can be selected 49 * defaulttime => default time value if the field is currently not set 50 * timezone => int|float|string (optional) timezone modifier used for edge case only. 51 * If not specified, then date is caclulated based on current user timezone. 52 * Note: dst will be calculated for string timezones only 53 * {@link http://docs.moodle.org/dev/Time_API#Timezone} 54 * step => step to increment minutes by 55 * optional => if true, show a checkbox beside the date to turn it on (or off) 56 * @var array 57 */ 58 protected $_options = array(); 59 60 /** 61 * @var array These complement separators, they are appended to the resultant HTML. 62 */ 63 protected $_wrap = array('', ''); 64 65 /** 66 * @var null|bool Keeps track of whether the date selector was initialised using createElement 67 * or addElement. If true, createElement was used signifying the element has been 68 * added to a group - see MDL-39187. 69 */ 70 protected $_usedcreateelement = true; 71 72 /** 73 * Class constructor 74 * 75 * @param string $elementName Element's name 76 * @param mixed $elementLabel Label(s) for an element 77 * @param array $options Options to control the element's display 78 * @param mixed $attributes Either a typical HTML attribute string or an associative array 79 */ 80 function MoodleQuickForm_date_time_selector($elementName = null, $elementLabel = null, $options = array(), $attributes = null) { 81 // Get the calendar type used - see MDL-18375. 82 $calendartype = \core_calendar\type_factory::get_calendar_instance(); 83 $this->_options = array('startyear' => $calendartype->get_min_year(), 'stopyear' => $calendartype->get_max_year(), 84 'defaulttime' => 0, 'timezone' => 99, 'step' => 5, 'optional' => false); 85 86 $this->HTML_QuickForm_element($elementName, $elementLabel, $attributes); 87 $this->_persistantFreeze = true; 88 $this->_appendName = true; 89 $this->_type = 'date_time_selector'; 90 // set the options, do not bother setting bogus ones 91 if (is_array($options)) { 92 foreach ($options as $name => $value) { 93 if (isset($this->_options[$name])) { 94 if (is_array($value) && is_array($this->_options[$name])) { 95 $this->_options[$name] = @array_merge($this->_options[$name], $value); 96 } else { 97 $this->_options[$name] = $value; 98 } 99 } 100 } 101 } 102 103 // The YUI2 calendar only supports the gregorian calendar type. 104 if ($calendartype->get_name() === 'gregorian') { 105 form_init_date_js(); 106 } 107 } 108 109 /** 110 * This will create date group element constisting of day, month and year. 111 * 112 * @access private 113 */ 114 function _createElements() { 115 global $OUTPUT; 116 117 // Get the calendar type used - see MDL-18375. 118 $calendartype = \core_calendar\type_factory::get_calendar_instance(); 119 120 for ($i = 0; $i <= 23; $i++) { 121 $hours[$i] = sprintf("%02d", $i); 122 } 123 for ($i = 0; $i < 60; $i += $this->_options['step']) { 124 $minutes[$i] = sprintf("%02d", $i); 125 } 126 127 $this->_elements = array(); 128 $dateformat = $calendartype->get_date_order($this->_options['startyear'], $this->_options['stopyear']); 129 foreach ($dateformat as $key => $date) { 130 // E_STRICT creating elements without forms is nasty because it internally uses $this 131 $this->_elements[] = @MoodleQuickForm::createElement('select', $key, get_string($key, 'form'), $date, $this->getAttributes(), true); 132 } 133 if (right_to_left()) { // Switch order of elements for Right-to-Left 134 $this->_elements[] = @MoodleQuickForm::createElement('select', 'minute', get_string('minute', 'form'), $minutes, $this->getAttributes(), true); 135 $this->_elements[] = @MoodleQuickForm::createElement('select', 'hour', get_string('hour', 'form'), $hours, $this->getAttributes(), true); 136 } else { 137 $this->_elements[] = @MoodleQuickForm::createElement('select', 'hour', get_string('hour', 'form'), $hours, $this->getAttributes(), true); 138 $this->_elements[] = @MoodleQuickForm::createElement('select', 'minute', get_string('minute', 'form'), $minutes, $this->getAttributes(), true); 139 } 140 // The YUI2 calendar only supports the gregorian calendar type so only display the calendar image if this is being used. 141 if ($calendartype->get_name() === 'gregorian') { 142 $image = $OUTPUT->pix_icon('i/calendar', get_string('calendar', 'calendar'), 'moodle'); 143 $this->_elements[] = @MoodleQuickForm::createElement('link', 'calendar', 144 null, '#', $image, 145 array('class' => 'visibleifjs')); 146 } 147 // If optional we add a checkbox which the user can use to turn if on 148 if ($this->_options['optional']) { 149 $this->_elements[] = @MoodleQuickForm::createElement('checkbox', 'enabled', null, get_string('enable'), $this->getAttributes(), true); 150 } 151 foreach ($this->_elements as $element){ 152 if (method_exists($element, 'setHiddenLabel')){ 153 $element->setHiddenLabel(true); 154 } 155 } 156 157 } 158 159 /** 160 * Called by HTML_QuickForm whenever form event is made on this element 161 * 162 * @param string $event Name of event 163 * @param mixed $arg event arguments 164 * @param object $caller calling object 165 * @return bool 166 */ 167 function onQuickFormEvent($event, $arg, &$caller) { 168 switch ($event) { 169 case 'updateValue': 170 // Constant values override both default and submitted ones 171 // default values are overriden by submitted. 172 $value = $this->_findValue($caller->_constantValues); 173 if (null === $value) { 174 // If no boxes were checked, then there is no value in the array 175 // yet we don't want to display default value in this case. 176 if ($caller->isSubmitted()) { 177 $value = $this->_findValue($caller->_submitValues); 178 } else { 179 $value = $this->_findValue($caller->_defaultValues); 180 } 181 } 182 $requestvalue=$value; 183 if ($value == 0) { 184 $value = $this->_options['defaulttime']; 185 if (!$value) { 186 $value = time(); 187 } 188 } 189 if (!is_array($value)) { 190 $calendartype = \core_calendar\type_factory::get_calendar_instance(); 191 $currentdate = $calendartype->timestamp_to_date_array($value, $this->_options['timezone']); 192 // Round minutes to the previous multiple of step. 193 $currentdate['minutes'] -= $currentdate['minutes'] % $this->_options['step']; 194 $value = array( 195 'minute' => $currentdate['minutes'], 196 'hour' => $currentdate['hours'], 197 'day' => $currentdate['mday'], 198 'month' => $currentdate['mon'], 199 'year' => $currentdate['year']); 200 // If optional, default to off, unless a date was provided. 201 if ($this->_options['optional']) { 202 $value['enabled'] = $requestvalue != 0; 203 } 204 } else { 205 $value['enabled'] = isset($value['enabled']); 206 } 207 if (null !== $value) { 208 $this->setValue($value); 209 } 210 break; 211 case 'createElement': 212 if ($arg[2]['optional']) { 213 // When using the function addElement, rather than createElement, we still 214 // enter this case, making this check necessary. 215 if ($this->_usedcreateelement) { 216 $caller->disabledIf($arg[0] . '[day]', $arg[0] . '[enabled]'); 217 $caller->disabledIf($arg[0] . '[month]', $arg[0] . '[enabled]'); 218 $caller->disabledIf($arg[0] . '[year]', $arg[0] . '[enabled]'); 219 $caller->disabledIf($arg[0] . '[hour]', $arg[0] . '[enabled]'); 220 $caller->disabledIf($arg[0] . '[minute]', $arg[0] . '[enabled]'); 221 } else { 222 $caller->disabledIf($arg[0], $arg[0] . '[enabled]'); 223 } 224 } 225 return parent::onQuickFormEvent($event, $arg, $caller); 226 break; 227 case 'addElement': 228 $this->_usedcreateelement = false; 229 return parent::onQuickFormEvent($event, $arg, $caller); 230 break; 231 default: 232 return parent::onQuickFormEvent($event, $arg, $caller); 233 } 234 } 235 236 /** 237 * Returns HTML for advchecbox form element. 238 * 239 * @return string 240 */ 241 function toHtml() { 242 include_once('HTML/QuickForm/Renderer/Default.php'); 243 $renderer = new HTML_QuickForm_Renderer_Default(); 244 $renderer->setElementTemplate('{element}'); 245 parent::accept($renderer); 246 247 $html = $this->_wrap[0]; 248 if ($this->_usedcreateelement) { 249 $html .= html_writer::tag('span', $renderer->toHtml(), array('class' => 'fdate_time_selector')); 250 } else { 251 $html .= $renderer->toHtml(); 252 } 253 $html .= $this->_wrap[1]; 254 255 return $html; 256 } 257 258 /** 259 * Accepts a renderer 260 * 261 * @param HTML_QuickForm_Renderer $renderer An HTML_QuickForm_Renderer object 262 * @param bool $required Whether a group is required 263 * @param string $error An error message associated with a group 264 */ 265 function accept(&$renderer, $required = false, $error = null) { 266 $renderer->renderElement($this, $required, $error); 267 } 268 269 /** 270 * Output a timestamp. Give it the name of the group. 271 * 272 * @param array $submitValues values submitted. 273 * @param bool $assoc specifies if returned array is associative 274 * @return array 275 */ 276 function exportValue(&$submitValues, $assoc = false) { 277 $value = null; 278 $valuearray = array(); 279 foreach ($this->_elements as $element){ 280 $thisexport = $element->exportValue($submitValues[$this->getName()], true); 281 if ($thisexport!=null){ 282 $valuearray += $thisexport; 283 } 284 } 285 if (count($valuearray)){ 286 if($this->_options['optional']) { 287 // If checkbox is on, the value is zero, so go no further 288 if(empty($valuearray['enabled'])) { 289 $value[$this->getName()] = 0; 290 return $value; 291 } 292 } 293 // Get the calendar type used - see MDL-18375. 294 $calendartype = \core_calendar\type_factory::get_calendar_instance(); 295 $gregoriandate = $calendartype->convert_to_gregorian($valuearray['year'], 296 $valuearray['month'], 297 $valuearray['day'], 298 $valuearray['hour'], 299 $valuearray['minute']); 300 $value[$this->getName()] = make_timestamp($gregoriandate['year'], 301 $gregoriandate['month'], 302 $gregoriandate['day'], 303 $gregoriandate['hour'], 304 $gregoriandate['minute'], 305 0, 306 $this->_options['timezone'], 307 true); 308 309 return $value; 310 } else { 311 return null; 312 } 313 } 314 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Fri Nov 28 20:29:05 2014 | Cross-referenced by PHPXref 0.7.1 |