Recursively check recurring date ranges (#185)
This commit is contained in:
parent
3cd85428df
commit
82aa988db4
1 changed files with 29 additions and 12 deletions
|
@ -133,7 +133,6 @@ class element extends \mod_customcert\element {
|
||||||
$mform->setType('numranges', PARAM_INT);
|
$mform->setType('numranges', PARAM_INT);
|
||||||
|
|
||||||
for ($i = 0; $i < $maxranges; $i++) {
|
for ($i = 0; $i < $maxranges; $i++) {
|
||||||
$datarange = array();
|
|
||||||
|
|
||||||
$mform->addElement('static',
|
$mform->addElement('static',
|
||||||
$this->build_element_name('group', $i),
|
$this->build_element_name('group', $i),
|
||||||
|
@ -433,15 +432,18 @@ 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)) {
|
if ($this->is_date_in_range($date, $range)) {
|
||||||
$range = $this->build_recurring_daterange($range);
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($date >= $range->startdate && $date <= $range->enddate) {
|
|
||||||
$rangematched = $range;
|
$rangematched = $range;
|
||||||
$outputstring = $range->datestring;
|
$outputstring = $range->datestring;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!empty($range->recurring)) {
|
||||||
|
if ($rangematched = $this->get_matched_recurring_range($date, $range)) {
|
||||||
|
$outputstring = $rangematched->datestring;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (empty($outputstring) && !empty($this->get_decoded_data()->fallbackstring)) {
|
if (empty($outputstring) && !empty($this->get_decoded_data()->fallbackstring)) {
|
||||||
|
@ -452,24 +454,39 @@ class element extends \mod_customcert\element {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Build a recurring daterange by updating start and end dates if required.
|
* Check if the provided date is in the date range.
|
||||||
*
|
*
|
||||||
|
* @param int $date Unix timestamp date to check.
|
||||||
* @param \stdClass $range Range object.
|
* @param \stdClass $range Range object.
|
||||||
*
|
*
|
||||||
* @return \stdClass
|
* @return bool
|
||||||
*/
|
*/
|
||||||
protected function build_recurring_daterange(\stdClass $range) {
|
protected function is_date_in_range($date, \stdClass $range) {
|
||||||
if ($range->enddate < time()) {
|
return ($date >= $range->startdate && $date <= $range->enddate);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get recurring date range matched provided date.
|
||||||
|
*
|
||||||
|
* @param int $date Unix timestamp date to check.
|
||||||
|
* @param \stdClass $range Range object.
|
||||||
|
*
|
||||||
|
* @return \stdClass || null
|
||||||
|
*/
|
||||||
|
protected function get_matched_recurring_range($date, \stdClass $range) {
|
||||||
|
while ($range->enddate < time()) {
|
||||||
$startyear = date('Y', $range->startdate) + 1; // Max recurring period is 12 months.
|
$startyear = date('Y', $range->startdate) + 1; // Max recurring period is 12 months.
|
||||||
$endyear = date('Y', $range->enddate) + 1; // Max recurring period is 12 months.
|
$endyear = date('Y', $range->enddate) + 1; // Max recurring period is 12 months.
|
||||||
|
|
||||||
$range->startdate = strtotime(date('d.m.', $range->startdate) . $startyear);
|
$range->startdate = strtotime(date('d.m.', $range->startdate) . $startyear);
|
||||||
$range->enddate = strtotime(date('d.m.', $range->enddate) . $endyear);
|
$range->enddate = strtotime(date('d.m.', $range->enddate) . $endyear);
|
||||||
|
|
||||||
$range = $this->build_recurring_daterange($range);
|
if ($this->is_date_in_range($date, $range)) {
|
||||||
|
return $range;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return $range;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in a new issue