public function execute($competition) { $flight_size = $competition->flight_size ? $competition->flight_size : Competition::FLIGHT_SIZE_DEFAULT; $flight_interval = $competition->flight_time ? $competition->flight_time : Competition::FLIGHT_TIME_DEFAULT; $flight_time = strtotime("-" . $flight_interval . " minutes", strtotime($competition->start_date)); Yii::trace('Flight size=' . $flight_size, 'BuildFlightChrono::execute'); $count = $flight_size; $position = 1; $registrations = $competition->getRegistrations()->andWhere(['status' => Registration::STATUS_REGISTERED])->orderBy('created_at'); foreach ($registrations->each() as $registration) { if ($count >= $flight_size) { $flight_time = strtotime("+" . $flight_interval . " minutes", strtotime($flight_time)); $count = 0; $flight = new Flight(); $flight->position = $position++; $flight->start_time = $flight_time; $flight->start_hole = $competition->start_hole; $flight->save(); $flight->refresh(); } Yii::trace('doing=' . $registration->id . '=' . $flight->id . ' at=' . $flight_time, 'BuildFlightChrono::execute'); $registration->flight_id = $flight->id; $registration->save(); $count++; } }
public static function addFlights($competition, $registrations) { $flight_size = $competition->flight_size ? $competition->flight_size : Competition::FLIGHT_SIZE_DEFAULT; $position = Flight::find()->andWhere(['competition_id' => $competition->id])->max('position'); if (!intval($position) > 0) { $position = 0; } $position++; $count = $flight_size; // we collect the teams for which we don't have a registration yet $team_ids = []; foreach ($registrations->each() as $registration) { $team_ids[$registration->team_id] = $registration->team_id; } foreach (Team::find()->andWhere(['id' => $team_ids])->each() as $team) { if ($count >= $flight_size) { $count = 0; $flight = new Flight(); $flight->position = $position++; $flight->save(); $flight->refresh(); } foreach ($team->getRegistrations()->each() as $registration) { $registration->flight_id = $flight->id; $registration->save(); } $count++; } }