forked from GNUsocial/gnu-social
[Poll] Added templates, response counting
This commit is contained in:
@@ -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()];
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
@@ -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()];
|
||||
}
|
||||
}
|
||||
|
@@ -38,6 +38,6 @@ class ShowPoll
|
||||
throw new NoSuchPollException(); //?
|
||||
}
|
||||
|
||||
return ['_template' => 'base.html.twig'];
|
||||
return ['_template' => 'Poll/showpoll.html.twig', 'poll' => $poll];
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user