| 
									
										
										
										
											2020-10-22 23:13:39 +01:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // {{{ License
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // This file is part of GNU social - https://www.gnu.org/software/social
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // GNU social is free software: you can redistribute it and/or modify
 | 
					
						
							|  |  |  | // it under the terms of the GNU Affero General Public License as published by
 | 
					
						
							|  |  |  | // the Free Software Foundation, either version 3 of the License, or
 | 
					
						
							|  |  |  | // (at your option) any later version.
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // GNU social is distributed in the hope that it will be useful,
 | 
					
						
							|  |  |  | // but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
					
						
							|  |  |  | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
					
						
							|  |  |  | // GNU Affero General Public License for more details.
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // You should have received a copy of the GNU Affero General Public License
 | 
					
						
							|  |  |  | // along with GNU social.  If not, see <http://www.gnu.org/licenses/>.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // }}}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-16 15:57:25 +00:00
										 |  |  | namespace Plugin\Poll\Entity; | 
					
						
							| 
									
										
										
										
											2020-10-22 23:13:39 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-29 18:32:35 +00:00
										 |  |  | use App\Core\DB\DB; | 
					
						
							| 
									
										
										
										
											2020-10-22 23:13:39 +01:00
										 |  |  | use App\Core\Entity; | 
					
						
							|  |  |  | use DateTimeInterface; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-10 02:01:31 +00:00
										 |  |  | /** | 
					
						
							|  |  |  |  * For storing a poll | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * @package  GNUsocial | 
					
						
							|  |  |  |  * @category PollPlugin | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * @author    Daniel Brandao <up201705812@fe.up.pt> | 
					
						
							|  |  |  |  * @copyright 2020 Free Software Foundation, Inc http://www.fsf.org | 
					
						
							|  |  |  |  * @license   https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2020-10-22 23:13:39 +01:00
										 |  |  | class Poll extends Entity | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2020-10-29 18:32:35 +00:00
										 |  |  |     // {{{ Autocode
 | 
					
						
							|  |  |  |     private int $id; | 
					
						
							| 
									
										
										
										
											2020-11-04 18:58:20 +00:00
										 |  |  |     private ?string $uri; | 
					
						
							|  |  |  |     private ?int $gsactor_id; | 
					
						
							| 
									
										
										
										
											2020-11-22 02:36:01 +00:00
										 |  |  |     private int $note_id; | 
					
						
							| 
									
										
										
										
											2020-10-29 18:32:35 +00:00
										 |  |  |     private ?string $question; | 
					
						
							|  |  |  |     private ?string $options; | 
					
						
							| 
									
										
										
										
											2021-04-27 21:24:48 +00:00
										 |  |  |     private \DateTimeInterface $created; | 
					
						
							|  |  |  |     private \DateTimeInterface $modified; | 
					
						
							| 
									
										
										
										
											2020-10-22 23:13:39 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-29 18:32:35 +00:00
										 |  |  |     public function setId(int $id): self | 
					
						
							| 
									
										
										
										
											2020-10-22 23:13:39 +01:00
										 |  |  |     { | 
					
						
							|  |  |  |         $this->id = $id; | 
					
						
							|  |  |  |         return $this; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-29 18:32:35 +00:00
										 |  |  |     public function getId(): int | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return $this->id; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-04 18:58:20 +00:00
										 |  |  |     public function setUri(?string $uri): self | 
					
						
							| 
									
										
										
										
											2020-10-29 18:32:35 +00:00
										 |  |  |     { | 
					
						
							|  |  |  |         $this->uri = $uri; | 
					
						
							|  |  |  |         return $this; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-04 18:58:20 +00:00
										 |  |  |     public function getUri(): ?string | 
					
						
							| 
									
										
										
										
											2020-10-29 18:32:35 +00:00
										 |  |  |     { | 
					
						
							|  |  |  |         return $this->uri; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-15 16:58:13 +00:00
										 |  |  |     public function setGSActorId(?int $gsactor_id): self | 
					
						
							| 
									
										
										
										
											2020-10-29 18:32:35 +00:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2020-11-04 18:58:20 +00:00
										 |  |  |         $this->gsactor_id = $gsactor_id; | 
					
						
							| 
									
										
										
										
											2020-10-29 18:32:35 +00:00
										 |  |  |         return $this; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-15 16:58:13 +00:00
										 |  |  |     public function getGSActorId(): ?int | 
					
						
							| 
									
										
										
										
											2020-10-29 18:32:35 +00:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2020-11-04 18:58:20 +00:00
										 |  |  |         return $this->gsactor_id; | 
					
						
							| 
									
										
										
										
											2020-10-29 18:32:35 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-22 02:36:01 +00:00
										 |  |  |     public function setNoteId(int $note_id): self | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->note_id = $note_id; | 
					
						
							|  |  |  |         return $this; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function getNoteId(): int | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return $this->note_id; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-29 18:32:35 +00:00
										 |  |  |     public function setQuestion(?string $question): self | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->question = $question; | 
					
						
							|  |  |  |         return $this; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function getQuestion(): ?string | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return $this->question; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function setOptions(?string $options): self | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->options = $options; | 
					
						
							|  |  |  |         return $this; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function getOptions(): ?string | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return $this->options; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-22 23:13:39 +01:00
										 |  |  |     public function setCreated(DateTimeInterface $created): self | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->created = $created; | 
					
						
							|  |  |  |         return $this; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-29 18:32:35 +00:00
										 |  |  |     public function getCreated(): DateTimeInterface | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return $this->created; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-09 21:25:57 +00:00
										 |  |  |     public function setModified(DateTimeInterface $modified): self | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->modified = $modified; | 
					
						
							|  |  |  |         return $this; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function getModified(): DateTimeInterface | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return $this->modified; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-29 18:32:35 +00:00
										 |  |  |     // }}} Autocode
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-22 23:13:39 +01:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2020-11-09 17:43:10 +00:00
										 |  |  |      * Entity schema definition | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return array schema definition | 
					
						
							| 
									
										
										
										
											2020-10-22 23:13:39 +01:00
										 |  |  |      */ | 
					
						
							|  |  |  |     public static function schemaDef(): array | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return [ | 
					
						
							|  |  |  |             'name'        => 'poll', | 
					
						
							|  |  |  |             'description' => 'Per-notice poll data for Poll plugin', | 
					
						
							|  |  |  |             'fields'      => [ | 
					
						
							| 
									
										
										
										
											2020-11-22 02:36:01 +00:00
										 |  |  |                 'id'         => ['type' => 'serial', 'not null' => true], | 
					
						
							|  |  |  |                 'uri'        => ['type' => 'varchar', 'length' => 191], | 
					
						
							| 
									
										
										
										
											2020-11-09 21:25:57 +00:00
										 |  |  |                 'gsactor_id' => ['type' => 'int'], | 
					
						
							| 
									
										
										
										
											2020-11-22 02:36:01 +00:00
										 |  |  |                 'note_id'    => ['type' => 'int', 'not null' => true], | 
					
						
							| 
									
										
										
										
											2020-10-22 23:13:39 +01:00
										 |  |  |                 'question'   => ['type' => 'text'], | 
					
						
							|  |  |  |                 'options'    => ['type' => 'text'], | 
					
						
							| 
									
										
										
										
											2020-11-09 21:25:57 +00:00
										 |  |  |                 'created'    => ['type' => 'datetime',  'not null' => true, 'default' => 'CURRENT_TIMESTAMP', 'description' => 'date this record was created'], | 
					
						
							|  |  |  |                 'modified'   => ['type' => 'timestamp', 'not null' => true, 'default' => 'CURRENT_TIMESTAMP', 'description' => 'date this record was modified'], | 
					
						
							| 
									
										
										
										
											2020-10-22 23:13:39 +01:00
										 |  |  |             ], | 
					
						
							|  |  |  |             'primary key' => ['id'], | 
					
						
							| 
									
										
										
										
											2020-11-22 02:36:01 +00:00
										 |  |  |             'unique keys' => [ | 
					
						
							|  |  |  |                 'poll_note_id' => ['note_id'], | 
					
						
							|  |  |  |             ], | 
					
						
							| 
									
										
										
										
											2020-10-22 23:13:39 +01:00
										 |  |  |         ]; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-09 17:43:10 +00:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Gets options in array format | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return array of options | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2020-11-04 19:43:36 +00:00
										 |  |  |     public function getOptionsArr(): array | 
					
						
							| 
									
										
										
										
											2020-11-04 18:58:20 +00:00
										 |  |  |     { | 
					
						
							|  |  |  |         return explode("\n", $this->options); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-22 23:13:39 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Is this a valid selection index? | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param int $selection (1-based) | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return bool | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2020-11-09 21:25:57 +00:00
										 |  |  |     public function isValidSelection(int $selection): bool | 
					
						
							| 
									
										
										
										
											2020-10-22 23:13:39 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2020-11-04 19:43:36 +00:00
										 |  |  |         if ($selection < 1 || $selection > count($this->getOptionsArr())) { | 
					
						
							| 
									
										
										
										
											2020-10-22 23:13:39 +01:00
										 |  |  |             return false; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return true; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-11-04 19:43:36 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-09 17:43:10 +00:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Counts responses from each option from a poll object, stores them into an array | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return array with question and num of responses | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2020-11-08 16:36:06 +00:00
										 |  |  |     public function countResponses(): array | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $responses = []; | 
					
						
							|  |  |  |         $options   = $this->getOptionsArr(); | 
					
						
							| 
									
										
										
										
											2020-11-09 17:43:10 +00:00
										 |  |  |         for ($i = 0; $i < count($options); ++$i) { | 
					
						
							| 
									
										
										
										
											2020-11-09 21:25:57 +00:00
										 |  |  |             $responses[$options[$i]] = DB::dql('select count(pr) from App\Entity\PollResponse pr ' . | 
					
						
							|  |  |  |                     'where pr.poll_id = :id and pr.selection = :selection', | 
					
						
							|  |  |  |                 ['id' => $this->id, 'selection' => $i + 1])[0][1]; | 
					
						
							| 
									
										
										
										
											2020-11-08 16:36:06 +00:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $responses; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-10-22 23:13:39 +01:00
										 |  |  | } |