1
0
mirror of https://framagit.org/tykayn/date-poll-api synced 2023-08-25 08:23:11 +02:00

enrich stats for choices

This commit is contained in:
Tykayn 2021-04-27 11:11:34 +02:00 committed by tykayn
parent 6831d8d1ae
commit 665cf6d173

View File

@ -10,7 +10,6 @@ use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use ErrorException;
use JMS\Serializer\Annotation as Serializer;
use RangeException;
/**
* @ORM\Entity(repositoryClass="App\Repository\PollRepository")
@ -196,503 +195,501 @@ class Poll {
public function __construct() {
$this->initiate();
$this->votes = new ArrayCollection();
$this->stacksOfVotes = new ArrayCollection();
$this->choices = new ArrayCollection();
$this->comments = new ArrayCollection();
}
$this->initiate();
$this->votes = new ArrayCollection();
$this->stacksOfVotes = new ArrayCollection();
$this->choices = new ArrayCollection();
$this->comments = new ArrayCollection();
}
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' ] );
}
$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' ] );
}
public function displayForAdmin() {
$content = $this->display();
$content[ 'owner' ] = $this->getOwner()->displayForAdmin();
$content[ 'admin_key' ] = $this->getAdminKey();
$content[ 'password_hash' ] = $this->getPassword();
$content[ 'id' ] = $this->getId();
$content = $this->display();
$content[ 'owner' ] = $this->getOwner()->displayForAdmin();
$content[ 'admin_key' ] = $this->getAdminKey();
$content[ 'password_hash' ] = $this->getPassword();
$content[ 'id' ] = $this->getId();
return $content;
}
return $content;
}
public function display() {
$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();
}
$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,
'choices_stats' => $computedAnswers[ 'answersWithStats' ],
'stacks' => $displayedStackOfVotes,
'comments' => $displayedComments,
];
}
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
public function computeAnswers() {
// counts each number of answer for this choice
$computedArray = [];
$maxScore = 0;
$computedArray = [];
$maxScore = 0;
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();
$scoreInfos = ['score' => 0,
'yes' => [
'count' => 0,
'people' => [],
],
'maybe' => [
'count' => 0,
'people' => [],
],
'no' => [
'count' => 0,
'people' => [],
]];
if ( ! isset( $computedArray[ $choice_id ] ) ) {
$computedArray[ $choice_id ] = [
'id' => $choice_id,
'url' => $choice_url,
'name' => $vote->getChoice()->getName(),
'score' => 0,
'yes' => [
'count' => 0,
'people' => [],
],
'maybe' => [
'count' => 0,
'people' => [],
],
'no' => [
'count' => 0,
'people' => [],
],
];
}
$computedArray[ $choice_id ][ $answer ][ 'count' ] ++;
$computedArray[ $choice_id ][ $answer ][ 'people' ][] = $stack_of_vote->getOwner()->getPseudo();
// 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 ( $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' ];
}
}
}
$answersWithStats = [];
foreach ( $computedArray as $choice_stat ) {
$answersWithStats[] = $choice_stat;
}
if ( ! isset( $computedArray[ $choice_id ] ) ) {
$computedArray[ $choice_id ] = [
'id' => $choice_id,
'url' => $choice_url,
'name' => $vote->getChoice()->getName(),
return [
'answers' => $computedArray,
'answersWithStats' => $answersWithStats,
'max_score' => $maxScore,
];
}
];
}
$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,
];
}
public function getStacksOfVotes() {
return $this->stacksOfVotes;
}
return $this->stacksOfVotes;
}
public function setStacksOfVotes( ?StackOfVotes $stacksOfVotes ): self {
$this->stacksOfVotes = $stacksOfVotes;
$this->stacksOfVotes = $stacksOfVotes;
return $this;
}
return $this;
}
/**
* @return Collection|Choice[]
*/
public function getChoices(): Collection {
return $this->choices;
}
return $this->choices;
}
/**
* @return Collection|Comment[]
*/
public function getComments(): Collection {
return $this->comments;
}
return $this->comments;
}
public function getTitle(): ?string {
return $this->title;
}
return $this->title;
}
public function setTitle( string $title ): self {
$this->title = $title;
$this->title = $title;
return $this;
}
return $this;
}
public function getDescription(): ?string {
return $this->description;
}
return $this->description;
}
public function setDescription( string $description ): self {
$this->description = $description;
$this->description = $description;
return $this;
}
return $this;
}
public function getCreationDate(): ?DateTimeInterface {
return $this->creationDate;
}
return $this->creationDate;
}
public function setCreationDate( DateTimeInterface $creationDate ): self {
$this->creationDate = $creationDate;
$this->creationDate = $creationDate;
return $this;
}
return $this;
}
public function getExpiracyDate(): ?DateTimeInterface {
return $this->expiracyDate;
}
return $this->expiracyDate;
}
public function setExpiracyDate( DateTimeInterface $expiracyDate ): self {
$this->expiracyDate = $expiracyDate;
$this->expiracyDate = $expiracyDate;
return $this;
}
return $this;
}
public function getVotesMax() {
return $this->votesMax;
}
return $this->votesMax;
}
public function setVotesMax( $votesMax ): self {
$this->votesMax = $votesMax;
$this->votesMax = $votesMax;
return $this;
}
return $this;
}
public function getChoicesMax() {
return $this->choicesMax;
}
return $this->choicesMax;
}
public function setChoicesMax( $choicesMax ): self {
$this->choicesMax = $choicesMax;
$this->choicesMax = $choicesMax;
return $this;
}
return $this;
}
public function getKind(): ?string {
return $this->kind;
}
return $this->kind;
}
public function setKind( string $kind ): self {
$this->kind = $kind;
$this->kind = $kind;
return $this;
}
return $this;
}
public function getAllowedAnswers(): ?array {
return $this->allowedAnswers;
}
return $this->allowedAnswers;
}
public function setAllowedAnswers( array $allowedAnswers ): self {
$this->allowedAnswers = $allowedAnswers;
$this->allowedAnswers = $allowedAnswers;
return $this;
}
return $this;
}
public function getVotesAllowed(): ?bool {
return $this->votesAllowed;
}
return $this->votesAllowed;
}
public function setVotesAllowed( ?bool $votesAllowed ): self {
$this->votesAllowed = $votesAllowed;
$this->votesAllowed = $votesAllowed;
return $this;
}
return $this;
}
public function getModificationPolicy(): ?string {
return $this->modificationPolicy;
}
return $this->modificationPolicy;
}
public function setModificationPolicy( string $modificationPolicy ): self {
$this->modificationPolicy = $modificationPolicy;
$this->modificationPolicy = $modificationPolicy;
return $this;
}
return $this;
}
public function getHideResults(): ?bool {
return $this->hideResults;
}
return $this->hideResults;
}
public function setHideResults( bool $hideResults ): self {
$this->hideResults = $hideResults;
$this->hideResults = $hideResults;
return $this;
}
return $this;
}
public function getShowResultEvenIfPasswords(): ?bool {
return $this->showResultEvenIfPasswords;
}
return $this->showResultEvenIfPasswords;
}
public function setShowResultEvenIfPasswords( bool $showResultEvenIfPasswords ): self {
$this->showResultEvenIfPasswords = $showResultEvenIfPasswords;
$this->showResultEvenIfPasswords = $showResultEvenIfPasswords;
return $this;
}
return $this;
}
public function getOwner(): ?Owner {
return $this->owner;
}
return $this->owner;
}
public function setOwner( ?Owner $owner ): self {
$this->owner = $owner;
$this->owner = $owner;
return $this;
}
return $this;
}
public function getPassword(): ?string {
return $this->password;
}
return $this->password;
}
public function setPassword( string $password ): self {
$this->password = md5( $password );
$this->password = md5( $password );
return $this;
}
return $this;
}
public function getAdminKey(): ?string {
return $this->adminKey;
}
return $this->adminKey;
}
public function setAdminKey( string $adminKey ): self {
$this->adminKey = $adminKey;
$this->adminKey = $adminKey;
return $this;
}
return $this;
}
public function getId(): ?int {
return $this->id;
}
return $this->id;
}
public function findChoiceById( int $id ) {
$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;
}
$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;
}
return null;
}
/**
* @return Collection|Vote[]
*/
public function getVotes(): Collection {
return $this->votes;
}
return $this->votes;
}
public function getCustomUrl(): ?string {
return $this->customUrl;
}
return $this->customUrl;
}
public function setCustomUrl( string $customUrl ): self {
$this->customUrl = $customUrl;
$this->customUrl = $customUrl;
return $this;
}
return $this;
}
public function getMailOnComment(): ?bool {
return $this->mailOnComment;
}
return $this->mailOnComment;
}
public function setMailOnComment( bool $mailOnComment ): self {
$this->mailOnComment = $mailOnComment;
$this->mailOnComment = $mailOnComment;
return $this;
}
return $this;
}
public function getMailOnVote(): ?bool {
return $this->mailOnVote;
}
return $this->mailOnVote;
}
public function setMailOnVote( bool $mailOnVote ): self {
$this->mailOnVote = $mailOnVote;
$this->mailOnVote = $mailOnVote;
return $this;
}
return $this;
}
public function addComment( Comment $comment ): self {
if ( ! $this->comments->contains( $comment ) ) {
$this->comments[] = $comment;
$comment->setPoll( $this );
}
if ( ! $this->comments->contains( $comment ) ) {
$this->comments[] = $comment;
$comment->setPoll( $this );
}
return $this;
}
return $this;
}
public function removeComment( Comment $comment ): self {
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 );
}
}
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;
}
return $this;
}
public function addStackOfVote( StackOfVotes $stackOfVote ): self {
if ( ! $this->stacksOfVotes->contains( $stackOfVote ) ) {
$this->stacksOfVotes[] = $stackOfVote;
$stackOfVote->setPoll( $this );
}
if ( ! $this->stacksOfVotes->contains( $stackOfVote ) ) {
$this->stacksOfVotes[] = $stackOfVote;
$stackOfVote->setPoll( $this );
}
return $this;
}
return $this;
}
public function removeStackOfVote( StackOfVotes $stackOfVote ): self {
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 );
}
}
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;
}
return $this;
}
public function addVote( Vote $vote ): self {
if ( ! $this->votes->contains( $vote ) ) {
$this->votes[] = $vote;
$vote->setPoll( $this );
}
if ( ! $this->votes->contains( $vote ) ) {
$this->votes[] = $vote;
$vote->setPoll( $this );
}
return $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->getPoll() === $this ) {
$vote->setPoll( null );
}
}
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;
}
return $this;
}
public function addTextChoiceArray( array $choiceTextArray ): self {
foreach ( $choiceTextArray as $text ) {
$newChoice = new Choice();
$newChoice->setName( $text );
$this->addChoice( $newChoice );
}
foreach ( $choiceTextArray as $text ) {
$newChoice = new Choice();
$newChoice->setName( $text );
$this->addChoice( $newChoice );
}
return $this;
}
return $this;
}
public function addChoice( Choice $choice ): self {
if ( ! is_null( $this->choices ) ) {
if ( ! $this->choices->contains( $choice ) ) {
$this->choices[] = $choice;
$choice->setPoll( $this );
}
} else {
$this->choices[] = $choice;
$choice->setPoll( $this );
}
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;
}
return $this;
}
public function addStacksOfVote( StackOfVotes $stacksOfVote ): self {
if ( ! $this->stacksOfVotes->contains( $stacksOfVote ) ) {
$this->stacksOfVotes[] = $stacksOfVote;
$stacksOfVote->setPoll( $this );
}
if ( ! $this->stacksOfVotes->contains( $stacksOfVote ) ) {
$this->stacksOfVotes[] = $stacksOfVote;
$stacksOfVote->setPoll( $this );
}
return $this;
}
return $this;
}
public function removeStacksOfVote( StackOfVotes $stacksOfVote ): self {
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 );
}
}
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;
}
return $this;
}
public function removeChoice( Choice $choice ): self {
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 );
}
}
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;
}
return $this;
}
public function getCommentsAllowed(): ?bool {
return $this->commentsAllowed;
}
return $this->commentsAllowed;
}
public function setCommentsAllowed( ?bool $commentsAllowed ): self {
$this->commentsAllowed = $commentsAllowed;
$this->commentsAllowed = $commentsAllowed;
return $this;
}
return $this;
}
}