2
// This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
4
* Define datetime fields
18
* Define datetime fields.
6
* @author Mark Nelson <mark@moodle.com.au>
20
* @package profilefield_datetime
21
* @copyright 2010 Mark Nelson <markn@moodle.com>
7
22
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
11
24
class profile_define_datetime extends profile_define_base {
14
* Define the setting for a datetime custom field
27
* Define the setting for a datetime custom field.
16
* @param object $form the user form
29
* @param moodleform $form the user form
18
function define_form_specific($form) {
19
// Create variables to store start and end
20
$currentyear = date('Y');
21
$startyear = $currentyear - 100;
22
$endyear = $currentyear + 20;
24
// Create array for the years
26
for ($i = $startyear; $i <= $endyear; $i++) {
31
public function define_form_specific($form) {
32
// Get the current calendar in use - see MDL-18375.
33
$calendartype = \core_calendar\type_factory::get_calendar_instance();
35
// Create variables to store start and end.
36
list($year, $month, $day) = explode('_', date('Y_m_d'));
37
$currentdate = $calendartype->convert_from_gregorian($year, $month, $day);
38
$currentyear = $currentdate['year'];
40
$arryears = $calendartype->get_years();
31
43
$form->addElement('select', 'param1', get_string('startyear', 'profilefield_datetime'), $arryears);
32
44
$form->setType('param1', PARAM_INT);
33
45
$form->setDefault('param1', $currentyear);
35
47
$form->addElement('select', 'param2', get_string('endyear', 'profilefield_datetime'), $arryears);
36
48
$form->setType('param2', PARAM_INT);
37
$form->setDefault('param2', $currentyear + 20);
49
$form->setDefault('param2', $currentyear);
39
51
$form->addElement('checkbox', 'param3', get_string('wanttime', 'profilefield_datetime'));
40
52
$form->setType('param3', PARAM_INT);
54
$form->addElement('hidden', 'startday', '1');
55
$form->setType('startday', PARAM_INT);
56
$form->addElement('hidden', 'startmonth', '1');
57
$form->setType('startmonth', PARAM_INT);
58
$form->addElement('hidden', 'startyear', '1');
59
$form->setType('startyear', PARAM_INT);
60
$form->addElement('hidden', 'endday', '1');
61
$form->setType('endday', PARAM_INT);
62
$form->addElement('hidden', 'endmonth', '1');
63
$form->setType('endmonth', PARAM_INT);
64
$form->addElement('hidden', 'endyear', '1');
65
$form->setType('endyear', PARAM_INT);
42
66
$form->addElement('hidden', 'defaultdata', '0');
43
67
$form->setType('defaultdata', PARAM_INT);
47
* Validate the data from the profile field form
71
* Validate the data from the profile field form.
49
* @param object data from the add/edit profile field form
51
* @return array associative array of error messages
73
* @param stdClass $data from the add/edit profile field form
75
* @return array associative array of error messages
53
function define_validate_specific($data, $files) {
77
public function define_validate_specific($data, $files) {
56
// Make sure the start year is not greater than the end year
80
// Make sure the start year is not greater than the end year.
57
81
if ($data->param1 > $data->param2) {
58
82
$errors['param1'] = get_string('startyearafterend', 'profilefield_datetime');
89
* Alter form based on submitted or existing data.
91
* @param moodleform $mform
93
public function define_after_data(&$mform) {
96
// If we are adding a new profile field then the dates have already been set
97
// by setDefault to the correct dates in the used calendar system. We only want
98
// to execute the rest of the code when we have the years in the DB saved in
99
// Gregorian that need converting to the date for this user.
100
$id = required_param('id', PARAM_INT);
105
// Get the field data from the DB.
106
$field = $DB->get_record('user_info_field', array('id' => $id), 'param1, param2', MUST_EXIST);
108
// Get the current calendar in use - see MDL-18375.
109
$calendartype = \core_calendar\type_factory::get_calendar_instance();
111
// An array to store form values.
114
// The start and end year will be set as a Gregorian year in the DB. We want
115
// convert these to the equivalent year in the current calendar type being used.
116
$startdate = $calendartype->convert_from_gregorian($field->param1, 1, 1);
117
$values['startday'] = $startdate['day'];
118
$values['startmonth'] = $startdate['month'];
119
$values['startyear'] = $startdate['year'];
120
$values['param1'] = $startdate['year'];
122
$stopdate = $calendartype->convert_from_gregorian($field->param2, 1, 1);
123
$values['endday'] = $stopdate['day'];
124
$values['endmonth'] = $stopdate['month'];
125
$values['endyear'] = $stopdate['year'];
126
$values['param2'] = $stopdate['year'];
129
foreach ($values as $key => $value) {
130
$param = $mform->getElement($key);
131
$param->setValue($value);
65
136
* Preprocess data from the profile field form before
68
* @param object data from the add/edit profile field form
69
* @return object processed data object
139
* @param stdClass $data from the add/edit profile field form
140
* @return stdClass processed data object
71
function define_save_preprocess($data) {
142
public function define_save_preprocess($data) {
143
// Get the current calendar in use - see MDL-18375.
144
$calendartype = \core_calendar\type_factory::get_calendar_instance();
146
// Check if the start year was changed, if it was then convert from the start of that year.
147
if ($data->param1 != $data->startyear) {
148
$startdate = $calendartype->convert_to_gregorian($data->param1, 1, 1);
150
$startdate = $calendartype->convert_to_gregorian($data->param1, $data->startmonth, $data->startday);
153
// Check if the end year was changed, if it was then convert from the start of that year.
154
if ($data->param2 != $data->endyear) {
155
$stopdate = $calendartype->convert_to_gregorian($data->param2, 1, 1);
157
$stopdate = $calendartype->convert_to_gregorian($data->param2, $data->endmonth, $data->endday);
160
$data->param1 = $startdate['year'];
161
$data->param2 = $stopdate['year'];
72
163
if (empty($data->param3)) {
164
$data->param3 = null;
76
// No valid value in the default data column needed
167
// No valid value in the default data column needed.
77
168
$data->defaultdata = '0';