From b26e8c92b97cfc250b0f97dcd38c975e89be1d64 Mon Sep 17 00:00:00 2001 From: Dmitrii Metelkin Date: Thu, 26 Jul 2018 15:31:19 +1000 Subject: [PATCH] Add recurring option to dateranges (#185) --- element/daterange/classes/element.php | 63 ++++++++++++++++++- .../lang/en/customcertelement_daterange.php | 3 +- 2 files changed, 62 insertions(+), 4 deletions(-) diff --git a/element/daterange/classes/element.php b/element/daterange/classes/element.php index 8968d43..691f6a1 100644 --- a/element/daterange/classes/element.php +++ b/element/daterange/classes/element.php @@ -38,6 +38,11 @@ require_once($CFG->dirroot . '/lib/grade/constants.php'); * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class element extends \mod_customcert\element { + /** + * Current year placeholder string. + */ + const CURRENT_YEAR_PLACEHOLDER = '{{current_year}}'; + /** * Default max number of dateranges per element. */ @@ -132,6 +137,12 @@ class element extends \mod_customcert\element { get_string('datestring', 'customcertelement_daterange') ); + $datarange[] = $mform->createElement( + 'checkbox', + $this->build_element_name('recurring', $i), + get_string('recurring', 'customcertelement_daterange') + ); + $datarange[] = $mform->createElement( 'checkbox', $this->build_element_name('enabled', $i), @@ -195,7 +206,8 @@ class element extends \mod_customcert\element { $mform->setDefault($groupelements[0]->getName(), $range->startdate); $mform->setDefault($groupelements[1]->getName(), $range->enddate); $mform->setDefault($groupelements[2]->getName(), $range->datestring); - $mform->setDefault($groupelements[3]->getName(), $range->enabled); + $mform->setDefault($groupelements[3]->getName(), $range->recurring); + $mform->setDefault($groupelements[4]->getName(), $range->enabled); } } @@ -266,6 +278,7 @@ class element extends \mod_customcert\element { $startdate = $this->build_element_name('startdate', $i); $enddate = $this->build_element_name('enddate', $i); $datestring = $this->build_element_name('datestring', $i); + $recurring = $this->build_element_name('recurring', $i); $enabled = $this->build_element_name('enabled', $i); if (!empty($data->$datestring)) { @@ -273,6 +286,7 @@ class element extends \mod_customcert\element { 'startdate' => $data->$startdate, 'enddate' => $data->$enddate, 'datestring' => $data->$datestring, + 'recurring' => !empty($data->$recurring), 'enabled' => !empty($data->$enabled), ]; $arrtostore['numranges']++; @@ -388,16 +402,59 @@ class element extends \mod_customcert\element { $outputstring = ''; foreach ($this->get_decoded_data()->dateranges as $key => $range) { + if (!empty($range->recurring)) { + $range->startdate = $this->build_recurring_date($range->startdate); + $range->enddate = $this->build_recurring_date($range->enddate); + } + if ($date >= $range->startdate && $date <= $range->enddate) { $outputstring = $range->datestring; + break; } } - if (!empty($this->get_decoded_data()->fallbackstring)) { + if (empty($outputstring) && !empty($this->get_decoded_data()->fallbackstring)) { $outputstring = $this->get_decoded_data()->fallbackstring; } - return $outputstring; + return $this->format_date_string($outputstring); + } + + /** + * Build requring date based on provided date. + * + * @param int $date Unix timestamp. + * + * @return false|int + */ + protected function build_recurring_date($date) { + return strtotime(date('d.m.', $date) . date('Y', time())); + } + + /** + * Format date string. + * + * @param string $datestring + * + * @return string + */ + protected function format_date_string($datestring) { + foreach ($this->get_placeholders() as $search => $replace) { + $datestring = str_replace($search, $replace, $datestring); + } + + return $datestring; + } + + /** + * Return a list of placeholders to replace in date string as search => $replace pairs. + * + * @return array + */ + protected function get_placeholders() { + return [ + self::CURRENT_YEAR_PLACEHOLDER => date('Y', time()), + ]; } /** diff --git a/element/daterange/lang/en/customcertelement_daterange.php b/element/daterange/lang/en/customcertelement_daterange.php index ea2a3bf..f185d6e 100644 --- a/element/daterange/lang/en/customcertelement_daterange.php +++ b/element/daterange/lang/en/customcertelement_daterange.php @@ -33,7 +33,7 @@ $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 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.'; +$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['issueddate'] = 'Issued date'; $string['maxranges'] = 'Maximum number ranges'; $string['maxranges_desc'] = 'Set a maximum number of date ranges per each element'; @@ -47,3 +47,4 @@ $string['error:enabled'] = 'You must have at least one datarange enabled'; $string['error:datestring'] = 'You must provide string representation for the enabled datarange'; $string['error:enddate'] = 'End date must be after Start date'; $string['preview'] = 'Preview {$a}'; +$string['recurring'] = 'Recurring?';