See Release Notes
Long Term Support Release
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 * Customfield date plugin 19 * 20 * @package customfield_date 21 * @copyright 2018 Daniel Neis Araujo <daniel@moodle.com> 22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 23 */ 24 25 namespace customfield_date; 26 27 use core_customfield\api; 28 29 defined('MOODLE_INTERNAL') || die; 30 31 /** 32 * Class data 33 * 34 * @package customfield_date 35 * @copyright 2018 Daniel Neis Araujo <daniel@moodle.com> 36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 37 */ 38 class data_controller extends \core_customfield\data_controller { 39 40 /** 41 * Return the name of the field where the information is stored 42 * @return string 43 */ 44 public function datafield() : string { 45 return 'intvalue'; 46 } 47 48 /** 49 * Add fields for editing data of a date field on a context. 50 * 51 * @param \MoodleQuickForm $mform 52 */ 53 public function instance_form_definition(\MoodleQuickForm $mform) { 54 $field = $this->get_field(); 55 // Get the current calendar in use - see MDL-18375. 56 $calendartype = \core_calendar\type_factory::get_calendar_instance(); 57 58 $config = $field->get('configdata'); 59 60 // Always set the form element to "optional", even when it's required. Otherwise it defaults to the 61 // current date and is easy to miss. 62 $attributes = ['optional' => true]; 63 64 if (!empty($config['mindate'])) { 65 $attributes['startyear'] = $calendartype->timestamp_to_date_array($config['mindate'])['year']; 66 } 67 68 if (!empty($config['maxdate'])) { 69 $attributes['stopyear'] = $calendartype->timestamp_to_date_array($config['maxdate'])['year']; 70 } 71 72 if (empty($config['includetime'])) { 73 $element = 'date_selector'; 74 } else { 75 $element = 'date_time_selector'; 76 } 77 $elementname = $this->get_form_element_name(); 78 $mform->addElement($element, $elementname, $this->get_field()->get_formatted_name(), $attributes); 79 $mform->setType($elementname, PARAM_INT); 80 $mform->setDefault($elementname, time()); 81 if ($field->get_configdata_property('required')) { 82 $mform->addRule($elementname, null, 'required', null, 'client'); 83 } 84 } 85 86 /** 87 * Validates data for this field. 88 * 89 * @param array $data 90 * @param array $files 91 * @return array 92 */ 93 public function instance_form_validation(array $data, array $files) : array { 94 $errors = parent::instance_form_validation($data, $files); 95 96 $elementname = $this->get_form_element_name(); 97 if (!empty($data[$elementname])) { 98 // Compare the date with min/max values, trim the date to the minute or to the day (depending on inludetime setting). 99 $includetime = $this->get_field()->get_configdata_property('includetime'); 100 $machineformat = $includetime ? '%Y-%m-%d %H:%M' : '%Y-%m-%d'; 101 $humanformat = $includetime ? get_string('strftimedatetimeshort') : get_string('strftimedatefullshort'); 102 $value = userdate($data[$elementname], $machineformat, 99, false, false); 103 $mindate = $this->get_field()->get_configdata_property('mindate'); 104 $maxdate = $this->get_field()->get_configdata_property('maxdate'); 105 106 if ($mindate && userdate($mindate, $machineformat, 99, false, false) > $value) { 107 $errors[$elementname] = get_string('errormindate', 'customfield_date', userdate($mindate, $humanformat)); 108 } 109 if ($maxdate && userdate($maxdate, $machineformat, 99, false, false) < $value) { 110 $errors[$elementname] = get_string('errormaxdate', 'customfield_date', userdate($maxdate, $humanformat)); 111 } 112 } 113 114 return $errors; 115 } 116 117 /** 118 * Returns the default value as it would be stored in the database (not in human-readable format). 119 * 120 * @return mixed 121 */ 122 public function get_default_value() { 123 return 0; 124 } 125 126 /** 127 * Returns value in a human-readable format 128 * 129 * @return mixed|null value or null if empty 130 */ 131 public function export_value() { 132 $value = $this->get_value(); 133 134 if ($this->is_empty($value)) { 135 return null; 136 } 137 138 // Check if time needs to be included. 139 if ($this->get_field()->get_configdata_property('includetime')) { 140 $format = get_string('strftimedaydatetime', 'langconfig'); 141 } else { 142 $format = get_string('strftimedate', 'langconfig'); 143 } 144 145 return userdate($value, $format); 146 } 147 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body