Add range related placeholders (#185)

This commit is contained in:
Dmitrii Metelkin 2018-08-02 11:01:13 +10:00 committed by Mark Nelson
parent 5a9d9792df
commit 4e30f1e851
2 changed files with 37 additions and 4 deletions

View file

@ -43,6 +43,16 @@ class element extends \mod_customcert\element {
*/ */
const CURRENT_YEAR_PLACEHOLDER = '{{current_year}}'; const CURRENT_YEAR_PLACEHOLDER = '{{current_year}}';
/**
* First year in a date range placeholder string.
*/
const FIRST_YEAR_PLACEHOLDER = '{{first_year}}';
/**
* Last year in a date range placeholder string.
*/
const LAST_YEAR_PLACEHOLDER = '{{last_year}}';
/** /**
* Default max number of dateranges per element. * Default max number of dateranges per element.
*/ */
@ -406,6 +416,7 @@ class element extends \mod_customcert\element {
* @return string * @return string
*/ */
protected function get_daterange_string($date) { protected function get_daterange_string($date) {
$rangematched = null;
$outputstring = ''; $outputstring = '';
foreach ($this->get_decoded_data()->dateranges as $key => $range) { foreach ($this->get_decoded_data()->dateranges as $key => $range) {
@ -415,6 +426,7 @@ class element extends \mod_customcert\element {
} }
if ($date >= $range->startdate && $date <= $range->enddate) { if ($date >= $range->startdate && $date <= $range->enddate) {
$rangematched = $range;
$outputstring = $range->datestring; $outputstring = $range->datestring;
break; break;
} }
@ -424,7 +436,7 @@ class element extends \mod_customcert\element {
$outputstring = $this->get_decoded_data()->fallbackstring; $outputstring = $this->get_decoded_data()->fallbackstring;
} }
return $this->format_date_string($outputstring); return $this->format_date_string($outputstring, $rangematched);
} }
/** /**
@ -441,15 +453,22 @@ class element extends \mod_customcert\element {
/** /**
* Format date string. * Format date string.
* *
* @param string $datestring * @param string $datestring Date string to format.
* @param \stdClass $range Optional range element element to process range related placeholders.
* *
* @return string * @return string
*/ */
protected function format_date_string($datestring) { protected function format_date_string($datestring, $range = null) {
foreach ($this->get_placeholders() as $search => $replace) { foreach ($this->get_placeholders() as $search => $replace) {
$datestring = str_replace($search, $replace, $datestring); $datestring = str_replace($search, $replace, $datestring);
} }
if ($range) {
foreach ($this->get_range_placeholders($range) as $search => $replace) {
$datestring = str_replace($search, $replace, $datestring);
}
}
return $datestring; return $datestring;
} }
@ -464,6 +483,20 @@ class element extends \mod_customcert\element {
]; ];
} }
/**
* Return a list of range related placeholders to replace in date string as search => $replace pairs.
*
* @param \stdClass $range
*
* @return array
*/
protected function get_range_placeholders(\stdClass $range) {
return [
self::FIRST_YEAR_PLACEHOLDER => date('Y', $range->startdate),
self::LAST_YEAR_PLACEHOLDER => date('Y', $range->enddate),
];
}
/** /**
* Render the element in html. * Render the element in html.
* *

View file

@ -33,7 +33,7 @@ $string['dateitem_help'] = 'This will be the date that is printed on the certifi
$string['dateranges'] = 'Dateranges'; $string['dateranges'] = 'Dateranges';
$string['fallbackstring'] = 'Fallback string'; $string['fallbackstring'] = 'Fallback string';
$string['fallbackstring_help'] = 'This string will be displayed if no daterange applied to a date. If Fallback string is not set, then there will be no output at all.'; $string['fallbackstring_help'] = 'This string will be displayed if no daterange applied 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 considerred and the current year will be used. Also there is {{current_year}} placeholder that could be used in the string representation. The placeholder will be replaced by the current year value in the certificate.'; $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 considerred and the current year will be used. Also there are {{first_year}} and {{last_year}} and {{current_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.';
$string['issueddate'] = 'Issued date'; $string['issueddate'] = 'Issued date';
$string['maxranges'] = 'Maximum number ranges'; $string['maxranges'] = 'Maximum number ranges';
$string['maxranges_desc'] = 'Set a maximum number of date ranges per each element'; $string['maxranges_desc'] = 'Set a maximum number of date ranges per each element';