[Poll] Added variable num of options

not sure if it is the right way to do it
This commit is contained in:
Daniel
2020-11-09 17:43:10 +00:00
committed by Hugo Sales
parent 8543c8c68e
commit 0a1ea8749b
8 changed files with 158 additions and 50 deletions

View File

@@ -22,39 +22,53 @@
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\InvalidFormException;
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;
const MAX_OPTS = 5;
const MIN_OPTS = 2;
class NewPoll
{
private int $numOptions = 3;
public function newpoll(Request $request)
/**
* Create poll
*
* @param Request $request
* @param int $num num of options
*
* @throws InvalidFormException invalid form
* @throws RedirectException
* @throws \App\Util\Exception\NoLoggedInUser user is not logged in
*
* @return array template
*/
public function newpoll(Request $request, int $num)
{
$user = Common::ensureLoggedIn();
$form = NewPollForm::make($this->numOptions);
$user = Common::ensureLoggedIn();
$numOptions = min(max($num,MIN_OPTS),MAX_OPTS);
$form = NewPollForm::make($numOptions);
$form->handleRequest($request);
$opt = [];
if ($form->isSubmitted()) {
$data = $form->getData();
//var_dump($data);
$question = $data['Question'];
for ($i = 1; $i <= $this->numOptions; ++$i) {
array_push($opt,$data['Option_' . $i]);
if ($form->isValid()) {
$data = $form->getData();
//var_dump($data);
$question = $data['Question'];
for ($i = 1; $i <= $numOptions; ++$i) {
array_push($opt, $data['Option_' . $i]);
}
$poll = Poll::make($user->getId(), $question, $opt);
DB::persist($poll);
DB::flush();
//var_dump($testPoll);
throw new RedirectException('showpoll', ['id' => $poll->getId()]);
} else {
throw new InvalidFormException();
}
$poll = Poll::make($question,$opt);
DB::persist($poll);
DB::flush();
//var_dump($testPoll);
throw new RedirectException('showpoll', ['id' => $poll->getId()]);
}
// testing
@@ -77,7 +91,7 @@ class NewPoll
if ($form->isSubmitted())
{
$data = $form->getData();
$this->numOptions = $data['Num_of_Questions'];
NewPoll::numOptions = $data['Num_of_Questions'];
var_dump($data);
}
return ['_template' => 'Poll/newpoll.html.twig', 'form' => $form->createView()];

View File

@@ -26,8 +26,9 @@ use App\Entity\Poll;
use App\Entity\PollResponse;
use App\Util\Common;
use App\Util\Exception\InvalidFormException;
use App\Util\Exception\NotFoundException;
use App\Util\Exception\RedirectException;
use League\Uri\Exception;
use App\Util\Exception\ServerException;
use Plugin\PollPlugin\Forms\PollResponseForm;
use Symfony\Component\HttpFoundation\Request;
@@ -35,6 +36,17 @@ class RespondPoll
{
/**
* Handle poll response
*
* @param Request $request
* @param string $id poll id
*
* @throws InvalidFormException invalid form
* @throws NotFoundException poll does not exist
* @throws RedirectException
* @throws ServerException User already responded to poll
* @throws \App\Util\Exception\NoLoggedInUser User is not logged in
*
* @return array template
*/
public function respondpoll(Request $request, string $id)
{
@@ -43,8 +55,8 @@ class RespondPoll
$poll = Poll::getFromId((int) $id);
//var_dump($poll);
if ($poll == null) {//|| !$poll->isVisibleTo($user)) { todo
throw new Exception(); //?fix
if ($poll == null) {
throw new NotFoundException('Poll does not exist');
}
$question = $poll->getQuestion();
// echo $question;
@@ -55,21 +67,25 @@ class RespondPoll
$form->handleRequest($request);
if ($form->isSubmitted()) {
$data = $form->getData();
$selection = array_values($data)[1];
//echo $selection;
if (!$poll->isValidSelection($selection)) {
if ($form->isValid()) {
$data = $form->getData();
$selection = array_values($data)[1];
//echo $selection;
if (!$poll->isValidSelection($selection)) {
throw new InvalidFormException();
}
if (PollResponse::exits($poll->getId(), $user->getId())) {
throw new ServerException('User already responded to poll');
}
$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()]);
} else {
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' => 'Poll/respondpoll.html.twig', 'question' => $question, 'form' => $form->createView()];

View File

@@ -23,10 +23,22 @@ namespace Plugin\PollPlugin\Controller;
use App\Entity\Poll;
use App\Util\Common;
use App\Util\Exception\NotFoundException;
use Symfony\Component\HttpFoundation\Request;
class ShowPoll
{
/**
* Show poll
*
* @param Request $request
* @param string $id poll id
*
* @throws NotFoundException poll does not exist
* @throws \App\Util\Exception\NoLoggedInUser user is not logged in
*
* @return array Template
*/
public function showpoll(Request $request, string $id)
{
$user = Common::ensureLoggedIn();
@@ -34,8 +46,8 @@ class ShowPoll
$poll = Poll::getFromId((int) $id);
//var_dump($poll);
if ($poll == null) {//|| !$poll->isVisibleTo($user)) { todo
throw new NoSuchPollException(); //?
if ($poll == null) {
throw new NotFoundException('Poll does not exist');
}
return ['_template' => 'Poll/showpoll.html.twig', 'poll' => $poll];

View File

@@ -30,6 +30,13 @@ use Symfony\Component\Form\Form as SymfForm;
const MAX_OPT = 5;
class NewPollForm extends Form
{
/**
* Creates a form with variable num of fields
*
* @param int $optionNum
*
* @return SymfForm
*/
public static function make(int $optionNum): SymfForm
{
$optionNum = min(MAX_OPT,$optionNum);

View File

@@ -29,6 +29,13 @@ use Symfony\Component\Form\Form as SymfForm;
class PollResponseForm extends Form
{
/**
* Creates a radio form with the options given
*
* @param array $opts options
*
* @return SymfForm
*/
public static function make(array $opts): SymfForm
{
$formOptions = [];

View File

@@ -22,7 +22,9 @@ namespace Plugin\PollPlugin;
use App\Core\Event;
use App\Core\Module;
use App\Core\Router\RouteLoader;
use Plugin\PollPlugin\Entity\Poll;
use Symfony\Bundle\FrameworkBundle\Controller\RedirectController;
const ID_FMT = '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}';
@@ -61,12 +63,20 @@ class PollPlugin extends Module
}
*/
public function onAddRoute($r)
/**
* Map URLs to actions
*
* @param RouteLoader $r
*
* @return bool hook value; true means continue processing, false means stop.
*/
public function onAddRoute(RouteLoader $r): bool
{
$r->connect('newpoll', 'main/poll/new', [Controller\NewPoll::class, 'newpoll']);
$r->connect('newpollnum', 'main/poll/new/{num<\\d*>}', [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('respondpoll', 'main/poll/{id<\\d*>}/respond',[Controller\RespondPoll::class, 'respondpoll']);
$r->connect('newpoll', 'main/poll/new', RedirectController::class, ['defaults' => ['route' => 'newpollnum', 'num' => 3]]);
return Event::next;
}