Poll.php 5.3 KB

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