From 5b44544e74e0438dc9fcc6b2107ea9ac42f4c365 Mon Sep 17 00:00:00 2001 From: Dmitrii Metelkin Date: Fri, 24 Aug 2018 15:31:47 +1000 Subject: [PATCH] Add new placeholders and update help description (#185) --- element/daterange/classes/element.php | 90 +++++++++++++------ .../lang/en/customcertelement_daterange.php | 3 +- .../daterange/tests/phpunit/element_test.php | 40 ++++++++- 3 files changed, 105 insertions(+), 28 deletions(-) diff --git a/element/daterange/classes/element.php b/element/daterange/classes/element.php index 9a93566..16188b7 100644 --- a/element/daterange/classes/element.php +++ b/element/daterange/classes/element.php @@ -52,12 +52,22 @@ class element extends \mod_customcert\element { /** * First year in a date range placeholder string. */ - const FIRST_YEAR_PLACEHOLDER = '{{range_first_year}}'; + const RANGE_FIRST_YEAR_PLACEHOLDER = '{{range_first_year}}'; /** * Last year in a date range placeholder string. */ - const LAST_YEAR_PLACEHOLDER = '{{range_last_year}}'; + const RANGE_LAST_YEAR_PLACEHOLDER = '{{range_last_year}}'; + + /** + * First year in a date range placeholder string. + */ + const RECUR_RANGE_FIRST_YEAR_PLACEHOLDER = '{{recurring_range_first_year}}'; + + /** + * Last year in a date range placeholder string. + */ + const RECUR_RANGE_LAST_YEAR_PLACEHOLDER = '{{recurring_range_last_year}}'; /** * A year in the user's date. @@ -119,6 +129,7 @@ class element extends \mod_customcert\element { $mform->addElement('header', 'dateranges', get_string('dateranges', 'customcertelement_daterange')); $mform->addElement('static', 'help', '', get_string('help', 'customcertelement_daterange')); + $mform->addElement('static', 'placeholders', '', get_string('placeholders', 'customcertelement_daterange')); $mform->addElement('text', 'fallbackstring', get_string('fallbackstring', 'customcertelement_daterange')); $mform->addHelpButton('fallbackstring', 'fallbackstring', 'customcertelement_daterange'); @@ -435,17 +446,21 @@ class element extends \mod_customcert\element { protected function get_daterange_string($date) { $matchedrange = null; $outputstring = ''; + $formatdata = []; + $formatdata['date'] = $date; foreach ($this->get_decoded_data()->dateranges as $key => $range) { if ($this->is_recurring_range($range)) { if ($matchedrange = $this->get_matched_recurring_range($date, $range)) { $outputstring = $matchedrange->datestring; + $formatdata['range'] = $range; + $formatdata['recurringrange'] = $matchedrange; break; } } else { if ($this->is_date_in_range($date, $range)) { - $matchedrange = $range; $outputstring = $range->datestring; + $formatdata['range'] = $range; break; } } @@ -455,7 +470,8 @@ class element extends \mod_customcert\element { $outputstring = $this->get_decoded_data()->fallbackstring; } - return $this->format_date_string($outputstring, $date, $matchedrange); + + return $this->format_date_string($outputstring, $formatdata); } /** @@ -569,30 +585,32 @@ class element extends \mod_customcert\element { return null; } - if ($this->has_turn_of_the_year($range)) { + $matchedrage = clone $range; - if ($this->in_start_year($date, $range)) { + if ($this->has_turn_of_the_year($matchedrage)) { + + if ($this->in_start_year($date, $matchedrage)) { $startyear = date('Y', $date); $endyear = $startyear + 1; - $range->startdate = strtotime(date('d.m.', $range->startdate) . $startyear); - $range->enddate = strtotime(date('d.m.', $range->enddate) . $endyear); + $matchedrage->startdate = strtotime(date('d.m.', $matchedrage->startdate) . $startyear); + $matchedrage->enddate = strtotime(date('d.m.', $matchedrage->enddate) . $endyear); - return $range; + return $matchedrage; } - if ($this->in_end_year($date, $range)) { + if ($this->in_end_year($date, $matchedrage)) { $endyear = date('Y', $date); $startyear = $endyear - 1; - $range->startdate = strtotime(date('d.m.', $range->startdate) . $startyear); - $range->enddate = strtotime(date('d.m.', $range->enddate) . $endyear); + $matchedrage->startdate = strtotime(date('d.m.', $matchedrage->startdate) . $startyear); + $matchedrage->enddate = strtotime(date('d.m.', $matchedrage->enddate) . $endyear); - return $range; + return $matchedrage; } } else { - $range->startdate = strtotime(date('d.m.', $range->startdate) . date('Y', $date)); - $range->enddate = strtotime(date('d.m.', $range->enddate) . date('Y', $date)); + $matchedrage->startdate = strtotime(date('d.m.', $matchedrage->startdate) . date('Y', $date)); + $matchedrage->enddate = strtotime(date('d.m.', $matchedrage->enddate) . date('Y', $date)); - return $range; + return $matchedrage; } return null; @@ -612,23 +630,29 @@ class element extends \mod_customcert\element { /** * Format date string based on different types of placeholders. * - * @param string $datestring Date string to format. - * @param int $date Unix timestamp date to check. - * @param \stdClass $range Optional range element element to process range related placeholders. + * @param array $formatdata A list of format data. * * @return string */ - protected function format_date_string($datestring, $date, $range = null) { + protected function format_date_string($datestring, array $formatdata) { foreach ($this->get_placeholders() as $search => $replace) { $datestring = str_replace($search, $replace, $datestring); } - foreach ($this->get_date_placeholders($date) as $search => $replace) { - $datestring = str_replace($search, $replace, $datestring); + if (!empty($formatdata['date'])) { + foreach ($this->get_date_placeholders($formatdata['date']) as $search => $replace) { + $datestring = str_replace($search, $replace, $datestring); + } } - if ($range) { - foreach ($this->get_range_placeholders($range) as $search => $replace) { + if (!empty($formatdata['range'])) { + foreach ($this->get_range_placeholders($formatdata['range']) as $search => $replace) { + $datestring = str_replace($search, $replace, $datestring); + } + } + + if (!empty($formatdata['recurringrange'])) { + foreach ($this->get_recurring_range_placeholders($formatdata['recurringrange']) as $search => $replace) { $datestring = str_replace($search, $replace, $datestring); } } @@ -669,8 +693,22 @@ class element extends \mod_customcert\element { */ protected function get_range_placeholders(\stdClass $range) { return [ - self::FIRST_YEAR_PLACEHOLDER => date('Y', $range->startdate), - self::LAST_YEAR_PLACEHOLDER => date('Y', $range->enddate), + self::RANGE_FIRST_YEAR_PLACEHOLDER => date('Y', $range->startdate), + self::RANGE_LAST_YEAR_PLACEHOLDER => date('Y', $range->enddate), + ]; + } + + /** + * Return a list of recurring range s placeholders to replace in date string as search => $replace pairs. + * + * @param \stdClass $range + * + * @return array + */ + protected function get_recurring_range_placeholders(\stdClass $range) { + return [ + self::RECUR_RANGE_FIRST_YEAR_PLACEHOLDER => date('Y', $range->startdate), + self::RECUR_RANGE_LAST_YEAR_PLACEHOLDER => date('Y', $range->enddate), ]; } diff --git a/element/daterange/lang/en/customcertelement_daterange.php b/element/daterange/lang/en/customcertelement_daterange.php index 6f628af..f1c2957 100644 --- a/element/daterange/lang/en/customcertelement_daterange.php +++ b/element/daterange/lang/en/customcertelement_daterange.php @@ -33,7 +33,8 @@ $string['dateitem_help'] = 'This will be the date that is printed on the certifi $string['dateranges'] = 'Dateranges'; $string['fallbackstring'] = 'Fallback string'; $string['fallbackstring_help'] = 'This string will be displayed if no daterange applies to a date. If Fallback string is not set, then there will be no output at all.'; -$string['help'] = 'Configure a string representation for each daterange. Set start and end dates as well as a string you would like to transform each range to. Make sure your ranges do not overlap, otherwise the first detected daterange will be applied. If no daterange applied to a date, then Fallback string will be displayed. If Fallback string is not set, then there will be no output at all. If you mark a date range as Recurring, then the configured year will not be considered. As the year of a recurring date range is not considered, you are not allowed to configure a recurring date range with more than 12 months as it would become ambiguous otherwise. Also there are {{range_first_year}}, {{range_last_year}}, {{current_year}} and {{date_year}} placeholders that could be used in the string representation. The placeholders will be replaced by first year or last year in the matched range or the current year or a year from the users\'s date.'; +$string['help'] = 'Configure a string representation for each daterange. Make sure your ranges do not overlap, otherwise the first matched daterange will be applied. If no daterange matched a date, then Fallback string will be displayed. If Fallback string is not set, then there will be no output.
If you mark a date range as Recurring, then the configured year will not be considered. As the year of a recurring date range is not considered, you are not allowed to configure a recurring date range with more than 12 months as it would become ambiguous otherwise.'; +$string['placeholders'] = 'Also following placeholders could be used in the string representation or fallback string.
{{range_first_year}} - first year of the matched range,
{{range_last_year}} - last year of the matched range,
{{recurring_range_first_year}} - first year of the matched recurring period,
{{recurring_range_last_year}} - last year of the matched recurring period,
{{current_year}} - the current year,
{{date_year}} - a year of the users\'s date.'; $string['issueddate'] = 'Issued date'; $string['maxranges'] = 'Maximum number ranges'; $string['maxranges_desc'] = 'Set a maximum number of date ranges per each element'; diff --git a/element/daterange/tests/phpunit/element_test.php b/element/daterange/tests/phpunit/element_test.php index ef5d72d..1176de9 100644 --- a/element/daterange/tests/phpunit/element_test.php +++ b/element/daterange/tests/phpunit/element_test.php @@ -125,7 +125,7 @@ class customcertelement_daterange_element_test extends advanced_testcase { (object)[ 'startdate' => strtotime('01.10.2017'), 'enddate' => strtotime('31.03.2018'), - 'datestring' => 'WS {{range_first_year}}/{{range_last_year}}', + 'datestring' => 'WS {{recurring_range_first_year}}/{{recurring_range_last_year}}', 'recurring' => true, 'enabled' => true, ], @@ -209,4 +209,42 @@ class customcertelement_daterange_element_test extends advanced_testcase { $this->assertEquals($fallbackstring, $element->get_daterange_string($date)); } + /** + * Test that display recurring_range_first_year and recurring_range_last_year placeholders. + */ + public function test_recurring_range_first_year_and_recurring_range_last_year_placeholders() { + $datestring = '{{range_first_year}}-{{range_last_year}}-{{recurring_range_first_year}}-{{recurring_range_last_year}}'; + $dateranges = [ + (object) [ + 'startdate' => strtotime('01.04.2017'), + 'enddate' => strtotime('30.09.2017'), + 'datestring' => $datestring, + 'recurring' => true, + 'enabled' => true, + ], + (object)[ + 'startdate' => strtotime('01.10.2017'), + 'enddate' => strtotime('31.03.2018'), + 'datestring' => $datestring, + 'recurring' => true, + 'enabled' => true, + ], + + ]; + + $element = $this->get_datarange_element($dateranges); + + $date = strtotime('1.05.2020'); + $this->assertEquals('2017-2017-2020-2020', $element->get_daterange_string($date)); + + $date = strtotime('1.05.2024'); + $this->assertEquals('2017-2017-2024-2024', $element->get_daterange_string($date)); + + $date = strtotime('1.02.2020'); + $this->assertEquals('2017-2018-2019-2020', $element->get_daterange_string($date)); + + $date = strtotime('1.02.2024'); + $this->assertEquals('2017-2018-2023-2024', $element->get_daterange_string($date)); + } + }