Add recurring option to dateranges (#185)

This commit is contained in:
Dmitrii Metelkin 2018-07-26 15:31:19 +10:00 committed by Mark Nelson
parent 59fe72c7ca
commit 057cf92f03
2 changed files with 62 additions and 4 deletions

View file

@ -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 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/ */
class element extends \mod_customcert\element { class element extends \mod_customcert\element {
/**
* Current year placeholder string.
*/
const CURRENT_YEAR_PLACEHOLDER = '{{current_year}}';
/** /**
* Default max number of dateranges per element. * Default max number of dateranges per element.
*/ */
@ -132,6 +137,12 @@ class element extends \mod_customcert\element {
get_string('datestring', 'customcertelement_daterange') get_string('datestring', 'customcertelement_daterange')
); );
$datarange[] = $mform->createElement(
'checkbox',
$this->build_element_name('recurring', $i),
get_string('recurring', 'customcertelement_daterange')
);
$datarange[] = $mform->createElement( $datarange[] = $mform->createElement(
'checkbox', 'checkbox',
$this->build_element_name('enabled', $i), $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[0]->getName(), $range->startdate);
$mform->setDefault($groupelements[1]->getName(), $range->enddate); $mform->setDefault($groupelements[1]->getName(), $range->enddate);
$mform->setDefault($groupelements[2]->getName(), $range->datestring); $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); $startdate = $this->build_element_name('startdate', $i);
$enddate = $this->build_element_name('enddate', $i); $enddate = $this->build_element_name('enddate', $i);
$datestring = $this->build_element_name('datestring', $i); $datestring = $this->build_element_name('datestring', $i);
$recurring = $this->build_element_name('recurring', $i);
$enabled = $this->build_element_name('enabled', $i); $enabled = $this->build_element_name('enabled', $i);
if (!empty($data->$datestring)) { if (!empty($data->$datestring)) {
@ -273,6 +286,7 @@ class element extends \mod_customcert\element {
'startdate' => $data->$startdate, 'startdate' => $data->$startdate,
'enddate' => $data->$enddate, 'enddate' => $data->$enddate,
'datestring' => $data->$datestring, 'datestring' => $data->$datestring,
'recurring' => !empty($data->$recurring),
'enabled' => !empty($data->$enabled), 'enabled' => !empty($data->$enabled),
]; ];
$arrtostore['numranges']++; $arrtostore['numranges']++;
@ -388,16 +402,59 @@ class element extends \mod_customcert\element {
$outputstring = ''; $outputstring = '';
foreach ($this->get_decoded_data()->dateranges as $key => $range) { 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) { if ($date >= $range->startdate && $date <= $range->enddate) {
$outputstring = $range->datestring; $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; $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()),
];
} }
/** /**

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.'; $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['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';
@ -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:datestring'] = 'You must provide string representation for the enabled datarange';
$string['error:enddate'] = 'End date must be after Start date'; $string['error:enddate'] = 'End date must be after Start date';
$string['preview'] = 'Preview {$a}'; $string['preview'] = 'Preview {$a}';
$string['recurring'] = 'Recurring?';