2020-10-23 07:13:39 +09:00
|
|
|
<?php
|
2021-10-10 17:26:18 +09:00
|
|
|
|
|
|
|
declare(strict_types = 1);
|
2020-10-23 07:13:39 +09:00
|
|
|
// {{{ 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/>.
|
|
|
|
|
|
|
|
// }}}
|
|
|
|
|
2020-11-10 06:25:57 +09:00
|
|
|
namespace Plugin\Poll;
|
2020-10-23 07:13:39 +09:00
|
|
|
|
2022-03-27 23:19:09 +09:00
|
|
|
use App\Core\DB;
|
2020-10-23 07:13:39 +09:00
|
|
|
use App\Core\Event;
|
2020-12-15 00:47:51 +09:00
|
|
|
use App\Core\Form;
|
|
|
|
use function App\Core\I18n\_m;
|
2021-08-19 03:14:24 +09:00
|
|
|
use App\Core\Modules\NoteHandlerPlugin;
|
2020-11-10 02:43:10 +09:00
|
|
|
use App\Core\Router\RouteLoader;
|
2020-11-20 23:59:15 +09:00
|
|
|
use App\Entity\Note;
|
|
|
|
use App\Util\Common;
|
|
|
|
use App\Util\Exception\InvalidFormException;
|
2020-11-22 11:36:01 +09:00
|
|
|
use App\Util\Exception\NotFoundException;
|
|
|
|
use App\Util\Exception\RedirectException;
|
2020-11-20 23:59:15 +09:00
|
|
|
use App\Util\Exception\ServerException;
|
2020-12-15 00:47:51 +09:00
|
|
|
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
|
|
|
|
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
|
|
|
|
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
|
2020-11-20 23:59:15 +09:00
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
2020-10-30 03:32:35 +09:00
|
|
|
|
2020-11-10 11:01:31 +09:00
|
|
|
/**
|
|
|
|
* Poll plugin main class
|
|
|
|
*
|
|
|
|
* @package GNUsocial
|
2020-12-15 00:47:51 +09:00
|
|
|
* @category Poll
|
2020-11-10 06:25:57 +09:00
|
|
|
*
|
|
|
|
* @author Daniel Brandao <up201705812@fe.up.pt>
|
2021-08-19 03:14:24 +09:00
|
|
|
* @author Hugo Sales <hugo@hsal.es>
|
|
|
|
* @copyright 2020-2021 Free Software Foundation, Inc http://www.fsf.org
|
2020-11-10 06:25:57 +09:00
|
|
|
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
|
|
|
|
*/
|
2021-08-19 03:14:24 +09:00
|
|
|
class Poll extends NoteHandlerPlugin
|
2020-10-23 07:13:39 +09:00
|
|
|
{
|
2020-11-10 02:43:10 +09:00
|
|
|
/**
|
|
|
|
* Map URLs to actions
|
|
|
|
*
|
2021-10-10 17:26:18 +09:00
|
|
|
* @return bool hook value; true means continue processing, false means stop
|
2020-11-10 02:43:10 +09:00
|
|
|
*/
|
|
|
|
public function onAddRoute(RouteLoader $r): bool
|
2020-10-23 07:13:39 +09:00
|
|
|
{
|
2020-12-15 00:47:51 +09:00
|
|
|
$r->connect('newpoll', 'main/poll/new/{num<\\d+>?3}', [Controller\NewPoll::class, 'newpoll']);
|
2020-10-23 07:13:39 +09:00
|
|
|
|
|
|
|
return Event::next;
|
|
|
|
}
|
2020-11-20 23:59:15 +09:00
|
|
|
|
2020-11-24 00:06:23 +09:00
|
|
|
/**
|
|
|
|
* Populate twig vars
|
|
|
|
*
|
|
|
|
* @return bool hook value; true means continue processing, false means stop.
|
2021-09-18 15:27:17 +09:00
|
|
|
*
|
|
|
|
* public function onStartTwigPopulateVars(array &$vars): bool
|
|
|
|
* {
|
|
|
|
* $vars['tabs'][] = ['title' => 'Poll',
|
|
|
|
* 'href' => 'newpoll',
|
|
|
|
* ];
|
|
|
|
* return Event::next;
|
|
|
|
* }*/
|
2020-11-20 23:59:15 +09:00
|
|
|
|
2020-11-24 00:06:23 +09:00
|
|
|
/**
|
|
|
|
* Output our dedicated stylesheet
|
|
|
|
*
|
|
|
|
* @param array $styles stylesheets path
|
|
|
|
*
|
2021-10-10 17:26:18 +09:00
|
|
|
* @return bool hook value; true means continue processing, false means stop
|
2020-11-24 00:06:23 +09:00
|
|
|
*/
|
2020-11-22 11:36:01 +09:00
|
|
|
public function onStartShowStyles(array &$styles): bool
|
|
|
|
{
|
|
|
|
$styles[] = 'poll/poll.css';
|
|
|
|
return Event::next;
|
|
|
|
}
|
|
|
|
|
2020-11-20 23:59:15 +09:00
|
|
|
/**
|
2021-11-26 22:05:23 +09:00
|
|
|
* Output our note content to the feed
|
2020-11-24 00:06:23 +09:00
|
|
|
*
|
2021-10-10 17:26:18 +09:00
|
|
|
* @param array $otherContent content
|
2020-11-24 00:06:23 +09:00
|
|
|
*
|
2021-10-10 17:26:18 +09:00
|
|
|
* @throws \App\Util\Exception\NoLoggedInUser user not logged in
|
2020-11-24 00:06:23 +09:00
|
|
|
* @throws InvalidFormException invalid forms
|
|
|
|
* @throws RedirectException
|
|
|
|
* @throws ServerException User already responded to poll
|
|
|
|
*
|
2021-10-10 17:26:18 +09:00
|
|
|
* @return bool hook value; true means continue processing, false means stop
|
2020-11-20 23:59:15 +09:00
|
|
|
*/
|
2021-10-10 17:26:18 +09:00
|
|
|
public function onShowNoteContent(Request $request, Note $note, array &$otherContent): bool
|
2020-11-20 23:59:15 +09:00
|
|
|
{
|
2020-11-22 11:36:01 +09:00
|
|
|
$responses = null;
|
|
|
|
$formView = null;
|
|
|
|
try {
|
|
|
|
$poll = DB::findOneBy('poll', ['note_id' => $note->getId()]);
|
|
|
|
} catch (NotFoundException $e) {
|
|
|
|
return Event::next;
|
|
|
|
}
|
2020-11-20 23:59:15 +09:00
|
|
|
|
2021-09-07 03:49:03 +09:00
|
|
|
if (Common::isLoggedIn() && !Entity\PollResponse::exits($poll->getId(), Common::ensureLoggedIn()->getId())) {
|
2020-12-15 00:47:51 +09:00
|
|
|
$opts = $poll->getOptionsArr();
|
|
|
|
$options = [];
|
2021-10-10 17:26:18 +09:00
|
|
|
for ($i = 1; $i <= \count($opts); ++$i) {
|
2020-12-15 00:47:51 +09:00
|
|
|
$options[$opts[$i - 1]] = $i;
|
|
|
|
}
|
|
|
|
$formOptions = [
|
|
|
|
['Options' . $poll->getId(), ChoiceType::class, [
|
|
|
|
'choices' => $options,
|
|
|
|
'expanded' => true,
|
|
|
|
]],
|
|
|
|
['note_id', HiddenType::class, ['data' => $note->getId()]],
|
|
|
|
['pollresponse', SubmitType::class, ['label' => _m('Vote')]],
|
|
|
|
];
|
|
|
|
$form = Form::create($formOptions);
|
|
|
|
|
2020-11-22 11:36:01 +09:00
|
|
|
$formView = $form->createView();
|
2020-11-23 06:45:43 +09:00
|
|
|
$ret = self::noteActionHandle($request, $form, $note, 'pollresponse', /** TODO needs documentation */ function ($note, $data) {
|
2020-11-20 23:59:15 +09:00
|
|
|
$user = Common::ensureLoggedIn();
|
2020-11-22 11:36:01 +09:00
|
|
|
|
|
|
|
try {
|
|
|
|
$poll = DB::findOneBy('poll', ['note_id' => $note->getId()]);
|
|
|
|
} catch (NotFoundException $e) {
|
|
|
|
return Event::next;
|
|
|
|
}
|
|
|
|
|
2021-09-07 03:49:03 +09:00
|
|
|
if (Entity\PollResponse::exits($poll->getId(), $user->getId())) {
|
2020-11-22 11:36:01 +09:00
|
|
|
return Event::next;
|
|
|
|
}
|
|
|
|
|
2020-11-20 23:59:15 +09:00
|
|
|
$selection = array_values($data)[1];
|
|
|
|
if (!$poll->isValidSelection($selection)) {
|
|
|
|
throw new InvalidFormException();
|
|
|
|
}
|
2021-09-07 03:49:03 +09:00
|
|
|
if (Entity\PollResponse::exits($poll->getId(), $user->getId())) {
|
2020-11-20 23:59:15 +09:00
|
|
|
throw new ServerException('User already responded to poll');
|
|
|
|
}
|
2021-09-18 11:22:27 +09:00
|
|
|
$pollResponse = Entity\PollResponse::create(['poll_id' => $poll->getId(), 'actor_id' => $user->getId(), 'selection' => $selection]);
|
2020-11-20 23:59:15 +09:00
|
|
|
DB::persist($pollResponse);
|
|
|
|
DB::flush();
|
2020-11-22 11:36:01 +09:00
|
|
|
|
|
|
|
throw new RedirectException();
|
2020-11-20 23:59:15 +09:00
|
|
|
});
|
2020-11-22 11:36:01 +09:00
|
|
|
if ($ret != null) {
|
|
|
|
return $ret;
|
|
|
|
}
|
2020-11-20 23:59:15 +09:00
|
|
|
} else {
|
|
|
|
$responses = $poll->countResponses();
|
|
|
|
}
|
2020-11-24 00:06:23 +09:00
|
|
|
$otherContent[] = ['name' => 'Poll', 'vars' => ['question' => $poll->getQuestion(), 'responses' => $responses, 'form' => $formView]];
|
2020-11-20 23:59:15 +09:00
|
|
|
return Event::next;
|
|
|
|
}
|
2020-10-23 07:13:39 +09:00
|
|
|
}
|