rate_team.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. <?php
  2. /**
  3. * File for the team score modification action.
  4. *
  5. * Implements an action function to be called from the {@see Controller}.
  6. *
  7. * @author Iñigo Valentin <i@inigovalentin.com>
  8. * @license https://www.gnu.org/licenses/gpl-3.0.en.html GNU General Public License V3
  9. * @package SWDB
  10. */
  11. /**
  12. * Sets the score for a team.
  13. *
  14. * Reads the POST parameters looking for the following KEYS:
  15. * player: Owner's player ID.
  16. * team: Team ID.
  17. * score: New score.
  18. * Validates the data and sets the score.
  19. *
  20. * @return int 201 on success, HTTP erro status codes on error.
  21. * @category Action
  22. */
  23. function action(){
  24. $player_id = filter_input(INPUT_GET, "player");
  25. $statement = get_context()->get_db()->prepare("
  26. SELECT count(user) AS c FROM player WHERE id = :player AND user = :user");
  27. $statement->bindValue(":player", $player_id, SQLITE3_TEXT);
  28. $statement->bindValue(":user", get_context()->get_user()->get_id(), SQLITE3_TEXT);
  29. if ($statement->execute()->fetchArray(SQLITE3_ASSOC)["c"] != 1){
  30. // The user doesn't own the team.
  31. return 401;
  32. }
  33. $team = filter_input(INPUT_GET, "team");
  34. $score = intval(filter_input(INPUT_GET, "score"));
  35. $statement = get_context()->get_db()->prepare("
  36. UPDATE team
  37. SET score = :score
  38. WHERE
  39. player = :player AND
  40. id = :team;
  41. ");
  42. $statement->bindValue(":score", $score, SQLITE3_TEXT);
  43. $statement->bindValue(":player", $player_id, SQLITE3_TEXT);
  44. $statement->bindValue(":team", $team, SQLITE3_TEXT);
  45. $statement->execute();
  46. return 201;
  47. }