Poll.php 5.4 KB

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