PollResponse.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. // {{{ License
  3. // This file is part of GNU social - https://www.gnu.org/software/social
  4. //
  5. // GNU social is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU Affero General Public License as published by
  7. // the Free Software Foundation, either version 3 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // GNU social is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU Affero General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU Affero General Public License
  16. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  17. // }}}
  18. namespace Plugin\Poll\Entity;
  19. use App\Core\DB\DB;
  20. use App\Core\Entity;
  21. use DateTimeInterface;
  22. /**
  23. * For storing a poll response
  24. *
  25. * @package GNUsocial
  26. * @category PollPlugin
  27. *
  28. * @author Daniel Brandao <up201705812@fe.up.pt>
  29. * @copyright 2020 Free Software Foundation, Inc http://www.fsf.org
  30. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  31. */
  32. class PollResponse extends Entity
  33. {
  34. // {{{ Autocode
  35. // @codeCoverageIgnoreStart
  36. private int $id;
  37. private ?string $uri;
  38. private int $poll_id;
  39. private ?int $gsactor_id;
  40. private ?int $selection;
  41. private \DateTimeInterface $created;
  42. private \DateTimeInterface $modified;
  43. public function setId(int $id): self
  44. {
  45. $this->id = $id;
  46. return $this;
  47. }
  48. public function getId(): int
  49. {
  50. return $this->id;
  51. }
  52. public function setUri(?string $uri): self
  53. {
  54. $this->uri = $uri;
  55. return $this;
  56. }
  57. public function getUri(): ?string
  58. {
  59. return $this->uri;
  60. }
  61. public function setPollId(int $poll_id): self
  62. {
  63. $this->poll_id = $poll_id;
  64. return $this;
  65. }
  66. public function getPollId(): int
  67. {
  68. return $this->poll_id;
  69. }
  70. public function setGSActorId(?int $gsactor_id): self
  71. {
  72. $this->gsactor_id = $gsactor_id;
  73. return $this;
  74. }
  75. public function getGSActorId(): ?int
  76. {
  77. return $this->gsactor_id;
  78. }
  79. public function setSelection(?int $selection): self
  80. {
  81. $this->selection = $selection;
  82. return $this;
  83. }
  84. public function getSelection(): ?int
  85. {
  86. return $this->selection;
  87. }
  88. public function setCreated(DateTimeInterface $created): self
  89. {
  90. $this->created = $created;
  91. return $this;
  92. }
  93. public function getCreated(): DateTimeInterface
  94. {
  95. return $this->created;
  96. }
  97. public function setModified(DateTimeInterface $modified): self
  98. {
  99. $this->modified = $modified;
  100. return $this;
  101. }
  102. public function getModified(): DateTimeInterface
  103. {
  104. return $this->modified;
  105. }
  106. // @codeCoverageIgnoreEnd
  107. // }}} Autocode
  108. /**
  109. * Entity schema definition
  110. *
  111. * @return array schema definition
  112. */
  113. public static function schemaDef()
  114. {
  115. return [
  116. 'name' => 'pollresponse',
  117. 'description' => 'Record of responses to polls',
  118. 'fields' => [
  119. 'id' => ['type' => 'serial', 'not null' => true],
  120. //'uri' => array('type' => 'varchar', 'length' => 191, 'not null' => true, 'description' => 'UUID to the response notice'),
  121. 'uri' => ['type' => 'varchar', 'length' => 191, 'description' => 'UUID to the response notice'],
  122. 'poll_id' => ['type' => 'int', 'length' => 36, 'not null' => true, 'description' => 'UUID of poll being responded to'],
  123. 'gsactor_id' => ['type' => 'int'],
  124. 'selection' => ['type' => 'int'],
  125. 'created' => ['type' => 'datetime', 'not null' => true, 'default' => 'CURRENT_TIMESTAMP', 'description' => 'date this record was created'],
  126. 'modified' => ['type' => 'timestamp', 'not null' => true, 'default' => 'CURRENT_TIMESTAMP', 'description' => 'date this record was modified'],
  127. ],
  128. 'primary key' => ['id'],
  129. 'unique keys' => [
  130. //'poll_uri_key' => array('uri'),
  131. //'poll_response_poll_id_gsactor_id_key' => ['poll_id', 'gsactor_id'], //doctrine bug?
  132. ],
  133. 'foreign keys' => [
  134. 'foreign_poll' => ['poll', ['poll_id' => 'id']],
  135. ],
  136. 'indexes' => [
  137. 'poll_response_gsactor_id_poll_id_index' => ['gsactor_id', 'poll_id'],
  138. ],
  139. ];
  140. }
  141. /**
  142. * Checks if a user already responded to the poll
  143. *
  144. * @param int $pollId
  145. * @param int $gsactorId user
  146. *
  147. * @return bool
  148. */
  149. public static function exits(int $pollId, int $gsactorId): bool
  150. {
  151. $res = DB::dql('select pr from App\Entity\PollResponse pr
  152. where pr.poll_id = :pollId and pr.gsactor_id = :gsactorId',
  153. ['pollId' => $pollId, 'gsactorId' => $gsactorId]);
  154. return count($res) != 0;
  155. }
  156. }