Bugjes uit programmaschema
This commit is contained in:
@@ -54,7 +54,7 @@ QUERY;
|
||||
$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";
|
||||
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');
|
||||
@@ -68,38 +68,46 @@ if($DEBUG) print "Creating schedule between {$start->format('Y-m-d H:i')} and {$
|
||||
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);
|
||||
$itemStart = new \DateTimeImmutable("last " . $WEEKDAYS[$item->startday] . " " . $start->format('Y-m-d') . " " . $item->starttime);
|
||||
$itemEnd = new \DateTimeImmutable("last " . $WEEKDAYS[$item->endday] . " " . $start->format('Y-m-d') . " " . $item->endtime);
|
||||
|
||||
if($itemEnd <= $itemStart) {
|
||||
$itemEnd->add($WEEK);
|
||||
$itemEnd = $itemEnd->add($WEEK);
|
||||
}
|
||||
|
||||
// If the first occurrence is completely before the period of interest, look at the next week
|
||||
while($itemEnd < $start) {
|
||||
$itemStart->add($WEEK);
|
||||
$itemEnd->add($WEEK);
|
||||
$itemStart = $itemStart->add($WEEK);
|
||||
$itemEnd = $itemEnd->add($WEEK);
|
||||
}
|
||||
|
||||
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;
|
||||
while($itemEnd <= $end) {
|
||||
if($DEBUG) {
|
||||
print "Considering {$item->name} {$item->suffix} (#$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')}, end is ". ($item->enddate ? $item->enddate->format('Y-m-d H:i') : "never") . ")<br/>\n";
|
||||
}
|
||||
|
||||
if($item->startdate && $itemStart <= $item->startdate) {
|
||||
if($DEBUG) print "-- Skipping {$item->name} {$item->suffix} (#$item->scheduleid) at {$itemStart->format('Y-m-d H:i')} because it is before the schedule instance start.<br/>\n";
|
||||
} else if($item->enddate && $itemStart >= $item->enddate) {
|
||||
if($DEBUG) print "-- Skipping {$item->name} {$item->suffix} (#$item->scheduleid) at {$itemStart->format('Y-m-d H:i')} because it is after the schedule instance end.<br/>\n";
|
||||
} else {
|
||||
// if($DEBUG) print "-- Adding {$item->name} {$item->suffix} (#$item->scheduleid) at {$itemStart->format('Y-m-d H:i')} <br/>\n";
|
||||
// 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] );
|
||||
}
|
||||
|
||||
// Also keep track of all priorities seen
|
||||
$active[$item->priority] = null;
|
||||
|
||||
$itemStart = $itemStart->add($WEEK);
|
||||
$itemEnd = $itemEnd->add($WEEK);
|
||||
}
|
||||
}
|
||||
|
||||
if($DEBUG) {
|
||||
print "<h2>Schedule changes - initial state</h2><ul>";
|
||||
print "<h2>Schedule changes</h2><ul>";
|
||||
foreach(clone $scheduleChanges as $c) {
|
||||
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>";
|
||||
}
|
||||
@@ -109,44 +117,36 @@ if($DEBUG) print "Creating schedule between {$start->format('Y-m-d H:i')} and {$
|
||||
// Go through all programs and keep going until we have one that starts after the end date.
|
||||
$activeProgram = null;
|
||||
$activeProgramStart = null;
|
||||
while(true) {
|
||||
while(!$scheduleChanges->isEmpty()) {
|
||||
$item = $scheduleChanges->extract();
|
||||
|
||||
|
||||
// If we passed the end time, we are (almost) done
|
||||
if($item[1] > $end) {
|
||||
if($DEBUG) { print "Reached end at {$item[1]->format('d-m-Y H:i')} ({$item[2]->name}).<br/>"; }
|
||||
break;
|
||||
}
|
||||
|
||||
// Only use this item if it is valid
|
||||
// MS 2018-01-12: Don't know why I originally had this, the list should only contain valid items.
|
||||
// This crashed the site once because it skipped a program that should have been in the list but generated 'program not active at priorty' message later on.
|
||||
// if(($item[2]->startdate == null || $item[2]->startdate <= $activeProgramStart) && ($item[2]->enddate == null || $item[2]->enddate > $activeProgramStart))
|
||||
{
|
||||
if($DEBUG) print "<b>{$item[1]->format('d-m-Y H:i')}: {$item[2]->name} {$item[2]->suffix} " . ($item[0] == $START ? "begint" : "eindigt") . "</b><br/>";
|
||||
$priority = $item[2]->priority;
|
||||
if($item[0] == $START) {
|
||||
if($active[$priority] != null) {
|
||||
print( "<b>Error:</b>" . $item[2]->name . ' starts at ' . $item[1]->format('d-m-Y H:i') . ' but ' . $active[$priority]->name . ' is still active then.' );
|
||||
print "<pre>"; var_dump($active); print "</pre>";
|
||||
}
|
||||
|
||||
$priority = $item[2]->priority;
|
||||
if($item[0] == $START) {
|
||||
if($active[$priority] != null) {
|
||||
print( "<b>Error:</b>" . $item[2]->name . ' starts at ' . $item[1]->format('d-m-Y H:i') . ' but ' . $active[$priority]->name . ' is still active then.' );
|
||||
print "<pre>"; var_dump($active); print "</pre>";
|
||||
}
|
||||
|
||||
// When a program starts, it becomes the active program in its layer
|
||||
$active[$priority] = $item[2];
|
||||
|
||||
// In addition, if it has higher priority than the currently active program, it replaces that.
|
||||
if($activeProgram == null || $priority < $activeProgram->priority) {
|
||||
if(($activeProgram != null) && ($item[1] > $start)) {
|
||||
if($DEBUG) print "{$activeProgramStart->format('d-m-Y H:i')} -- {$item[1]->format('d-m-Y H:i')}: {$activeProgram->name} (reason: {$item[2]->name} starts).<br/>";
|
||||
$this->addToSchedule($schedule, $activeProgramStart, $item[1], $activeProgram);
|
||||
}
|
||||
|
||||
$activeProgramStart = ($item[1] < $start) ? $start : clone $item[1];
|
||||
$activeProgram = $item[2];
|
||||
}
|
||||
|
||||
} else if($item[0] == $END) {
|
||||
// When a program starts, it becomes the active program in its layer
|
||||
$active[$priority] = $item[2];
|
||||
|
||||
// In addition, if it has higher priority than the currently active program, it replaces that.
|
||||
if($activeProgram == null || $priority < $activeProgram->priority) {
|
||||
if(($activeProgram != null) && ($item[1] > $start)) {
|
||||
if($DEBUG) print "{$activeProgramStart->format('d-m-Y H:i')} -- {$item[1]->format('d-m-Y H:i')}: {$activeProgram->name} (reason: {$item[2]->name} starts).<br/>";
|
||||
$this->addToSchedule($schedule, $activeProgramStart, $item[1], $activeProgram);
|
||||
}
|
||||
|
||||
$activeProgramStart = ($item[1] < $start) ? $start : clone $item[1];
|
||||
$activeProgram = $item[2];
|
||||
}
|
||||
} else if($item[0] == $END) {
|
||||
if($active[$priority] == null) {
|
||||
print( "<b>Error:</b>" . $item[2]->name . ' ends at ' . $item[1]->format('d-m-Y H:i') . ' but no program is active at that priority and timestamp.');
|
||||
print "<pre>"; var_dump($active); print "</pre>";
|
||||
@@ -157,7 +157,7 @@ if($DEBUG) print "Creating schedule between {$start->format('Y-m-d H:i')} and {$
|
||||
|
||||
// In addition, when the ending program was the active program, we need to schedule it and find the new currently active program
|
||||
if($activeProgram == $item[2]) {
|
||||
if($DEBUG) print "{$activeProgramStart->format('d-m-Y H:i')} -- {$item[1]->format('d-m-Y H:i')}: {$activeProgram->name} (reason: {$item[2]->name} ends).<br/>";
|
||||
if($DEBUG) print "{$activeProgramStart->format('d-m-Y H:i')} -- {$item[1]->format('d-m-Y H:i')}: {$activeProgram->name} {$activeProgram->suffix} (reason: {$item[2]->name} ends).<br/>";
|
||||
$this->addToSchedule($schedule, $activeProgramStart, $item[1], $activeProgram);
|
||||
|
||||
$activeProgram = null;
|
||||
@@ -170,26 +170,14 @@ if($DEBUG) print "Creating schedule between {$start->format('Y-m-d H:i')} and {$
|
||||
if($activeProgram != null) {
|
||||
$activeProgramStart = clone $item[1];
|
||||
}
|
||||
|
||||
if($DEBUG) print " New active program is " . ($activeProgram ? $activeProgram->name : "---") . "<br/>";
|
||||
}
|
||||
} else {
|
||||
return response()->abort(500, "Invalid item type: expected START ($START) or END ($END), but got {$item[0]}.");
|
||||
}
|
||||
//} else if($DEBUG) {
|
||||
// print "<i>Skipped {$item[2]->name} {$item[2]->suffix} on {$item[1]->format('d-m-Y H:i')} <br/>"
|
||||
// . " because startdate is " . ($item[2]->startdate == null ? "[null]" : $item[2]->startdate->format('d-m-Y H:i')) . " <br/>"
|
||||
// . " and end date is " . ($item[2]->enddate == null ? "[null]" : $item[2]->enddate->format('d-m-Y H:i')) . " <br/>"
|
||||
// . " Active program " . ($activeProgram ? $activeProgram->name : '[null]') . " starts at " . $activeProgramStart->format('c') . "</i><br/>";
|
||||
}
|
||||
|
||||
// The item will recur in a week
|
||||
$item[1]->add($WEEK);
|
||||
$scheduleChanges->insert($item, [-$item[1]->getTimestamp(), -$item[0]]);
|
||||
}
|
||||
|
||||
if(($activeProgram != null) && ($activeProgramStart != $end)) {
|
||||
if($DEBUG) print "{$activeProgramStart->format('d-m-Y H:i')} -- {$end->format('d-m-Y H:i')}: {$activeProgram->name} (reason: schedule finished).<br/>";
|
||||
if($DEBUG) print "{$activeProgramStart->format('d-m-Y H:i')} -- {$end->format('d-m-Y H:i')}: {$activeProgram->name} {$activeProgram->suffix} (reason: schedule finished).<br/>";
|
||||
$this->addToSchedule($schedule, $activeProgramStart, $end, $activeProgram);
|
||||
}
|
||||
|
||||
@@ -200,7 +188,8 @@ if($DEBUG) print "Creating schedule between {$start->format('Y-m-d H:i')} and {$
|
||||
{
|
||||
$start = new \DateTimeImmutable(urldecode($from));
|
||||
$end = new \DateTimeImmutable(urldecode($to));
|
||||
print '<pre>' . json_encode($this->createSchedule($start, $end, true)) . '</pre>';
|
||||
$this->createSchedule($start, $end, true);
|
||||
return "";
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user