[Poll] Added templates, response counting

This commit is contained in:
Daniel 2020-11-08 16:36:06 +00:00 committed by Hugo Sales
parent 27a0c43f7b
commit cdbf7da8be
Signed by: someonewithpc
GPG Key ID: 7D0C7EAFC9D835A0
11 changed files with 187 additions and 45 deletions

View File

@ -22,33 +22,39 @@
namespace Plugin\PollPlugin\Controller; namespace Plugin\PollPlugin\Controller;
use App\Core\DB\DB; use App\Core\DB\DB;
use App\Core\Form;
use function App\Core\I18n\_m;
use App\Entity\Poll; use App\Entity\Poll;
use App\Util\Common; use App\Util\Common;
use App\Util\Exception\RedirectException;
use Plugin\PollPlugin\Forms\NewPollForm; 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; use Symfony\Component\HttpFoundation\Request;
class NewPoll class NewPoll
{ {
private int $numOptions = 3;
public function newpoll(Request $request) public function newpoll(Request $request)
{ {
$user = Common::ensureLoggedIn(); $user = Common::ensureLoggedIn();
$numOptions = 3; //temporary $form = NewPollForm::make($this->numOptions);
$form = NewPollForm::make($numOptions);
$form->handleRequest($request); $form->handleRequest($request);
$question = 'Test Question?'; $opt = [];
$opt = [];
if ($form->isSubmitted()) { if ($form->isSubmitted()) {
$data = $form->getData(); $data = $form->getData();
//var_dump($data); //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]); array_push($opt,$data['Option_' . $i]);
} }
$testPoll = Poll::make($question,$opt); $poll = Poll::make($question,$opt);
DB::persist($testPoll); DB::persist($poll);
DB::flush(); DB::flush();
//var_dump($testPoll); //var_dump($testPoll);
throw new RedirectException('showpoll', ['id' => $poll->getId()]);
} }
// testing // testing
@ -63,4 +69,18 @@ class NewPoll
return ['_template' => 'Poll/newpoll.html.twig', 'form' => $form->createView()]; 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()];
}
*/
} }

View File

@ -26,6 +26,7 @@ use App\Entity\Poll;
use App\Entity\PollResponse; use App\Entity\PollResponse;
use App\Util\Common; use App\Util\Common;
use App\Util\Exception\InvalidFormException; use App\Util\Exception\InvalidFormException;
use App\Util\Exception\RedirectException;
use League\Uri\Exception; use League\Uri\Exception;
use Plugin\PollPlugin\Forms\PollResponseForm; use Plugin\PollPlugin\Forms\PollResponseForm;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
@ -45,6 +46,8 @@ class RespondPoll
if ($poll == null) {//|| !$poll->isVisibleTo($user)) { todo if ($poll == null) {//|| !$poll->isVisibleTo($user)) { todo
throw new Exception(); //?fix throw new Exception(); //?fix
} }
$question = $poll->getQuestion();
// echo $question;
$opts = $poll->getOptionsArr(); $opts = $poll->getOptionsArr();
//var_dump($opts); //var_dump($opts);
@ -54,17 +57,21 @@ class RespondPoll
if ($form->isSubmitted()) { if ($form->isSubmitted()) {
$data = $form->getData(); $data = $form->getData();
$selection = array_values($data)[1]; $selection = array_values($data)[1];
echo $selection; //echo $selection;
if (!$poll->isValidSelection($selection)) { if (!$poll->isValidSelection($selection)) {
throw new InvalidFormException(); 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]); $pollResponse = PollResponse::create(['poll_id' => $poll->getId(), 'gsactor_id' => $user->getId(), 'selection' => $selection]);
DB::persist($pollResponse); DB::persist($pollResponse);
DB::flush(); DB::flush();
//var_dump($pollResponse); //var_dump($pollResponse);
throw new RedirectException('showpoll', ['id' => $poll->getId()]);
} }
//return ['_template' => 'base.html.twig']; return ['_template' => 'Poll/respondpoll.html.twig', 'question' => $question, 'form' => $form->createView()];
return ['_template' => 'Poll/newpoll.html.twig', 'form' => $form->createView()];
} }
} }

View File

@ -38,6 +38,6 @@ class ShowPoll
throw new NoSuchPollException(); //? throw new NoSuchPollException(); //?
} }
return ['_template' => 'base.html.twig']; return ['_template' => 'Poll/showpoll.html.twig', 'poll' => $poll];
} }
} }

View File

@ -34,6 +34,7 @@ class NewPollForm extends Form
{ {
$optionNum = min(MAX_OPT,$optionNum); $optionNum = min(MAX_OPT,$optionNum);
$options = []; $options = [];
array_push($options,['Question', TextType::class, ['label' => _m(('Question'))]]);
for ($i = 1; $i <= $optionNum; ++$i) { for ($i = 1; $i <= $optionNum; ++$i) {
//['Option_i', TextType::class, ['label' => _m('Option i')]], //['Option_i', TextType::class, ['label' => _m('Option i')]],
array_push($options,['Option_' . $i, TextType::class, ['label' => _m(('Option ' . $i))]]); array_push($options,['Option_' . $i, TextType::class, ['label' => _m(('Option ' . $i))]]);

View File

@ -36,7 +36,7 @@ class PollResponseForm extends Form
for ($i = 1; $i <= count($opts); ++$i) { for ($i = 1; $i <= count($opts); ++$i) {
$options[$opts[$i - 1]] = $i; $options[$opts[$i - 1]] = $i;
} }
array_push($formOptions, ['Question', ChoiceType::class, [ array_push($formOptions, ['Options:', ChoiceType::class, [
'choices' => $options, 'choices' => $options,
'expanded' => true, 'expanded' => true,
]]); ]]);

View File

@ -66,7 +66,7 @@ class PollPlugin extends Module
$r->connect('newpoll', 'main/poll/new', [Controller\NewPoll::class, 'newpoll']); $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<' . 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*>}',[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; return Event::next;
} }

View File

@ -24,6 +24,7 @@ namespace App\Entity;
use App\Core\DB\DB; use App\Core\DB\DB;
use App\Core\Entity; use App\Core\Entity;
use DateTimeInterface; use DateTimeInterface;
use function Functional\id;
class Poll extends Entity class Poll extends Entity
{ {
@ -162,6 +163,21 @@ class Poll extends Entity
return true; 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 //from old version
/** /**
* Get a bookmark based on a notice * Get a bookmark based on a notice

View File

@ -21,6 +21,7 @@
namespace App\Entity; namespace App\Entity;
use App\Core\DB\DB;
use App\Core\Entity; use App\Core\Entity;
use DateTimeInterface; use DateTimeInterface;
@ -30,7 +31,7 @@ class PollResponse extends Entity
private int $id; private int $id;
private ?string $uri; private ?string $uri;
private string $poll_id; private int $poll_id;
private ?int $gsactor_id; private ?int $gsactor_id;
private ?int $selection; private ?int $selection;
private DateTimeInterface $created; private DateTimeInterface $created;
@ -57,13 +58,13 @@ class PollResponse extends Entity
return $this->uri; return $this->uri;
} }
public function setPollId(string $poll_id): self public function setPollId(int $poll_id): self
{ {
$this->poll_id = $poll_id; $this->poll_id = $poll_id;
return $this; return $this;
} }
public function getPollId(): string public function getPollId(): int
{ {
return $this->poll_id; return $this->poll_id;
} }
@ -115,22 +116,30 @@ class PollResponse extends Entity
'id' => ['type' => 'serial', 'not null' => true], 'id' => ['type' => 'serial', 'not null' => true],
//'uri' => array('type' => 'varchar', 'length' => 191, 'not null' => true, 'description' => 'UUID to the response notice'), //'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'], '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'], 'gsactor_id' => ['type' => 'int'],
'selection' => ['type' => 'int'], 'selection' => ['type' => 'int'],
'created' => ['type' => 'datetime', 'not null' => true], 'created' => ['type' => 'datetime', 'not null' => true],
], ],
'primary key' => ['id'], '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( 'unique keys' => [
'poll_response_profile_id_poll_id_index' => array('profile_id', 'poll_id'), //'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;
}
} }

View File

@ -1,20 +1,34 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>{% block title %}Poll test page!{% endblock %}</title>
{% block stylesheets %}{% endblock %}
<style>
body {
background-color: #333;
color: #ddd;
}
</style>
</head>
<body>
{% block body %}{% endblock %}
{% block javascripts %}{% endblock %}
{% extends 'left/left.html.twig' %}
{% block meta %}
{{ parent() }}
{% endblock %}
{% block title %}New Poll{% endblock %}
{% block stylesheets %}
{{ parent() }}
<link rel='stylesheet' type='text/css' href="{{ asset('assets/css/network/public.css') }}"
media="screen and (min-width: 1300px)">
<link rel='stylesheet' type='text/css' href="{{ asset('assets/css/network/public_mid.css') }}"
media="screen and (max-width: 1300px)">
<link rel='stylesheet' type='text/css' href="{{ asset('assets/css/network/public_small.css') }}"
media="screen and (max-width: 750px)">
{% endblock %}
{% block header %}
{{ parent() }}
{% endblock %}
{% block left %}
{{ parent() }}
{% endblock %}
{% block body %}
<div class="content">
{{ form(form) }} {{ form(form) }}
</body> </div>
</html> {% endblock body %}
{% block javascripts %}{% endblock %}

View File

@ -0,0 +1,36 @@
{% extends 'left/left.html.twig' %}
{% block meta %}
{{ parent() }}
{% endblock %}
{% block title %}Poll{% endblock %}
{% block stylesheets %}
{{ parent() }}
<link rel='stylesheet' type='text/css' href="{{ asset('assets/css/network/public.css') }}"
media="screen and (min-width: 1300px)">
<link rel='stylesheet' type='text/css' href="{{ asset('assets/css/network/public_mid.css') }}"
media="screen and (max-width: 1300px)">
<link rel='stylesheet' type='text/css' href="{{ asset('assets/css/network/public_small.css') }}"
media="screen and (max-width: 750px)">
{% endblock %}
{% block header %}
{{ parent() }}
{% endblock %}
{% block left %}
{{ parent() }}
{% endblock %}
{% block body %}
<div class="content">
<div>
<h2>{{ question }}</h2>
{{ form(form) }}
</div>
</div>
{% endblock body %}
{% block javascripts %}{% endblock %}

View File

@ -0,0 +1,39 @@
{% extends 'left/left.html.twig' %}
{% block meta %}
{{ parent() }}
{% endblock %}
{% block title %}Poll{% endblock %}
{% block stylesheets %}
{{ parent() }}
<link rel='stylesheet' type='text/css' href="{{ asset('assets/css/network/public.css') }}"
media="screen and (min-width: 1300px)">
<link rel='stylesheet' type='text/css' href="{{ asset('assets/css/network/public_mid.css') }}"
media="screen and (max-width: 1300px)">
<link rel='stylesheet' type='text/css' href="{{ asset('assets/css/network/public_small.css') }}"
media="screen and (max-width: 750px)">
{% endblock %}
{% block header %}
{{ parent() }}
{% endblock %}
{% block left %}
{{ parent() }}
{% endblock %}
{% block body %}
<div class="content">
<ul>
<h2>{{ poll.question }}</h2>
{% for question, votes in poll.countResponses() %}
<li>{{ question }}: {{ votes }}</li>
{% endfor %}
</ul>
</div>
{% endblock body %}
{% block javascripts %}{% endblock %}