mirror of
https://framagit.org/tykayn/date-poll-api
synced 2023-08-25 08:23:11 +02:00
Compare commits
6 Commits
16389690b6
...
5694a703fb
Author | SHA1 | Date | |
---|---|---|---|
5694a703fb | |||
5bb76de67a | |||
c0791d52ac | |||
723c774d51 | |||
9ff5429051 | |||
4eb18c3f1f |
@ -7,13 +7,12 @@ use App\Entity\Choice;
|
|||||||
use App\Entity\Owner;
|
use App\Entity\Owner;
|
||||||
use App\Entity\Poll;
|
use App\Entity\Poll;
|
||||||
use App\Repository\PollRepository;
|
use App\Repository\PollRepository;
|
||||||
|
use DateTime;
|
||||||
use FOS\RestBundle\Controller\Annotations\Delete;
|
use FOS\RestBundle\Controller\Annotations\Delete;
|
||||||
use FOS\RestBundle\Controller\Annotations\Get;
|
use FOS\RestBundle\Controller\Annotations\Get;
|
||||||
use FOS\RestBundle\Controller\Annotations\Post;
|
use FOS\RestBundle\Controller\Annotations\Post;
|
||||||
use FOS\RestBundle\Controller\Annotations\Put;
|
use FOS\RestBundle\Controller\Annotations\Put;
|
||||||
use FOS\RestBundle\Controller\Annotations\Route;
|
use FOS\RestBundle\Controller\Annotations\Route;
|
||||||
use JMS\Serializer\Exception\RuntimeException;
|
|
||||||
use JMS\Serializer\SerializerBuilder;
|
|
||||||
use JMS\Serializer\SerializerInterface;
|
use JMS\Serializer\SerializerInterface;
|
||||||
use Swift_Mailer;
|
use Swift_Mailer;
|
||||||
use Symfony\Component\HttpFoundation\JsonResponse;
|
use Symfony\Component\HttpFoundation\JsonResponse;
|
||||||
@ -211,23 +210,22 @@ class PollController extends EmailsController {
|
|||||||
public function newPollAction( Request $request ) {
|
public function newPollAction( Request $request ) {
|
||||||
|
|
||||||
$data = $request->getContent();
|
$data = $request->getContent();
|
||||||
|
|
||||||
$serializer = SerializerBuilder::create()->build();
|
|
||||||
try {
|
|
||||||
$newpoll = $serializer->deserialize( $data, 'App\Entity\Poll', 'json' );
|
|
||||||
} catch ( RuntimeException $e ) {
|
|
||||||
return $this->json( [ "message" => "Incorrect JSON in request" ], 400 );
|
|
||||||
}
|
|
||||||
$newpoll
|
|
||||||
->setAdminKey( $newpoll->generateAdminKey() )
|
|
||||||
->setCreationDate( new DateTime() )
|
|
||||||
->setModificationPolicy( 'nobody' );
|
|
||||||
$timeStamp = time() + ( 3600 * 24 * 90 ); // 90 days by default
|
|
||||||
$newpoll->setExpiracyDate( ( new DateTime() )->setTimestamp( $timeStamp ),
|
|
||||||
new DateTimeZone( 'Europe/Paris' ) );
|
|
||||||
$data = json_decode( $data, true );
|
$data = json_decode( $data, true );
|
||||||
$em = $this->getDoctrine()->getRepository( Owner::class );
|
|
||||||
$foundOwner = $em->findOneBy( [ 'email' => $data[ 'owner' ][ 'email' ] ] );
|
$newpoll = new Poll();
|
||||||
|
$newpoll
|
||||||
|
->setModificationPolicy( $data[ 'modification_policy' ] )
|
||||||
|
->setTitle( $data[ 'title' ] )
|
||||||
|
->setCustomUrl( $data[ 'custom_url' ] );
|
||||||
|
if ( count( $data[ 'allowed_answers' ] ) ) {
|
||||||
|
$newpoll->setAllowedAnswers( $data[ 'allowed_answers' ] );
|
||||||
|
}
|
||||||
|
$expiracyCalculated = $newpoll->addDaysToDate( new DateTime(),
|
||||||
|
$data[ 'default_expiracy_days_from_now' ] ) ;
|
||||||
|
|
||||||
|
$newpoll->setExpiracyDate( $expiracyCalculated );
|
||||||
|
$emOwner = $this->getDoctrine()->getRepository( Owner::class );
|
||||||
|
$foundOwner = $emOwner->findOneByEmail( $data[ 'owner' ][ 'email' ] );
|
||||||
|
|
||||||
|
|
||||||
$userWasFound = false;
|
$userWasFound = false;
|
||||||
@ -252,41 +250,69 @@ class PollController extends EmailsController {
|
|||||||
|
|
||||||
// emails
|
// emails
|
||||||
$newpoll->setMailOnComment( true );
|
$newpoll->setMailOnComment( true );
|
||||||
$newpoll->setMailOnVote( true );
|
$newpoll->setMailOnVote( $data['isOwnerNotifiedByEmailOnNewVote'] );
|
||||||
|
$newpoll->setDescription( $data['description'] );
|
||||||
$newpoll->setHideResults( false );
|
$newpoll->setHideResults( false );
|
||||||
// possible answers
|
// possible answers
|
||||||
$newpoll->setAllowedAnswers( [ 'yes' ] );
|
$newpoll->setAllowedAnswers( [ 'yes' ] );
|
||||||
if ( $data[ 'voteChoices' ] ) {
|
$newpoll->setVotesMax( $data[ 'maxCountOfAnswers' ] );
|
||||||
switch ( $data[ 'voteChoices' ] ) {
|
$newpoll->setCommentsAllowed( $data['allowComments'] );
|
||||||
case "only_yes":
|
|
||||||
default:
|
|
||||||
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// setup the password, converting the raw with md5 hash
|
// setup the password, converting the raw with md5 hash
|
||||||
if ( $data[ 'password' ] ) {
|
if ( $data[ 'password' ] ) {
|
||||||
$newpoll->setPassword( $data[ 'password' ] );
|
$newpoll->setPassword( $data[ 'password' ] );
|
||||||
}
|
}
|
||||||
// manage choices
|
// manage choices
|
||||||
// text kind of answers, dates are below
|
// text kind of answers, dates are below
|
||||||
if ( $data[ 'pollType' ] == 'classic' ) {
|
if ( $data[ 'kind' ] == 'text' ) {
|
||||||
$choices = $data[ 'dateList' ];
|
|
||||||
|
$choices = $data[ 'dateChoices' ];
|
||||||
foreach ( $choices as $c ) {
|
foreach ( $choices as $c ) {
|
||||||
$newChoice = new Choice();
|
$newChoice = new Choice();
|
||||||
$newChoice
|
$newChoice
|
||||||
->setPoll( $newpoll )
|
->setPoll( $newpoll )
|
||||||
->setName( $c[ 'literal' ] );
|
->setName( $c[ 'literal' ] );
|
||||||
$em->persist( $newChoice );
|
$em->persist( $newChoice );
|
||||||
// TODO add also choices for each time range in a day
|
|
||||||
}
|
}
|
||||||
} elseif ( $data[ 'pollType' ] == 'dates' ) {
|
} // date kind of poll
|
||||||
if ( $data[ 'allowSeveralHours' ] == true ) {
|
elseif ( $data[ 'kind' ] == 'date' ) {
|
||||||
// different hours spans
|
|
||||||
$choices = $data[ 'dateList' ];
|
$choices = $data[ 'dateChoices' ];
|
||||||
|
if ( $data[ 'hasSeveralHours' ] == true ) {
|
||||||
|
// different hours spans make more choices
|
||||||
|
|
||||||
|
foreach ( $choices as $c ) {
|
||||||
|
$currentDate = $c[ 'literal' ];
|
||||||
|
|
||||||
|
$timeSlicesOfThisChoice = $c[ 'timeList' ];
|
||||||
|
foreach ( $timeSlicesOfThisChoice as $t ) {
|
||||||
|
|
||||||
|
$newChoice = new Choice();
|
||||||
|
$newChoice
|
||||||
|
->setPoll( $newpoll )
|
||||||
|
->setName( $currentDate . ' >>> ' . $t[ 'literal' ] );
|
||||||
|
$em->persist( $newChoice );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
//TODO (Sébastien) I assume this shouldn't be empty ?
|
// all choices will be having the same time slices from timeSlices
|
||||||
// all days have the same hour spans
|
$timeSlicesForAllChoices = $data[ 'timeSlices' ];
|
||||||
|
foreach ( $choices as $c ) {
|
||||||
|
$currentDate = $c[ 'literal' ];
|
||||||
|
|
||||||
|
foreach ( $timeSlicesForAllChoices as $t ) {
|
||||||
|
|
||||||
|
$newChoice = new Choice();
|
||||||
|
$newChoice
|
||||||
|
->setPoll( $newpoll )
|
||||||
|
->setName( $currentDate . ' >>> ' . $t[ 'literal' ] );
|
||||||
|
$em->persist( $newChoice );
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -303,7 +329,7 @@ class PollController extends EmailsController {
|
|||||||
|
|
||||||
return $this->json( [
|
return $this->json( [
|
||||||
'message' => 'you created a poll ' . $precision,
|
'message' => 'you created a poll ' . $precision,
|
||||||
'poll' => $newpoll->displayForAdmin,
|
'poll' => $newpoll->displayForAdmin(),
|
||||||
'password_protected' => is_string( $newpoll->getPassword() ),
|
'password_protected' => is_string( $newpoll->getPassword() ),
|
||||||
|
|
||||||
],
|
],
|
||||||
@ -336,7 +362,7 @@ class PollController extends EmailsController {
|
|||||||
$poll = $foundOwner->getPolls()[ 0 ];
|
$poll = $foundOwner->getPolls()[ 0 ];
|
||||||
$comment = $foundOwner->getComments()[ 0 ];
|
$comment = $foundOwner->getComments()[ 0 ];
|
||||||
|
|
||||||
$sent = $this->sendOwnerPollsAction( $foundOwner, $poll );
|
$sent = $this->sendOwnerPollsAction( $foundOwner );
|
||||||
if ( $sent ) {
|
if ( $sent ) {
|
||||||
return $this->json( [ "message" => "test email sent to " . $foundOwner->getEmail() . "!" ], 200 );
|
return $this->json( [ "message" => "test email sent to " . $foundOwner->getEmail() . "!" ], 200 );
|
||||||
}
|
}
|
||||||
@ -441,28 +467,14 @@ class PollController extends EmailsController {
|
|||||||
if ( $pollFound ) {
|
if ( $pollFound ) {
|
||||||
|
|
||||||
$poll = $pollFound;
|
$poll = $pollFound;
|
||||||
$comments = $poll->getComments();
|
|
||||||
$stacks = $poll->getStacksOfVotes();
|
|
||||||
|
|
||||||
$returnedPoll = [
|
$returnedPoll = [
|
||||||
'message' => 'your poll config',
|
'message' => 'your poll config',
|
||||||
'poll' => $poll,
|
'poll' => $poll->displayForAdmin(),
|
||||||
'stacks_count' => count( $stacks ),
|
|
||||||
'stacks' => $stacks,
|
|
||||||
'choices_count' => $poll->computeAnswers(),
|
|
||||||
'choices' => $poll->getChoices(),
|
|
||||||
'comments' => $comments,
|
|
||||||
'comments_count' => count( $comments ),
|
|
||||||
'token' => $token,
|
|
||||||
];
|
];
|
||||||
|
|
||||||
$jsonResponse = $serializer->serialize( $returnedPoll, 'json' );
|
return $this->json( $returnedPoll,
|
||||||
|
200 );;
|
||||||
$response = new Response( $jsonResponse );
|
|
||||||
$response->headers->set( 'Content-Type', 'application/json' );
|
|
||||||
$response->setStatusCode( 200 );
|
|
||||||
|
|
||||||
return $response;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->json( [
|
return $this->json( [
|
||||||
|
@ -4,7 +4,6 @@ namespace App\Entity;
|
|||||||
|
|
||||||
use App\Traits\TimeStampableTrait;
|
use App\Traits\TimeStampableTrait;
|
||||||
use DateTime;
|
use DateTime;
|
||||||
use DateTimeInterface;
|
|
||||||
use Doctrine\Common\Collections\ArrayCollection;
|
use Doctrine\Common\Collections\ArrayCollection;
|
||||||
use Doctrine\Common\Collections\Collection;
|
use Doctrine\Common\Collections\Collection;
|
||||||
use Doctrine\ORM\Mapping as ORM;
|
use Doctrine\ORM\Mapping as ORM;
|
||||||
@ -56,28 +55,27 @@ class Choice {
|
|||||||
$this->setCreatedAt( new DateTime() );
|
$this->setCreatedAt( new DateTime() );
|
||||||
$this->poll = new ArrayCollection();
|
$this->poll = new ArrayCollection();
|
||||||
$this->votes = new ArrayCollection();
|
$this->votes = new ArrayCollection();
|
||||||
$this->setDateTime( new DateTime() );
|
|
||||||
if ( $optionalName ) {
|
if ( $optionalName ) {
|
||||||
$this->setName( $optionalName );
|
$this->setName( $optionalName );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function setDateTime( ?DateTimeInterface $dateTime ): self {
|
public function display( $kind = 'text' ): array {
|
||||||
$this->dateTime = $dateTime;
|
if($kind == 'text'){
|
||||||
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function display( $kind = 'text' ) {
|
|
||||||
$fields = [
|
$fields = [
|
||||||
'id' => $this->getId(),
|
'id' => $this->getId(),
|
||||||
'created_at' => $this->getCreatedAtAsString(),
|
'created_at' => $this->getCreatedAtAsString(),
|
||||||
'name' => $this->getName(),
|
'name' => $this->getName(),
|
||||||
'url' => $this->getUrl(),
|
'url' => $this->getUrl(),
|
||||||
];
|
];
|
||||||
if ( $kind === 'date' ) {
|
}
|
||||||
$date = new DateTime( $this->getName() );
|
elseif($kind=='date'){
|
||||||
$fields[ 'name' ] = $date->format( 'c' );
|
$fields = [
|
||||||
|
'id' => $this->getId(),
|
||||||
|
'created_at' => $this->getCreatedAtAsString(),
|
||||||
|
'name' => $this->getName(),
|
||||||
|
// 'url' => $this->getUrl(),
|
||||||
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
return $fields;
|
return $fields;
|
||||||
@ -107,11 +105,7 @@ class Choice {
|
|||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getDateTime(): ?DateTimeInterface {
|
public function getPoll(): ?ArrayCollection {
|
||||||
return $this->dateTime;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getPoll(): ?Poll {
|
|
||||||
return $this->poll;
|
return $this->poll;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -196,6 +196,7 @@
|
|||||||
|
|
||||||
$this->initiate();
|
$this->initiate();
|
||||||
$this->setCreatedAt( new DateTime() );
|
$this->setCreatedAt( new DateTime() );
|
||||||
|
$this->setAdminKey( $this->generateRandomKey() );
|
||||||
$this->votes = new ArrayCollection();
|
$this->votes = new ArrayCollection();
|
||||||
$this->stacksOfVotes = new ArrayCollection();
|
$this->stacksOfVotes = new ArrayCollection();
|
||||||
$this->choices = new ArrayCollection();
|
$this->choices = new ArrayCollection();
|
||||||
@ -243,7 +244,7 @@
|
|||||||
}
|
}
|
||||||
$displayedChoices = [];
|
$displayedChoices = [];
|
||||||
foreach ( $this->getChoices() as $choice ) {
|
foreach ( $this->getChoices() as $choice ) {
|
||||||
$displayedChoices[] = $choice->display();
|
$displayedChoices[] = $choice->display( $this->getKind() );
|
||||||
}
|
}
|
||||||
$displayedComments = [];
|
$displayedComments = [];
|
||||||
foreach ( $this->getComments() as $comment ) {
|
foreach ( $this->getComments() as $comment ) {
|
||||||
@ -276,14 +277,38 @@
|
|||||||
'comments' => $displayedComments,
|
'comments' => $displayedComments,
|
||||||
];
|
];
|
||||||
|
|
||||||
|
if ( $this->getKind() == 'text' ) {
|
||||||
|
$resp[ 'choices' ] = $computedAnswers[ 'answers' ];
|
||||||
|
} elseif ( $this->getKind() == 'date' ) {
|
||||||
|
$resp[ 'choices_grouped' ] = $computedAnswers[ 'grouped_dates' ];
|
||||||
|
}
|
||||||
|
|
||||||
return $resp;
|
return $resp;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function computeAnswers() {
|
public function computeAnswers() {
|
||||||
$computedArray = [];
|
$computedArray = [];
|
||||||
|
$grouped_dates = [];
|
||||||
$maxScore = 0;
|
$maxScore = 0;
|
||||||
|
|
||||||
|
if ( $this->getKind() == 'date' ) {
|
||||||
|
foreach ( $this->getChoices() as $choice ) {
|
||||||
|
$boom = explode( ' >>> ', $choice->getName() );
|
||||||
|
|
||||||
|
|
||||||
|
if ( ! isset( $grouped_dates[ $boom[ 0 ] ] ) ) {
|
||||||
|
|
||||||
|
$grouped_dates[ $boom[ 0 ] ] = [
|
||||||
|
|
||||||
|
"date_string" => $boom[ 0 ],
|
||||||
|
"choices" => [],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
$grouped_dates[ $boom[ 0 ] ][ "choices" ][] = [
|
||||||
|
"choice_id" => $choice->getId(),
|
||||||
|
"name" => $boom[ 1 ] ];
|
||||||
|
}
|
||||||
|
}
|
||||||
$scoreInfos = [
|
$scoreInfos = [
|
||||||
'score' => 0,
|
'score' => 0,
|
||||||
'yes' => [
|
'yes' => [
|
||||||
@ -350,9 +375,25 @@
|
|||||||
foreach ( $computedArray as $choice_stat ) {
|
foreach ( $computedArray as $choice_stat ) {
|
||||||
$answersWithStats[] = $choice_stat;
|
$answersWithStats[] = $choice_stat;
|
||||||
}
|
}
|
||||||
|
$groupsOfDates = [];
|
||||||
|
foreach ( $grouped_dates as $group ) {
|
||||||
|
$ii =0;
|
||||||
|
foreach ( $group["choices"] as $slice ) {
|
||||||
|
$slice['score'] = $computedArray[ $slice['choice_id'] ]['score'];
|
||||||
|
$slice['yes'] = $computedArray[ $slice['choice_id'] ]['yes'];
|
||||||
|
$slice['maybe'] = $computedArray[ $slice['choice_id'] ]['maybe'];
|
||||||
|
$slice['no'] = $computedArray[ $slice['choice_id'] ]['no'];
|
||||||
|
$slice['id'] = $slice['choice_id'];
|
||||||
|
$group["choices"][$ii] = $slice;
|
||||||
|
$ii++;
|
||||||
|
}
|
||||||
|
|
||||||
|
$groupsOfDates[] = $group ;
|
||||||
|
}
|
||||||
|
|
||||||
return [
|
return [
|
||||||
'answers' => $answersWithStats,
|
'answers' => $answersWithStats,
|
||||||
|
'grouped_dates' => $groupsOfDates,
|
||||||
'max_score' => $maxScore,
|
'max_score' => $maxScore,
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user