MDL-43887 formslib: Allow not set date field

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
This commit is contained in:
Cameron Ball
2016-05-27 01:53:24 +08:00
parent b1b097e2b6
commit 1ad3ddcd24
+15 -3
View 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);
}
}