public function actionDetails($id)
 {
     $user = Yii::$app->user;
     Yii::$app->user->returnUrl = Yii::$app->request->url;
     $tournament = Tournaments::findOne($id);
     if ($tournament === null) {
         throw new NotFoundHttpException('Запрашиваемая страница не найдена.');
     }
     $teamParticipants = new ArrayDataProvider(['allModels' => Result::getStandings($id), 'pagination' => false]);
     //games list prep
     if (Yii::$app->request->post('tours')) {
         $tours = Yii::$app->request->post('tours');
     } else {
         $tours = range(1, $tournament->num_tours, 1);
     }
     for ($i = 1; $i <= $tournament->num_tours; $i++) {
         $tour_list[$i] = "{$i} тур";
     }
     $forecasters = new ActiveDataProvider(['query' => UsersTournaments::find()->forecastersStandings($id), 'pagination' => false]);
     //if guest or not participating in the tournament
     if ($user->isGuest || !$user->identity->isUserParticipatesInTournament($id)) {
         $tourGames = Games::getGamesGroupedByTour($id, $tours);
         $viewFile = 'tournamentDetailsGuest';
         $winners = [];
         $forecastedStandings = [];
     } else {
         $forecastedStandings = new ArrayDataProvider(['allModels' => Result::getForecastedStandings($id, $user->id), 'pagination' => false]);
         $tourGames = Games::getGamesGroupedByTourWithForecast($id, $tours, $user->id);
         $viewFile = 'tournamentDetailsUser';
         //preparing tournament winners forecast form
         $winners = new TopTeamsForm($user->id, $id);
         if ($winners->load(Yii::$app->request->post()) && $winners->validate()) {
             if (time() < $tournament->wfDueTo) {
                 $winners->edit();
                 Yii::$app->getSession()->setFlash('success', 'Прогноз на призеров турнира успешно сохранен');
             } else {
                 Yii::$app->getSession()->setFlash('success', 'Не нужно пытаться обмануть :)');
             }
             return $this->refresh();
         }
     }
     $data = compact('tournament', 'teamParticipants', 'forecasters', 'tour_list', 'tourGames', 'winners', 'forecastedStandings');
     if ($tournament->is_active == Tournaments::FINISHED) {
         $data['additionalPoints'] = implode('</br>', Top3TeamsForecast::getClarifications($user->id, $id));
         $userTournamentsModel = UsersTournaments::find()->where(['id_tournament' => $id])->andWhere(['id_user' => $user->id])->with('winnersForecast')->one();
         if (!empty($userTournamentsModel)) {
             $data['totalAdditionalPoints'] = $userTournamentsModel->calculateAdditionalPoints();
         }
     } else {
         $data['additionalPoints'] = '';
         $data['totalAdditionalPoints'] = '';
     }
     return $this->render($viewFile, $data);
 }
 /**
  * Updates an existing tournaments model.
  * If update is successful, the browser will be redirected to the 'view' page.
  * @param integer $id
  * @return mixed
  */
 public function actionUpdate($id)
 {
     Yii::$app->user->returnUrl = Yii::$app->request->url;
     $model = $this->findModel($id);
     //getting standings with participants
     $participants = new ArrayDataProvider(['allModels' => Result::getStandings($id), 'pagination' => false]);
     //all teams(potential participants) for the current tournament
     $teams = Teams::getTeamCandidates($model->country0, TeamTournaments::getTournamentParticipantsID($id));
     $forecasters = new ArrayDataProvider(['allModels' => $model->getForecastersListNew()]);
     //getting the next tour number
     $nextTour = Tournaments::getNextTour($id);
     if ($model->load(Yii::$app->request->post()) && $model->save()) {
         return $this->goBack();
     } else {
         return $this->render('update', compact('nextTour', 'model', 'teams', 'participants', 'forecasters'));
     }
 }
 public static function generateFinalNews($tournament)
 {
     $trn = self::findOne($tournament);
     $forecasters = new ArrayDataProvider(['allModels' => UsersTournaments::topThreeForecastersForTournament($tournament)]);
     $standings = new ArrayDataProvider(['allModels' => Result::getStandings($tournament)]);
     return Yii::$app->controller->renderPartial('@app/mail/_tournamentFinishedNews', ['trn' => $trn, 'forecasters' => $forecasters, 'standings' => $standings]);
 }