MDL-24806 new time/date profile field
authorPetr Skoda <skodak@moodle.org>
Thu, 11 Nov 2010 00:34:44 +0000 (00:34 +0000)
committerPetr Skoda <skodak@moodle.org>
Thu, 11 Nov 2010 00:34:44 +0000 (00:34 +0000)
Credit goes to Mark Nelson - Pukunui Technology

user/profile/field/datetime/define.class.php [new file with mode: 0644]
user/profile/field/datetime/field.class.php [new file with mode: 0644]
user/profile/field/datetime/lang/en/profilefield_datetime.php [new file with mode: 0644]

diff --git a/user/profile/field/datetime/define.class.php b/user/profile/field/datetime/define.class.php
new file mode 100644 (file)
index 0000000..9beb35f
--- /dev/null
@@ -0,0 +1,80 @@
+<?php
+
+/**
+ * Define datetime fields
+ *
+ * @author Mark Nelson <mark@moodle.com.au>
+ * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
+ * @version 20101022
+ */
+
+class profile_define_datetime extends profile_define_base {
+
+    /**
+     * Define the setting for a datetime custom field
+     *
+     * @param object $form the user form
+     */
+    function define_form_specific($form) {
+        // Create variables to store start and end
+        $currentyear = date('Y');
+        $startyear = $currentyear - 100;
+        $endyear = $currentyear + 20;
+
+        // Create array for the years
+        $arryears = array();
+        for ($i = $startyear; $i <= $endyear; $i++) {
+            $arryears[$i] = $i;
+        }
+
+        // Add elements
+        $form->addElement('select', 'param1', get_string('startyear', 'profilefield_datetime'), $arryears);
+        $form->setType('param1', PARAM_INT);
+        $form->setDefault('param1', $currentyear);
+
+        $form->addElement('select', 'param2', get_string('endyear', 'profilefield_datetime'), $arryears);
+        $form->setType('param2', PARAM_INT);
+        $form->setDefault('param2', $currentyear + 20);
+
+        $form->addElement('checkbox', 'param3', get_string('wanttime', 'profilefield_datetime'));
+        $form->setType('param4', PARAM_INT);
+
+        $form->addElement('hidden', 'defaultdata', '0');
+        $form->setType('defaultdata', PARAM_INT);
+    }
+
+    /**
+     * Validate the data from the profile field form
+     *
+     * @param   object   data from the add/edit profile field form
+     * @return  array    associative array of error messages
+     */
+    function define_validate_specific($data) {
+        $errors = array();
+
+        // Make sure the start year is not greater than the end year
+        if ($data->param1 > $data->param2) {
+            $errors['param1'] = get_string('startyearafterend', 'profilefield_datetime');
+        }
+
+        return $errors;
+    }
+
+    /**
+     * Preprocess data from the profile field form before
+     * it is saved.
+     *
+     * @param   object   data from the add/edit profile field form
+     * @return  object   processed data object
+     */
+    function define_save_preprocess($data) {
+        if (empty($data->param3)) {
+            $data->param3 = NULL;
+        }
+
+        // No valid value in the default data column needed
+        $data->defaultdata = '0';
+
+        return $data;
+    }
+}
diff --git a/user/profile/field/datetime/field.class.php b/user/profile/field/datetime/field.class.php
new file mode 100644 (file)
index 0000000..9bab24c
--- /dev/null
@@ -0,0 +1,63 @@
+<?php
+
+/**
+ * Handles displaying and editing the datetime field
+ *
+ * @author Mark Nelson <mark@moodle.com.au>
+ * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
+ * @version 20101022
+ */
+
+class profile_field_datetime extends profile_field_base {
+
+    /**
+     * Handles editing datetime fields
+     *
+     * @param object moodleform instance
+     */
+    function edit_field_add($mform) {
+        // Check if the field is required
+        if ($this->field->required) {
+            $optional = false;
+        } else {
+            $optional = true;
+        }
+
+        $attributes = array(
+            'startyear' => $this->field->param1,
+            'stopyear'  => $this->field->param2,
+            'timezone'  => 99,
+            'applydst'  => true,
+            'optional'  => $optional
+        );
+
+        // Check if they wanted to include time as well
+        if (!empty($this->field->param3)) {
+            $mform->addElement('date_time_selector', $this->inputname, format_string($this->field->name), $attributes);
+        } else {
+            $mform->addElement('date_selector', $this->inputname, format_string($this->field->name), $attributes);
+        }
+
+        $mform->setType($this->inputname, PARAM_INT);
+        $mform->setDefault($this->inputname, time());
+    }
+
+    /**
+     * Display the data for this field
+     */
+    function display_data() {
+        // Check if time was specified
+        if (!empty($this->field->param3)) {
+            $format = get_string('strftimedaydatetime', 'langconfig');
+        } else {
+            $format = get_string('strftimedate', 'langconfig');
+        }
+
+        // Check if a date has been specified
+        if (empty($this->data)) {
+            return get_string('notset', 'profilefield_datetime');
+        } else {
+            return userdate($this->data, $format);
+        }
+    }
+}
\ No newline at end of file
diff --git a/user/profile/field/datetime/lang/en/profilefield_datetime.php b/user/profile/field/datetime/lang/en/profilefield_datetime.php
new file mode 100644 (file)
index 0000000..132d2cb
--- /dev/null
@@ -0,0 +1,19 @@
+<?php
+
+/**
+ * The english language pack used in this profile field type
+ *
+ * @author Mark Nelson - Pukunui Technology
+ * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
+ * @version 20100611
+ */
+
+$string['currentdatedefault'] = 'Check to use current date as default';
+$string['defaultdate'] = 'Default date';
+$string['endyear'] = 'End year';
+$string['notset'] = 'Not set';
+$string['pluginname'] = 'Date/Time';
+$string['specifydatedefault'] = 'or specify a date';
+$string['startyearafterend'] = 'The start year can\'t occur after the end year';
+$string['startyear'] = 'Start year';
+$string['wanttime'] = 'Include time?';