From 511e6c64a104acf257c4045df17ba6761f1a9ebd Mon Sep 17 00:00:00 2001 From: Daniel Neis Araujo Date: Tue, 28 Sep 2021 13:13:55 -0300 Subject: [PATCH 1/3] add time fields and display format --- locallib.php | 3 ++- pages/process.php | 15 +++++++++++++-- renderer.php | 16 ++++++++++++++-- 3 files changed, 29 insertions(+), 5 deletions(-) diff --git a/locallib.php b/locallib.php index da33a2a..7b84717 100644 --- a/locallib.php +++ b/locallib.php @@ -146,6 +146,7 @@ function get_date_formats() { $formats = []; // Add supported formats to array. + $formats['d/m/Y h:i A'] = date('d/m/Y h:i A'); $formats['d/m/Y'] = date('d/m/Y'); $formats['j/n/y'] = date('j/n/y'); $formats['m-d-Y'] = date('m-d-Y'); @@ -154,4 +155,4 @@ function get_date_formats() { $formats['j F Y'] = date('j F Y'); return $formats; -} \ No newline at end of file +} diff --git a/pages/process.php b/pages/process.php index 77ee9a1..612a0ee 100644 --- a/pages/process.php +++ b/pages/process.php @@ -64,6 +64,8 @@ $dismissible = optional_param('dismissible', null, PARAM_TEXT); $datefrom = optional_param('date_from', null, PARAM_TEXT); $dateto = optional_param('date_to', null, PARAM_TEXT); +$timefrom = optional_param('time_from', null, PARAM_TEXT); +$timeto = optional_param('time_to', null, PARAM_TEXT); $dismiss = optional_param('dismiss', null, PARAM_TEXT); // User dismissed notification. $purpose = optional_param('purpose', null, PARAM_TEXT); // Purpose of request. @@ -157,6 +159,9 @@ if ($purpose == 'edit') { $enotification = $DB->get_record('block_advnotifications', array('id' => $tableaction)); + $enotification->time_from = date('H:i', $enotification->date_from); + $enotification->time_to = date('H:i', $enotification->date_to); + $enotification->date_from = date('Y-m-d', $enotification->date_from); $enotification->date_to = date('Y-m-d', $enotification->date_to); @@ -250,8 +255,14 @@ $urow->global = $global; $urow->blockid = $blockinstance; $urow->dismissible = $dismissible; - $urow->date_from = $datefrom; - $urow->date_to = $dateto; + + list($hours, $minutes) = explode(':', $timefrom, 2); + $seconds = $minutes * 60 + $hours * 3600; + $urow->date_from = $datefrom + $seconds; + + list($hours, $minutes) = explode(':', $timeto, 2); + $seconds = $minutes * 60 + $hours * 3600; + $urow->date_to = $dateto + $seconds; $urow->times = $times; $DB->update_record('block_advnotifications', $urow); diff --git a/renderer.php b/renderer.php index 1647aec..061cf3c 100644 --- a/renderer.php +++ b/renderer.php @@ -197,14 +197,26 @@ class="form-check-input" id="add_notification_date_from" class="form-control" name="date_from" - placeholder="yyyy-mm-dd"/> + placeholder="yyyy-mm-dd" + style="width: 10em;"/> + + placeholder="yyyy-mm-dd" + style="width: 10em;"/> + ' . get_string('advnotifications_date_info', 'block_advnotifications') . ' From 73e273f84382fdb60f001bc15241e1056a322bf1 Mon Sep 17 00:00:00 2001 From: Daniel Neis Araujo Date: Thu, 7 Oct 2021 12:10:47 -0300 Subject: [PATCH 2/3] fix time when no time is set --- pages/process.php | 40 +++++++++++++++++++++++++++++++--------- 1 file changed, 31 insertions(+), 9 deletions(-) diff --git a/pages/process.php b/pages/process.php index 612a0ee..ffecfff 100644 --- a/pages/process.php +++ b/pages/process.php @@ -256,14 +256,21 @@ $urow->blockid = $blockinstance; $urow->dismissible = $dismissible; - list($hours, $minutes) = explode(':', $timefrom, 2); - $seconds = $minutes * 60 + $hours * 3600; - $urow->date_from = $datefrom + $seconds; + if (empty($timefrom)) { + $urow->date_from = $datefrom; + } else { + list($hours, $minutes) = explode(':', $timefrom, 2); + $seconds = $minutes * 60 + $hours * 3600; + $urow->date_from = $datefrom + $seconds; + } - list($hours, $minutes) = explode(':', $timeto, 2); - $seconds = $minutes * 60 + $hours * 3600; - $urow->date_to = $dateto + $seconds; - $urow->times = $times; + if (empty($timeto)) { + $urow->date_to = $dateto; + } else { + list($hours, $minutes) = explode(':', $timeto, 2); + $seconds = $minutes * 60 + $hours * 3600; + $urow->date_to = $dateto + $seconds; + } $DB->update_record('block_advnotifications', $urow); @@ -326,8 +333,23 @@ $row->global = $global; $row->blockid = $blockinstance; $row->dismissible = $dismissible; - $row->date_from = $datefrom; - $row->date_to = $dateto; + + if (empty($timefrom)) { + $row->date_from = $datefrom; + } else { + list($hours, $minutes) = explode(':', $timefrom, 2); + $seconds = $minutes * 60 + $hours * 3600; + $row->date_from = $datefrom + $seconds; + } + + if (empty($timeto)) { + $row->date_to = $dateto; + } else { + list($hours, $minutes) = explode(':', $timeto, 2); + $seconds = $minutes * 60 + $hours * 3600; + $row->date_to = $dateto + $seconds; + } + $row->times = $times; $row->deleted = 0; $row->deleted_at = 0; From 380ed15c7b0e2c61007c5eb1f220550d1197c1e6 Mon Sep 17 00:00:00 2001 From: Daniel Neis Araujo Date: Thu, 14 Oct 2021 16:08:17 -0300 Subject: [PATCH 3/3] use moodle time strings (localized) to format dates and times --- classes/base_table.php | 6 +++--- locallib.php | 28 ++++++++++++++++++---------- 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/classes/base_table.php b/classes/base_table.php index 22d6e4b..eaf9973 100644 --- a/classes/base_table.php +++ b/classes/base_table.php @@ -196,7 +196,7 @@ public function col_date_from($values) { return '-'; } - return date(get_config('block_advnotifications', 'dateformat'), $values->date_from); + return userdate($values->date_from, get_string(get_config('block_advnotifications', 'dateformat'), 'langconfig')); } /** @@ -212,7 +212,7 @@ public function col_date_to($values) { return '-'; } - return date(get_config('block_advnotifications', 'dateformat'), $values->date_to); + return userdate($values->date_to, get_string(get_config('block_advnotifications', 'dateformat'), 'langconfig')); } /** @@ -223,4 +223,4 @@ public function print_nothing_to_display() { echo '

' . get_string('advnotifications_table_empty', 'block_advnotifications') . '

'; } -} \ No newline at end of file +} diff --git a/locallib.php b/locallib.php index 7b84717..f7ed196 100644 --- a/locallib.php +++ b/locallib.php @@ -143,16 +143,24 @@ function prep_notifications($instanceid) { * @return array Array of formats as key and today's date in that format as value. */ function get_date_formats() { - $formats = []; + // Hard-code date so users can see the difference between short dates with and without the leading zero. + // Eg. 06/07/18 vs 6/07/18. + $date = 1530849658; // Add supported formats to array. - $formats['d/m/Y h:i A'] = date('d/m/Y h:i A'); - $formats['d/m/Y'] = date('d/m/Y'); - $formats['j/n/y'] = date('j/n/y'); - $formats['m-d-Y'] = date('m-d-Y'); - $formats['n-j-y'] = date('n-j-y'); - $formats['j M y'] = date('j M y'); - $formats['j F Y'] = date('j F Y'); - - return $formats; + $dateformats = []; + + $strdateformats = [ + 'strftimedatetime', + 'strftimedatetimeshort', + 'strftimedaydatetime', + 'strftimerecent', + 'strftimerecentfull', + ]; + + foreach ($strdateformats as $strdateformat) { + $dateformats[$strdateformat] = userdate($date, get_string($strdateformat, 'langconfig')); + } + + return $dateformats; }