[Poll] Started testing with note integration

This commit is contained in:
Daniel 2020-11-20 14:59:15 +00:00 committed by Hugo Sales
parent 50ec306243
commit 090c593a61
Signed by: someonewithpc
GPG Key ID: 7D0C7EAFC9D835A0
7 changed files with 188 additions and 8 deletions

View File

@ -23,8 +23,11 @@ namespace Plugin\Poll\Forms;
use App\Core\Form;
use function App\Core\I18n\_m;
use App\Entity\Poll;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Form as SymfForm;
/**
@ -42,23 +45,29 @@ class PollResponseForm extends Form
/**
* Creates a radio form with the options given
*
* @param array $opts options
* @param Poll $poll
* @param int $noteId
*
* @return SymfForm
*/
public static function make(array $opts): SymfForm
public static function make(Poll $poll,int $noteId): SymfForm
{
$opts = $poll->getOptionsArr();
$question = $poll->getQuestion();
$formOptions = [];
$options = [];
for ($i = 1; $i <= count($opts); ++$i) {
$options[$opts[$i - 1]] = $i;
}
$formOptions[0] = ['Options:', ChoiceType::class, [
'choices' => $options,
'expanded' => true,
]];
$formOptions[1] = ['save', SubmitType::class, ['label' => _m('Submit')]];
$formOptions = [
['Question', TextType::class, ['data' => $question, 'label' => _m(('Question')), 'disabled' => true]],
['Options:', ChoiceType::class, [
'choices' => $options,
'expanded' => true,
]],
['note_id', HiddenType::class, ['data' => $noteId]],
['pollresponse', SubmitType::class, ['label' => _m('Submit')]],
];
return parent::create($formOptions);
}
}

View File

@ -0,0 +1,66 @@
<?php
// {{{ License
// This file is part of GNU social - https://www.gnu.org/software/social
//
// GNU social is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// GNU social is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with GNU social. If not, see <http://www.gnu.org/licenses/>.
// }}}
namespace Plugin\Poll\Forms;
use App\Core\Form;
use function App\Core\I18n\_m;
use App\Entity\Poll;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Form as SymfForm;
/**
* Form to respond a Poll
*
* @package GNUsocial
* @category PollPlugin
*
* @author Daniel Brandao <up201705812@fe.up.pt>
* @copyright 2020 Free Software Foundation, Inc http://www.fsf.org
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
*/
class ShowPollForm extends Form
{
/**
* Creates a radio form with the options given
*
* @param array $opts options
*
* @return SymfForm
*/
public static function make(Poll $poll): SymfForm
{
$opts = $poll->getOptionsArr();
$question = $poll->getQuestion();
$formOptions = [];
for ($i = 1; $i <= count($opts); ++$i) {
$options[$opts[$i - 1]] = $i;
}
$formOptions[] = ['Question', TextType::class, ['data' => $question, 'label' => _m(('Question')), 'disabled' => true]];
$formOptions[] = ['Options:', ChoiceType::class, [
'choices' => $options,
'expanded' => true,
]];
return parent::create($formOptions);
}
}

View File

@ -20,10 +20,23 @@
namespace Plugin\Poll;
use App\Core\DB\DB;
use App\Core\Event;
use App\Core\Form;
use function App\Core\I18n\_m;
use App\Core\Module;
use App\Core\Router\RouteLoader;
use App\Entity\Note;
use App\Entity\Poll as PollEntity;
use App\Entity\PollResponse;
use App\Util\Common;
use App\Util\Exception\InvalidFormException;
use App\Util\Exception\ServerException;
use Plugin\Poll\Forms\PollResponseForm;
use Symfony\Bundle\FrameworkBundle\Controller\RedirectController;
use Symfony\Component\Form\Extension\Core\Type\NumberType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\HttpFoundation\Request;
/**
* Poll plugin main class
@ -65,4 +78,52 @@ class Poll extends Module
return Event::next;
}
public function onStartTwigPopulateVars(array &$vars): bool
{
$vars['tabs'] = [['title' => 'Poll',
'href' => 'newpoll',
]];
return Event::next;
}
/**
* Display a poll in the timeline
*/
public function onShowNoteContent(Request $request, Note $note, array &$actions)
{
$user = Common::ensureLoggedIn();
$poll = PollEntity::getFromId(21);
if (!PollResponse::exits($poll->getId(), $user->getId())) {
$form = PollResponseForm::make($poll, $note->getId());
$ret = self::noteActionHandle($request, $form, $note, 'pollresponse', function ($note, $data) {
$user = Common::ensureLoggedIn();
$poll = PollEntity::getFromId(21); //substituir por get from note
$selection = array_values($data)[1];
if (!$poll->isValidSelection($selection)) {
throw new InvalidFormException();
}
if (PollResponse::exits($poll->getId(), $user->getId())) {
throw new ServerException('User already responded to poll');
}
$pollResponse = PollResponse::create(['poll_id' => $poll->getId(), 'gsactor_id' => $user->getId(), 'selection' => $selection]);
DB::persist($pollResponse);
DB::flush();
return Event::stop;
});
} else {
$options[] = ['Question', TextType::class, ['data' => $poll->getQuestion(), 'label' => _m(('Question')), 'disabled' => true]];
$responses = $poll->countResponses();
$i = 0;
foreach ($responses as $option => $num) {
//['Option_i', TextType::class, ['label' => _m('Option i')]],
$options[] = ['Option_' . $i, NumberType::class, ['data' => $num, 'label' => $option, 'disabled' => true]];
++$i;
}
$form = Form::create($options);
}
$actions[] = $form->createView();
return Event::next;
}
}

View File

@ -49,6 +49,7 @@ class Extension extends AbstractExtension
new TwigFunction('active', [Runtime::class, 'isCurrentRouteActive']),
new TwigFunction('is_route', [Runtime::class, 'isCurrentRoute']),
new TwigFunction('get_note_actions', [Runtime::class, 'getNoteActions']),
new TwigFunction('get_note_test', [Runtime::class, 'getNoteTest']),
new TwigFunction('config', [Runtime::class, 'getConfig']),
new TwigFunction('icon', [Runtime::class, 'embedSvgIcon'], ['needs_environment' => true]),
];

View File

@ -31,11 +31,15 @@
namespace App\Twig;
use App\Core\Event;
use App\Core\Form;
use function App\Core\I18n\_m;
use App\Entity\Note;
use App\Util\Common;
use App\Util\Formatting;
use Functional as F;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Form\Extension\Core\Type\NumberType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\KernelEvents;
@ -71,6 +75,32 @@ class Runtime implements RuntimeExtensionInterface, EventSubscriberInterface
return $actions;
}
public function getNoteTest(Note $note)
{
$test = [];
Event::handle('show_note_content', [$this->request, $note, &$test]);
/*
$options = [];
$options[0] = ['Question', TextType::class, ['data'=>'fav color?','label' => _m(('Question')),'disabled'=>true]];
$options[1] = ['Option_1', NumberType::class, ['data'=>3,'label' => 'blue','disabled'=>true]];
*/
/*
$options[0] = ['Question', TextType::class, ['data'=>'fav color?','label' => _m(('Question')),'disabled'=>true]];
$options[1] = ['Options:', ChoiceType::class, [
'choices' => ['blue','green'],
'expanded' => true,
]];
$options[2] = ['save', SubmitType::class, ['label' => _m('Submit')]];
*/
/*
$form = Form::create($options);
$test[0] = $form->createView();
*/
return $test;
}
public function getConfig(...$args)
{
return Common::config(...$args);

View File

@ -40,6 +40,11 @@
<div class="create-right">
<div class="scope">
{{ form_row(post_form.visibility) }}
{% for tab in tabs %}
<a href={{ path(tab['href']) }}>{{ tab['title'] }}</a>
{% endfor %}
</div>
<div class="input-wrapper">
<div class="content-input">

View File

@ -12,6 +12,14 @@
</div>
<div class="note-content">
{{ note.getContent() }}
{% if have_user %}
{% for test in get_note_test(note) %}
{{ form(test) }}
{% endfor %}
{% endif %}
<div class="note-attachments">
{% for attachment in note.getAttachments() %}
{% if attachment.mimetype starts with 'image/' %}