Exemplo n.º 1
0
 /**
  * Calculates the next session for a course
  * @param Course $course
  * @return Offering
  */
 public static function getNextSession(Course $course)
 {
     $offerings = $course->getOfferings();
     // Remove all the offerings that are not valid
     $offerings->filter(function ($offering) {
         return $offering->getStatus() == Offering::COURSE_NA;
     });
     if ($offerings->isEmpty()) {
         return null;
     }
     // Categorize offerings into finished, ongoing, selfpaced, upcoming
     // Initialize the state map
     $offeringStateMap = array();
     foreach (Offering::$stateMap as $key => $value) {
         $offeringStateMap[$key] = array();
     }
     // Build the state map;
     foreach ($offerings as $offering) {
         $states = self::getStates($offering);
         foreach ($states as $state) {
             $offeringStateMap[$state][] = $offering;
         }
     }
     // Now that the state map is build - traverse in the following order until the next session is found
     // upcoming, self paced, ongoing, finished
     // upcoming
     if (!empty($offeringStateMap['upcoming'])) {
         if (count($offeringStateMap['upcoming']) == 1) {
             return array_pop($offeringStateMap['upcoming']);
         }
         // Multiple sessions. Pick the next earliest upcoming session
         $next = array_shift($offeringStateMap['upcoming']);
         foreach ($offeringStateMap['upcoming'] as $uo) {
             if ($uo->getStartDate() < $next->getStartDate()) {
                 $next = $uo;
             }
         }
         return $next;
     }
     // selfpaced
     if (!empty($offeringStateMap['selfpaced'])) {
         return array_pop($offeringStateMap['selfpaced']);
     }
     // Ongoing
     if (!empty($offeringStateMap['ongoing'])) {
         return array_pop($offeringStateMap['ongoing']);
     }
     // finished
     if (!empty($offeringStateMap['past'])) {
         if (count($offeringStateMap['past']) == 1) {
             return array_pop($offeringStateMap['past']);
         }
         // Multiple sessions. Pick the last finished session
         $last = array_shift($offeringStateMap['past']);
         foreach ($offeringStateMap['past'] as $uo) {
             if ($uo->getStartDate() > $last->getStartDate()) {
                 $last = $uo;
             }
         }
         return $last;
     }
     // Error: Should not come here
     return null;
 }