Guest User

Untitled

a guest
Jun 20th, 2018
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.87 KB | None | 0 0
  1. <?php
  2.  
  3. class Model_Question extends WebApp_Model
  4. {
  5. protected $id;
  6. protected $question;
  7. protected $choices = array();
  8. protected $asker;
  9. protected $asked;
  10. protected $visible = true;
  11.  
  12. public function setId($id) {
  13. $this->id = (int) $id;
  14. }
  15.  
  16. public function setQuestion($question) {
  17. $question = trim($question);
  18. if (empty($question)) {
  19. throw new InvalidArgument("Question cannot be empty");
  20. }
  21.  
  22. if (strlen($question) > 140) {
  23. throw new InvalidArgument("Question must be shorter than 140 characters");
  24. }
  25.  
  26. $this->question = htmlentities($question);
  27. }
  28.  
  29. public function setChoices(array $choices) {
  30. foreach ($choices as $choice) {
  31. if (!$choice instanceof Model_Choice) {
  32. throw new InvalidArgument("Choices must be instances of Model_Choice");
  33. }
  34. }
  35. }
  36.  
  37. public function addChoice($choice) {
  38. if (!$choice instanceof Model_Choice) {
  39. if (is_int($choice)) {
  40. $choice = Model_Choice::fetch($choice);
  41. if (!$choice) {
  42. throw new InvalidArgument("Supply choice as an instance of Model_Choice, a choice id or a string");
  43. }
  44. } else {
  45. $choice = new Model_Choice(array(
  46. "label" => $choice,
  47. "post" => $this
  48. ));
  49. }
  50. }
  51.  
  52. $this->choices[] = $choice;
  53. }
  54.  
  55. public function setAsker($user) {
  56. if (!$user instanceof Model_User) {
  57. $user = Model_User::fetch($user);
  58. if (!$user) {
  59. throw new InvalidArgument("Supply asker as an instance of Model_User or a user id");
  60. }
  61. }
  62.  
  63. $this->asker = $user;
  64. }
  65.  
  66. public function setAsked($when) {
  67. if (!is_int($when)) {
  68. $when = strtotime($when);
  69. if (!$when) {
  70. throw new InvalidArgument("Supply asked as a timestamp or valid date string accepted by strtotime");
  71. }
  72. }
  73.  
  74. $this->asked = $when;
  75. }
  76.  
  77. public function getAsked() {
  78. if ($this->asked == null) {
  79. $this->asked = time();
  80. }
  81.  
  82. return $this->asked;
  83. }
  84.  
  85. public function setVisible($visible) {
  86. $this->visible = (bool) $visible;
  87. }
  88.  
  89. public function save() {
  90. $values = array(
  91. ":id" => $this->id,
  92. ":question" => $this->question,
  93. ":asker" => $this->asker->id,
  94. ":asked" => date("Y-m-d H:i:s", $this->asked),
  95. ":visible" => $this->visible
  96. );
  97.  
  98. if ($this->id) {
  99. $sql = "INSERT INTO question (question, asker, asked, visible) " .
  100. "VALUES (question, :asker, :asked, :visible)";
  101. unset($values[":id"]);
  102. } else {
  103. $sql = "UPDATE question " .
  104. "SET question = :question, asker = :asker, asked = :asked, visible = :visible " .
  105. "WHERE id = :id LIMIT 1";
  106. }
  107.  
  108. // @todo Move to WebApp_Model::save() and call parent::save()
  109. $stmt = parent::getDb()->prepare($sql);
  110. $success = $stmt->execute($values);
  111. $stmt->closeCursor();
  112.  
  113. // Check for an error
  114. // @todo Also move, base 'question' off the class name
  115. if (!$success) {
  116. $action = isset($id) ? "update" : "insert";
  117. throw new DataError("Could not $action the question", parent::getDb()->errorInfo());
  118. }
  119.  
  120. // Set the new id
  121. $this->id = $id;
  122.  
  123. // Save any choices that have been added
  124. foreach ($this->choices as $choice) {
  125. $choice->save();
  126. }
  127.  
  128. // @todo Depends on whether askers can be anonymous, change later
  129. //$this->asker->save();
  130. }
  131. }
Add Comment
Please, Sign In to add comment