diff --git a/plugins/PollPlugin/Controller/RespondPoll.php b/plugins/PollPlugin/Controller/RespondPoll.php index 15fab73201..f07de00dfd 100644 --- a/plugins/PollPlugin/Controller/RespondPoll.php +++ b/plugins/PollPlugin/Controller/RespondPoll.php @@ -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); @@ -45,9 +52,16 @@ class RespondPoll $form->handleRequest($request); if ($form->isSubmitted()) { - $data = $form->getData(); - $choice = array_values($data)[1]; - //echo $choice; + $data = $form->getData(); + $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']; diff --git a/plugins/PollPlugin/Forms/PollResponseForm.php b/plugins/PollPlugin/Forms/PollResponseForm.php index 46e5887a15..ef09e3584d 100644 --- a/plugins/PollPlugin/Forms/PollResponseForm.php +++ b/plugins/PollPlugin/Forms/PollResponseForm.php @@ -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, diff --git a/src/Entity/Poll.php b/src/Entity/Poll.php index daba73760f..e2e3eda8a8 100644 --- a/src/Entity/Poll.php +++ b/src/Entity/Poll.php @@ -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() {