Rate_Team_Action.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. require_once(PATH::ACTION . "Action.php");
  12. /**
  13. * Sets the score for a team.
  14. *
  15. * Reads the POST parameters looking for the following KEYS:
  16. * player: Owner's player ID.
  17. * team: Team ID.
  18. * score: New score.
  19. * Validates the data and sets the score.
  20. *
  21. * @category Action
  22. */
  23. class Rate_Team_Action extends Action{
  24. /**
  25. * Executes the action.
  26. */
  27. function execute(){
  28. $player_id = filter_input(INPUT_GET, "player");
  29. $statement = get_context()->get_db()->prepare("
  30. SELECT count(user) AS c FROM player WHERE id = :player AND user = :user");
  31. $statement->bindValue(":player", $player_id, SQLITE3_TEXT);
  32. $statement->bindValue(":user", get_context()->get_user()->get_id(), SQLITE3_TEXT);
  33. if ($statement->execute()->fetchArray(SQLITE3_ASSOC)["c"] != 1){
  34. // The user doesn't own the team.
  35. $this->code = 401;
  36. $this->message = "Unauthorized.";
  37. return;
  38. }
  39. $team = filter_input(INPUT_GET, "team");
  40. $score = intval(filter_input(INPUT_GET, "score"));
  41. $statement = get_context()->get_db()->prepare("
  42. UPDATE team
  43. SET score = :score
  44. WHERE
  45. player = :player AND
  46. id = :team;
  47. ");
  48. $statement->bindValue(":score", $score, SQLITE3_TEXT);
  49. $statement->bindValue(":player", $player_id, SQLITE3_TEXT);
  50. $statement->bindValue(":team", $team, SQLITE3_TEXT);
  51. $statement->execute();
  52. $this->code = 204;
  53. $this->message = "No content";
  54. return;
  55. }
  56. }