public function afterSave($insert, $changedAttributes)
 {
     parent::afterSave($insert, $changedAttributes);
     if (isset($changedAttributes['is_active']) && $changedAttributes['is_active'] != self::FINISHED && $this->is_active == self::FINISHED) {
         News::updateAll(['archive' => News::ARCHIVE_TRUE], ['id_tournament' => $this->id_tournament]);
         //assigning Events for winners forecast
         Top3TeamsForecast::setEventForTournament($this->id_tournament);
         //adding additional points to the total points field of UserTournaments model
         $this->assignAdditionalPoints();
     }
     //if setting tournament back to active from finished, need to remove calculated additional points and clean additional forecast events
     if (isset($changedAttributes['is_active']) && $changedAttributes['is_active'] == self::FINISHED && $this->is_active == self::GOING) {
         //removing additional points to the total points field of UserTournaments model
         $this->removeAdditionalPoints();
         //assigning Events for winners forecast to NULL
         Top3TeamsForecast::clearEventForTournament($this->id_tournament);
     }
     if ($insert) {
         $news = new News();
         $news->scenario = 'send';
         $news->id_tournament = 0;
         $news->subject = 'Добавлен новый турнир';
         $news->body = "<p>Для прогноза доступен новый турнир {$this->tournament_name}, первый тур которого состоится " . date('d.m.y', $this->startsOn) . " </p>" . "<p>Вы также можете попробовать угадать призеров турнира и заработать дополнительные очки! Прогноз на призеров принимается до " . date('d.m.y', $this->wfDueTo) . " </p>" . "<p>Спешите принять участие! Зайдите в <strong>Профиль->Мои турниры</strong> чтобы начать делать прогнозы</p>";
         $news->save();
     }
 }
 private function getForecast($user, $tournament)
 {
     for ($i = 1; $i <= 3; $i++) {
         if (Top3TeamsForecast::find()->findModel($user, $tournament, $i)->exists()) {
             $this->_models[$i] = Top3TeamsForecast::find()->findModel($user, $tournament, $i)->one();
         } else {
             $this->_models[$i] = new Top3TeamsForecast();
             $this->_models[$i]->id_user = $user;
             $this->_models[$i]->id_tournament = $tournament;
             $this->_models[$i]->forecasted_position = $i;
         }
     }
     return $this->_models;
 }
 public function actionUser($user, $tournament)
 {
     $userModel = Users::findOne($user);
     $forecastStatus = Forecasts::getUserForecastStatus($tournament, $user);
     $forecast = new ArrayDataProvider(['allModels' => $forecastStatus, 'pagination' => false]);
     $winnersForecast = Top3TeamsForecast::find()->where(['id_tournament' => $tournament])->andWhere(['id_user' => $user])->with('team.idTeam')->orderBy(['forecasted_position' => SORT_ASC])->asArray()->all();
     $isFinished = Tournaments::findOne($tournament)->is_active == Tournaments::FINISHED;
     $winnersForecastDataProvider = new ArrayDataProvider(['allModels' => $winnersForecast, 'pagination' => false]);
     $data = ['forecast' => $forecast, 'user' => $userModel, 'winnersForecast' => $winnersForecastDataProvider, 'isFinished' => $isFinished];
     if ($isFinished) {
         $userTournamentModel = UsersTournaments::find()->where(['id_user' => $user])->andWhere(['id_tournament' => $tournament])->with('winnersForecast')->one();
         $data['winnersForecastDetails'] = implode('</br>', Top3TeamsForecast::getClarifications($user, $tournament));
         $data['totalAdditionalPoints'] = $userTournamentModel->calculateAdditionalPoints();
     }
     return $this->renderAjax('user', $data);
 }
 public function getWinnersForecast()
 {
     return $this->hasMany(Top3TeamsForecast::className(), ['id_tournament' => 'id_tournament', 'id_user' => 'id_user']);
 }