~ubuntu-branches/ubuntu/utopic/moodle/utopic

« back to all changes in this revision

Viewing changes to user/profile/field/datetime/define.class.php

  • Committer: Package Import Robot
  • Author(s): Thijs Kinkhorst
  • Date: 2014-05-12 16:10:38 UTC
  • mfrom: (36.1.3 sid)
  • Revision ID: package-import@ubuntu.com-20140512161038-puyqf65k4e0s8ytz
Tags: 2.6.3-1
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
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/>.
2
16
 
3
17
/**
4
 
 * Define datetime fields
 
18
 * Define datetime fields.
5
19
 *
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
8
 
 * @version 20101022
9
23
 */
10
 
 
11
24
class profile_define_datetime extends profile_define_base {
12
25
 
13
26
    /**
14
 
     * Define the setting for a datetime custom field
 
27
     * Define the setting for a datetime custom field.
15
28
     *
16
 
     * @param object $form the user form
 
29
     * @param moodleform $form the user form
17
30
     */
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;
23
 
 
24
 
        // Create array for the years
25
 
        $arryears = array();
26
 
        for ($i = $startyear; $i <= $endyear; $i++) {
27
 
            $arryears[$i] = $i;
28
 
        }
29
 
 
30
 
        // Add elements
 
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();
 
34
 
 
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'];
 
39
 
 
40
        $arryears = $calendartype->get_years();
 
41
 
 
42
        // Add elements.
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);
34
46
 
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);
38
50
 
39
51
        $form->addElement('checkbox', 'param3', get_string('wanttime', 'profilefield_datetime'));
40
52
        $form->setType('param3', PARAM_INT);
41
53
 
 
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);
44
68
    }
45
69
 
46
70
    /**
47
 
     * Validate the data from the profile field form
 
71
     * Validate the data from the profile field form.
48
72
     *
49
 
     * @param   object   data from the add/edit profile field form
50
 
     * @param   array    files
51
 
     * @return  array    associative array of error messages
 
73
     * @param stdClass $data from the add/edit profile field form
 
74
     * @param array $files
 
75
     * @return array associative array of error messages
52
76
     */
53
 
    function define_validate_specific($data, $files) {
 
77
    public function define_validate_specific($data, $files) {
54
78
        $errors = array();
55
79
 
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');
59
83
        }
62
86
    }
63
87
 
64
88
    /**
 
89
     * Alter form based on submitted or existing data.
 
90
     *
 
91
     * @param moodleform $mform
 
92
     */
 
93
    public function define_after_data(&$mform) {
 
94
        global $DB;
 
95
 
 
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);
 
101
        if ($id === 0) {
 
102
            return;
 
103
        }
 
104
 
 
105
        // Get the field data from the DB.
 
106
        $field = $DB->get_record('user_info_field', array('id' => $id), 'param1, param2', MUST_EXIST);
 
107
 
 
108
        // Get the current calendar in use - see MDL-18375.
 
109
        $calendartype = \core_calendar\type_factory::get_calendar_instance();
 
110
 
 
111
        // An array to store form values.
 
112
        $values = array();
 
113
 
 
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'];
 
121
 
 
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'];
 
127
 
 
128
        // Set the values.
 
129
        foreach ($values as $key => $value) {
 
130
            $param = $mform->getElement($key);
 
131
            $param->setValue($value);
 
132
        }
 
133
    }
 
134
 
 
135
    /**
65
136
     * Preprocess data from the profile field form before
66
137
     * it is saved.
67
138
     *
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
70
141
     */
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();
 
145
 
 
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);
 
149
        } else {
 
150
            $startdate = $calendartype->convert_to_gregorian($data->param1, $data->startmonth, $data->startday);
 
151
        }
 
152
 
 
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);
 
156
        } else {
 
157
            $stopdate = $calendartype->convert_to_gregorian($data->param2, $data->endmonth, $data->endday);
 
158
        }
 
159
 
 
160
        $data->param1 = $startdate['year'];
 
161
        $data->param2 = $stopdate['year'];
 
162
 
72
163
        if (empty($data->param3)) {
73
 
            $data->param3 = NULL;
 
164
            $data->param3 = null;
74
165
        }
75
166
 
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';
78
169
 
79
170
        return $data;