From cdbf7da8be8f63fa14dad61b90b496b22342c133 Mon Sep 17 00:00:00 2001 From: Daniel Date: Sun, 8 Nov 2020 16:36:06 +0000 Subject: [PATCH] [Poll] Added templates, response counting --- plugins/PollPlugin/Controller/NewPoll.php | 36 ++++++++++--- plugins/PollPlugin/Controller/RespondPoll.php | 13 +++-- plugins/PollPlugin/Controller/ShowPoll.php | 2 +- plugins/PollPlugin/Forms/NewPollForm.php | 1 + plugins/PollPlugin/Forms/PollResponseForm.php | 2 +- plugins/PollPlugin/PollPlugin.php | 2 +- src/Entity/Poll.php | 16 ++++++ src/Entity/PollResponse.php | 35 ++++++++----- templates/Poll/newpoll.html.twig | 50 ++++++++++++------- templates/Poll/respondpoll.html.twig | 36 +++++++++++++ templates/Poll/showpoll.html.twig | 39 +++++++++++++++ 11 files changed, 187 insertions(+), 45 deletions(-) create mode 100644 templates/Poll/respondpoll.html.twig create mode 100644 templates/Poll/showpoll.html.twig diff --git a/plugins/PollPlugin/Controller/NewPoll.php b/plugins/PollPlugin/Controller/NewPoll.php index 038c5b86da..c30a32ed82 100644 --- a/plugins/PollPlugin/Controller/NewPoll.php +++ b/plugins/PollPlugin/Controller/NewPoll.php @@ -22,33 +22,39 @@ namespace Plugin\PollPlugin\Controller; use App\Core\DB\DB; +use App\Core\Form; +use function App\Core\I18n\_m; use App\Entity\Poll; use App\Util\Common; +use App\Util\Exception\RedirectException; use Plugin\PollPlugin\Forms\NewPollForm; +use Symfony\Component\Form\Extension\Core\Type\NumberType; +use Symfony\Component\Form\Extension\Core\Type\SubmitType; use Symfony\Component\HttpFoundation\Request; class NewPoll { + private int $numOptions = 3; + public function newpoll(Request $request) { $user = Common::ensureLoggedIn(); - $numOptions = 3; //temporary - $form = NewPollForm::make($numOptions); - + $form = NewPollForm::make($this->numOptions); $form->handleRequest($request); - $question = 'Test Question?'; - $opt = []; + $opt = []; if ($form->isSubmitted()) { $data = $form->getData(); //var_dump($data); - for ($i = 1; $i <= $numOptions; ++$i) { + $question = $data['Question']; + for ($i = 1; $i <= $this->numOptions; ++$i) { array_push($opt,$data['Option_' . $i]); } - $testPoll = Poll::make($question,$opt); - DB::persist($testPoll); + $poll = Poll::make($question,$opt); + DB::persist($poll); DB::flush(); //var_dump($testPoll); + throw new RedirectException('showpoll', ['id' => $poll->getId()]); } // testing @@ -63,4 +69,18 @@ class NewPoll return ['_template' => 'Poll/newpoll.html.twig', 'form' => $form->createView()]; } + /* + public function pollsettings(Request $request) + { + $form = Form::create([['Num_of_Questions', NumberType::class, ['label' => _m(('Number of questions:'))]],['save', SubmitType::class, ['label' => _m('Continue')]]]); + $form->handleRequest($request); + if ($form->isSubmitted()) + { + $data = $form->getData(); + $this->numOptions = $data['Num_of_Questions']; + var_dump($data); + } + return ['_template' => 'Poll/newpoll.html.twig', 'form' => $form->createView()]; + } + */ } diff --git a/plugins/PollPlugin/Controller/RespondPoll.php b/plugins/PollPlugin/Controller/RespondPoll.php index f07de00dfd..f4e3e53672 100644 --- a/plugins/PollPlugin/Controller/RespondPoll.php +++ b/plugins/PollPlugin/Controller/RespondPoll.php @@ -26,6 +26,7 @@ use App\Entity\Poll; use App\Entity\PollResponse; use App\Util\Common; use App\Util\Exception\InvalidFormException; +use App\Util\Exception\RedirectException; use League\Uri\Exception; use Plugin\PollPlugin\Forms\PollResponseForm; use Symfony\Component\HttpFoundation\Request; @@ -45,6 +46,8 @@ class RespondPoll if ($poll == null) {//|| !$poll->isVisibleTo($user)) { todo throw new Exception(); //?fix } + $question = $poll->getQuestion(); + // echo $question; $opts = $poll->getOptionsArr(); //var_dump($opts); @@ -54,17 +57,21 @@ class RespondPoll if ($form->isSubmitted()) { $data = $form->getData(); $selection = array_values($data)[1]; - echo $selection; + //echo $selection; if (!$poll->isValidSelection($selection)) { throw new InvalidFormException(); } + if (PollResponse::exits($poll->getId(),$user->getId())) { + throw new Exception(); + } + $pollResponse = PollResponse::create(['poll_id' => $poll->getId(), 'gsactor_id' => $user->getId(), 'selection' => $selection]); DB::persist($pollResponse); DB::flush(); //var_dump($pollResponse); + throw new RedirectException('showpoll', ['id' => $poll->getId()]); } - //return ['_template' => 'base.html.twig']; - return ['_template' => 'Poll/newpoll.html.twig', 'form' => $form->createView()]; + return ['_template' => 'Poll/respondpoll.html.twig', 'question' => $question, 'form' => $form->createView()]; } } diff --git a/plugins/PollPlugin/Controller/ShowPoll.php b/plugins/PollPlugin/Controller/ShowPoll.php index 1b7ccc89b3..442bb981cd 100644 --- a/plugins/PollPlugin/Controller/ShowPoll.php +++ b/plugins/PollPlugin/Controller/ShowPoll.php @@ -38,6 +38,6 @@ class ShowPoll throw new NoSuchPollException(); //? } - return ['_template' => 'base.html.twig']; + return ['_template' => 'Poll/showpoll.html.twig', 'poll' => $poll]; } } diff --git a/plugins/PollPlugin/Forms/NewPollForm.php b/plugins/PollPlugin/Forms/NewPollForm.php index d012daf76e..b9fd410919 100644 --- a/plugins/PollPlugin/Forms/NewPollForm.php +++ b/plugins/PollPlugin/Forms/NewPollForm.php @@ -34,6 +34,7 @@ class NewPollForm extends Form { $optionNum = min(MAX_OPT,$optionNum); $options = []; + array_push($options,['Question', TextType::class, ['label' => _m(('Question'))]]); for ($i = 1; $i <= $optionNum; ++$i) { //['Option_i', TextType::class, ['label' => _m('Option i')]], array_push($options,['Option_' . $i, TextType::class, ['label' => _m(('Option ' . $i))]]); diff --git a/plugins/PollPlugin/Forms/PollResponseForm.php b/plugins/PollPlugin/Forms/PollResponseForm.php index ef09e3584d..2ff5765335 100644 --- a/plugins/PollPlugin/Forms/PollResponseForm.php +++ b/plugins/PollPlugin/Forms/PollResponseForm.php @@ -36,7 +36,7 @@ class PollResponseForm extends Form for ($i = 1; $i <= count($opts); ++$i) { $options[$opts[$i - 1]] = $i; } - array_push($formOptions, ['Question', ChoiceType::class, [ + array_push($formOptions, ['Options:', ChoiceType::class, [ 'choices' => $options, 'expanded' => true, ]]); diff --git a/plugins/PollPlugin/PollPlugin.php b/plugins/PollPlugin/PollPlugin.php index 3edce22127..5bf7f60258 100644 --- a/plugins/PollPlugin/PollPlugin.php +++ b/plugins/PollPlugin/PollPlugin.php @@ -66,7 +66,7 @@ class PollPlugin extends Module $r->connect('newpoll', 'main/poll/new', [Controller\NewPoll::class, 'newpoll']); //$r->connect('showpoll', 'main/poll/:{id<' . ID_FMT . '>}' , [Controller\ShowPoll::class, 'showpoll']); //doesnt work $r->connect('showpoll', 'main/poll/{id<\\d*>}',[Controller\ShowPoll::class, 'showpoll']); - $r->connect('showpoll', 'main/poll/{id<\\d*>}/respond',[Controller\RespondPoll::class, 'respondpoll']); + $r->connect('respondpoll', 'main/poll/{id<\\d*>}/respond',[Controller\RespondPoll::class, 'respondpoll']); return Event::next; } diff --git a/src/Entity/Poll.php b/src/Entity/Poll.php index e2e3eda8a8..82c8e7d393 100644 --- a/src/Entity/Poll.php +++ b/src/Entity/Poll.php @@ -24,6 +24,7 @@ namespace App\Entity; use App\Core\DB\DB; use App\Core\Entity; use DateTimeInterface; +use function Functional\id; class Poll extends Entity { @@ -162,6 +163,21 @@ class Poll extends Entity return true; } + public function countResponses(): array + { + $responses = []; + $options = $this->getOptionsArr(); + for ($i = 1; $i <= count($options); ++$i) { + $responses[$options[$i - 1]] = $count = DB::dql('select count(pr) from App\Entity\Poll p, App\Entity\PollResponse pr + where pr.poll_id = :id and pr.selection = :selection', + ['id' => $this->id, 'selection' => $i])[0][1] / $this->id; //todo: fix + } + + //echo var_dump($count); + //var_dump($responses); + return $responses; + } + //from old version /** * Get a bookmark based on a notice diff --git a/src/Entity/PollResponse.php b/src/Entity/PollResponse.php index 81f97f8a8c..5e0fdfcb46 100644 --- a/src/Entity/PollResponse.php +++ b/src/Entity/PollResponse.php @@ -21,6 +21,7 @@ namespace App\Entity; +use App\Core\DB\DB; use App\Core\Entity; use DateTimeInterface; @@ -30,7 +31,7 @@ class PollResponse extends Entity private int $id; private ?string $uri; - private string $poll_id; + private int $poll_id; private ?int $gsactor_id; private ?int $selection; private DateTimeInterface $created; @@ -57,13 +58,13 @@ class PollResponse extends Entity return $this->uri; } - public function setPollId(string $poll_id): self + public function setPollId(int $poll_id): self { $this->poll_id = $poll_id; return $this; } - public function getPollId(): string + public function getPollId(): int { return $this->poll_id; } @@ -115,22 +116,30 @@ class PollResponse extends Entity 'id' => ['type' => 'serial', 'not null' => true], //'uri' => array('type' => 'varchar', 'length' => 191, 'not null' => true, 'description' => 'UUID to the response notice'), 'uri' => ['type' => 'varchar', 'length' => 191, 'description' => 'UUID to the response notice'], - 'poll_id' => ['type' => 'char', 'length' => 36, 'not null' => true, 'description' => 'UUID of poll being responded to'], + 'poll_id' => ['type' => 'int', 'length' => 36, 'not null' => true, 'description' => 'UUID of poll being responded to'], 'gsactor_id' => ['type' => 'int'], 'selection' => ['type' => 'int'], 'created' => ['type' => 'datetime', 'not null' => true], ], 'primary key' => ['id'], - /* - 'unique keys' => array( - 'poll_uri_key' => array('uri'), - 'poll_response_poll_id_profile_id_key' => array('poll_id', 'profile_id'), - ), - 'indexes' => array( - 'poll_response_profile_id_poll_id_index' => array('profile_id', 'poll_id'), - ) - */ + 'unique keys' => [ + //'poll_uri_key' => array('uri'), + //'poll_response_poll_id_gsactor_id_key' => ['poll_id', 'gsactor_id'], //doctrine bug? + ], + + 'indexes' => [ + 'poll_response_gsactor_id_poll_id_index' => ['gsactor_id', 'poll_id'], + ], ]; } + + public static function exits(int $pollId, int $gsactorId): bool + { + $res = DB::dql('select pr from App\Entity\PollResponse pr + where pr.poll_id = :pollId and pr.gsactor_id = :gsactorId', + ['pollId' => $pollId, 'gsactorId' => $gsactorId]); + //var_dump( $res); + return count($res) != 0; + } } diff --git a/templates/Poll/newpoll.html.twig b/templates/Poll/newpoll.html.twig index ed9fe05ef1..d7c1bbbaf9 100644 --- a/templates/Poll/newpoll.html.twig +++ b/templates/Poll/newpoll.html.twig @@ -1,20 +1,34 @@ - - - - - {% block title %}Poll test page!{% endblock %} - {% block stylesheets %}{% endblock %} - - - - {% block body %}{% endblock %} - {% block javascripts %}{% endblock %} +{% extends 'left/left.html.twig' %} + +{% block meta %} + {{ parent() }} +{% endblock %} + +{% block title %}New Poll{% endblock %} + +{% block stylesheets %} + {{ parent() }} + + + +{% endblock %} + +{% block header %} + {{ parent() }} +{% endblock %} + +{% block left %} + {{ parent() }} +{% endblock %} + +{% block body %} +
{{ form(form) }} - - +
+{% endblock body %} + +{% block javascripts %}{% endblock %} \ No newline at end of file diff --git a/templates/Poll/respondpoll.html.twig b/templates/Poll/respondpoll.html.twig new file mode 100644 index 0000000000..da4570f034 --- /dev/null +++ b/templates/Poll/respondpoll.html.twig @@ -0,0 +1,36 @@ +{% extends 'left/left.html.twig' %} + +{% block meta %} + {{ parent() }} +{% endblock %} + +{% block title %}Poll{% endblock %} + +{% block stylesheets %} + {{ parent() }} + + + +{% endblock %} + +{% block header %} + {{ parent() }} +{% endblock %} + +{% block left %} + {{ parent() }} +{% endblock %} + +{% block body %} +
+
+

{{ question }}

+ {{ form(form) }} +
+
+{% endblock body %} + +{% block javascripts %}{% endblock %} \ No newline at end of file diff --git a/templates/Poll/showpoll.html.twig b/templates/Poll/showpoll.html.twig new file mode 100644 index 0000000000..5d612c4b95 --- /dev/null +++ b/templates/Poll/showpoll.html.twig @@ -0,0 +1,39 @@ +{% extends 'left/left.html.twig' %} + +{% block meta %} + {{ parent() }} +{% endblock %} + +{% block title %}Poll{% endblock %} + +{% block stylesheets %} + {{ parent() }} + + + +{% endblock %} + +{% block header %} + {{ parent() }} +{% endblock %} + +{% block left %} + {{ parent() }} +{% endblock %} + +{% block body %} +
+ +
+{% endblock body %} + +{% block javascripts %}{% endblock %} \ No newline at end of file