Ejemplo n.º 1
0
 /**
  * Checks if round is ready to be saved. If not, displays round edit page with information already inputted. If yes, attempts to update selected round and related scores.
  *
  * @param int $id Id of selected round.
  */
 public static function update($id)
 {
     $player = self::get_user_logged_in();
     if (!$player) {
         View::make('player/login.html', array('error' => 'Vain kirjautuneet käyttäjät voivat muokata ratoja.'));
     }
     $params = $_POST;
     $players = Player::all();
     $course = Course::find($params['course']);
     $courses = Course::all();
     $played = $params['played'];
     $round = Round::find($id);
     $holes = Hole::find_by_course($course->id);
     $scores = Score::find_by_round($id);
     $numberOfPlayers = sizeof($scores);
     if ($params['course'] != $params['course_orig']) {
         // If course selection has changed we need to display edit page again with new information.
         $playerScores = array();
         $numberOfHoles = Hole::count_holes($params['course_orig']);
         for ($i = 1; $i <= $numberOfPlayers; $i++) {
             $holeScores = array();
             for ($j = 1; $j <= $numberOfHoles; $j++) {
                 $holeScores[] = $params['p' . $i . '_h' . $j];
             }
             $playerScores[] = array('player' => Player::find($params['player_' . $i]), 'holes' => $holeScores);
         }
         View::make('round/edit.html', array('numberOfPlayers' => $numberOfPlayers, 'players' => $players, 'course' => $course, 'holes' => $holes, 'courses' => $courses, 'playerScores' => $playerScores, 'round' => $round, 'scores' => $scores, 'played' => $played));
     } else {
         $attributes = array('courseId' => $params['course'], 'played' => $params['played']);
         $playerScores = array();
         $numberOfHoles = Hole::count_holes($params['course_orig']);
         $round = new Round(array_merge(array("id" => $round->id), $attributes));
         $errors = $round->errors();
         if (count($errors) == 0) {
             $round->update();
             for ($i = 1; $i <= $numberOfPlayers; $i++) {
                 $holeScores = array();
                 $scoreId = $params['score_' . $i];
                 for ($j = 1; $j <= $numberOfHoles; $j++) {
                     $hole = Hole::find_by_course_and_holenumber($params['course_orig'], $j);
                     $holeScores[] = array('throws' => $params['p' . $i . '_h' . $j], 'holeId' => $hole->id, 'holenumber' => $j);
                 }
                 $score = new Score(array('id' => $scoreId, 'playerId' => $params['player_' . $i], 'roundId' => $round->id, 'scores' => $holeScores));
                 $score->update();
             }
             Redirect::to('/round/' . $round->id, array('message' => 'Kierrosta muokattu.'));
         } else {
             View::make('round/new.html', array('errors' => $errors, 'attributes' => $attributes));
         }
     }
 }
Ejemplo n.º 2
0
 });
 // POST Create round for a team and station
 Route::post('(:any)/nyttlag', function ($slug) {
     Round::create(Input::all());
     return Redirect::to($slug);
 });
 // GET Form for editing a round
 Route::get('(:any)/(:num)', function ($slug, $team_id) {
     $station_id = Station::where('slug', '=', $slug)->only('id');
     $round = Round::where_team_id_and_station_id($team_id, $station_id)->first();
     return View::make('round.edit', array('round' => $round, 'station' => $round->station, 'team' => $round->team));
 });
 // GET Form for editing a round
 Route::put('(:any)/(:num)', function ($slug, $team_id) {
     $id = Round::where_station_id_and_team_id(Input::get('station_id'), Input::get('team_id'))->only('id');
     Round::update($id, Input::all());
     return Redirect::to($slug);
 });
 // GET One station
 Route::get('(:any)', function ($slug) {
     $station = Station::where('slug', '=', $slug)->first();
     if (empty($station)) {
         return Response::error('404');
     }
     $teams = Team::all();
     $stationRounds = Round::where('station_id', '=', $station->id)->get();
     $teamsAdded = array();
     $teamslist = array();
     foreach ($stationRounds as $sr) {
         $teamsAdded[] = $sr->team->id;
     }