From 3725818e4f21cfb06d997e57f4524e48851dee23 Mon Sep 17 00:00:00 2001 From: Daniel Date: Wed, 4 Nov 2020 18:58:20 +0000 Subject: [PATCH] [Poll] Added New Route, RespondPoll, Poll Response, PollResponseForm --- plugins/PollPlugin/Controller/NewPoll.php | 25 +++- plugins/PollPlugin/Controller/RespondPoll.php | 56 ++++++++ plugins/PollPlugin/Forms/PollResponseForm.php | 47 ++++++ plugins/PollPlugin/PollPlugin.php | 1 + src/Entity/Poll.php | 46 +++--- src/Entity/PollResponse.php | 136 ++++++++++++++++++ 6 files changed, 286 insertions(+), 25 deletions(-) create mode 100644 plugins/PollPlugin/Controller/RespondPoll.php create mode 100644 plugins/PollPlugin/Forms/PollResponseForm.php create mode 100644 src/Entity/PollResponse.php diff --git a/plugins/PollPlugin/Controller/NewPoll.php b/plugins/PollPlugin/Controller/NewPoll.php index 23e0587833..038c5b86da 100644 --- a/plugins/PollPlugin/Controller/NewPoll.php +++ b/plugins/PollPlugin/Controller/NewPoll.php @@ -23,6 +23,7 @@ namespace Plugin\PollPlugin\Controller; use App\Core\DB\DB; use App\Entity\Poll; +use App\Util\Common; use Plugin\PollPlugin\Forms\NewPollForm; use Symfony\Component\HttpFoundation\Request; @@ -30,18 +31,32 @@ class NewPoll { public function newpoll(Request $request) { - $form = NewPollForm::make(3); + $user = Common::ensureLoggedIn(); + + $numOptions = 3; //temporary + $form = NewPollForm::make($numOptions); $form->handleRequest($request); + $question = 'Test Question?'; + $opt = []; if ($form->isSubmitted()) { $data = $form->getData(); //var_dump($data); + for ($i = 1; $i <= $numOptions; ++$i) { + array_push($opt,$data['Option_' . $i]); + } + $testPoll = Poll::make($question,$opt); + DB::persist($testPoll); + DB::flush(); + //var_dump($testPoll); } - /* testing - $test = Poll::create(['id' => '0', 'uri' => 'a']); - DB::persist($test); - DB::flush(); + // testing + + //$test = Poll::create(['id' => '0', 'uri' => 'a']); + //DB::persist($test); + //DB::flush(); + /* $loadpoll = Poll::getFromId('0'); var_dump($loadpoll); */ diff --git a/plugins/PollPlugin/Controller/RespondPoll.php b/plugins/PollPlugin/Controller/RespondPoll.php new file mode 100644 index 0000000000..15fab73201 --- /dev/null +++ b/plugins/PollPlugin/Controller/RespondPoll.php @@ -0,0 +1,56 @@ +. + +// }}} + +namespace Plugin\PollPlugin\Controller; + +use App\Entity\Poll; +use App\Util\Common; +use Plugin\PollPlugin\Forms\PollResponseForm; +use Symfony\Component\HttpFoundation\Request; + +class RespondPoll +{ + public function respondpoll(Request $request, string $id) + { + $user = Common::ensureLoggedIn(); + + $poll = Poll::getFromId((int) $id); + //var_dump($poll); + + if ($poll == null) {//|| !$poll->isVisibleTo($user)) { todo + throw new NoSuchPollException(); //? + } + $opts = $poll->getOptionsArr(); + //var_dump($opts); + + $form = PollResponseForm::make($opts); + + $form->handleRequest($request); + if ($form->isSubmitted()) { + $data = $form->getData(); + $choice = array_values($data)[1]; + //echo $choice; + } + + //return ['_template' => 'base.html.twig']; + return ['_template' => 'Poll/newpoll.html.twig', 'form' => $form->createView()]; + } +} diff --git a/plugins/PollPlugin/Forms/PollResponseForm.php b/plugins/PollPlugin/Forms/PollResponseForm.php new file mode 100644 index 0000000000..46e5887a15 --- /dev/null +++ b/plugins/PollPlugin/Forms/PollResponseForm.php @@ -0,0 +1,47 @@ +. + +// }}} + +namespace Plugin\PollPlugin\Forms; + +use App\Core\Form; +use function App\Core\I18n\_m; +use Symfony\Component\Form\Extension\Core\Type\ChoiceType; +use Symfony\Component\Form\Extension\Core\Type\SubmitType; +use Symfony\Component\Form\Form as SymfForm; + +class PollResponseForm extends Form +{ + public static function make(array $opts): SymfForm + { + $formOptions = []; + $options = []; + for ($i = 0; $i < count($opts); ++$i) { + $options[$opts[$i]] = $i; + } + array_push($formOptions, ['Question', ChoiceType::class, [ + 'choices' => $options, + 'expanded' => true, + ]]); + array_push($formOptions, ['save', SubmitType::class, ['label' => _m('Submit')]]); + + return parent::create($formOptions); + } +} diff --git a/plugins/PollPlugin/PollPlugin.php b/plugins/PollPlugin/PollPlugin.php index f47a403da7..3edce22127 100644 --- a/plugins/PollPlugin/PollPlugin.php +++ b/plugins/PollPlugin/PollPlugin.php @@ -66,6 +66,7 @@ class PollPlugin extends Module $r->connect('newpoll', 'main/poll/new', [Controller\NewPoll::class, 'newpoll']); //$r->connect('showpoll', 'main/poll/:{id<' . ID_FMT . '>}' , [Controller\ShowPoll::class, 'showpoll']); //doesnt work $r->connect('showpoll', 'main/poll/{id<\\d*>}',[Controller\ShowPoll::class, 'showpoll']); + $r->connect('showpoll', 'main/poll/{id<\\d*>}/respond',[Controller\RespondPoll::class, 'respondpoll']); return Event::next; } diff --git a/src/Entity/Poll.php b/src/Entity/Poll.php index 784ee2fc94..daba73760f 100644 --- a/src/Entity/Poll.php +++ b/src/Entity/Poll.php @@ -30,8 +30,8 @@ class Poll extends Entity // {{{ Autocode private int $id; - private string $uri; - private ?int $profile_id; + private ?string $uri; + private ?int $gsactor_id; private ?string $question; private ?string $options; private DateTimeInterface $created; @@ -47,26 +47,26 @@ class Poll extends Entity return $this->id; } - public function setUri(string $uri): self + public function setUri(?string $uri): self { $this->uri = $uri; return $this; } - public function getUri(): string + public function getUri(): ?string { return $this->uri; } - public function setProfileId(?int $profile_id): self + public function setGsactorId(?int $gsactor_id): self { - $this->profile_id = $profile_id; + $this->gsactor_id = $gsactor_id; return $this; } - public function getProfileId(): ?int + public function getGsactorId(): ?int { - return $this->profile_id; + return $this->gsactor_id; } public function setQuestion(?string $question): self @@ -113,17 +113,18 @@ class Poll extends Entity 'name' => 'poll', 'description' => 'Per-notice poll data for Poll plugin', 'fields' => [ - 'id' => ['type' => 'int', 'not null' => true], - 'uri' => ['type' => 'varchar', 'length' => 191, 'not null' => true], - 'profile_id' => ['type' => 'int'], //-> gsactor id? + 'id' => ['type' => 'serial', 'not null' => true], + 'uri' => ['type' => 'varchar', 'length' => 191], + // 'uri' => ['type' => 'varchar', 'length' => 191, 'not null' => true], + 'gsactor_id' => ['type' => 'int'], //-> gsactor id? 'question' => ['type' => 'text'], 'options' => ['type' => 'text'], 'created' => ['type' => 'datetime', 'not null' => true], ], 'primary key' => ['id'], - 'unique keys' => [ - 'poll_uri_key' => ['uri'], - ], + // 'unique keys' => [ + // 'poll_uri_key' => ['uri'], + // ], ]; } @@ -132,6 +133,17 @@ class Poll extends Entity return DB::find('poll', ['id' => $id]); } + public static function make(string $question, array $opt): self + { + $options = implode("\n",$opt); + return self::create(['question' => $question, 'options' => $options]); + } + + public function getOptionsArr() + { + return explode("\n", $this->options); + } + //from old version /** * Get a bookmark based on a notice @@ -146,12 +158,6 @@ class Poll extends Entity return self::getKV('uri', $notice->uri); } */ - /* - public function getOptions() - { - return explode("\n", $this->options); - } - */ /** * Is this a valid selection index? diff --git a/src/Entity/PollResponse.php b/src/Entity/PollResponse.php new file mode 100644 index 0000000000..81f97f8a8c --- /dev/null +++ b/src/Entity/PollResponse.php @@ -0,0 +1,136 @@ +. + +// }}} + +namespace App\Entity; + +use App\Core\Entity; +use DateTimeInterface; + +class PollResponse extends Entity +{ + // {{{ Autocode + + private int $id; + private ?string $uri; + private string $poll_id; + private ?int $gsactor_id; + private ?int $selection; + private DateTimeInterface $created; + + public function setId(int $id): self + { + $this->id = $id; + return $this; + } + + public function getId(): int + { + return $this->id; + } + + public function setUri(?string $uri): self + { + $this->uri = $uri; + return $this; + } + + public function getUri(): ?string + { + return $this->uri; + } + + public function setPollId(string $poll_id): self + { + $this->poll_id = $poll_id; + return $this; + } + + public function getPollId(): string + { + return $this->poll_id; + } + + public function setGsactorId(?int $gsactor_id): self + { + $this->gsactor_id = $gsactor_id; + return $this; + } + + public function getGsactorId(): ?int + { + return $this->gsactor_id; + } + + public function setSelection(?int $selection): self + { + $this->selection = $selection; + return $this; + } + + public function getSelection(): ?int + { + return $this->selection; + } + + public function setCreated(DateTimeInterface $created): self + { + $this->created = $created; + return $this; + } + + public function getCreated(): DateTimeInterface + { + return $this->created; + } + + // }}} Autocode + + /** + * The One True Thingy that must be defined and declared. + */ + public static function schemaDef() + { + return [ + 'name' => 'pollresponse', + 'description' => 'Record of responses to polls', + 'fields' => [ + 'id' => ['type' => 'serial', 'not null' => true], + //'uri' => array('type' => 'varchar', 'length' => 191, 'not null' => true, 'description' => 'UUID to the response notice'), + 'uri' => ['type' => 'varchar', 'length' => 191, 'description' => 'UUID to the response notice'], + 'poll_id' => ['type' => 'char', 'length' => 36, 'not null' => true, 'description' => 'UUID of poll being responded to'], + 'gsactor_id' => ['type' => 'int'], + 'selection' => ['type' => 'int'], + 'created' => ['type' => 'datetime', 'not null' => true], + ], + 'primary key' => ['id'], + /* + 'unique keys' => array( + 'poll_uri_key' => array('uri'), + 'poll_response_poll_id_profile_id_key' => array('poll_id', 'profile_id'), + ), + + 'indexes' => array( + 'poll_response_profile_id_poll_id_index' => array('profile_id', 'poll_id'), + ) + */ + ]; + } +}