Fixed another bug with the schedule

This commit is contained in:
2018-03-13 00:50:44 +01:00
parent 23173e0e19
commit e7a3a8587b

View File

@@ -53,7 +53,9 @@ QUERY;
$startDay = $start->format('N');
$endDay = $end->format('N');
$schedule = [];
if($DEBUG) print "Creating schedule between {$start->format('Y-m-d H:i')} and {$end->format('Y-m-d H:i')}<br/>\n";
$START = 1; $END = -1;
$WEEK = new \DateInterval('P7D'); $DAY = new \DateInterval('P1D');
$WEEKDAYS = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" /* == 0 and 7 */];
@@ -64,9 +66,11 @@ QUERY;
foreach($scheduleItems as $item) {
if($item->startdate) { $item->startdate = new \DateTimeImmutable($item->startdate); }
if($item->enddate) { $item->enddate = (new \DateTimeImmutable($item->enddate))->add($DAY); }
// Find the start time and end time of the last occurrence on or before the start time
$itemStart = new \DateTime("last " . $WEEKDAYS[$item->startday] . " " . $start->format('Y-m-d') . " " . $item->starttime);
$itemEnd = new \DateTime("last " . $WEEKDAYS[$item->endday] . " " . $start->format('Y-m-d') . " " . $item->endtime);
if($itemEnd <= $itemStart) {
$itemEnd->add($WEEK);
}
@@ -77,9 +81,18 @@ QUERY;
$itemEnd->add($WEEK);
}
// Create a list in which all start and end times are separate entries
$scheduleChanges->insert( [$START, $itemStart, $item], [-$itemStart->getTimestamp(), -$START] );
$scheduleChanges->insert( [$END, $itemEnd, $item], [-$itemEnd->getTimestamp(), -$END] );
if($DEBUG) {
print "Adding {$item->name} (#$item->scheduleid) at {$itemStart->format('Y-m-d H:i')} to {$itemEnd->format('Y-m-d H:i')}.<br/>\n";
if($item->startdate) print " (Item start is {$item->startdate->format('Y-m-d H:i')})<br/>\n";
}
if((!$item->startdate || $item->startdate <= $itemEnd) && (!$item->enddate || $item->enddate >= $itemStart)) {
// Create a list in which all start and end times are separate entries
$scheduleChanges->insert( [$START, $itemStart, $item], [-$itemStart->getTimestamp(), -$START] );
$scheduleChanges->insert( [$END, $itemEnd, $item], [-$itemEnd->getTimestamp(), -$END] );
} else if($DEBUG) {
print "Skipping {$item->name} (#$item->scheduleid) at {$itemStart->format('Y-m-d H:i')} because it is out of range.<br/>\n";
}
// Also keep track of all priorities seen
$active[$item->priority] = null;
@@ -88,7 +101,7 @@ QUERY;
if($DEBUG) {
print "<h2>Schedule changes - initial state</h2><ul>";
foreach(clone $scheduleChanges as $c) {
print "<li>{$c[1]->format('d-m-Y H:i')}: " . ($c[0] == $START ? 'Start' : 'Einde') . " {$c[2]->name} {$c[2]->suffix}</li>";
print "<li>#{$c[2]->scheduleid}: {$c[1]->format('d-m-Y H:i')}: " . ($c[0] == $START ? 'Start' : 'Einde') . " {$c[2]->name} {$c[2]->suffix}</li>";
}
print "</ul><hr/>";
}