public function getForecastersListNew() { $forecasters = UsersTournaments::find()->where(['id_tournament' => $this->id_tournament])->with('idUser')->asArray()->all(); $tours = Games::getNumberOfGamesPerTour($this->id_tournament); foreach ($forecasters as &$one) { $one['tours'] = Forecasts::getUserForecastTour($one['id_user'], $this->id_tournament, $tours); } return $forecasters; }
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); } }
public static function getAutoReminderRecipients($tournament, $tour) { $candidates = self::find()->joinWith('idUser')->where(['id_tournament' => $tournament, 'notification' => self::NOTIFICATION_ENABLED, 'active' => Users::STATUS_ACTIVE])->all(); $recipients = []; $tours = Games::getNumberOfGamesPerTour($tournament); foreach ($candidates as $one) { if (Forecasts::getUserForecastTour($one['id_user'], $tournament, $tours)[$tour] != '2' && Reminders::ifEligible($tournament, $tour, $one['id_user'])) { $recipients[] = Users::find()->where(['id' => $one['id_user']])->one(); } } return $recipients; }
public static function getForecastedStandings($id_tournament, $id_user) { //getting list of games for the tournament $games = Games::getGamesForTournament($id_tournament); $gameIDs = array_map(function ($item) { return $item->id_game; }, $games); //getting forecasts for the user for the tournament $forecasts = Forecasts::find()->where(['id_game' => $gameIDs, 'id_user' => $id_user])->indexBy('id_game')->asArray()->all(); //if there's a forecast for the game -> put forecast score as a game score + recalculating points foreach ($games as &$game) { if (isset($forecasts[$game->id_game])) { $game->score_home = $forecasts[$game->id_game]['fscore_home']; $game->score_guest = $forecasts[$game->id_game]['fscore_guest']; $game->getGamePoints(); } } $games = ArrayHelper::toArray($games); $teams = array_unique(array_merge(array_map(function ($value) { return $value['id_team_home']; }, $games), array_map(function ($value) { return $value['id_team_guest']; }, $games))); $teamObjects = TeamTournaments::find()->where(['id' => $teams])->joinWith('idTeam')->indexBy('id')->all(); $result = array_map(function ($item) use($games, $teamObjects) { $row['pts'] = 0; $row['participant'] = $item; $row['id'] = $item; $row['team_logo'] = $teamObjects[$item]->idTeam->team_logo; $row['team_name'] = $teamObjects[$item]->idTeam->team_name; $row['games_played'] = 0; foreach ($games as $game) { if ($game['id_team_home'] == $item) { $row['pts'] += $game['points_home']; if (isset($game['points_home'])) { $row['games_played'] += 1; } } if ($game['id_team_guest'] == $item) { $row['pts'] += $game['points_guest']; if (isset($game['points_guest'])) { $row['games_played'] += 1; } } } return $row; }, $teams); usort($result, function ($a, $b) { return $b['pts'] - $a['pts']; }); return $result; }
protected function matchWebDB() { foreach ($this->gamesFromDB as $gameDB) { foreach ($this->gamesFromWeb as $k => $gameWeb) { if ($gameWeb['tour'] == $gameDB->tour && $gameWeb['id_team_home'] == $gameDB->id_team_home && $gameWeb['id_team_guest'] == $gameDB->id_team_guest) { if ($gameDB->date_time_game != $gameWeb['date_time_game']) { $gameDB->date_time_game = $gameWeb['date_time_game']; } if ($gameDB->score_home !== $gameWeb['score_home']) { $gameDB->score_home = $gameWeb['score_home']; } if ($gameDB->score_guest !== $gameWeb['score_guest']) { $gameDB->score_guest = $gameWeb['score_guest']; } $dirtyAttr = $gameDB->getDirtyAttributes(); if (!empty($dirtyAttr)) { $gameDB->save(false); } $unset = ArrayHelper::remove($this->gamesFromWeb, $k); continue; } //if home and guest switched if ($gameWeb['tour'] == $gameDB->tour && $gameWeb['id_team_home'] == $gameDB->id_team_guest && $gameWeb['id_team_guest'] == $gameDB->id_team_home) { $gameDB->id_team_home = $gameWeb['id_team_home']; $gameDB->id_team_guest = $gameWeb['id_team_guest']; if ($gameDB->date_time_game != $gameWeb['date_time_game']) { $gameDB->date_time_game = $gameWeb['date_time_game']; } //need to switch forecast home <=> guest $forecasts = Forecasts::find()->where(['id_game' => $gameDB->id_game])->all(); foreach ($forecasts as $forecast) { $temp = $forecast->fscore_home; $forecast->fscore_home = $forecast->fscore_guest; $forecast->fscore_guest = $temp; $forecast->save(false); } $gameDB->save(false); $unset = ArrayHelper::remove($this->gamesFromWeb, $k); continue; } } } }
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; }
private static function generateTourNews($tournament, $tour) { $trn = Tournaments::findOne($tournament); $games = new ArrayDataProvider(['allModels' => self::getTourResults($tournament, $tour), 'pagination' => false, 'sort' => false]); $forecasters = new ArrayDataProvider(['allModels' => Forecasts::getTopFiveForecastersWithPoints($tournament, $tour)]); return Yii::$app->controller->renderPartial('@app/mail/_tourResultNews', ['trn' => $trn, 'games' => $games, 'forecasters' => $forecasters, 'tour' => $tour]); }
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); }
private function getForecastModel($idGame) { $model = Forecasts::findOne(['id_game' => $idGame, 'id_user' => Yii::$app->user->id]); if ($model != null) { return $model; } $model = new Forecasts(); $model->id_game = $idGame; $model->id_user = Yii::$app->user->id; return $model; }
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 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]]); }
/** * @return \yii\db\ActiveQuery */ public function getForecasts() { return $this->hasMany(Forecasts::className(), ['id_user' => 'id']); }