/** * Get the next offering for this course */ public function getNextOffering() { /** * Filter out offerings which are not available */ $this->getOfferings()->filter(function ($offering) { return $offering->getStatus() == Offering::COURSE_NA; }); $offerings = $this->getOfferings(); if ($offerings->isEmpty()) { //TODO: Handle it correctly // Create a offering $offering = new Offering(); $offering->setCourse($this); $offering->setId(-1); $offering->setUrl($this->getUrl()); $dt = new \DateTime(); $dt->add(new \DateInterval("P1Y")); $offering->setStartDate($dt); $offering->setStatus(Offering::START_DATES_UNKNOWN); return $offering; } $nextOffering = $offerings->first(); $now = new \DateTime(); $upcoming = array(); foreach ($offerings as $offering) { if ($offering->getStartDate() > $now) { $upcoming[] = $offering; } if ($offering->getStartDate() > $nextOffering->getStartDate()) { $nextOffering = $offering; } } if (count($upcoming) > 1) { // Multiple upcoming. Pick the earliest one $nextOffering = array_pop($upcoming); foreach ($upcoming as $offering) { if ($offering->getStartDate() < $nextOffering->getStartDate()) { $nextOffering = $offering; } } } return $nextOffering; }
private function buildOffering($id, $date, $state) { $o = new Offering(); $o->setId($id); $o->setStartDate($date); // End date $endDate = clone $date; $endDate->add(new \DateInterval("P1M")); $o->setEndDate($endDate); $o->setStatus($state); return $o; }