diff --git a/src/DataFixtures/AppPollFixtures.php b/src/DataFixtures/AppPollFixtures.php index 6fcfef6..73ffeeb 100644 --- a/src/DataFixtures/AppPollFixtures.php +++ b/src/DataFixtures/AppPollFixtures.php @@ -54,7 +54,6 @@ class AppPollFixtures extends Fixture { ->addChoice( $choiceB ); $manager->persist( $poll ); - $stack1 = new StackOfVotes(); $stack1 ->setPoll( $poll ) @@ -72,10 +71,7 @@ class AppPollFixtures extends Fixture { ->setStacksOfVotes( $stack1 ) ->setValue( "yes" ) ->setChoice( $choiceB ); - $stack1->setPseudo( 'chuck norris' ); - - $manager->persist( $stack1 ); // voter guy votes again with an other pseudo diff --git a/src/DataFixtures/CommentFixtures.php b/src/DataFixtures/CommentFixtures.php new file mode 100644 index 0000000..64d0055 --- /dev/null +++ b/src/DataFixtures/CommentFixtures.php @@ -0,0 +1,76 @@ +getRepository( Poll::class ); + + $commenterMan = new Owner(); + $commenterMan->setEmail( 'tktest_commentateur_other@tktest.com' ) + ->setPseudo( 'Bill Murray' ); + $commenterMan2 = new Owner(); + $commenterMan2->setEmail( 'wulfila@tktest.com' ) + ->setPseudo( 'Wulfila' ); + + // comment on "citron ou orange" + $pollCitronOrange = $emPoll->find( 1 ); + $comment = new Comment(); + $comment->setOwner( $commenterMan ) + ->setText( 'quelle indécision wololo! finalement citron. heu non orange. AAAAh!' ); + $pollCitronOrange->addComment( $comment ); + $manager->persist( $comment ); + $manager->persist( $pollCitronOrange ); + $manager->persist( $commenterMan ); + $manager->flush(); + + // comment on "démo sondage de texte avec deux commentaires" + $poll = $emPoll->find( 2 ); + $comment = new Comment(); + $comment->setOwner( $commenterMan2 ) + ->setText( 'il est écrit Squalala.' ); + $pollCitronOrange->addComment( $comment ); + $manager->persist( $comment ); + $manager->persist( $poll ); + $manager->persist( $commenterMan2 ); + $manager->flush(); + + $comment = new Comment(); + $comment->setOwner( $commenterMan ) + ->setText( "Zelda. Orange." ); + $pollCitronOrange->addComment( $comment ); + $manager->persist( $comment ); + $manager->persist( $poll ); + $manager->persist( $commenterMan ); + $manager->flush(); + + + // comment on "c'est pour aujourdhui ou pour demain" + $poll = $emPoll->find( 3 ); + $comment = new Comment(); + $comment->setOwner( $commenterMan ) + ->setText( "va pour demain" ); + $pollCitronOrange->addComment( $comment ); + $manager->persist( $comment ); + $manager->persist( $poll ); + $manager->persist( $commenterMan ); + $manager->flush(); + + // comment on "dessin animé préféré" + $poll = $emPoll->find( 4 ); + $comment = new Comment(); + $comment->setOwner( $commenterMan2 ) + ->setText( "Ceci est un commentaire de fixture créé avec le CipherBliss poweur." ); + $pollCitronOrange->addComment( $comment ); + $manager->persist( $comment ); + $manager->persist( $poll ); + $manager->persist( $commenterMan2 ); + $manager->flush(); + } +} diff --git a/src/DataFixtures/VotesStacksFixtures.php b/src/DataFixtures/VotesStacksFixtures.php new file mode 100644 index 0000000..2fb35d5 --- /dev/null +++ b/src/DataFixtures/VotesStacksFixtures.php @@ -0,0 +1,148 @@ +getRepository( Poll::class ); + + $people1 = new Owner(); + $people1->setEmail( 'tktest_nikolas_edison@tktest.com' ) + ->setPseudo( 'Nikolas Edison' ); + $people2 = new Owner(); + $people2->setEmail( 'wulfila@tktest.com' ) + ->setPseudo( 'Wulfila' ); + $people3 = new Owner(); + $people3->setEmail( 'billie_jean@tktest.com' ) + ->setPseudo( 'Billie Jean' ); + + // "citron ou orange" + $poll = $emPoll->find( 1 ); + $stack1 = new StackOfVotes(); + $stack1 + ->setPoll( $poll ) + ->setOwner( $people1 ); + $voteA = new Vote(); + $voteA + ->setPoll( $poll ) + ->setStacksOfVotes( $stack1 ) + ->setValue( "yes" ) + ->setChoice( $poll->getChoices()[ 0 ] ); + $voteB = new Vote(); + $voteB + ->setPoll( $poll ) + ->setStacksOfVotes( $stack1 ) + ->setValue( "maybe" ) + ->setChoice( $poll->getChoices()[ 1 ] ); + $poll->addStackOfVote( $stack1 ); + $manager->persist( $poll ); + $manager->persist( $people1 ); + $manager->persist( $stack1 ); + + $stack2 = new StackOfVotes(); + $stack2 + ->setPoll( $poll ) + ->setOwner( $people2 ); + $voteA = new Vote(); + $voteA + ->setPoll( $poll ) + ->setStacksOfVotes( $stack2 ) + ->setValue( "no" ) + ->setChoice( $poll->getChoices()[ 0 ] ); + $voteB = new Vote(); + $voteB + ->setPoll( $poll ) + ->setStacksOfVotes( $stack2 ) + ->setValue( "yes" ) + ->setChoice( $poll->getChoices()[ 1 ] ); + $poll->addStackOfVote( $stack2 ); + $manager->persist( $poll ); + $manager->persist( $stack2 ); + $manager->persist( $people2 ); + + // comment on "démo sondage de texte avec deux commentaires" + $poll = $emPoll->find( 2 ); + + + // comment on "c'est pour aujourdhui ou pour demain" + $poll = $emPoll->find( 3 ); + + + // comment on "dessin animé préféré" + $poll = $emPoll->find( 4 ); + + $stack1 = new StackOfVotes(); + $stack1 + ->setPoll( $poll ) + ->setOwner( $people1 ); + $voteA = new Vote(); + $voteA + ->setPoll( $poll ) + ->setStacksOfVotes( $stack1 ) + ->setValue( "maybe" ) + ->setChoice( $poll->getChoices()[ 2 ] ); + $voteB = new Vote(); + $voteB + ->setPoll( $poll ) + ->setStacksOfVotes( $stack1 ) + ->setValue( "maybe" ) + ->setChoice( $poll->getChoices()[ 4 ] ); + $poll->addStackOfVote( $stack1 ); + $manager->persist( $poll ); + $manager->persist( $people1 ); + $manager->persist( $stack1 ); + + + $stack2 = new StackOfVotes(); + $stack2 + ->setPoll( $poll ) + ->setOwner( $people2 ); + $voteA = new Vote(); + $voteA + ->setPoll( $poll ) + ->setStacksOfVotes( $stack2 ) + ->setValue( "maybe" ) + ->setChoice( $poll->getChoices()[ 3 ] ); + $voteB = new Vote(); + $voteB + ->setPoll( $poll ) + ->setStacksOfVotes( $stack2 ) + ->setValue( "yes" ) + ->setChoice( $poll->getChoices()[ 5 ] ); + $poll->addStackOfVote( $stack2 ); + $manager->persist( $poll ); + $manager->persist( $people2 ); + $manager->persist( $stack2 ); + + + $stack3 = new StackOfVotes(); + $stack3 + ->setPoll( $poll ) + ->setOwner( $people3 ); + $voteA = new Vote(); + $voteA + ->setPoll( $poll ) + ->setStacksOfVotes( $stack3 ) + ->setValue( "yes" ) + ->setChoice( $poll->getChoices()[ 1 ] ); + $voteB = new Vote(); + $voteB + ->setPoll( $poll ) + ->setStacksOfVotes( $stack3 ) + ->setValue( "yes" ) + ->setChoice( $poll->getChoices()[ 3 ] ); + $poll->addStackOfVote( $stack3 ); + $manager->persist( $poll ); + $manager->persist( $people3 ); + $manager->persist( $stack3 ); + + $manager->flush(); + } +} diff --git a/src/Entity/StackOfVotes.php b/src/Entity/StackOfVotes.php index 9bb40d6..56fe536 100644 --- a/src/Entity/StackOfVotes.php +++ b/src/Entity/StackOfVotes.php @@ -1,141 +1,149 @@ $this->getId(), - 'pseudo' => '', - 'creation_date' => '', - 'votes' => [], + public function display() { + $tab = [ + 'id' => $this->getId(), + 'pseudo' => '', + 'creation_date' => '', + 'votes' => [], + ]; + // prefill votes with all choices ids + foreach ( $this->getPoll()->getChoices() as $choice ) { + $tab[ 'votes' ][ $choice->getId() ] = [ + 'choice_id' => $choice->getId(), ]; - // prefill votes with all choices ids - foreach ( $this->getPoll()->getChoices() as $choice ) { - $tab[ 'votes' ][ $choice->getId() ] = [ - 'choice_id' => $choice->getId(), - ]; - } - - foreach ( $this->getVotes() as $vote ) { - $tab[ 'votes' ][ $vote->getChoice()->getId() ] = [ - 'id' => $this->getId(), - 'vote_id' => $vote->getId(), - 'value' => $vote->getValue(), - 'choice_id' => $vote->getChoice()->getId(), - 'text' => $vote->getChoice()->getName(), - ]; - $tab[ 'pseudo' ] = $this->getOwner()->getPseudo(); - $tab[ 'creation_date' ] = $vote->getCreationDate(); - } - - return $tab; } - public function __construct() { - $this->votes = new ArrayCollection(); + foreach ( $this->getVotes() as $vote ) { + $tab[ 'votes' ][ $vote->getChoice()->getId() ] = [ + 'id' => $this->getId(), + 'vote_id' => $vote->getId(), + 'value' => $vote->getValue(), + 'choice_id' => $vote->getChoice()->getId(), + 'text' => $vote->getChoice()->getName(), + ]; + $tab[ 'pseudo' ] = $this->getOwner()->getPseudo(); + $tab[ 'creation_date' ] = $vote->getCreationDate(); } - public function getId(): ?int { - return $this->id; - } - - /** - * @return Collection|poll[] - */ - public function getVotes(): Collection { - return $this->votes; - } - - public function addVote( Vote $vote ): self { - if ( ! $this->votes->contains( $vote ) ) { - $vote->setPoll( $this->getPoll() ); - - $this->votes[] = $vote; - $vote->setStacksOfVotes( $this ); - } - - return $this; - } - - public function removeVote( Vote $vote ): self { - if ( $this->votes->contains( $vote ) ) { - $this->votes->removeElement( $vote ); - // set the owning side to null (unless already changed) - if ( $vote->getStacksOfVotes() === $this ) { - $vote->setStacksOfVotes( null ); - } - } - - return $this; - } - - public function getPseudo(): ?string { - return $this->pseudo; - } - - public function setPseudo( ?string $pseudo ): self { - $this->pseudo = $pseudo; - - return $this; - } - - public function getOwner(): ?Owner { - return $this->owner; - } - - public function setOwner( ?Owner $owner ): self { - $this->owner = $owner; - - return $this; - } - - public function getPoll(): ?Poll { - return $this->poll; - } - - public function setPoll( ?Poll $poll ): self { - $this->poll = $poll; - - return $this; - } + return $tab; } + + /** + * @ORM\PrePersist + */ + public function prePersist() { + $this->setPseudo( $this->getOwner()->getPseudo() ); + } + + public function __construct() { + $this->votes = new ArrayCollection(); + } + + public function getId(): ?int { + return $this->id; + } + + /** + * @return Collection|poll[] + */ + public function getVotes(): Collection { + return $this->votes; + } + + public function addVote( Vote $vote ): self { + if ( ! $this->votes->contains( $vote ) ) { + $vote->setPoll( $this->getPoll() ); + + $this->votes[] = $vote; + $vote->setStacksOfVotes( $this ); + } + + return $this; + } + + public function removeVote( Vote $vote ): self { + if ( $this->votes->contains( $vote ) ) { + $this->votes->removeElement( $vote ); + // set the owning side to null (unless already changed) + if ( $vote->getStacksOfVotes() === $this ) { + $vote->setStacksOfVotes( null ); + } + } + + return $this; + } + + public function getPseudo(): ?string { + return $this->pseudo; + } + + public function setPseudo( ?string $pseudo ): self { + $this->pseudo = $pseudo; + + return $this; + } + + public function getOwner(): ?Owner { + return $this->owner; + } + + public function setOwner( ?Owner $owner ): self { + $this->owner = $owner; + + return $this; + } + + public function getPoll(): ?Poll { + return $this->poll; + } + + public function setPoll( ?Poll $poll ): self { + $this->poll = $poll; + + return $this; + } +}