From 82b5cf2a7182babcfa07b3f46e5f21816d771963 Mon Sep 17 00:00:00 2001 From: Koala Yeung Date: Tue, 21 Nov 2023 14:33:34 +0800 Subject: [PATCH] refactor: rewritten Format::dateTimeReadable usages 2 * Use Format::dateIntlReadable to replace all advance Format::dateTimeReadable usages. * Add unit test for fidelity checks. --- modules/Attendance/attendance_future_byPerson.php | 2 +- modules/Attendance/attendance_take_byPerson.php | 4 ++-- modules/Students/firstAidRecord.php | 4 ++-- modules/Students/firstAidRecord_edit.php | 4 ++-- tests/unit/Services/FormatTest.php | 15 +++++++++++++++ 5 files changed, 22 insertions(+), 7 deletions(-) diff --git a/modules/Attendance/attendance_future_byPerson.php b/modules/Attendance/attendance_future_byPerson.php index 8ed53627a9..53f22761e8 100644 --- a/modules/Attendance/attendance_future_byPerson.php +++ b/modules/Attendance/attendance_future_byPerson.php @@ -252,7 +252,7 @@ } }); $table->addColumn('staff', __('Recorded By'))->format(Format::using('name', ['', 'preferredName', 'surname', 'Staff', false, true])); - $table->addColumn('timestamp', __('On'))->format(Format::using('dateTimeReadable', 'timestampTaken', '%R, %b %d')); + $table->addColumn('timestamp', __('On'))->format(Format::using('dateIntlReadable', 'timestampTaken', 'HH:mm, MMM dd')); $table->addActionColumn() ->addParam('gibbonPersonID', $gibbonPersonIDList[0] ?? '') diff --git a/modules/Attendance/attendance_take_byPerson.php b/modules/Attendance/attendance_take_byPerson.php index 214c5168a6..13e2bac663 100644 --- a/modules/Attendance/attendance_take_byPerson.php +++ b/modules/Attendance/attendance_take_byPerson.php @@ -129,8 +129,8 @@ if (empty($log['timestampTaken'])) return Format::small(__('N/A')); return $currentDate != substr($log['timestampTaken'], 0, 10) - ? Format::dateTimeReadable($log['timestampTaken'], '%H:%M, %b %d') - : Format::dateTimeReadable($log['timestampTaken'], '%H:%M'); + ? Format::dateIntlReadable($log['timestampTaken'], 'HH:mm, MMM dd') + : Format::dateIntlReadable($log['timestampTaken'], 'HH:mm'); }); $table->addColumn('direction', __('Attendance')) diff --git a/modules/Students/firstAidRecord.php b/modules/Students/firstAidRecord.php index b887f606c0..db86bd8186 100644 --- a/modules/Students/firstAidRecord.php +++ b/modules/Students/firstAidRecord.php @@ -102,10 +102,10 @@ $output = ''; if ($person['description'] != '') $output .= ''.__('Description').'
'.nl2br($person['description']).'

'; if ($person['actionTaken'] != '') $output .= ''.__('Action Taken').'
'.nl2br($person['actionTaken']).'

'; - if ($person['followUp'] != '') $output .= ''.__("Follow Up by {name} at {date}", ['name' => Format::name('', $person['preferredNameFirstAider'], $person['surnameFirstAider']), 'date' => Format::dateTimeReadable($person['timestamp'], '%H:%M, %b %d %Y')]).'
'.nl2br($person['followUp']).'

'; + if ($person['followUp'] != '') $output .= ''.__("Follow Up by {name} at {date}", ['name' => Format::name('', $person['preferredNameFirstAider'], $person['surnameFirstAider']), 'date' => Format::dateIntlReadable($person['timestamp'], 'HH:mm, MMM dd yyyy')]).'
'.nl2br($person['followUp']).'

'; $resultLog = $firstAidGateway->queryFollowUpByFirstAidID($person['gibbonFirstAidID']); foreach ($resultLog AS $rowLog) { - $output .= ''.__("Follow Up by {name} at {date}", ['name' => Format::name('', $rowLog['preferredName'], $rowLog['surname']), 'date' => Format::dateTimeReadable($rowLog['timestamp'], '%H:%M, %b %d %Y')]).'
'.nl2br($rowLog['followUp']).'

'; + $output .= ''.__("Follow Up by {name} at {date}", ['name' => Format::name('', $rowLog['preferredName'], $rowLog['surname']), 'date' => Format::dateIntlReadable($rowLog['timestamp'], 'HH:mm, MMM dd yyyy')]).'
'.nl2br($rowLog['followUp']).'

'; } return $output; diff --git a/modules/Students/firstAidRecord_edit.php b/modules/Students/firstAidRecord_edit.php index 6214ded44d..1f0419b8af 100644 --- a/modules/Students/firstAidRecord_edit.php +++ b/modules/Students/firstAidRecord_edit.php @@ -53,7 +53,7 @@ if ($gibbonFirstAidID == '') { $page->addError(__('You have not specified one or more required parameters.')); } else { - + $data = array('gibbonSchoolYearID' => $session->get('gibbonSchoolYearID'), 'gibbonFirstAidID' => $gibbonFirstAidID); $sql = "SELECT gibbonFirstAid.*, patient.gibbonPersonID AS gibbonPersonIDPatient, patient.surname AS surnamePatient, patient.preferredName AS preferredNamePatient, firstAider.title, firstAider.surname AS surnameFirstAider, firstAider.preferredName AS preferredNameFirstAider FROM gibbonFirstAid @@ -124,7 +124,7 @@ if (!empty($values['followUp'])) { $row = $form->addRow(); $column = $row->addColumn(); - $column->addLabel('followUp0', __("Follow Up by {name} at {date}", ['name' => Format::name('', $values['preferredNameFirstAider'], $values['surnameFirstAider']), 'date' => Format::dateTimeReadable($values['timestamp'], '%H:%M, %b %d %Y')])); + $column->addLabel('followUp0', __("Follow Up by {name} at {date}", ['name' => Format::name('', $values['preferredNameFirstAider'], $values['surnameFirstAider']), 'date' => Format::dateIntlReadable($values['timestamp'], 'HH:mm, MMM dd yyyy')])); $column->addContent($values['followUp'])->setClass('fullWidth'); } diff --git a/tests/unit/Services/FormatTest.php b/tests/unit/Services/FormatTest.php index 20ab3fd0e4..91085eb1a6 100644 --- a/tests/unit/Services/FormatTest.php +++ b/tests/unit/Services/FormatTest.php @@ -184,6 +184,21 @@ public function testFormatsReadableDates() // Note: %e has a leading space for single digit days, but cannot do the same with intl date formats. $this->assertEquals('Sat 3 Feb, 2018', Format::dateReadable($dateString, '%a %e %b, %Y')); $this->assertEquals('Sat 3 Feb, 2018', Format::dateIntlReadable($dateString, 'EEE d MMM, yyyy')); + + // modules/Attendance/attendance_future_byPerson.php + // modules/Attendance/attendance_take_byPerson.php + $this->assertEquals('13:24, Feb 03', Format::dateTimeReadable($dateString, '%R, %b %d')); + $this->assertEquals('13:24, Feb 03', Format::dateTimeReadable($dateString, '%H:%M, %b %d')); + $this->assertEquals('13:24, Feb 03', Format::dateIntlReadable($dateString, 'HH:mm, MMM dd')); + + // modules/Students/firstAidRecord_edit.php + // modules/Students/firstAidRecord.php + $this->assertEquals('13:24, Feb 03 2018', Format::dateTimeReadable($dateString, '%H:%M, %b %d %Y')); + $this->assertEquals('13:24, Feb 03 2018', Format::dateIntlReadable($dateString, 'HH:mm, MMM dd yyyy')); + + // modules/Attendance/attendance_take_byPerson.php + $this->assertEquals('13:24', Format::dateTimeReadable($dateString, '%H:%M')); + $this->assertEquals('13:24', Format::dateIntlReadable($dateString, 'HH:mm')); } public function testFormatsDateRanges()