use trait timestamp and replace

This commit is contained in:
Tykayn 2021-04-27 12:51:10 +02:00 committed by tykayn
parent ecdbf3fb33
commit 94c02be927
9 changed files with 121 additions and 119 deletions

View File

@ -74,7 +74,7 @@ class MigrationController extends EmailsController {
->setChoicesMax( $d->ValueMax )
->setPassword( $d->password_hash )
->setDescription( $d->description )
->setCreationDate( date_create( $d->creation_date ) );
->setCreatedAt( date_create( $d->creation_date ) );
$pollsBySlug[ $d->id ] = $newPoll;

View File

@ -142,7 +142,7 @@ class PollController extends EmailsController {
return $this->notFoundPoll( $customUrl );
}
if ( $poll->getPassword() === $md5 ) {
if ( md5($poll->getPassword()) === $md5 ) {
// good matching pass
return $this->json( $poll->display() );
} else {

View File

@ -53,6 +53,7 @@ class Choice {
private $poll;
public function __construct( $optionalName = null ) {
$this->setCreatedAt( new DateTime());
$this->poll = new ArrayCollection();
$this->votes = new ArrayCollection();
$this->setDateTime( new DateTime() );

View File

@ -66,6 +66,7 @@ class Owner {
$this->stackOfVotes = new ArrayCollection();
$this->setCreatedAt( new DateTime() );
$this->setModifierToken( uniqid() );
$this->setCreatedAt( new DateTime());
}
public function setCreatedAt( DateTimeInterface $createdAt ): self {

View File

@ -3,6 +3,7 @@
namespace App\Entity;
use App\Traits\RandomTrait;
use App\Traits\TimeStampableTrait;
use DateTime;
use DateTimeInterface;
use Doctrine\Common\Collections\ArrayCollection;
@ -19,6 +20,8 @@ class Poll {
use RandomTrait;
use TimeStampableTrait;
/**
* @ORM\Id()
* @ORM\GeneratedValue()
@ -46,11 +49,7 @@ class Poll {
* @Serializer\Type("string")
*/
public $description;
/**
* @ORM\Column(type="datetime" , options={"default"="CURRENT_TIMESTAMP"})
* @Serializer\Expose()
*/
public $creationDate;
/**
* @ORM\Column(type="datetime")
* @Serializer\Expose()
@ -196,6 +195,7 @@ class Poll {
public function __construct() {
$this->initiate();
$this->setCreatedAt( new DateTime());
$this->votes = new ArrayCollection();
$this->stacksOfVotes = new ArrayCollection();
$this->choices = new ArrayCollection();
@ -208,7 +208,7 @@ class Poll {
$this->choices = new ArrayCollection();
$this->comments = new ArrayCollection();
$this->setAdminKey( $this->generateRandomKey() );
$this->setCreationDate( new DateTime() );
$this->setCreatedAt( new DateTime() );
$this->setExpiracyDate( $this->addDaysToDate(
new DateTime(),
$this->defaultExpiracyDaysFromNow
@ -247,7 +247,7 @@ class Poll {
return [
'title' => $this->getTitle(),
'description' => $this->getDescription(),
'created_at' => $this->getCreationDate()->format( 'c' ),
'created_at' => $this->getCreatedAt()->format( 'c' ),
'expiracy_date' => $this->getExpiracyDate()->format( 'c' ),
'votes_max' => $this->getVotesMax(),
'choices_max' => $this->getChoicesMax(),
@ -274,23 +274,25 @@ class Poll {
$computedArray = [];
$maxScore = 0;
$scoreInfos = ['score' => 0,
'yes' => [
'count' => 0,
'people' => [],
],
'maybe' => [
'count' => 0,
'people' => [],
],
'no' => [
'count' => 0,
'people' => [],
]];
$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($this->getKind()));
$computedArray[ $choice->getId() ] = array_merge( $scoreInfos, $choice->display( $this->getKind() ) );
}
// then, compute stack of votes scores on each choice
foreach ( $this->getStacksOfVotes() as $stack_of_vote ) {
@ -301,9 +303,9 @@ class Poll {
if ( ! isset( $computedArray[ $choice_id ] ) ) {
$computedArray[ $choice_id ] = [
'id' => $choice_id,
'url' => $choice_url,
'name' => $vote->getChoice()->getName(),
'id' => $choice_id,
'url' => $choice_url,
'name' => $vote->getChoice()->getName(),
];
}
@ -327,8 +329,8 @@ class Poll {
}
return [
'answers' => $answersWithStats,
'max_score' => $maxScore,
'answers' => $answersWithStats,
'max_score' => $maxScore,
];
}
@ -376,12 +378,12 @@ class Poll {
return $this;
}
public function getCreationDate(): ?DateTimeInterface {
return $this->creationDate;
public function getCreatedAt(): ?DateTimeInterface {
return $this->createdAt;
}
public function setCreationDate( DateTimeInterface $creationDate ): self {
$this->creationDate = $creationDate;
public function setCreatedAt( DateTimeInterface $createdAt ): self {
$this->createdAt = $createdAt;
return $this;
}

View File

@ -3,6 +3,7 @@
namespace App\Entity;
use App\Traits\TimeStampableTrait;
use DateTime;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
@ -52,114 +53,113 @@ class StackOfVotes {
private $ip;
public function __construct() {
$this->votes = new ArrayCollection();
}
$this->setCreatedAt( new DateTime() );
$this->votes = new ArrayCollection();
}
public function display() {
$votes = $this->getVotes();
$tab = [
// 'id' => $this->getId(),
// 'modifier_token' => $this->getOwner()->getModifierToken(),
'pseudo' => $this->getPseudo(),
'created_at' => $this->getCreatedAtAsString(),
'votes' => [],
];
// prefill votes with all choices ids
// foreach ( $this->getPoll()->getChoices() as $choice ) {
// $tab[ 'votes' ][ $choice->getId() ] = [
// 'choice_id' => $choice->getId(),
// 'value' => null,
// ];
// }
foreach ( $votes as $vote ) {
$tab[ 'votes' ][ $vote->getChoice()->getId() ] = $vote->display();
}
return $tab;
}
$votes = $this->getVotes();
$tab = [
// 'id' => $this->getId(),
// 'modifier_token' => $this->getOwner()->getModifierToken(),
'pseudo' => $this->getPseudo(),
'created_at' => $this->getCreatedAtAsString(),
'votes' => [],
];
// prefill votes with all choices ids
// foreach ( $this->getPoll()->getChoices() as $choice ) {
// $tab[ 'votes' ][ $choice->getId() ] = [
// 'choice_id' => $choice->getId(),
// 'value' => null,
// ];
// }
foreach ( $votes as $vote ) {
$tab[ 'votes' ][ $vote->getChoice()->getId() ] = $vote->display();
}
return $tab;
}
/**
* @return Collection|poll[]
*/
public function getVotes(): Collection {
return $this->votes;
}
return $this->votes;
}
public function getPseudo(): ?string {
return $this->pseudo;
}
return $this->pseudo;
}
public function setPseudo( ?string $pseudo ): self {
$this->pseudo = $pseudo;
return $this;
}
$this->pseudo = $pseudo;
return $this;
}
/**
* @ORM\PrePersist
*/
public function prePersist() {
$this->setPseudo( $this->getOwner()->getPseudo() );
}
$this->setPseudo( $this->getOwner()->getPseudo() );
}
public function getOwner(): ?Owner {
return $this->owner;
}
return $this->owner;
}
public function setOwner( ?Owner $owner ): self {
$this->owner = $owner;
return $this;
}
$this->owner = $owner;
return $this;
}
public function getId(): ?int {
return $this->id;
}
return $this->id;
}
public function addVote( Vote $vote ): self {
if ( ! $this->votes->contains( $vote ) ) {
$vote->setPoll( $this->getPoll() );
$this->votes[] = $vote;
$vote->setStacksOfVotes( $this );
}
return $this;
}
if ( ! $this->votes->contains( $vote ) ) {
$vote->setPoll( $this->getPoll() );
$this->votes[] = $vote;
$vote->setStacksOfVotes( $this );
}
return $this;
}
public function getPoll(): ?Poll {
return $this->poll;
}
return $this->poll;
}
public function setPoll( ?Poll $poll ): self {
$this->poll = $poll;
return $this;
}
$this->poll = $poll;
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;
}
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 );
}
}
public function getIp(): ?string
{
return $this->ip;
}
return $this;
}
public function setIp(string $ip): self
{
$this->ip = $ip;
public function getIp(): ?string {
return $this->ip;
}
return $this;
}
public function setIp( string $ip ): self {
$this->ip = $ip;
return $this;
}
}

View File

@ -2,6 +2,7 @@
namespace App\Entity;
use App\Traits\TimeStampableTrait;
use DateTime;
use DateTimeInterface;
use Doctrine\ORM\Mapping as ORM;
@ -12,6 +13,9 @@ use JMS\Serializer\Annotation as Serializer;
* @Serializer\ExclusionPolicy("all")
*/
class Vote {
use TimeStampableTrait;
/**
* for a text kind of choice: could be "yes" "no" "maybe" and empty.
* for a date kind, the choice linked is equivalent to the value selected
@ -20,12 +24,7 @@ class Vote {
* @Serializer\Expose()
*/
public $value;
/**
* @ORM\Column(type="datetime" , options={"default"="CURRENT_TIMESTAMP"})
* @Serializer\Type("datetime")
* @Serializer\Expose()
*/
public $creationDate;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Choice", inversedBy="votes", cascade={"persist"})
* @ORM\JoinColumn(nullable=false)
@ -54,7 +53,7 @@ class Vote {
private $stacksOfVotes;
public function __construct() {
$this->setCreationDate( new DateTime() );
$this->setCreatedAt( new DateTime());
}
public function display() {

View File

@ -13,7 +13,7 @@ class PollType extends AbstractType {
->add( 'title' )
->add( 'customUrl' )
->add( 'description' )
->add( 'creationDate' )
->add( 'createdAt' )
->add( 'expiracyDate' )
->add( 'kind' )
->add( 'allowedAnswers' )

View File

@ -28,9 +28,8 @@ trait TimeStampableTrait {
public function addDaysToDate( DateTime $date, int $days ) {
$st = strtotime( $date->getTimestamp() );
return new DateTime( $st );
return $date->add(new \DateInterval('P'.$days.'D'));
}