MDL-43887 formslib: Allow not set date field
authorCameron Ball <cameron@moodle.com>
Thu, 26 May 2016 17:21:34 +0000 (01:21 +0800)
committerCameron Ball <cameron@moodle.com>
Thu, 26 May 2016 17:52:57 +0000 (01:52 +0800)
Previously if the date field was not enabled it would
be treated as having value 0. A bug would allow this 0
to pass through and save the date as 1 January 1970

user/profile/field/datetime/field.class.php

index 1dadf04..be4af37 100644 (file)
@@ -76,15 +76,18 @@ class profile_field_datetime extends profile_field_base {
      * @since Moodle 2.5
      */
     public function edit_save_data_preprocess($datetime, $datarecord) {
-        // If timestamp then explode it to check if year is within field limit.
-        $isstring = strpos($datetime, '-');
-        if (empty($isstring)) {
+        if (!$datetime) {
+            return 0;
+        }
+
+        if (is_numeric($datetime)) {
             $datetime = userdate($datetime, '%Y-%m-%d-%H-%M-%S');
         }
 
         $datetime = explode('-', $datetime);
         // Bound year with start and end year.
         $datetime[0] = min(max($datetime[0], $this->field->param1), $this->field->param2);
+
         if (!empty($this->field->param3) && count($datetime) == 6) {
             return make_timestamp($datetime[0], $datetime[1], $datetime[2], $datetime[3], $datetime[4], $datetime[5]);
         } else {
@@ -110,4 +113,13 @@ class profile_field_datetime extends profile_field_base {
             return userdate($this->data, $format);
         }
     }
+
+    /**
+     * Check if the field data is considered empty
+     *
+     * @return boolean
+     */
+    public function is_empty() {
+        return empty($this->data);
+    }
 }