Modify recurring dateranges (#185)

This commit is contained in:
Dmitrii Metelkin 2018-08-03 11:18:03 +10:00 committed by Mark Nelson
parent 2852683dff
commit 36ec9188fa
2 changed files with 31 additions and 8 deletions

View file

@ -38,6 +38,12 @@ 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 {
/**
* Max recurring period in seconds.
*/
const MAX_RECURRING_PERIOD = 31556926; // 12 months.
/**
* Current year placeholder string.
*/
@ -262,15 +268,22 @@ class element extends \mod_customcert\element {
}
}
// Check that date is correctly set.
for ($i = 0; $i < $data['numranges']; $i++) {
$enabled = $this->build_element_name('enabled', $i);
$recurring = $this->build_element_name('recurring', $i);
$startdate = $this->build_element_name('startdate', $i);
$enddate = $this->build_element_name('enddate', $i);
$rangeperiod = $data[$enddate] - $data[$startdate];
// Check that end date is correctly set.
if (!empty($data[$enabled]) && $data[$startdate] >= $data[$enddate] ) {
$errors[$this->build_element_name('enddate', $i)] = get_string('error:enddate', 'customcertelement_daterange');
}
// Check that recurring dateranges are not longer than 12 months.
if (!empty($data[$recurring]) && $rangeperiod > self::MAX_RECURRING_PERIOD ) {
$errors[$this->build_element_name('enddate', $i)] = get_string('error:recurring', 'customcertelement_daterange');
}
}
return $errors;
@ -421,8 +434,7 @@ class element extends \mod_customcert\element {
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);
$range = $this->build_recurring_daterange($range);
}
if ($date >= $range->startdate && $date <= $range->enddate) {
@ -440,14 +452,24 @@ class element extends \mod_customcert\element {
}
/**
* Build requring date based on provided date.
* Build a recurring daterange by updating start and end dates if required.
*
* @param int $date Unix timestamp.
* @param \stdClass $range Range object.
*
* @return false|int
* @return \stdClass
*/
protected function build_recurring_date($date) {
return strtotime(date('d.m.', $date) . date('Y', time()));
protected function build_recurring_daterange(\stdClass $range) {
if ($range->enddate < time()) {
$startyear = date('Y', $range->startdate) + 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->enddate = strtotime(date('d.m.', $range->enddate) . $endyear);
$range = $this->build_recurring_daterange($range);
}
return $range;
}
/**

View file

@ -46,5 +46,6 @@ $string['daterange'] = 'Daterange {$a}';
$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['error:recurring'] = 'Recurring range should not be longer than 12 months';
$string['preview'] = 'Preview {$a}';
$string['recurring'] = 'Recurring?';