add ip on stack of vote
This commit is contained in:
parent
eaee01838d
commit
6831d8d1ae
|
@ -67,10 +67,11 @@ class VoteController extends EmailsController {
|
||||||
}
|
}
|
||||||
// TODO anti flood
|
// TODO anti flood
|
||||||
$foundOwner
|
$foundOwner
|
||||||
->setModifierToken( $poll->generateAdminKey() );
|
->setModifierToken( $poll->generateRandomKey() );
|
||||||
$stack = new StackOfVotes();
|
$stack = new StackOfVotes();
|
||||||
$stack
|
$stack
|
||||||
->setOwner( $foundOwner )
|
->setOwner( $foundOwner )
|
||||||
|
->setIp($_SERVER['REMOTE_ADDR'])
|
||||||
->setPseudo( $data[ 'pseudo' ] )
|
->setPseudo( $data[ 'pseudo' ] )
|
||||||
->setPoll( $poll );
|
->setPoll( $poll );
|
||||||
foreach ( $data[ 'votes' ] as $voteInfo ) {
|
foreach ( $data[ 'votes' ] as $voteInfo ) {
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
namespace App\Entity;
|
namespace App\Entity;
|
||||||
|
|
||||||
|
use App\Traits\RandomTrait;
|
||||||
use DateTime;
|
use DateTime;
|
||||||
use DateTimeInterface;
|
use DateTimeInterface;
|
||||||
use Doctrine\Common\Collections\ArrayCollection;
|
use Doctrine\Common\Collections\ArrayCollection;
|
||||||
|
@ -16,6 +17,9 @@ use RangeException;
|
||||||
* @Serializer\ExclusionPolicy("all")
|
* @Serializer\ExclusionPolicy("all")
|
||||||
*/
|
*/
|
||||||
class Poll {
|
class Poll {
|
||||||
|
|
||||||
|
use RandomTrait;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @ORM\Id()
|
* @ORM\Id()
|
||||||
* @ORM\GeneratedValue()
|
* @ORM\GeneratedValue()
|
||||||
|
@ -191,540 +195,504 @@ class Poll {
|
||||||
private $maxChoicesLimit = 25;
|
private $maxChoicesLimit = 25;
|
||||||
|
|
||||||
public function __construct() {
|
public function __construct() {
|
||||||
|
|
||||||
$this->initiate();
|
$this->initiate();
|
||||||
}
|
$this->votes = new ArrayCollection();
|
||||||
|
$this->stacksOfVotes = new ArrayCollection();
|
||||||
|
$this->choices = new ArrayCollection();
|
||||||
|
$this->comments = new ArrayCollection();
|
||||||
|
}
|
||||||
|
|
||||||
private function initiate() {
|
private function initiate() {
|
||||||
$this->votes = new ArrayCollection();
|
$this->votes = new ArrayCollection();
|
||||||
$this->stacksOfVotes = new ArrayCollection();
|
$this->stacksOfVotes = new ArrayCollection();
|
||||||
$this->choices = new ArrayCollection();
|
$this->choices = new ArrayCollection();
|
||||||
$this->comments = new ArrayCollection();
|
$this->comments = new ArrayCollection();
|
||||||
$this->setAdminKey( $this->generateAdminKey() );
|
$this->setAdminKey( $this->generateRandomKey() );
|
||||||
$this->setCreationDate( new DateTime() );
|
$this->setCreationDate( new DateTime() );
|
||||||
$this->setExpiracyDate( $this->addDaysToDate(
|
$this->setExpiracyDate( $this->addDaysToDate(
|
||||||
new DateTime(),
|
new DateTime(),
|
||||||
$this->defaultExpiracyDaysFromNow
|
$this->defaultExpiracyDaysFromNow
|
||||||
) );
|
) );
|
||||||
$this->setAllowedAnswers( [ 'yes', 'maybe', 'no' ] );
|
$this->setAllowedAnswers( [ 'yes', 'maybe', 'no' ] );
|
||||||
}
|
}
|
||||||
|
|
||||||
public function generateAdminKey() {
|
|
||||||
$rand = random_int( PHP_INT_MIN, PHP_INT_MAX );
|
|
||||||
|
|
||||||
return str_shuffle( md5( $rand ) . $rand . $this->random_str() );
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Generate a random string, using a cryptographically secure
|
|
||||||
* pseudorandom number generator (random_int)
|
|
||||||
*
|
|
||||||
* This function uses type hints now (PHP 7+ only), but it was originally
|
|
||||||
* written for PHP 5 as well.
|
|
||||||
*
|
|
||||||
* For PHP 7, random_int is a PHP core function
|
|
||||||
* For PHP 5.x, depends on https://github.com/paragonie/random_compat
|
|
||||||
*
|
|
||||||
* @param int $length How many characters do we want?
|
|
||||||
* @param string $keyspace A string of all possible characters
|
|
||||||
* to select from
|
|
||||||
*
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public function random_str(
|
|
||||||
int $length = 64,
|
|
||||||
string $keyspace = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
|
|
||||||
): string {
|
|
||||||
if ( $length < 1 ) {
|
|
||||||
throw new RangeException( "Length must be a positive integer" );
|
|
||||||
}
|
|
||||||
$pieces = [];
|
|
||||||
$max = mb_strlen( $keyspace, '8bit' ) - 1;
|
|
||||||
for ( $i = 0 ; $i < $length ; ++ $i ) {
|
|
||||||
$pieces [] = $keyspace[ random_int( 0, $max ) ];
|
|
||||||
}
|
|
||||||
|
|
||||||
return implode( '', $pieces );
|
|
||||||
}
|
|
||||||
|
|
||||||
public function addDaysToDate( DateTime $date, int $days ) {
|
|
||||||
$st = strtotime( $date->getTimestamp() );
|
|
||||||
|
|
||||||
return new DateTime( $st );
|
|
||||||
}
|
|
||||||
|
|
||||||
public function displayForAdmin() {
|
public function displayForAdmin() {
|
||||||
$content = $this->display();
|
$content = $this->display();
|
||||||
$content[ 'owner' ] = $this->getOwner()->displayForAdmin();
|
$content[ 'owner' ] = $this->getOwner()->displayForAdmin();
|
||||||
$content[ 'admin_key' ] = $this->getAdminKey();
|
$content[ 'admin_key' ] = $this->getAdminKey();
|
||||||
$content[ 'password_hash' ] = $this->getPassword();
|
$content[ 'password_hash' ] = $this->getPassword();
|
||||||
$content[ 'id' ] = $this->getId();
|
$content[ 'id' ] = $this->getId();
|
||||||
|
|
||||||
return $content;
|
return $content;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function display() {
|
public function display() {
|
||||||
|
|
||||||
$computedAnswers = $this->computeAnswers();
|
$computedAnswers = $this->computeAnswers();
|
||||||
$displayedStackOfVotes = [];
|
$displayedStackOfVotes = [];
|
||||||
foreach ( $this->getStacksOfVotes() as $stack ) {
|
foreach ( $this->getStacksOfVotes() as $stack ) {
|
||||||
$displayedStackOfVotes[] = $stack->display();
|
$displayedStackOfVotes[] = $stack->display();
|
||||||
}
|
}
|
||||||
$displayedChoices = [];
|
$displayedChoices = [];
|
||||||
foreach ( $this->getChoices() as $choice ) {
|
foreach ( $this->getChoices() as $choice ) {
|
||||||
$displayedChoices[] = $choice->display();
|
$displayedChoices[] = $choice->display();
|
||||||
}
|
}
|
||||||
$displayedComments = [];
|
$displayedComments = [];
|
||||||
foreach ( $this->getComments() as $comment ) {
|
foreach ( $this->getComments() as $comment ) {
|
||||||
$displayedComments[] = $comment->display();
|
$displayedComments[] = $comment->display();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
return [
|
return [
|
||||||
'title' => $this->getTitle(),
|
'title' => $this->getTitle(),
|
||||||
'description' => $this->getDescription(),
|
'description' => $this->getDescription(),
|
||||||
'created_at' => $this->getCreationDate()->format( 'c' ),
|
'created_at' => $this->getCreationDate()->format( 'c' ),
|
||||||
'expiracy_date' => $this->getExpiracyDate()->format( 'c' ),
|
'expiracy_date' => $this->getExpiracyDate()->format( 'c' ),
|
||||||
'votes_max' => $this->getVotesMax(),
|
'votes_max' => $this->getVotesMax(),
|
||||||
'choices_max' => $this->getChoicesMax(),
|
'choices_max' => $this->getChoicesMax(),
|
||||||
'kind' => $this->getKind(),
|
'kind' => $this->getKind(),
|
||||||
'allowed_answers' => $this->getAllowedAnswers(),
|
'allowed_answers' => $this->getAllowedAnswers(),
|
||||||
'votes_allowed' => $this->getVotesAllowed(),
|
'votes_allowed' => $this->getVotesAllowed(),
|
||||||
'modification_policy' => $this->getModificationPolicy(),
|
'modification_policy' => $this->getModificationPolicy(),
|
||||||
'hide_results' => $this->getHideResults(),
|
'hide_results' => $this->getHideResults(),
|
||||||
'show_results_even_if_password' => $this->getShowResultEvenIfPasswords(),
|
'show_results_even_if_password' => $this->getShowResultEvenIfPasswords(),
|
||||||
'owner' => [
|
'owner' => [
|
||||||
'pseudo' => $this->getOwner()->getPseudo(),
|
'pseudo' => $this->getOwner()->getPseudo(),
|
||||||
]
|
]
|
||||||
,
|
,
|
||||||
'password_protected' => $this->getPassword() ? 'yes' : 'no',
|
'password_protected' => $this->getPassword() ? 'yes' : 'no',
|
||||||
'max_score' => $computedAnswers[ 'max_score' ],
|
'max_score' => $computedAnswers[ 'max_score' ],
|
||||||
'choices' => $displayedChoices,
|
'choices' => $displayedChoices,
|
||||||
'choices_stats' => $computedAnswers[ 'answersWithStats' ],
|
'choices_stats' => $computedAnswers[ 'answersWithStats' ],
|
||||||
'stacks' => $displayedStackOfVotes,
|
'stacks' => $displayedStackOfVotes,
|
||||||
'comments' => $displayedComments,
|
'comments' => $displayedComments,
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
public function computeAnswers() {
|
public function computeAnswers() {
|
||||||
// counts each number of answer for this choice
|
// counts each number of answer for this choice
|
||||||
$computedArray = [];
|
$computedArray = [];
|
||||||
$maxScore = 0;
|
$maxScore = 0;
|
||||||
|
|
||||||
foreach ( $this->getStacksOfVotes() as $stack_of_vote ) {
|
foreach ( $this->getStacksOfVotes() as $stack_of_vote ) {
|
||||||
foreach ( $stack_of_vote->getVotes() as $vote ) {
|
foreach ( $stack_of_vote->getVotes() as $vote ) {
|
||||||
$answer = $vote->getValue();
|
$answer = $vote->getValue();
|
||||||
$choice_id = $vote->getChoice()->getId();
|
$choice_id = $vote->getChoice()->getId();
|
||||||
$choice_url = $vote->getChoice()->getUrl();
|
$choice_url = $vote->getChoice()->getUrl();
|
||||||
|
|
||||||
if ( ! isset( $computedArray[ $choice_id ] ) ) {
|
if ( ! isset( $computedArray[ $choice_id ] ) ) {
|
||||||
$computedArray[ $choice_id ] = [
|
$computedArray[ $choice_id ] = [
|
||||||
'id' => $choice_id,
|
'id' => $choice_id,
|
||||||
'url' => $choice_url,
|
'url' => $choice_url,
|
||||||
'name' => $vote->getChoice()->getName(),
|
'name' => $vote->getChoice()->getName(),
|
||||||
'score' => 0,
|
'score' => 0,
|
||||||
'yes' => [
|
'yes' => [
|
||||||
'count' => 0,
|
'count' => 0,
|
||||||
'people' => [],
|
'people' => [],
|
||||||
],
|
],
|
||||||
'maybe' => [
|
'maybe' => [
|
||||||
'count' => 0,
|
'count' => 0,
|
||||||
'people' => [],
|
'people' => [],
|
||||||
],
|
],
|
||||||
'no' => [
|
'no' => [
|
||||||
'count' => 0,
|
'count' => 0,
|
||||||
'people' => [],
|
'people' => [],
|
||||||
],
|
],
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
$computedArray[ $choice_id ][ $answer ][ 'count' ] ++;
|
$computedArray[ $choice_id ][ $answer ][ 'count' ] ++;
|
||||||
$computedArray[ $choice_id ][ $answer ][ 'people' ][] = $stack_of_vote->getOwner()->getPseudo();
|
$computedArray[ $choice_id ][ $answer ][ 'people' ][] = $stack_of_vote->getOwner()->getPseudo();
|
||||||
|
|
||||||
if ( $answer == 'yes' ) {
|
if ( $answer == 'yes' ) {
|
||||||
$computedArray[ $choice_id ][ 'score' ] += 1;
|
$computedArray[ $choice_id ][ 'score' ] += 1;
|
||||||
} elseif ( $answer == 'maybe' ) {
|
} elseif ( $answer == 'maybe' ) {
|
||||||
$computedArray[ $choice_id ][ 'score' ] += 0.5;
|
$computedArray[ $choice_id ][ 'score' ] += 0.5;
|
||||||
}
|
}
|
||||||
// compare with max value
|
// compare with max value
|
||||||
if ( $computedArray[ $choice_id ][ 'score' ] > $maxScore ) {
|
if ( $computedArray[ $choice_id ][ 'score' ] > $maxScore ) {
|
||||||
$maxScore = $computedArray[ $choice_id ][ 'score' ];
|
$maxScore = $computedArray[ $choice_id ][ 'score' ];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
$answersWithStats = [];
|
$answersWithStats = [];
|
||||||
foreach ( $computedArray as $choice_stat ) {
|
foreach ( $computedArray as $choice_stat ) {
|
||||||
$answersWithStats[] = $choice_stat;
|
$answersWithStats[] = $choice_stat;
|
||||||
}
|
}
|
||||||
|
|
||||||
return [
|
return [
|
||||||
'answers' => $computedArray,
|
'answers' => $computedArray,
|
||||||
'answersWithStats' => $answersWithStats,
|
'answersWithStats' => $answersWithStats,
|
||||||
'max_score' => $maxScore,
|
'max_score' => $maxScore,
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getStacksOfVotes() {
|
public function getStacksOfVotes() {
|
||||||
return $this->stacksOfVotes;
|
return $this->stacksOfVotes;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function setStacksOfVotes( ?StackOfVotes $stacksOfVotes ): self {
|
public function setStacksOfVotes( ?StackOfVotes $stacksOfVotes ): self {
|
||||||
$this->stacksOfVotes = $stacksOfVotes;
|
$this->stacksOfVotes = $stacksOfVotes;
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return Collection|Choice[]
|
* @return Collection|Choice[]
|
||||||
*/
|
*/
|
||||||
public function getChoices(): Collection {
|
public function getChoices(): Collection {
|
||||||
return $this->choices;
|
return $this->choices;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return Collection|Comment[]
|
* @return Collection|Comment[]
|
||||||
*/
|
*/
|
||||||
public function getComments(): Collection {
|
public function getComments(): Collection {
|
||||||
return $this->comments;
|
return $this->comments;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getTitle(): ?string {
|
public function getTitle(): ?string {
|
||||||
return $this->title;
|
return $this->title;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function setTitle( string $title ): self {
|
public function setTitle( string $title ): self {
|
||||||
$this->title = $title;
|
$this->title = $title;
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getDescription(): ?string {
|
public function getDescription(): ?string {
|
||||||
return $this->description;
|
return $this->description;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function setDescription( string $description ): self {
|
public function setDescription( string $description ): self {
|
||||||
$this->description = $description;
|
$this->description = $description;
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getCreationDate(): ?DateTimeInterface {
|
public function getCreationDate(): ?DateTimeInterface {
|
||||||
return $this->creationDate;
|
return $this->creationDate;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function setCreationDate( DateTimeInterface $creationDate ): self {
|
public function setCreationDate( DateTimeInterface $creationDate ): self {
|
||||||
$this->creationDate = $creationDate;
|
$this->creationDate = $creationDate;
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getExpiracyDate(): ?DateTimeInterface {
|
public function getExpiracyDate(): ?DateTimeInterface {
|
||||||
return $this->expiracyDate;
|
return $this->expiracyDate;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function setExpiracyDate( DateTimeInterface $expiracyDate ): self {
|
public function setExpiracyDate( DateTimeInterface $expiracyDate ): self {
|
||||||
$this->expiracyDate = $expiracyDate;
|
$this->expiracyDate = $expiracyDate;
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getVotesMax() {
|
public function getVotesMax() {
|
||||||
return $this->votesMax;
|
return $this->votesMax;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function setVotesMax( $votesMax ): self {
|
public function setVotesMax( $votesMax ): self {
|
||||||
$this->votesMax = $votesMax;
|
$this->votesMax = $votesMax;
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getChoicesMax() {
|
public function getChoicesMax() {
|
||||||
return $this->choicesMax;
|
return $this->choicesMax;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function setChoicesMax( $choicesMax ): self {
|
public function setChoicesMax( $choicesMax ): self {
|
||||||
$this->choicesMax = $choicesMax;
|
$this->choicesMax = $choicesMax;
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getKind(): ?string {
|
public function getKind(): ?string {
|
||||||
return $this->kind;
|
return $this->kind;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function setKind( string $kind ): self {
|
public function setKind( string $kind ): self {
|
||||||
$this->kind = $kind;
|
$this->kind = $kind;
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getAllowedAnswers(): ?array {
|
public function getAllowedAnswers(): ?array {
|
||||||
return $this->allowedAnswers;
|
return $this->allowedAnswers;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function setAllowedAnswers( array $allowedAnswers ): self {
|
public function setAllowedAnswers( array $allowedAnswers ): self {
|
||||||
$this->allowedAnswers = $allowedAnswers;
|
$this->allowedAnswers = $allowedAnswers;
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getVotesAllowed(): ?bool {
|
public function getVotesAllowed(): ?bool {
|
||||||
return $this->votesAllowed;
|
return $this->votesAllowed;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function setVotesAllowed( ?bool $votesAllowed ): self {
|
public function setVotesAllowed( ?bool $votesAllowed ): self {
|
||||||
$this->votesAllowed = $votesAllowed;
|
$this->votesAllowed = $votesAllowed;
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getModificationPolicy(): ?string {
|
public function getModificationPolicy(): ?string {
|
||||||
return $this->modificationPolicy;
|
return $this->modificationPolicy;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function setModificationPolicy( string $modificationPolicy ): self {
|
public function setModificationPolicy( string $modificationPolicy ): self {
|
||||||
$this->modificationPolicy = $modificationPolicy;
|
$this->modificationPolicy = $modificationPolicy;
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getHideResults(): ?bool {
|
public function getHideResults(): ?bool {
|
||||||
return $this->hideResults;
|
return $this->hideResults;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function setHideResults( bool $hideResults ): self {
|
public function setHideResults( bool $hideResults ): self {
|
||||||
$this->hideResults = $hideResults;
|
$this->hideResults = $hideResults;
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getShowResultEvenIfPasswords(): ?bool {
|
public function getShowResultEvenIfPasswords(): ?bool {
|
||||||
return $this->showResultEvenIfPasswords;
|
return $this->showResultEvenIfPasswords;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function setShowResultEvenIfPasswords( bool $showResultEvenIfPasswords ): self {
|
public function setShowResultEvenIfPasswords( bool $showResultEvenIfPasswords ): self {
|
||||||
$this->showResultEvenIfPasswords = $showResultEvenIfPasswords;
|
$this->showResultEvenIfPasswords = $showResultEvenIfPasswords;
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getOwner(): ?Owner {
|
public function getOwner(): ?Owner {
|
||||||
return $this->owner;
|
return $this->owner;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function setOwner( ?Owner $owner ): self {
|
public function setOwner( ?Owner $owner ): self {
|
||||||
$this->owner = $owner;
|
$this->owner = $owner;
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getPassword(): ?string {
|
public function getPassword(): ?string {
|
||||||
return $this->password;
|
return $this->password;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function setPassword( string $password ): self {
|
public function setPassword( string $password ): self {
|
||||||
$this->password = md5( $password );
|
$this->password = md5( $password );
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getAdminKey(): ?string {
|
public function getAdminKey(): ?string {
|
||||||
return $this->adminKey;
|
return $this->adminKey;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function setAdminKey( string $adminKey ): self {
|
public function setAdminKey( string $adminKey ): self {
|
||||||
$this->adminKey = $adminKey;
|
$this->adminKey = $adminKey;
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getId(): ?int {
|
public function getId(): ?int {
|
||||||
return $this->id;
|
return $this->id;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function findChoiceById( int $id ) {
|
public function findChoiceById( int $id ) {
|
||||||
|
|
||||||
$choices = $this->getChoices();
|
$choices = $this->getChoices();
|
||||||
$counter = 0;
|
$counter = 0;
|
||||||
// there must be something cleaner than this in Doctrine ArrayCollection
|
// there must be something cleaner than this in Doctrine ArrayCollection
|
||||||
foreach ( $choices as $choice ) {
|
foreach ( $choices as $choice ) {
|
||||||
$counter ++;
|
$counter ++;
|
||||||
if ( $counter > $this->maxChoicesLimit ) {
|
if ( $counter > $this->maxChoicesLimit ) {
|
||||||
throw new ErrorException( "max number of choices reached for this poll" );
|
throw new ErrorException( "max number of choices reached for this poll" );
|
||||||
}
|
}
|
||||||
if ( $choice && $choice->getId() == $id ) {
|
if ( $choice && $choice->getId() == $id ) {
|
||||||
return $choice;
|
return $choice;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return Collection|Vote[]
|
* @return Collection|Vote[]
|
||||||
*/
|
*/
|
||||||
public function getVotes(): Collection {
|
public function getVotes(): Collection {
|
||||||
return $this->votes;
|
return $this->votes;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getCustomUrl(): ?string {
|
public function getCustomUrl(): ?string {
|
||||||
return $this->customUrl;
|
return $this->customUrl;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function setCustomUrl( string $customUrl ): self {
|
public function setCustomUrl( string $customUrl ): self {
|
||||||
$this->customUrl = $customUrl;
|
$this->customUrl = $customUrl;
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getMailOnComment(): ?bool {
|
public function getMailOnComment(): ?bool {
|
||||||
return $this->mailOnComment;
|
return $this->mailOnComment;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function setMailOnComment( bool $mailOnComment ): self {
|
public function setMailOnComment( bool $mailOnComment ): self {
|
||||||
$this->mailOnComment = $mailOnComment;
|
$this->mailOnComment = $mailOnComment;
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getMailOnVote(): ?bool {
|
public function getMailOnVote(): ?bool {
|
||||||
return $this->mailOnVote;
|
return $this->mailOnVote;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function setMailOnVote( bool $mailOnVote ): self {
|
public function setMailOnVote( bool $mailOnVote ): self {
|
||||||
$this->mailOnVote = $mailOnVote;
|
$this->mailOnVote = $mailOnVote;
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function addComment( Comment $comment ): self {
|
public function addComment( Comment $comment ): self {
|
||||||
if ( ! $this->comments->contains( $comment ) ) {
|
if ( ! $this->comments->contains( $comment ) ) {
|
||||||
$this->comments[] = $comment;
|
$this->comments[] = $comment;
|
||||||
$comment->setPoll( $this );
|
$comment->setPoll( $this );
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function removeComment( Comment $comment ): self {
|
public function removeComment( Comment $comment ): self {
|
||||||
if ( $this->comments->contains( $comment ) ) {
|
if ( $this->comments->contains( $comment ) ) {
|
||||||
$this->comments->removeElement( $comment );
|
$this->comments->removeElement( $comment );
|
||||||
// set the owning side to null (unless already changed)
|
// set the owning side to null (unless already changed)
|
||||||
if ( $comment->getPoll() === $this ) {
|
if ( $comment->getPoll() === $this ) {
|
||||||
$comment->setPoll( null );
|
$comment->setPoll( null );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function addStackOfVote( StackOfVotes $stackOfVote ): self {
|
public function addStackOfVote( StackOfVotes $stackOfVote ): self {
|
||||||
if ( ! $this->stacksOfVotes->contains( $stackOfVote ) ) {
|
if ( ! $this->stacksOfVotes->contains( $stackOfVote ) ) {
|
||||||
$this->stacksOfVotes[] = $stackOfVote;
|
$this->stacksOfVotes[] = $stackOfVote;
|
||||||
$stackOfVote->setPoll( $this );
|
$stackOfVote->setPoll( $this );
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function removeStackOfVote( StackOfVotes $stackOfVote ): self {
|
public function removeStackOfVote( StackOfVotes $stackOfVote ): self {
|
||||||
if ( $this->stacksOfVotes->contains( $stackOfVote ) ) {
|
if ( $this->stacksOfVotes->contains( $stackOfVote ) ) {
|
||||||
$this->stacksOfVotes->removeElement( $stackOfVote );
|
$this->stacksOfVotes->removeElement( $stackOfVote );
|
||||||
// set the owning side to null (unless already changed)
|
// set the owning side to null (unless already changed)
|
||||||
if ( $stackOfVote->getPoll() === $this ) {
|
if ( $stackOfVote->getPoll() === $this ) {
|
||||||
$stackOfVote->setPoll( null );
|
$stackOfVote->setPoll( null );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function addVote( Vote $vote ): self {
|
public function addVote( Vote $vote ): self {
|
||||||
if ( ! $this->votes->contains( $vote ) ) {
|
if ( ! $this->votes->contains( $vote ) ) {
|
||||||
$this->votes[] = $vote;
|
$this->votes[] = $vote;
|
||||||
$vote->setPoll( $this );
|
$vote->setPoll( $this );
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function removeVote( Vote $vote ): self {
|
public function removeVote( Vote $vote ): self {
|
||||||
if ( $this->votes->contains( $vote ) ) {
|
if ( $this->votes->contains( $vote ) ) {
|
||||||
$this->votes->removeElement( $vote );
|
$this->votes->removeElement( $vote );
|
||||||
// set the owning side to null (unless already changed)
|
// set the owning side to null (unless already changed)
|
||||||
if ( $vote->getPoll() === $this ) {
|
if ( $vote->getPoll() === $this ) {
|
||||||
$vote->setPoll( null );
|
$vote->setPoll( null );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function addTextChoiceArray( array $choiceTextArray ): self {
|
public function addTextChoiceArray( array $choiceTextArray ): self {
|
||||||
foreach ( $choiceTextArray as $text ) {
|
foreach ( $choiceTextArray as $text ) {
|
||||||
$newChoice = new Choice();
|
$newChoice = new Choice();
|
||||||
$newChoice->setName( $text );
|
$newChoice->setName( $text );
|
||||||
$this->addChoice( $newChoice );
|
$this->addChoice( $newChoice );
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function addChoice( Choice $choice ): self {
|
public function addChoice( Choice $choice ): self {
|
||||||
if ( ! is_null( $this->choices ) ) {
|
if ( ! is_null( $this->choices ) ) {
|
||||||
if ( ! $this->choices->contains( $choice ) ) {
|
if ( ! $this->choices->contains( $choice ) ) {
|
||||||
$this->choices[] = $choice;
|
$this->choices[] = $choice;
|
||||||
$choice->setPoll( $this );
|
$choice->setPoll( $this );
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
$this->choices[] = $choice;
|
$this->choices[] = $choice;
|
||||||
$choice->setPoll( $this );
|
$choice->setPoll( $this );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function addStacksOfVote( StackOfVotes $stacksOfVote ): self {
|
public function addStacksOfVote( StackOfVotes $stacksOfVote ): self {
|
||||||
if ( ! $this->stacksOfVotes->contains( $stacksOfVote ) ) {
|
if ( ! $this->stacksOfVotes->contains( $stacksOfVote ) ) {
|
||||||
$this->stacksOfVotes[] = $stacksOfVote;
|
$this->stacksOfVotes[] = $stacksOfVote;
|
||||||
$stacksOfVote->setPoll( $this );
|
$stacksOfVote->setPoll( $this );
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function removeStacksOfVote( StackOfVotes $stacksOfVote ): self {
|
public function removeStacksOfVote( StackOfVotes $stacksOfVote ): self {
|
||||||
if ( $this->stacksOfVotes->contains( $stacksOfVote ) ) {
|
if ( $this->stacksOfVotes->contains( $stacksOfVote ) ) {
|
||||||
$this->stacksOfVotes->removeElement( $stacksOfVote );
|
$this->stacksOfVotes->removeElement( $stacksOfVote );
|
||||||
// set the owning side to null (unless already changed)
|
// set the owning side to null (unless already changed)
|
||||||
if ( $stacksOfVote->getPoll() === $this ) {
|
if ( $stacksOfVote->getPoll() === $this ) {
|
||||||
$stacksOfVote->setPoll( null );
|
$stacksOfVote->setPoll( null );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function removeChoice( Choice $choice ): self {
|
public function removeChoice( Choice $choice ): self {
|
||||||
if ( $this->choices->contains( $choice ) ) {
|
if ( $this->choices->contains( $choice ) ) {
|
||||||
$this->choices->removeElement( $choice );
|
$this->choices->removeElement( $choice );
|
||||||
// set the owning side to null (unless already changed)
|
// set the owning side to null (unless already changed)
|
||||||
if ( $choice->getPoll() === $this ) {
|
if ( $choice->getPoll() === $this ) {
|
||||||
$choice->setPoll( null );
|
$choice->setPoll( null );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getCommentsAllowed(): ?bool {
|
public function getCommentsAllowed(): ?bool {
|
||||||
return $this->commentsAllowed;
|
return $this->commentsAllowed;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function setCommentsAllowed( ?bool $commentsAllowed ): self {
|
public function setCommentsAllowed( ?bool $commentsAllowed ): self {
|
||||||
$this->commentsAllowed = $commentsAllowed;
|
$this->commentsAllowed = $commentsAllowed;
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -46,103 +46,120 @@ class StackOfVotes {
|
||||||
*/
|
*/
|
||||||
private $owner;
|
private $owner;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @ORM\Column(type="string", length=255)
|
||||||
|
*/
|
||||||
|
private $ip;
|
||||||
|
|
||||||
public function __construct() {
|
public function __construct() {
|
||||||
$this->votes = new ArrayCollection();
|
$this->votes = new ArrayCollection();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function display() {
|
public function display() {
|
||||||
$votes = $this->getVotes();
|
$votes = $this->getVotes();
|
||||||
|
|
||||||
$tab = [
|
$tab = [
|
||||||
// 'id' => $this->getId(),
|
// 'id' => $this->getId(),
|
||||||
// 'modifier_token' => $this->getOwner()->getModifierToken(),
|
// 'modifier_token' => $this->getOwner()->getModifierToken(),
|
||||||
'pseudo' => $this->getPseudo(),
|
'pseudo' => $this->getPseudo(),
|
||||||
'created_at' => $this->getCreatedAtAsString(),
|
'created_at' => $this->getCreatedAtAsString(),
|
||||||
'votes' => [],
|
'votes' => [],
|
||||||
];
|
];
|
||||||
// prefill votes with all choices ids
|
// prefill votes with all choices ids
|
||||||
// foreach ( $this->getPoll()->getChoices() as $choice ) {
|
// foreach ( $this->getPoll()->getChoices() as $choice ) {
|
||||||
// $tab[ 'votes' ][ $choice->getId() ] = [
|
// $tab[ 'votes' ][ $choice->getId() ] = [
|
||||||
// 'choice_id' => $choice->getId(),
|
// 'choice_id' => $choice->getId(),
|
||||||
// 'value' => null,
|
// 'value' => null,
|
||||||
// ];
|
// ];
|
||||||
// }
|
// }
|
||||||
|
|
||||||
foreach ( $votes as $vote ) {
|
foreach ( $votes as $vote ) {
|
||||||
$tab[ 'votes' ][ $vote->getChoice()->getId() ] = $vote->display();
|
$tab[ 'votes' ][ $vote->getChoice()->getId() ] = $vote->display();
|
||||||
}
|
}
|
||||||
|
|
||||||
return $tab;
|
return $tab;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return Collection|poll[]
|
* @return Collection|poll[]
|
||||||
*/
|
*/
|
||||||
public function getVotes(): Collection {
|
public function getVotes(): Collection {
|
||||||
return $this->votes;
|
return $this->votes;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getPseudo(): ?string {
|
public function getPseudo(): ?string {
|
||||||
return $this->pseudo;
|
return $this->pseudo;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function setPseudo( ?string $pseudo ): self {
|
public function setPseudo( ?string $pseudo ): self {
|
||||||
$this->pseudo = $pseudo;
|
$this->pseudo = $pseudo;
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @ORM\PrePersist
|
* @ORM\PrePersist
|
||||||
*/
|
*/
|
||||||
public function prePersist() {
|
public function prePersist() {
|
||||||
$this->setPseudo( $this->getOwner()->getPseudo() );
|
$this->setPseudo( $this->getOwner()->getPseudo() );
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getOwner(): ?Owner {
|
public function getOwner(): ?Owner {
|
||||||
return $this->owner;
|
return $this->owner;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function setOwner( ?Owner $owner ): self {
|
public function setOwner( ?Owner $owner ): self {
|
||||||
$this->owner = $owner;
|
$this->owner = $owner;
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getId(): ?int {
|
public function getId(): ?int {
|
||||||
return $this->id;
|
return $this->id;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function addVote( Vote $vote ): self {
|
public function addVote( Vote $vote ): self {
|
||||||
if ( ! $this->votes->contains( $vote ) ) {
|
if ( ! $this->votes->contains( $vote ) ) {
|
||||||
$vote->setPoll( $this->getPoll() );
|
$vote->setPoll( $this->getPoll() );
|
||||||
|
|
||||||
$this->votes[] = $vote;
|
$this->votes[] = $vote;
|
||||||
$vote->setStacksOfVotes( $this );
|
$vote->setStacksOfVotes( $this );
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getPoll(): ?Poll {
|
public function getPoll(): ?Poll {
|
||||||
return $this->poll;
|
return $this->poll;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function setPoll( ?Poll $poll ): self {
|
public function setPoll( ?Poll $poll ): self {
|
||||||
$this->poll = $poll;
|
$this->poll = $poll;
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function removeVote( Vote $vote ): self {
|
public function removeVote( Vote $vote ): self {
|
||||||
if ( $this->votes->contains( $vote ) ) {
|
if ( $this->votes->contains( $vote ) ) {
|
||||||
$this->votes->removeElement( $vote );
|
$this->votes->removeElement( $vote );
|
||||||
// set the owning side to null (unless already changed)
|
// set the owning side to null (unless already changed)
|
||||||
if ( $vote->getStacksOfVotes() === $this ) {
|
if ( $vote->getStacksOfVotes() === $this ) {
|
||||||
$vote->setStacksOfVotes( null );
|
$vote->setStacksOfVotes( null );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
return $this;
|
public function getIp(): ?string
|
||||||
}
|
{
|
||||||
|
return $this->ip;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setIp(string $ip): self
|
||||||
|
{
|
||||||
|
$this->ip = $ip;
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,6 +3,43 @@
|
||||||
namespace App\Traits;
|
namespace App\Traits;
|
||||||
|
|
||||||
trait RandomTrait {
|
trait RandomTrait {
|
||||||
|
public function generateRandomKey() {
|
||||||
|
$rand = random_int( PHP_INT_MIN, PHP_INT_MAX );
|
||||||
|
|
||||||
|
return str_shuffle( md5( $rand ) . $rand . $this->random_str() );
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generate a random string, using a cryptographically secure
|
||||||
|
* pseudorandom number generator (random_int)
|
||||||
|
*
|
||||||
|
* This function uses type hints now (PHP 7+ only), but it was originally
|
||||||
|
* written for PHP 5 as well.
|
||||||
|
*
|
||||||
|
* For PHP 7, random_int is a PHP core function
|
||||||
|
* For PHP 5.x, depends on https://github.com/paragonie/random_compat
|
||||||
|
*
|
||||||
|
* @param int $length How many characters do we want?
|
||||||
|
* @param string $keyspace A string of all possible characters
|
||||||
|
* to select from
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function random_str(
|
||||||
|
int $length = 64,
|
||||||
|
string $keyspace = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
|
||||||
|
): string {
|
||||||
|
if ( $length < 1 ) {
|
||||||
|
throw new RangeException( "Length must be a positive integer" );
|
||||||
|
}
|
||||||
|
$pieces = [];
|
||||||
|
$max = mb_strlen( $keyspace, '8bit' ) - 1;
|
||||||
|
for ( $i = 0 ; $i < $length ; ++ $i ) {
|
||||||
|
$pieces [] = $keyspace[ random_int( 0, $max ) ];
|
||||||
|
}
|
||||||
|
|
||||||
|
return implode( '', $pieces );
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
namespace App\Traits;
|
namespace App\Traits;
|
||||||
|
|
||||||
|
use DateTime;
|
||||||
use DateTimeInterface;
|
use DateTimeInterface;
|
||||||
use Doctrine\ORM\Mapping as ORM;
|
use Doctrine\ORM\Mapping as ORM;
|
||||||
|
|
||||||
|
@ -25,4 +26,12 @@ trait TimeStampableTrait {
|
||||||
return $this->createdAt->format( 'c' );
|
return $this->createdAt->format( 'c' );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public function addDaysToDate( DateTime $date, int $days ) {
|
||||||
|
$st = strtotime( $date->getTimestamp() );
|
||||||
|
|
||||||
|
return new DateTime( $st );
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue