public function index(\App\Gameweek $gameweek = null, \App\Month $month = null)
 {
     $user = null;
     $users = \App\User::ranked()->orderBy('rank')->get();
     $lastFixture = \App\Fixture::over()->orderBy('kickoff', 'desc')->first();
     $activeTabs = ['active', '', ''];
     if (!$month or !$month->hasStarted()) {
         $month = $lastFixture->gameweek->month;
     } else {
         $activeTabs = ['', 'active', ''];
     }
     if (!$gameweek or !$gameweek->hasCompletedFixture()) {
         $gameweek = $lastFixture->gameweek;
     } else {
         $activeTabs = ['', '', 'active'];
     }
     if (\Auth::check()) {
         $user = \Auth::user();
     }
     $months = \App\Month::orderBy('id', 'desc')->get()->filter(function ($month) {
         return $month->hasStarted();
     });
     $gameweeks = \App\Gameweek::orderBy('id', 'desc')->get()->filter(function ($gw) {
         return $gw->hasCompletedFixture();
     });
     $gameweekUsers = $gameweek->predictors()->orderBy('gameweek_user.rank', 'asc')->get();
     $monthUsers = $month->users()->orderBy('month_user.rank', 'asc')->get();
     $nameOfPage = 'standings';
     return view('pages.standings', compact('gameweeks', 'months', 'users', 'gameweekUsers', 'monthUsers', 'month', 'gameweek', 'user', 'activeTabs', 'nameOfPage'));
 }
 public function index()
 {
     $usersData = [];
     $usersData['count'] = User::all()->count();
     $usersData['incompleteCount'] = User::where('status', 0)->count();
     $picsData = [];
     $picsData['count'] = Pic::all()->count();
     $clubsData = [];
     $clubsData['count'] = Club::all()->count();
     $badgesData = [];
     $badgesData['count'] = Badge::all()->count();
     $fixtures = Fixture::all();
     $fixturesData = [];
     $fixturesData['count'] = $fixtures->count();
     $fixturesData['overCount'] = $fixtures->filter(function ($fxt) {
         return $fxt->isOver();
     })->count();
     $fixturesData['closedNotOverCount'] = $fixtures->filter(function ($fxt) {
         return $fxt->isClosed() and !$fxt->isOver();
     })->count();
     $gameweeks = Gameweek::all();
     $gameweeksData = [];
     $gameweeksData['count'] = $gameweeks->count();
     $gameweeksData['completeCount'] = Gameweek::complete()->get()->count();
     $gameweeksData['pendingCount'] = Gameweek::incomplete()->get()->filter(function ($gw) {
         return $gw->hasCompletedFixture();
     })->count();
     $adminsData = [];
     $adminsData['count'] = Admin::all()->count();
     return view('admin.dashboard.index', compact('usersData', 'picsData', 'clubsData', 'badgesData', 'fixturesData', 'gameweeksData', 'adminsData'));
 }
 /**
  * Update the specified resource in storage.
  *
  * @param  \Illuminate\Http\Request  $request
  * @param  int  $id
  * @return \Illuminate\Http\Response
  */
 public function update(Request $request, $id)
 {
     $this->validate($request, ['team_1_score' => 'required', 'team_2_score' => 'required']);
     $fixture = Fixture::find($id);
     $fixture->team_1_score = $request->get('team_1_score');
     $fixture->team_2_score = $request->get('team_2_score');
     $fixture->save();
     flash()->success('', 'Rezultatas redaguotas!');
     return redirect()->back();
 }
 public function show(\App\User $user, \App\Gameweek $gameweekInFocus = null)
 {
     //Get the last "over" fixture
     $lastFixture = \App\Fixture::over()->orderBy('kickoff', 'desc')->first();
     if (!$gameweekInFocus or !$gameweekInFocus->hasCompletedFixture()) {
         //Get the corresponding gameweek and eager load its fixtures
         $gameweekInFocus = $lastFixture->gameweek()->with(['fixtures' => function ($query) {
             $query->over();
         }])->first();
     } else {
         $gameweekInFocus->load(['fixtures' => function ($query) {
             $query->over();
         }]);
     }
     $gameweeks = \App\Gameweek::orderBy('id', 'desc')->get()->filter(function ($gw) {
         return $gw->hasCompletedFixture();
     });
     $badges = $user->badges()->orderBy('badge_user.gameweek_id', 'asc')->get();
     return view('users.show', compact('user', 'gameweekInFocus', 'gameweeks', 'lastFixture', 'badges'));
 }
 /**
  * Show the application home (predict) page to the user
  * 
  * @return Response
  */
 public function index()
 {
     $user = \Auth::user();
     $gws = \App\Gameweek::incomplete()->take(2)->with('fixtures')->with(['fixtures.predictions' => function ($query) use($user) {
         $query->where('user_id', $user->id);
     }])->get();
     foreach ($gws as $gw) {
         $userGameweek = $user->gameweeks()->where('gameweek_id', $gw->id)->first();
         $boostId = null;
         $boostedClosed = false;
         if ($userGameweek) {
             $boostId = $userGameweek->pivot->boost_id;
             if ($boostId) {
                 $boostedClosed = \App\Fixture::find($boostId)->isClosed();
             }
         }
         $gw->boostId = $boostId;
         $gw->boostedClosed = $boostedClosed;
     }
     $nameOfPage = 'home';
     return view('pages.home', compact('gws', 'user', 'nameOfPage'));
 }
 public function store(Request $request)
 {
     $input = $request->all();
     $predictions = [];
     foreach ($input['home_score'] as $key => $score) {
         $predictions[$key]['home_score'] = $score;
     }
     foreach ($input['away_score'] as $key => $score) {
         $predictions[$key]['away_score'] = $score;
     }
     foreach ($input['fix_id'] as $key => $id) {
         $predictions[$key]['fixture_id'] = $id;
         $predictions[$key]['user_id'] = $this->user->id;
     }
     foreach ($predictions as $prediction) {
         $fxt = \App\Fixture::findOrFail($prediction['fixture_id']);
         if ($fxt->isClosed()) {
             continue;
         }
         $pred = \App\Prediction::firstOrNew(['user_id' => $prediction['user_id'], 'fixture_id' => $prediction['fixture_id']]);
         $pred->home_score = $prediction['home_score'];
         $pred->away_score = $prediction['away_score'];
         if ($pred->valid()) {
             $user = \App\User::find($prediction['user_id']);
             $gameweek = $pred->fixture->gameweek;
             $exists = $user->gameweeks->contains($gameweek->id);
             if (!$exists) {
                 $user->gameweeks()->attach($gameweek->id, ['score' => 0, 'rank' => null, 'boost_id' => null]);
             }
             if (isset($input['boost_id']) and $pred->fixture_id == $input['boost_id']) {
                 $user->gameweeks()->updateExistingPivot($gameweek->id, ['boost_id' => $input['boost_id']], false);
             }
             $pred->save();
         }
     }
     return redirect('/');
 }
 public function compute($id)
 {
     $fixture = Fixture::findOrFail($id);
     $gameweek = $fixture->gameweek;
     if ($fixture->home_score === null or $fixture->away_score === null) {
         return redirect('/admin/gameweek/' . $gameweek->id);
     }
     $allPredictions = Prediction::where('fixture_id', $fixture->id)->get();
     foreach ($allPredictions as $prediction) {
         $prediction->grade = $prediction->grading($fixture);
         $boostId = $prediction->user->gameweeks()->where('gameweek_id', $gameweek->id)->first()->pivot->boost_id;
         if ($boostId == $prediction->fixture->id) {
             $prediction->grade += 6;
         }
         $prediction->save();
     }
     if (!$fixture->isOver()) {
         $fixture->over = true;
         $fixture->save();
     }
     return redirect('/admin/gameweek/' . $gameweek->id);
 }