date-poll-api/src/Entity/Poll.php

696 lines
18 KiB
PHP
Raw Normal View History

2019-10-25 14:59:20 +02:00
<?php
namespace App\Entity;
2019-10-25 14:59:20 +02:00
2021-04-27 10:41:21 +02:00
use App\Traits\RandomTrait;
2021-04-27 10:22:16 +02:00
use DateTime;
use DateTimeInterface;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use ErrorException;
use JMS\Serializer\Annotation as Serializer;
2019-11-11 10:44:19 +01:00
/**
* @ORM\Entity(repositoryClass="App\Repository\PollRepository")
* @Serializer\ExclusionPolicy("all")
*/
class Poll {
2021-04-27 10:41:21 +02:00
use RandomTrait;
2019-11-27 16:14:49 +01:00
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
* @Serializer\Expose()
* @Serializer\Type("integer")
2019-11-27 16:14:49 +01:00
*/
public $id;
/**
* @ORM\Column(type="string", length=255)
* @Serializer\Expose()
* @Serializer\Type("string")
*/
public $title;
/**
2021-04-21 11:47:06 +02:00
* @ORM\Column(type="string", length=255, nullable=false, unique=true)
* @Serializer\Expose()
* @Serializer\Type("string")
*/
public $customUrl;
/**
* @ORM\Column(type="string", length=1000)
* @Serializer\Expose()
* @Serializer\Type("string")
*/
public $description;
/**
* @ORM\Column(type="datetime" , options={"default"="CURRENT_TIMESTAMP"})
* @Serializer\Expose()
*/
public $creationDate;
/**
* @ORM\Column(type="datetime")
* @Serializer\Expose()
*/
public $expiracyDate;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Owner", inversedBy="polls",cascade={"persist"})
* @ORM\JoinColumn(nullable=false)
* @Serializer\Type("App\Entity\Owner")
* @Serializer\Expose()
*/
public $owner;
/**
* kind of poll, 'text' or 'date'
* @ORM\Column(type="string", length=255)
* @Serializer\Type("string")
* @Serializer\Expose()
*/
public $kind = 'text';
/**
* array of possible answer to each choice, by default: "yes" or nothing only.
* could be also "yes", "maybe", "no". extensible to anything
* @ORM\Column(type="array")
* @Serializer\Type("array")
* @Serializer\Expose()
*/
public $allowedAnswers;
/**
* people can add votes
* @ORM\Column(type="boolean", nullable=true)
* @Serializer\Type("boolean")
* @Serializer\Expose()
*/
2021-04-26 13:47:44 +02:00
public $votesAllowed = true;
/**
* max number of stack of votes possible.
* limits the number of people who can answer. as long as you trust the people to give only one answer with a reliable system.
* @ORM\Column(type="smallint", nullable=true)
* @Serializer\Type("smallint")
* @Serializer\Expose()
*/
2021-04-26 13:47:44 +02:00
public $votesMax = 1024;
/**
* max number of choices people can answer in a stack of vote. for text polls only, not date kind.
* by default, people can check as many answers as they want.
* If the question is "check your 3 favourite flavours" and displays 10 flavours, only the first 3 clicked will be taken into account. GUI should be able to make this clear and togglable so that people can change their 3 favourite flavours in their answer.
* @ORM\Column(type="smallint", nullable=true)
* @Serializer\Type("smallint")
* @Serializer\Expose()
*/
2021-04-27 10:22:16 +02:00
public $choicesMax = - 1;
/**
* people can add comments
* @ORM\Column(type="boolean", nullable=true)
* @Serializer\Type("boolean")
* @Serializer\Expose()
*/
2021-04-26 13:47:44 +02:00
public $commentsAllowed = true;
/**
* kind of way the people can modify the poll
* everybody - can modify votes
* self - one can only modify its own vote
* nobody - no one can modify the votes (excepted admin), pray to have it right at first
* @ORM\Column(type="string", length=255)
* @Serializer\Type("string")
* @Serializer\Expose()
*/
2021-04-26 13:47:44 +02:00
public $modificationPolicy = 'everybody';
/**
* send a mail on a new comment
* @ORM\Column(type="boolean", nullable=true)
* @Serializer\Type("boolean")
* @Serializer\Expose()
*/
2021-04-26 13:47:44 +02:00
public $mailOnComment = true;
/**
* send a mail on a new vote
* @ORM\Column(type="boolean", nullable=true)
* @Serializer\Type("boolean")
* @Serializer\Expose()
*/
2021-04-26 13:47:44 +02:00
public $mailOnVote = false;
/**
* hide publicly results
* @ORM\Column(type="boolean", nullable=true)
* @Serializer\Type("boolean")
* @Serializer\Expose()
*/
2021-04-26 13:47:44 +02:00
public $hideResults = false;
/**
* show publicly results even if there is a password to access the vote
* @ORM\Column(type="boolean", nullable=true)
* @Serializer\Type("boolean")
* @Serializer\Expose()
*/
2021-04-26 13:47:44 +02:00
public $showResultEvenIfPasswords = false;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Vote", mappedBy="poll", orphanRemoval=true, cascade={"persist", "remove"})
* @Serializer\Type("ArrayCollection<App\Entity\Vote>")
* @Serializer\Expose()
*/
public $votes;
/**
* @ORM\OneToMany(targetEntity="App\Entity\StackOfVotes", mappedBy="poll", orphanRemoval=true, cascade={"persist", "remove"})
* @Serializer\Expose()
*/
public $stacksOfVotes;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Choice", mappedBy="poll", orphanRemoval=true, cascade={"persist", "remove"})
* @Serializer\Expose()
* @Serializer\Type("ArrayCollection<App\Entity\Choice>")
*/
public $choices;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Comment", mappedBy="poll", orphanRemoval=true, cascade={"persist", "remove"})
* @Serializer\Expose()
* @Serializer\Type("ArrayCollection<App\Entity\Comment>")
*/
public $comments;
2021-04-27 10:22:16 +02:00
/**
* number of days from now for default expiracy date
* @var int
* @Serializer\Expose()
*/
public $defaultExpiracyDaysFromNow = 60;
/**
* vote restricted by a password in md5 format
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $password;
/**
* used to allow administration
* @ORM\Column(type="string", length=255)
* @Serializer\Type("string")
*/
private $adminKey;
private $maxChoicesLimit = 25;
public function __construct() {
2019-11-27 16:14:49 +01:00
2021-04-27 11:11:34 +02:00
$this->initiate();
$this->votes = new ArrayCollection();
$this->stacksOfVotes = new ArrayCollection();
$this->choices = new ArrayCollection();
$this->comments = new ArrayCollection();
}
2021-04-27 10:22:16 +02:00
2021-04-27 11:11:34 +02:00
private function initiate() {
$this->votes = new ArrayCollection();
$this->stacksOfVotes = new ArrayCollection();
$this->choices = new ArrayCollection();
$this->comments = new ArrayCollection();
$this->setAdminKey( $this->generateRandomKey() );
$this->setCreationDate( new DateTime() );
$this->setExpiracyDate( $this->addDaysToDate(
new DateTime(),
$this->defaultExpiracyDaysFromNow
) );
$this->setAllowedAnswers( [ 'yes', 'maybe', 'no' ] );
}
2021-04-27 10:22:16 +02:00
2021-04-27 10:41:21 +02:00
public function displayForAdmin() {
2021-04-27 11:11:34 +02:00
$content = $this->display();
$content[ 'owner' ] = $this->getOwner()->displayForAdmin();
$content[ 'admin_key' ] = $this->getAdminKey();
$content[ 'password_hash' ] = $this->getPassword();
$content[ 'id' ] = $this->getId();
return $content;
}
2021-04-27 10:41:21 +02:00
public function display() {
2019-10-25 14:59:20 +02:00
2021-04-27 11:11:34 +02:00
$computedAnswers = $this->computeAnswers();
$displayedStackOfVotes = [];
foreach ( $this->getStacksOfVotes() as $stack ) {
$displayedStackOfVotes[] = $stack->display();
}
$displayedChoices = [];
foreach ( $this->getChoices() as $choice ) {
$displayedChoices[] = $choice->display();
}
$displayedComments = [];
foreach ( $this->getComments() as $comment ) {
$displayedComments[] = $comment->display();
}
return [
'title' => $this->getTitle(),
'description' => $this->getDescription(),
'created_at' => $this->getCreationDate()->format( 'c' ),
'expiracy_date' => $this->getExpiracyDate()->format( 'c' ),
'votes_max' => $this->getVotesMax(),
'choices_max' => $this->getChoicesMax(),
'kind' => $this->getKind(),
'allowed_answers' => $this->getAllowedAnswers(),
'votes_allowed' => $this->getVotesAllowed(),
'modification_policy' => $this->getModificationPolicy(),
'hide_results' => $this->getHideResults(),
'show_results_even_if_password' => $this->getShowResultEvenIfPasswords(),
'owner' => [
'pseudo' => $this->getOwner()->getPseudo(),
]
,
'password_protected' => $this->getPassword() ? 'yes' : 'no',
'max_score' => $computedAnswers[ 'max_score' ],
'choices' => $displayedChoices,
'stacks' => $displayedStackOfVotes,
'comments' => $displayedComments,
];
}
// counts each number of answer for this choice
2021-04-27 10:22:16 +02:00
public function computeAnswers() {
2021-04-27 11:11:34 +02:00
$computedArray = [];
$maxScore = 0;
$scoreInfos = ['score' => 0,
'yes' => [
'count' => 0,
'people' => [],
],
'maybe' => [
'count' => 0,
'people' => [],
],
'no' => [
'count' => 0,
'people' => [],
]];
// first, prefill all choices
foreach ( $this->getChoices() as $choice ) {
$computedArray[ $choice->getId() ] = array_merge($scoreInfos, $choice->display());
}
// then, compute stack of votes scores on each choice
foreach ( $this->getStacksOfVotes() as $stack_of_vote ) {
foreach ( $stack_of_vote->getVotes() as $vote ) {
$answer = $vote->getValue();
$choice_id = $vote->getChoice()->getId();
$choice_url = $vote->getChoice()->getUrl();
if ( ! isset( $computedArray[ $choice_id ] ) ) {
$computedArray[ $choice_id ] = [
'id' => $choice_id,
'url' => $choice_url,
'name' => $vote->getChoice()->getName(),
];
}
$computedArray[ $choice_id ][ $answer ][ 'count' ] ++;
$computedArray[ $choice_id ][ $answer ][ 'people' ][] = $stack_of_vote->getOwner()->getPseudo();
if ( $answer == 'yes' ) {
$computedArray[ $choice_id ][ 'score' ] += 1;
} elseif ( $answer == 'maybe' ) {
$computedArray[ $choice_id ][ 'score' ] += 0.5;
}
// compare with max value
if ( $computedArray[ $choice_id ][ 'score' ] > $maxScore ) {
$maxScore = $computedArray[ $choice_id ][ 'score' ];
}
}
}
return [
'answers' => $computedArray,
'max_score' => $maxScore,
];
}
2019-11-12 11:26:19 +01:00
2021-04-27 10:22:16 +02:00
public function getStacksOfVotes() {
2021-04-27 11:11:34 +02:00
return $this->stacksOfVotes;
}
2021-04-27 10:22:16 +02:00
public function setStacksOfVotes( ?StackOfVotes $stacksOfVotes ): self {
2021-04-27 11:11:34 +02:00
$this->stacksOfVotes = $stacksOfVotes;
return $this;
}
2021-04-27 10:22:16 +02:00
/**
* @return Collection|Choice[]
*/
public function getChoices(): Collection {
2021-04-27 11:11:34 +02:00
return $this->choices;
}
2021-04-27 10:22:16 +02:00
/**
* @return Collection|Comment[]
*/
public function getComments(): Collection {
2021-04-27 11:11:34 +02:00
return $this->comments;
}
2019-11-12 11:26:19 +01:00
public function getTitle(): ?string {
2021-04-27 11:11:34 +02:00
return $this->title;
}
2019-11-28 14:16:56 +01:00
public function setTitle( string $title ): self {
2021-04-27 11:11:34 +02:00
$this->title = $title;
return $this;
}
2019-11-28 14:16:56 +01:00
public function getDescription(): ?string {
2021-04-27 11:11:34 +02:00
return $this->description;
}
public function setDescription( string $description ): self {
2021-04-27 11:11:34 +02:00
$this->description = $description;
return $this;
}
public function getCreationDate(): ?DateTimeInterface {
2021-04-27 11:11:34 +02:00
return $this->creationDate;
}
2019-10-25 14:59:20 +02:00
public function setCreationDate( DateTimeInterface $creationDate ): self {
2021-04-27 11:11:34 +02:00
$this->creationDate = $creationDate;
return $this;
}
2019-11-28 14:16:56 +01:00
2021-04-27 10:22:16 +02:00
public function getExpiracyDate(): ?DateTimeInterface {
2021-04-27 11:11:34 +02:00
return $this->expiracyDate;
}
2021-04-27 10:22:16 +02:00
public function setExpiracyDate( DateTimeInterface $expiracyDate ): self {
2021-04-27 11:11:34 +02:00
$this->expiracyDate = $expiracyDate;
return $this;
}
2019-11-05 17:22:30 +01:00
2021-04-27 10:22:16 +02:00
public function getVotesMax() {
2021-04-27 11:11:34 +02:00
return $this->votesMax;
}
2019-11-05 17:22:30 +01:00
2021-04-27 10:22:16 +02:00
public function setVotesMax( $votesMax ): self {
2021-04-27 11:11:34 +02:00
$this->votesMax = $votesMax;
return $this;
}
2019-11-28 14:16:56 +01:00
2021-04-27 10:22:16 +02:00
public function getChoicesMax() {
2021-04-27 11:11:34 +02:00
return $this->choicesMax;
}
2019-11-05 17:22:30 +01:00
2021-04-27 10:22:16 +02:00
public function setChoicesMax( $choicesMax ): self {
2021-04-27 11:11:34 +02:00
$this->choicesMax = $choicesMax;
return $this;
}
2019-11-28 14:16:56 +01:00
2021-04-27 10:22:16 +02:00
public function getKind(): ?string {
2021-04-27 11:11:34 +02:00
return $this->kind;
}
2021-04-27 10:22:16 +02:00
public function setKind( string $kind ): self {
2021-04-27 11:11:34 +02:00
$this->kind = $kind;
return $this;
}
2019-11-05 17:22:30 +01:00
2021-04-27 10:22:16 +02:00
public function getAllowedAnswers(): ?array {
2021-04-27 11:11:34 +02:00
return $this->allowedAnswers;
}
2019-11-28 14:16:56 +01:00
2021-04-27 10:22:16 +02:00
public function setAllowedAnswers( array $allowedAnswers ): self {
2021-04-27 11:11:34 +02:00
$this->allowedAnswers = $allowedAnswers;
return $this;
}
2019-11-05 17:22:30 +01:00
2021-04-27 10:22:16 +02:00
public function getVotesAllowed(): ?bool {
2021-04-27 11:11:34 +02:00
return $this->votesAllowed;
}
2019-11-28 14:16:56 +01:00
2021-04-27 10:22:16 +02:00
public function setVotesAllowed( ?bool $votesAllowed ): self {
2021-04-27 11:11:34 +02:00
$this->votesAllowed = $votesAllowed;
return $this;
}
2019-11-05 17:22:30 +01:00
2021-04-27 10:22:16 +02:00
public function getModificationPolicy(): ?string {
2021-04-27 11:11:34 +02:00
return $this->modificationPolicy;
}
2019-11-28 14:16:56 +01:00
2021-04-27 10:22:16 +02:00
public function setModificationPolicy( string $modificationPolicy ): self {
2021-04-27 11:11:34 +02:00
$this->modificationPolicy = $modificationPolicy;
return $this;
}
2021-04-27 10:22:16 +02:00
public function getHideResults(): ?bool {
2021-04-27 11:11:34 +02:00
return $this->hideResults;
}
2021-04-27 10:22:16 +02:00
public function setHideResults( bool $hideResults ): self {
2021-04-27 11:11:34 +02:00
$this->hideResults = $hideResults;
return $this;
}
2021-04-27 10:22:16 +02:00
public function getShowResultEvenIfPasswords(): ?bool {
2021-04-27 11:11:34 +02:00
return $this->showResultEvenIfPasswords;
}
2021-04-27 10:22:16 +02:00
public function setShowResultEvenIfPasswords( bool $showResultEvenIfPasswords ): self {
2021-04-27 11:11:34 +02:00
$this->showResultEvenIfPasswords = $showResultEvenIfPasswords;
return $this;
}
2021-04-27 10:22:16 +02:00
public function getOwner(): ?Owner {
2021-04-27 11:11:34 +02:00
return $this->owner;
}
2021-04-27 10:22:16 +02:00
public function setOwner( ?Owner $owner ): self {
2021-04-27 11:11:34 +02:00
$this->owner = $owner;
return $this;
}
2019-11-05 17:22:30 +01:00
public function getPassword(): ?string {
2021-04-27 11:11:34 +02:00
return $this->password;
}
2019-11-28 14:16:56 +01:00
public function setPassword( string $password ): self {
2021-04-27 11:11:34 +02:00
$this->password = md5( $password );
return $this;
}
2019-11-05 17:22:30 +01:00
2021-04-27 10:22:16 +02:00
public function getAdminKey(): ?string {
2021-04-27 11:11:34 +02:00
return $this->adminKey;
}
2019-11-28 14:16:56 +01:00
2021-04-27 10:22:16 +02:00
public function setAdminKey( string $adminKey ): self {
2021-04-27 11:11:34 +02:00
$this->adminKey = $adminKey;
return $this;
}
2019-11-05 17:22:30 +01:00
2021-04-27 10:22:16 +02:00
public function getId(): ?int {
2021-04-27 11:11:34 +02:00
return $this->id;
}
2019-11-28 14:16:56 +01:00
2021-04-27 10:22:16 +02:00
public function findChoiceById( int $id ) {
2021-04-27 11:11:34 +02:00
$choices = $this->getChoices();
$counter = 0;
// there must be something cleaner than this in Doctrine ArrayCollection
foreach ( $choices as $choice ) {
$counter ++;
if ( $counter > $this->maxChoicesLimit ) {
throw new ErrorException( "max number of choices reached for this poll" );
}
if ( $choice && $choice->getId() == $id ) {
return $choice;
}
}
return null;
}
2019-11-05 17:22:30 +01:00
2021-04-27 10:22:16 +02:00
/**
* @return Collection|Vote[]
*/
public function getVotes(): Collection {
2021-04-27 11:11:34 +02:00
return $this->votes;
}
2019-11-28 14:16:56 +01:00
2021-04-27 10:22:16 +02:00
public function getCustomUrl(): ?string {
2021-04-27 11:11:34 +02:00
return $this->customUrl;
}
2019-11-28 14:16:56 +01:00
2021-04-27 10:22:16 +02:00
public function setCustomUrl( string $customUrl ): self {
2021-04-27 11:11:34 +02:00
$this->customUrl = $customUrl;
return $this;
}
2019-11-06 14:54:04 +01:00
2021-04-27 10:22:16 +02:00
public function getMailOnComment(): ?bool {
2021-04-27 11:11:34 +02:00
return $this->mailOnComment;
}
2019-11-06 14:54:04 +01:00
2021-04-27 10:22:16 +02:00
public function setMailOnComment( bool $mailOnComment ): self {
2021-04-27 11:11:34 +02:00
$this->mailOnComment = $mailOnComment;
return $this;
}
2019-11-06 14:54:04 +01:00
2021-04-27 10:22:16 +02:00
public function getMailOnVote(): ?bool {
2021-04-27 11:11:34 +02:00
return $this->mailOnVote;
}
2019-11-28 14:16:56 +01:00
2021-04-27 10:22:16 +02:00
public function setMailOnVote( bool $mailOnVote ): self {
2021-04-27 11:11:34 +02:00
$this->mailOnVote = $mailOnVote;
return $this;
}
2019-11-28 11:51:25 +01:00
public function addComment( Comment $comment ): self {
2021-04-27 11:11:34 +02:00
if ( ! $this->comments->contains( $comment ) ) {
$this->comments[] = $comment;
$comment->setPoll( $this );
}
return $this;
}
2019-11-28 14:16:56 +01:00
public function removeComment( Comment $comment ): self {
2021-04-27 11:11:34 +02:00
if ( $this->comments->contains( $comment ) ) {
$this->comments->removeElement( $comment );
// set the owning side to null (unless already changed)
if ( $comment->getPoll() === $this ) {
$comment->setPoll( null );
}
}
return $this;
}
public function addStackOfVote( StackOfVotes $stackOfVote ): self {
2021-04-27 11:11:34 +02:00
if ( ! $this->stacksOfVotes->contains( $stackOfVote ) ) {
$this->stacksOfVotes[] = $stackOfVote;
$stackOfVote->setPoll( $this );
}
return $this;
}
2019-11-12 17:26:56 +01:00
public function removeStackOfVote( StackOfVotes $stackOfVote ): self {
2021-04-27 11:11:34 +02:00
if ( $this->stacksOfVotes->contains( $stackOfVote ) ) {
$this->stacksOfVotes->removeElement( $stackOfVote );
// set the owning side to null (unless already changed)
if ( $stackOfVote->getPoll() === $this ) {
$stackOfVote->setPoll( null );
}
}
return $this;
}
2019-11-12 17:26:56 +01:00
public function addVote( Vote $vote ): self {
2021-04-27 11:11:34 +02:00
if ( ! $this->votes->contains( $vote ) ) {
$this->votes[] = $vote;
$vote->setPoll( $this );
}
return $this;
}
2019-11-27 10:57:06 +01:00
public function removeVote( Vote $vote ): self {
2021-04-27 11:11:34 +02:00
if ( $this->votes->contains( $vote ) ) {
$this->votes->removeElement( $vote );
// set the owning side to null (unless already changed)
if ( $vote->getPoll() === $this ) {
$vote->setPoll( null );
}
}
return $this;
}
2019-11-28 14:16:56 +01:00
public function addTextChoiceArray( array $choiceTextArray ): self {
2021-04-27 11:11:34 +02:00
foreach ( $choiceTextArray as $text ) {
$newChoice = new Choice();
$newChoice->setName( $text );
$this->addChoice( $newChoice );
}
return $this;
}
2019-11-27 10:57:06 +01:00
2021-04-27 10:22:16 +02:00
public function addChoice( Choice $choice ): self {
2021-04-27 11:11:34 +02:00
if ( ! is_null( $this->choices ) ) {
if ( ! $this->choices->contains( $choice ) ) {
$this->choices[] = $choice;
$choice->setPoll( $this );
}
} else {
$this->choices[] = $choice;
$choice->setPoll( $this );
}
return $this;
}
public function addStacksOfVote( StackOfVotes $stacksOfVote ): self {
2021-04-27 11:11:34 +02:00
if ( ! $this->stacksOfVotes->contains( $stacksOfVote ) ) {
$this->stacksOfVotes[] = $stacksOfVote;
$stacksOfVote->setPoll( $this );
}
return $this;
}
public function removeStacksOfVote( StackOfVotes $stacksOfVote ): self {
2021-04-27 11:11:34 +02:00
if ( $this->stacksOfVotes->contains( $stacksOfVote ) ) {
$this->stacksOfVotes->removeElement( $stacksOfVote );
// set the owning side to null (unless already changed)
if ( $stacksOfVote->getPoll() === $this ) {
$stacksOfVote->setPoll( null );
}
}
return $this;
}
public function removeChoice( Choice $choice ): self {
2021-04-27 11:11:34 +02:00
if ( $this->choices->contains( $choice ) ) {
$this->choices->removeElement( $choice );
// set the owning side to null (unless already changed)
if ( $choice->getPoll() === $this ) {
$choice->setPoll( null );
}
}
return $this;
}
public function getCommentsAllowed(): ?bool {
2021-04-27 11:11:34 +02:00
return $this->commentsAllowed;
}
public function setCommentsAllowed( ?bool $commentsAllowed ): self {
2021-04-27 11:11:34 +02:00
$this->commentsAllowed = $commentsAllowed;
return $this;
}
}