PollResponse.php 5.0 KB

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