[Poll] Store poll response to DB
This commit is contained in:
parent
3725818e4f
commit
27a0c43f7b
@ -21,13 +21,20 @@
|
|||||||
|
|
||||||
namespace Plugin\PollPlugin\Controller;
|
namespace Plugin\PollPlugin\Controller;
|
||||||
|
|
||||||
|
use App\Core\DB\DB;
|
||||||
use App\Entity\Poll;
|
use App\Entity\Poll;
|
||||||
|
use App\Entity\PollResponse;
|
||||||
use App\Util\Common;
|
use App\Util\Common;
|
||||||
|
use App\Util\Exception\InvalidFormException;
|
||||||
|
use League\Uri\Exception;
|
||||||
use Plugin\PollPlugin\Forms\PollResponseForm;
|
use Plugin\PollPlugin\Forms\PollResponseForm;
|
||||||
use Symfony\Component\HttpFoundation\Request;
|
use Symfony\Component\HttpFoundation\Request;
|
||||||
|
|
||||||
class RespondPoll
|
class RespondPoll
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* Handle poll response
|
||||||
|
*/
|
||||||
public function respondpoll(Request $request, string $id)
|
public function respondpoll(Request $request, string $id)
|
||||||
{
|
{
|
||||||
$user = Common::ensureLoggedIn();
|
$user = Common::ensureLoggedIn();
|
||||||
@ -36,7 +43,7 @@ class RespondPoll
|
|||||||
//var_dump($poll);
|
//var_dump($poll);
|
||||||
|
|
||||||
if ($poll == null) {//|| !$poll->isVisibleTo($user)) { todo
|
if ($poll == null) {//|| !$poll->isVisibleTo($user)) { todo
|
||||||
throw new NoSuchPollException(); //?
|
throw new Exception(); //?fix
|
||||||
}
|
}
|
||||||
$opts = $poll->getOptionsArr();
|
$opts = $poll->getOptionsArr();
|
||||||
//var_dump($opts);
|
//var_dump($opts);
|
||||||
@ -45,9 +52,16 @@ class RespondPoll
|
|||||||
|
|
||||||
$form->handleRequest($request);
|
$form->handleRequest($request);
|
||||||
if ($form->isSubmitted()) {
|
if ($form->isSubmitted()) {
|
||||||
$data = $form->getData();
|
$data = $form->getData();
|
||||||
$choice = array_values($data)[1];
|
$selection = array_values($data)[1];
|
||||||
//echo $choice;
|
echo $selection;
|
||||||
|
if (!$poll->isValidSelection($selection)) {
|
||||||
|
throw new InvalidFormException();
|
||||||
|
}
|
||||||
|
$pollResponse = PollResponse::create(['poll_id' => $poll->getId(), 'gsactor_id' => $user->getId(), 'selection' => $selection]);
|
||||||
|
DB::persist($pollResponse);
|
||||||
|
DB::flush();
|
||||||
|
//var_dump($pollResponse);
|
||||||
}
|
}
|
||||||
|
|
||||||
//return ['_template' => 'base.html.twig'];
|
//return ['_template' => 'base.html.twig'];
|
||||||
|
@ -33,8 +33,8 @@ class PollResponseForm extends Form
|
|||||||
{
|
{
|
||||||
$formOptions = [];
|
$formOptions = [];
|
||||||
$options = [];
|
$options = [];
|
||||||
for ($i = 0; $i < count($opts); ++$i) {
|
for ($i = 1; $i <= count($opts); ++$i) {
|
||||||
$options[$opts[$i]] = $i;
|
$options[$opts[$i - 1]] = $i;
|
||||||
}
|
}
|
||||||
array_push($formOptions, ['Question', ChoiceType::class, [
|
array_push($formOptions, ['Question', ChoiceType::class, [
|
||||||
'choices' => $options,
|
'choices' => $options,
|
||||||
|
@ -139,11 +139,29 @@ class Poll extends Entity
|
|||||||
return self::create(['question' => $question, 'options' => $options]);
|
return self::create(['question' => $question, 'options' => $options]);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getOptionsArr()
|
public function getOptionsArr(): array
|
||||||
{
|
{
|
||||||
return explode("\n", $this->options);
|
return explode("\n", $this->options);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Is this a valid selection index?
|
||||||
|
*
|
||||||
|
* @param int $selection (1-based)
|
||||||
|
*
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function isValidSelection($selection): bool
|
||||||
|
{
|
||||||
|
if ($selection != (int) $selection) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if ($selection < 1 || $selection > count($this->getOptionsArr())) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
//from old version
|
//from old version
|
||||||
/**
|
/**
|
||||||
* Get a bookmark based on a notice
|
* Get a bookmark based on a notice
|
||||||
@ -159,26 +177,6 @@ class Poll extends Entity
|
|||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/**
|
|
||||||
* Is this a valid selection index?
|
|
||||||
*
|
|
||||||
* @param int $selection (1-based)
|
|
||||||
*
|
|
||||||
* @return bool
|
|
||||||
*/
|
|
||||||
/*
|
|
||||||
public function isValidSelection($selection)
|
|
||||||
{
|
|
||||||
if ($selection != intval($selection)) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
if ($selection < 1 || $selection > count($this->getOptions())) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
public function getNotice()
|
public function getNotice()
|
||||||
{
|
{
|
||||||
|
Loading…
Reference in New Issue
Block a user