public function up()
 {
     $this->addColumn('{{%users_tournaments}}', 'points', $this->integer()->defaultValue(NULL));
     $tournaments = UsersTournaments::find()->all();
     foreach ($tournaments as $one) {
         $games = ArrayHelper::getColumn(Result::find()->select('id_game')->where(['id_tournament' => $one->id_tournament])->all(), 'id_game');
         $points = Forecasts::find()->select(['sum(points) as points'])->joinWith('idUser')->where(['in', 'id_game', $games])->andWhere(['id_user' => $one->id_user])->scalar();
         $one->points = $points;
         $one->save(false);
     }
 }
 /**
  * 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'));
     }
 }
 private static function sendReminder($tour, $tournament, $users)
 {
     $subject = "Напоминание: сделайте прогноз на матчи {$tour} тура турнира " . Tournaments::findOne($tournament)->tournament_name;
     $firstGameStarts = date('d-m-Y в H:i', ArrayHelper::getValue(Result::find()->select(['min(dtime) as dtime'])->where(['id_tournament' => $tournament, 'tour' => $tour])->all()[0], 'dtime'));
     $tours = Games::getNumberOfGamesPerTour($tournament);
     foreach ($users as $user) {
         if (Forecasts::getUserForecastTour($user->id, $tournament, $tours)[$tour] == 0) {
             $description = "Вы не сделали прогноз на матчи {$tour} тура турнира " . Tournaments::findOne($tournament)->tournament_name . ". Поторопитесь, первая игра тура начинается <strong>{$firstGameStarts}</strong>";
         } else {
             $description = "Вы сделали прогноз не на все матчи: {$tour} тура турнира " . Tournaments::findOne($tournament)->tournament_name . ". Поторопитесь, первая игра тура начинается {$firstGameStarts}";
         }
         $content = new ArrayDataProvider(['allModels' => Forecasts::getTourUserForecastStatus($tour, $tournament, $user->id), 'sort' => false, 'pagination' => false]);
         $messages[] = Yii::$app->mailer->compose('reminder', ['content' => $content, 'description' => $description, 'user' => $user])->setFrom([Yii::$app->params['adminEmail'] => 'Sportforecast'])->setTo($user->email)->setSubject($subject);
         //adding a record to the DB
         $reminder = self::getReminder($user->id, $tournament, $tour);
         $reminder->save();
     }
     if (!empty($messages)) {
         Yii::$app->mailer->sendMultiple($messages);
     }
     return true;
 }
Example #4
0
 public static function getNumberOfGamesPerTour($tournament)
 {
     $games = ArrayHelper::getColumn(Result::find()->select('id_game')->where(['id_tournament' => $tournament])->all(), 'id_game');
     $tours = (new Query())->select('tour')->from('sf_games')->where(['in', 'id_game', $games])->all();
     if ($tours) {
         $tours = ArrayHelper::getColumn($tours, 'tour');
         $tours = array_count_values($tours);
         ksort($tours);
     }
     return $tours;
 }
 public static function setEventForTournament($id_tournament)
 {
     $winners = array_keys(ArrayHelper::index(Result::getWinners($id_tournament), 'participant'));
     foreach ($winners as $position => $team) {
         self::updateAll(['event' => self::TEAM_IN_TOP_3], ['and', ['id_participant_team' => $team], ['id_tournament' => $id_tournament], ['not', ['forecasted_position' => $position + 1]]]);
         self::updateAll(['event' => self::TEAM_POSITION], ['and', ['id_participant_team' => $team], ['id_tournament' => $id_tournament], ['forecasted_position' => $position + 1]]);
         self::updateAll(['event' => NULL], ['and', ['id_tournament' => $id_tournament], ['not in', 'id_participant_team', $winners]]);
     }
 }
Example #6
0
 public static function getForecastResultUserTourTournament($user, $tour, $tournament)
 {
     $games = Result::getGamesTourTournament($tour, $tournament);
     $forecasts = self::find()->where(['and', "id_user = {$user}", ['in', 'sf_forecasts.id_game', ArrayHelper::getColumn($games, 'id_game')]])->asArray()->all();
     $forecasts = ArrayHelper::index($forecasts, 'id_game');
     $games = ArrayHelper::toArray($games);
     foreach ($games as &$one) {
         if (array_key_exists($one['id_game'], $forecasts)) {
             $one['fscore_home'] = $forecasts[$one['id_game']]['fscore_home'];
             $one['fscore_guest'] = $forecasts[$one['id_game']]['fscore_guest'];
             $one['fpoints'] = $forecasts[$one['id_game']]['points'];
             $one['status'] = true;
         } else {
             $one['fscore_home'] = '-';
             $one['fscore_guest'] = '-';
             $one['fpoints'] = '-';
             $one['status'] = false;
         }
     }
     ArrayHelper::multisort($games, 'dtime');
     return $games;
 }
 public function actionGames($id)
 {
     $games = new ArrayDataProvider(['allModels' => Result::getParticipantGames($id), 'pagination' => false]);
     $tournament = TeamTournaments::findOne($id);
     $team = TeamTournaments::findOne($id);
     return $this->render('gamesGuest', compact('games', 'tournament', 'team'));
 }
 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]);
 }
Example #9
0
 public function afterSave($insert, $changedAttributes)
 {
     parent::afterSave($insert, $changedAttributes);
     //getting the tournament number
     $tournament = ArrayHelper::getValue(Result::find()->where(['id_game' => $this->id_game])->one(), 'id_tournament');
     //updating the forecast points for the game
     if (!$insert) {
         Forecasts::setForecastPoints($this->id_game, $this->score_home, $this->score_guest, $tournament);
     }
     //if tour finished, need to send notifications (if not already sent) and put tournament as finished if it was last tour
     /**
      * @var $tournamentModel \app\models\tournaments\Tournaments
      */
     if (self::isTourFinished($tournament, $this->tour)) {
         $tournamentModel = Tournaments::findOne($tournament);
         if ($tournamentModel->num_tours == $this->tour) {
             $tournamentModel->is_active = Tournaments::FINISHED;
             $tournamentModel->save(false);
         }
         if (!TourResultNotifications::ifExists($this->tour, $tournament)) {
             self::sendTourResults($this->tour, $tournament);
             $notification = new TourResultNotifications();
             $notification->tour = $this->tour;
             $notification->tournament = $tournament;
             $notification->save();
         }
     }
 }
 public function actionAutoreminder()
 {
     $tournaments = Tournaments::find()->where(['not', ['is_active' => Tournaments::FINISHED]])->all();
     foreach ($tournaments as &$tournament) {
         if ($tournament->is_active == Tournaments::NOT_STARTED && $tournament->startsOn - time() < 60 * 60 * 24 * 5) {
             $tournament->is_active = Tournaments::GOING;
             $tournament->save(false);
         }
         $nextTour = Tournaments::getNextTour($tournament->id_tournament);
         if ($nextTour != NULL) {
             $firstGameStarts = ArrayHelper::getValue(Result::find()->select(['min(dtime) as dtime'])->where(['id_tournament' => $tournament->id_tournament, 'tour' => $nextTour])->all()[0], 'dtime');
             if ($firstGameStarts > time() + 60 * 60 * 24 * 4 && $firstGameStarts < time() + 60 * 60 * 24 * 5 || $firstGameStarts > time() + 60 * 60 * 24 * 2 && $firstGameStarts < time() + 60 * 60 * 24 * 3) {
                 $sendReminders = Reminders::sendAutoReminder($nextTour, $tournament->id_tournament);
                 Yii::info("Task Autoreminder for {$tournament->tournament_name} {$nextTour} tour has been executed", 'console');
             }
         }
     }
     return 0;
 }
 public function deleteForecasts()
 {
     $games = ArrayHelper::getColumn(Result::find()->where(['id_tournament' => $this->id_tournament])->all(), 'id_game');
     return Forecasts::deleteAll(['and', ['in', 'id_game', $games], ['id_user' => $this->id_user]]);
 }