[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];