Poll.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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
  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 Poll extends Entity
  34. {
  35. // {{{ Autocode
  36. // @codeCoverageIgnoreStart
  37. private int $id;
  38. private ?string $uri = null;
  39. private ?int $actor_id = null;
  40. private int $note_id;
  41. private ?string $question = null;
  42. private ?string $options = null;
  43. private DateTimeInterface $created;
  44. private DateTimeInterface $modified;
  45. public function setId(int $id): self
  46. {
  47. $this->id = $id;
  48. return $this;
  49. }
  50. public function getId(): int
  51. {
  52. return $this->id;
  53. }
  54. public function setUri(?string $uri): self
  55. {
  56. $this->uri = \is_null($uri) ? null : mb_substr($uri, 0, 191);
  57. return $this;
  58. }
  59. public function getUri(): ?string
  60. {
  61. return $this->uri;
  62. }
  63. public function setActorId(?int $actor_id): self
  64. {
  65. $this->actor_id = $actor_id;
  66. return $this;
  67. }
  68. public function getActorId(): ?int
  69. {
  70. return $this->actor_id;
  71. }
  72. public function setNoteId(int $note_id): self
  73. {
  74. $this->note_id = $note_id;
  75. return $this;
  76. }
  77. public function getNoteId(): int
  78. {
  79. return $this->note_id;
  80. }
  81. public function setQuestion(?string $question): self
  82. {
  83. $this->question = $question;
  84. return $this;
  85. }
  86. public function getQuestion(): ?string
  87. {
  88. return $this->question;
  89. }
  90. public function setOptions(?string $options): self
  91. {
  92. $this->options = $options;
  93. return $this;
  94. }
  95. public function getOptions(): ?string
  96. {
  97. return $this->options;
  98. }
  99. public function setCreated(DateTimeInterface $created): self
  100. {
  101. $this->created = $created;
  102. return $this;
  103. }
  104. public function getCreated(): DateTimeInterface
  105. {
  106. return $this->created;
  107. }
  108. public function setModified(DateTimeInterface $modified): self
  109. {
  110. $this->modified = $modified;
  111. return $this;
  112. }
  113. public function getModified(): DateTimeInterface
  114. {
  115. return $this->modified;
  116. }
  117. // @codeCoverageIgnoreEnd
  118. // }}} Autocode
  119. /**
  120. * Entity schema definition
  121. *
  122. * @return array schema definition
  123. */
  124. public static function schemaDef(): array
  125. {
  126. return [
  127. 'name' => 'poll',
  128. 'description' => 'Per-notice poll data for Poll plugin',
  129. 'fields' => [
  130. 'id' => ['type' => 'serial', 'not null' => true],
  131. 'uri' => ['type' => 'varchar', 'length' => 191],
  132. 'actor_id' => ['type' => 'int'],
  133. 'note_id' => ['type' => 'int', 'not null' => true],
  134. 'question' => ['type' => 'text'],
  135. 'options' => ['type' => 'text'],
  136. 'created' => ['type' => 'datetime', 'not null' => true, 'default' => 'CURRENT_TIMESTAMP', 'description' => 'date this record was created'],
  137. 'modified' => ['type' => 'timestamp', 'not null' => true, 'default' => 'CURRENT_TIMESTAMP', 'description' => 'date this record was modified'],
  138. ],
  139. 'primary key' => ['id'],
  140. 'unique keys' => [
  141. 'poll_note_id' => ['note_id'],
  142. ],
  143. ];
  144. }
  145. /**
  146. * Gets options in array format
  147. *
  148. * @return array of options
  149. */
  150. public function getOptionsArr(): array
  151. {
  152. return explode("\n", $this->options);
  153. }
  154. /**
  155. * Is this a valid selection index?
  156. *
  157. * @param int $selection (1-based)
  158. */
  159. public function isValidSelection(int $selection): bool
  160. {
  161. return !($selection < 1 || $selection > \count($this->getOptionsArr()));
  162. }
  163. /**
  164. * Counts responses from each option from a poll object, stores them into an array
  165. *
  166. * @return array with question and num of responses
  167. */
  168. public function countResponses(): array
  169. {
  170. $responses = [];
  171. $options = $this->getOptionsArr();
  172. for ($i = 0; $i < \count($options); ++$i) {
  173. $responses[$options[$i]] = DB::dql(
  174. 'select count(pr) from App\Entity\PollResponse pr '
  175. . 'where pr.poll_id = :id and pr.selection = :selection',
  176. ['id' => $this->id, 'selection' => $i + 1],
  177. )[0][1];
  178. }
  179. return $responses;
  180. }
  181. }