/** * Attempts to update edited course information and displays the edited course. * * @param int $id Id of course to be updated. */ 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; $params['url'] = self::fix_url($params['url']); $params['mapLink'] = self::fix_url($params['mapLink']); $attributes = array('name' => $params['name'], 'description' => $params['description'], 'address' => $params['address'], 'mapLink' => $params['mapLink'], 'url' => $params['url'], 'id' => $id); $course = new Course($attributes); $errors = $course->errors(); if (count($errors) == 0) { $course->update(); for ($i = 1; $i <= Hole::count_holes($course->id); $i++) { $hole = Hole::find_by_course_and_holenumber($course->id, $i); $hole->name = $params[$i . '_name']; $hole->par = $params[$i . '_par']; $hole->update(); } Redirect::to('/course/' . $course->id, array('message' => 'Radan tietoja muutettu.')); } else { View::make('course/edit.html', array('errors' => $errors, 'attributes' => $attributes)); } }
/** * 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)); } } }
/** * Updates score and updates related throws and relations to holes into score_hole table. */ public function update() { $query = DB::connection()->prepare('UPDATE Score SET roundid = :roundId, playerid = :playerId WHERE id = :id'); $query->execute(array('roundId' => $this->roundId, 'playerId' => $this->playerId, 'id' => $this->id)); if ($this->scores) { $round = Round::find($this->roundId); $numberOfHoles = Hole::count_holes($round->courseId); $scores = array_slice($this->scores, 0, $numberOfHoles); foreach ($scores as $holeScore) { $hole = Hole::find_by_course_and_holenumber($round->courseId, $holeScore['holenumber']); $query = DB::connection()->prepare('UPDATE Score_Hole SET throws = :throws WHERE holeid = :holeId AND scoreid = :scoreId'); $exists = $query->execute(array('throws' => $holeScore['throws'], 'holeId' => $hole->id, 'scoreId' => $this->id)); if (!$exists) { $query = DB::connection()->prepare('INSERT INTO Score_Hole (holeid, scoreid, throws) VALUES (:holeId, :scoreId, :throws)'); $query->execute(array('throws' => $holeScore['throws'], 'holeId' => $holeScore['holeId'], 'scoreId' => $this->id)); } } } $this->clean(); }