2020-10-22 23:13:39 +01:00
|
|
|
<?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/>.
|
|
|
|
|
|
|
|
// }}}
|
|
|
|
|
2020-11-09 21:25:57 +00:00
|
|
|
namespace Plugin\Poll;
|
2020-10-22 23:13:39 +01:00
|
|
|
|
2020-11-20 14:59:15 +00:00
|
|
|
use App\Core\DB\DB;
|
2020-10-22 23:13:39 +01:00
|
|
|
use App\Core\Event;
|
|
|
|
use App\Core\Module;
|
2020-11-09 17:43:10 +00:00
|
|
|
use App\Core\Router\RouteLoader;
|
2020-11-20 14:59:15 +00:00
|
|
|
use App\Entity\Note;
|
|
|
|
use App\Entity\PollResponse;
|
|
|
|
use App\Util\Common;
|
|
|
|
use App\Util\Exception\InvalidFormException;
|
2020-11-22 02:36:01 +00:00
|
|
|
use App\Util\Exception\NotFoundException;
|
|
|
|
use App\Util\Exception\RedirectException;
|
2020-11-20 14:59:15 +00:00
|
|
|
use App\Util\Exception\ServerException;
|
|
|
|
use Plugin\Poll\Forms\PollResponseForm;
|
2020-11-09 17:43:10 +00:00
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\RedirectController;
|
2020-11-20 14:59:15 +00:00
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
2020-10-29 18:32:35 +00:00
|
|
|
|
2020-11-10 02:01:31 +00:00
|
|
|
/**
|
|
|
|
* Poll plugin main class
|
|
|
|
*
|
|
|
|
* @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
|
|
|
|
*/
|
2020-10-29 18:32:35 +00:00
|
|
|
const ID_FMT = '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}';
|
2020-10-22 23:13:39 +01:00
|
|
|
|
2020-11-09 21:25:57 +00:00
|
|
|
/**
|
|
|
|
* Poll plugin main class
|
|
|
|
*
|
|
|
|
* @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 Poll extends Module
|
2020-10-22 23:13:39 +01:00
|
|
|
{
|
2020-11-09 17:43:10 +00:00
|
|
|
/**
|
|
|
|
* Map URLs to actions
|
|
|
|
*
|
|
|
|
* @param RouteLoader $r
|
|
|
|
*
|
|
|
|
* @return bool hook value; true means continue processing, false means stop.
|
|
|
|
*/
|
|
|
|
public function onAddRoute(RouteLoader $r): bool
|
2020-10-22 23:13:39 +01:00
|
|
|
{
|
2020-11-09 17:43:10 +00:00
|
|
|
$r->connect('newpollnum', 'main/poll/new/{num<\\d*>}', [Controller\NewPoll::class, 'newpoll']);
|
2020-11-03 15:53:46 +00:00
|
|
|
$r->connect('showpoll', 'main/poll/{id<\\d*>}',[Controller\ShowPoll::class, 'showpoll']);
|
2020-11-09 21:25:57 +00:00
|
|
|
$r->connect('answerpoll', 'main/poll/{id<\\d*>}/respond',[Controller\AnswerPoll::class, 'answerpoll']);
|
2020-11-09 17:43:10 +00:00
|
|
|
$r->connect('newpoll', 'main/poll/new', RedirectController::class, ['defaults' => ['route' => 'newpollnum', 'num' => 3]]);
|
2020-10-22 23:13:39 +01:00
|
|
|
|
|
|
|
return Event::next;
|
|
|
|
}
|
2020-11-20 14:59:15 +00:00
|
|
|
|
|
|
|
public function onStartTwigPopulateVars(array &$vars): bool
|
|
|
|
{
|
|
|
|
$vars['tabs'] = [['title' => 'Poll',
|
|
|
|
'href' => 'newpoll',
|
|
|
|
]];
|
|
|
|
return Event::next;
|
|
|
|
}
|
|
|
|
|
2020-11-22 02:36:01 +00:00
|
|
|
public function onStartShowStyles(array &$styles): bool
|
|
|
|
{
|
|
|
|
$styles[] = 'poll/poll.css';
|
|
|
|
return Event::next;
|
|
|
|
}
|
|
|
|
|
2020-11-20 14:59:15 +00:00
|
|
|
/**
|
|
|
|
* Display a poll in the timeline
|
|
|
|
*/
|
2020-11-22 02:36:01 +00:00
|
|
|
public function onShowNoteContent(Request $request, Note $note, array &$test)
|
2020-11-20 14:59:15 +00:00
|
|
|
{
|
2020-11-22 02:36:01 +00:00
|
|
|
$responses = null;
|
|
|
|
$formView = null;
|
|
|
|
try {
|
|
|
|
$poll = DB::findOneBy('poll', ['note_id' => $note->getId()]);
|
|
|
|
} catch (NotFoundException $e) {
|
|
|
|
return Event::next;
|
|
|
|
}
|
2020-11-20 14:59:15 +00:00
|
|
|
|
2020-11-22 02:36:01 +00:00
|
|
|
if (Common::isLoggedIn() && !PollResponse::exits($poll->getId(), Common::ensureLoggedIn()->getId())) {
|
|
|
|
$form = PollResponseForm::make($poll, $note->getId());
|
|
|
|
$formView = $form->createView();
|
|
|
|
$ret = self::noteActionHandle($request, $form, $note, 'pollresponse', /** TODO Documentation */ function ($note, $data) {
|
2020-11-20 14:59:15 +00:00
|
|
|
$user = Common::ensureLoggedIn();
|
2020-11-22 02:36:01 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
$poll = DB::findOneBy('poll', ['note_id' => $note->getId()]);
|
|
|
|
} catch (NotFoundException $e) {
|
|
|
|
return Event::next;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (PollResponse::exits($poll->getId(), $user->getId())) {
|
|
|
|
return Event::next;
|
|
|
|
}
|
|
|
|
|
2020-11-20 14:59:15 +00:00
|
|
|
$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();
|
2020-11-22 02:36:01 +00:00
|
|
|
|
|
|
|
throw new RedirectException();
|
2020-11-20 14:59:15 +00:00
|
|
|
});
|
2020-11-22 02:36:01 +00:00
|
|
|
if ($ret != null) {
|
|
|
|
return $ret;
|
|
|
|
}
|
2020-11-20 14:59:15 +00:00
|
|
|
} else {
|
|
|
|
$responses = $poll->countResponses();
|
|
|
|
}
|
2020-11-22 02:36:01 +00:00
|
|
|
//$test[] = $form->createView();
|
|
|
|
$test[] = ['name' => 'Poll', 'vars' => ['question' => $poll->getQuestion(), 'responses' => $responses, 'form' => $formView]];
|
2020-11-20 14:59:15 +00:00
|
|
|
return Event::next;
|
|
|
|
}
|
2020-10-22 23:13:39 +01:00
|
|
|
}
|