[Poll] Store poll response to DB
This commit is contained in:
parent
3725818e4f
commit
27a0c43f7b
@ -21,13 +21,20 @@
|
||||
|
||||
namespace Plugin\PollPlugin\Controller;
|
||||
|
||||
use App\Core\DB\DB;
|
||||
use App\Entity\Poll;
|
||||
use App\Entity\PollResponse;
|
||||
use App\Util\Common;
|
||||
use App\Util\Exception\InvalidFormException;
|
||||
use League\Uri\Exception;
|
||||
use Plugin\PollPlugin\Forms\PollResponseForm;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
|
||||
class RespondPoll
|
||||
{
|
||||
/**
|
||||
* Handle poll response
|
||||
*/
|
||||
public function respondpoll(Request $request, string $id)
|
||||
{
|
||||
$user = Common::ensureLoggedIn();
|
||||
@ -36,7 +43,7 @@ class RespondPoll
|
||||
//var_dump($poll);
|
||||
|
||||
if ($poll == null) {//|| !$poll->isVisibleTo($user)) { todo
|
||||
throw new NoSuchPollException(); //?
|
||||
throw new Exception(); //?fix
|
||||
}
|
||||
$opts = $poll->getOptionsArr();
|
||||
//var_dump($opts);
|
||||
@ -46,8 +53,15 @@ class RespondPoll
|
||||
$form->handleRequest($request);
|
||||
if ($form->isSubmitted()) {
|
||||
$data = $form->getData();
|
||||
$choice = array_values($data)[1];
|
||||
//echo $choice;
|
||||
$selection = array_values($data)[1];
|
||||
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'];
|
||||
|
@ -33,8 +33,8 @@ class PollResponseForm extends Form
|
||||
{
|
||||
$formOptions = [];
|
||||
$options = [];
|
||||
for ($i = 0; $i < count($opts); ++$i) {
|
||||
$options[$opts[$i]] = $i;
|
||||
for ($i = 1; $i <= count($opts); ++$i) {
|
||||
$options[$opts[$i - 1]] = $i;
|
||||
}
|
||||
array_push($formOptions, ['Question', ChoiceType::class, [
|
||||
'choices' => $options,
|
||||
|
@ -139,11 +139,29 @@ class Poll extends Entity
|
||||
return self::create(['question' => $question, 'options' => $options]);
|
||||
}
|
||||
|
||||
public function getOptionsArr()
|
||||
public function getOptionsArr(): array
|
||||
{
|
||||
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
|
||||
/**
|
||||
* 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()
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user