[Poll] Added modified param for Poll/Poll response, added PollTest

This commit is contained in:
Daniel 2020-11-09 21:25:57 +00:00 committed by Hugo Sales
parent f34fb9c7b9
commit ffb4b9df23
No known key found for this signature in database
GPG Key ID: 7D0C7EAFC9D835A0
12 changed files with 204 additions and 298 deletions

View File

@ -19,7 +19,7 @@
// }}}
namespace Plugin\PollPlugin\Controller;
namespace Plugin\Poll\Controller;
use App\Core\DB\DB;
use App\Entity\Poll;
@ -29,10 +29,20 @@ use App\Util\Exception\InvalidFormException;
use App\Util\Exception\NotFoundException;
use App\Util\Exception\RedirectException;
use App\Util\Exception\ServerException;
use Plugin\PollPlugin\Forms\PollResponseForm;
use Plugin\Poll\Forms\PollResponseForm;
use Symfony\Component\HttpFoundation\Request;
class RespondPoll
/**
* Respond to 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 AnswerPoll
{
/**
* Handle poll response
@ -48,40 +58,33 @@ class RespondPoll
*
* @return array template
*/
public function respondpoll(Request $request, string $id)
public function answerPoll(Request $request, string $id)
{
$user = Common::ensureLoggedIn();
$poll = Poll::getFromId((int) $id);
//var_dump($poll);
if ($poll == null) {
throw new NotFoundException('Poll does not exist');
}
$question = $poll->getQuestion();
// echo $question;
$opts = $poll->getOptionsArr();
//var_dump($opts);
$form = PollResponseForm::make($opts);
$opts = $poll->getOptionsArr();
$form = PollResponseForm::make($opts);
$form->handleRequest($request);
if ($form->isSubmitted()) {
if ($form->isValid()) {
$data = $form->getData();
$selection = array_values($data)[1];
//echo $selection;
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();
//var_dump($pollResponse);
throw new RedirectException('showpoll', ['id' => $poll->getId()]);
} else {
throw new InvalidFormException();

View File

@ -19,14 +19,15 @@
// }}}
namespace Plugin\PollPlugin\Controller;
namespace Plugin\Poll\Controller;
use App\Core\DB\DB;
use App\Core\Security;
use App\Entity\Poll;
use App\Util\Common;
use App\Util\Exception\InvalidFormException;
use App\Util\Exception\RedirectException;
use Plugin\PollPlugin\Forms\NewPollForm;
use Plugin\Poll\Forms\NewPollForm;
use Symfony\Component\HttpFoundation\Request;
const MAX_OPTS = 5;
@ -46,55 +47,31 @@ class NewPoll
*
* @return array template
*/
public function newpoll(Request $request, int $num)
public function newPoll(Request $request, int $num)
{
$user = Common::ensureLoggedIn();
$numOptions = min(max($num,MIN_OPTS),MAX_OPTS);
$numOptions = Common::clamp($num,MIN_OPTS,MAX_OPTS);
$form = NewPollForm::make($numOptions);
$form->handleRequest($request);
$opt = [];
if ($form->isSubmitted()) {
if ($form->isValid()) {
$data = $form->getData();
//var_dump($data);
$question = $data['Question'];
Security::sanitize($question = $data['Question']);
for ($i = 1; $i <= $numOptions; ++$i) {
array_push($opt, $data['Option_' . $i]);
Security::sanitize($opt[$i - 1] = $data['Option_' . $i]);
}
$poll = Poll::make($user->getId(), $question, $opt);
$options = implode("\n",$opt);
$poll = Poll::create(['gsactor_id' => $user->getId(), 'question' => $question, 'options' => $options]);
DB::persist($poll);
DB::flush();
//var_dump($testPoll);
throw new RedirectException('showpoll', ['id' => $poll->getId()]);
} else {
throw new InvalidFormException();
}
}
// testing
//$test = Poll::create(['id' => '0', 'uri' => 'a']);
//DB::persist($test);
//DB::flush();
/*
$loadpoll = Poll::getFromId('0');
var_dump($loadpoll);
*/
return ['_template' => 'Poll/newpoll.html.twig', 'form' => $form->createView()];
}
/*
public function pollsettings(Request $request)
{
$form = Form::create([['Num_of_Questions', NumberType::class, ['label' => _m(('Number of questions:'))]],['save', SubmitType::class, ['label' => _m('Continue')]]]);
$form->handleRequest($request);
if ($form->isSubmitted())
{
$data = $form->getData();
NewPoll::numOptions = $data['Num_of_Questions'];
var_dump($data);
}
return ['_template' => 'Poll/newpoll.html.twig', 'form' => $form->createView()];
}
*/
}

View File

@ -19,7 +19,7 @@
// }}}
namespace Plugin\PollPlugin\Controller;
namespace Plugin\Poll\Controller;
use App\Entity\Poll;
use App\Util\Common;
@ -39,12 +39,11 @@ class ShowPoll
*
* @return array Template
*/
public function showpoll(Request $request, string $id)
public function showPoll(Request $request, string $id)
{
$user = Common::ensureLoggedIn();
$poll = Poll::getFromId((int) $id);
//var_dump($poll);
if ($poll == null) {
throw new NotFoundException('Poll does not exist');

View File

@ -19,7 +19,7 @@
// }}}
namespace Plugin\PollPlugin\Forms;
namespace Plugin\Poll\Forms;
use App\Core\Form;
use function App\Core\I18n\_m;
@ -27,7 +27,16 @@ use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Form as SymfForm;
const MAX_OPT = 5;
/**
* Form to add 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 NewPollForm extends Form
{
/**
@ -39,14 +48,14 @@ class NewPollForm extends Form
*/
public static function make(int $optionNum): SymfForm
{
$optionNum = min(MAX_OPT,$optionNum);
$options = [];
array_push($options,['Question', TextType::class, ['label' => _m(('Question'))]]);
for ($i = 1; $i <= $optionNum; ++$i) {
$options = [];
$options[0] = ['Question', TextType::class, ['label' => _m(('Question'))]];
$i = 1;
for ($i; $i <= $optionNum; ++$i) {
//['Option_i', TextType::class, ['label' => _m('Option i')]],
array_push($options,['Option_' . $i, TextType::class, ['label' => _m(('Option ' . $i))]]);
$options[$i] = ['Option_' . $i, TextType::class, ['label' => _m(('Option ' . $i))]];
}
array_push($options, ['save', SubmitType::class, ['label' => _m('Submit Poll')]]);
$options[$i + 1] = ['save', SubmitType::class, ['label' => _m('Submit Poll')]];
return parent::create($options);
}

View File

@ -19,7 +19,7 @@
// }}}
namespace Plugin\PollPlugin\Forms;
namespace Plugin\Poll\Forms;
use App\Core\Form;
use function App\Core\I18n\_m;
@ -43,11 +43,11 @@ class PollResponseForm extends Form
for ($i = 1; $i <= count($opts); ++$i) {
$options[$opts[$i - 1]] = $i;
}
array_push($formOptions, ['Options:', ChoiceType::class, [
$formOptions[0] = ['Options:', ChoiceType::class, [
'choices' => $options,
'expanded' => true,
]]);
array_push($formOptions, ['save', SubmitType::class, ['label' => _m('Submit')]]);
]];
$formOptions[1] = ['save', SubmitType::class, ['label' => _m('Submit')]];
return parent::create($formOptions);
}

View File

@ -18,51 +18,27 @@
// }}}
namespace Plugin\PollPlugin;
namespace Plugin\Poll;
use App\Core\Event;
use App\Core\Module;
use App\Core\Router\RouteLoader;
use Plugin\PollPlugin\Entity\Poll;
use Symfony\Bundle\FrameworkBundle\Controller\RedirectController;
const ID_FMT = '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}';
class PollPlugin extends Module
/**
* 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
{
/**
* Map URLs to actions
*
* @param URLMapper $m path-to-action mapper
* @param mixed $r
*
* @return bool hook value; true means continue processing, false means stop.
*/
/*
public function onRouterInitialized(URLMapper $m)
{
$m->connect('main/poll/new',
['action' => 'newpoll']);
$m->connect('main/poll/:id',
['action' => 'showpoll'],
['id' => '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}']);
$m->connect('main/poll/response/:id',
['action' => 'showpollresponse'],
['id' => '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}']);
$m->connect('main/poll/:id/respond',
['action' => 'respondpoll'],
['id' => '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}']);
$m->connect('settings/poll',
['action' => 'pollsettings']);
return true;
}
*/
/**
* Map URLs to actions
*
@ -73,9 +49,8 @@ class PollPlugin extends Module
public function onAddRoute(RouteLoader $r): bool
{
$r->connect('newpollnum', 'main/poll/new/{num<\\d*>}', [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('respondpoll', 'main/poll/{id<\\d*>}/respond',[Controller\RespondPoll::class, 'respondpoll']);
$r->connect('answerpoll', 'main/poll/{id<\\d*>}/respond',[Controller\AnswerPoll::class, 'answerpoll']);
$r->connect('newpoll', 'main/poll/new', RedirectController::class, ['defaults' => ['route' => 'newpollnum', 'num' => 3]]);
return Event::next;

View File

@ -22,6 +22,7 @@
namespace App\Core;
use App\Core\DB\DB;
use App\Util\Exception\NotFoundException;
use App\Util\Formatting;
use DateTime;
@ -76,4 +77,22 @@ abstract class Entity
}
DB::remove($obj);
}
/**
* Get an Entity from its id
*
* @param int $id
*
* @return null|static
*/
public static function getFromId(int $id): ?self
{
$array = explode('\\', get_called_class());
$class = end($array);
try {
return DB::findOneBy($class, ['id' => $id]);
} catch (NotFoundException $e) {
return null;
}
}
}

View File

@ -24,7 +24,6 @@ namespace App\Entity;
use App\Core\DB\DB;
use App\Core\Entity;
use DateTimeInterface;
use function Functional\id;
class Poll extends Entity
{
@ -36,6 +35,7 @@ class Poll extends Entity
private ?string $question;
private ?string $options;
private DateTimeInterface $created;
private DateTimeInterface $modified;
public function setId(int $id): self
{
@ -103,6 +103,17 @@ class Poll extends Entity
return $this->created;
}
public function setModified(DateTimeInterface $modified): self
{
$this->modified = $modified;
return $this;
}
public function getModified(): DateTimeInterface
{
return $this->modified;
}
// }}} Autocode
/**
@ -119,10 +130,11 @@ class Poll extends Entity
'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?
'gsactor_id' => ['type' => 'int'],
'question' => ['type' => 'text'],
'options' => ['type' => 'text'],
'created' => ['type' => 'datetime', 'not null' => true],
'created' => ['type' => 'datetime', 'not null' => true, 'default' => 'CURRENT_TIMESTAMP', 'description' => 'date this record was created'],
'modified' => ['type' => 'timestamp', 'not null' => true, 'default' => 'CURRENT_TIMESTAMP', 'description' => 'date this record was modified'],
],
'primary key' => ['id'],
// 'unique keys' => [
@ -131,33 +143,6 @@ class Poll extends Entity
];
}
/**
* Get poll object from its id
*
* @param int $id
*
* @return null|static
*/
public static function getFromId(int $id): ?self
{
return DB::find('poll', ['id' => $id]);
}
/**
* Make new poll object
*
* @param int $gsactorId
* @param string $question
* @param array $opt poll options
*
* @return static poll object
*/
public static function make(int $gsactorId, string $question, array $opt): self
{
$options = implode("\n",$opt);
return self::create(['gsactor_id' => $gsactorId, 'question' => $question, 'options' => $options]);
}
/**
* Gets options in array format
*
@ -175,11 +160,8 @@ class Poll extends Entity
*
* @return bool
*/
public function isValidSelection($selection): bool
public function isValidSelection(int $selection): bool
{
if ($selection != (int) $selection) {
return false;
}
if ($selection < 1 || $selection > count($this->getOptionsArr())) {
return false;
}
@ -196,169 +178,11 @@ class Poll extends Entity
$responses = [];
$options = $this->getOptionsArr();
for ($i = 0; $i < count($options); ++$i) {
$responses[$options[$i]] = DB::dql('select count(pr) from App\Entity\Poll p, App\Entity\PollResponse pr
where pr.poll_id = :id and pr.selection = :selection',
['id' => $this->id, 'selection' => $i + 1])[0][1] / $this->id; //todo: fix
/*
var_dump(DB::dql('select count(pr) from App\Entity\Poll p, App\Entity\PollResponse pr
where pr.poll_id = :id and pr.selection = :selection',
['id' => $this->id, 'selection' => $i + 1])[0][1]);
*/
$responses[$options[$i]] = DB::dql('select count(pr) from App\Entity\PollResponse pr ' .
'where pr.poll_id = :id and pr.selection = :selection',
['id' => $this->id, 'selection' => $i + 1])[0][1];
}
return $responses;
}
//from old version
/**
* Get a bookmark based on a notice
*
* @param Notice $notice Notice to check for
*
* @return get_called_class found poll or null
*/
/*
public static function getByNotice($notice)
{
return self::getKV('uri', $notice->uri);
}
*/
/*
public function getNotice()
{
return Notice::getKV('uri', $this->uri);
}
public function getUrl()
{
return $this->getNotice()->getUrl();
}*/
/**
* Get the response of a particular user to this poll, if any.
*
* @param Profile $profile
*
* @return get_called_class object or null
*/
/*
public function getResponse(Profile $profile)
{
$pr = Poll_response::pkeyGet(array('poll_id' => $this->id,
'profile_id' => $profile->id));
return $pr;
}
*/
/*
public function countResponses()
{
$pr = new Poll_response();
$pr->poll_id = $this->id;
$pr->groupBy('selection');
$pr->selectAdd('count(profile_id) as votes');
$pr->find();
$raw = array();
while ($pr->fetch()) {
// Votes list 1-based
// Array stores 0-based
$raw[$pr->selection - 1] = $pr->votes;
}
$counts = array();
foreach (array_keys($this->getOptions()) as $key) {
if (isset($raw[$key])) {
$counts[$key] = $raw[$key];
} else {
$counts[$key] = 0;
}
}
return $counts;
}*/
/**
* Save a new poll notice
*
* @param Profile $profile
* @param string $question
* @param array $opts (poll responses)
* @param null $options
*
* @throws ClientException
*
* @return Notice saved notice
*/
/*
public static function saveNew($profile, $question, $opts, $options = null)
{
if (empty($options)) {
$options = array();
}
$p = new Poll();
$p->id = UUID::gen();
$p->profile_id = $profile->id;
$p->question = $question;
$p->options = implode("\n", $opts);
if (array_key_exists('created', $options)) {
$p->created = $options['created'];
} else {
$p->created = common_sql_now();
}
if (array_key_exists('uri', $options)) {
$p->uri = $options['uri'];
} else {
$p->uri = common_local_url(
'showpoll',
array('id' => $p->id)
);
}
common_log(LOG_DEBUG, "Saving poll: $p->id $p->uri");
$p->insert();
// TRANS: Notice content creating a poll.
// TRANS: %1$s is the poll question, %2$s is a link to the poll.
$content = sprintf(
_m('Poll: %1$s %2$s'),
$question,
$p->uri
);
$link = '<a href="' . htmlspecialchars($p->uri) . '">' . htmlspecialchars($question) . '</a>';
// TRANS: Rendered version of the notice content creating a poll.
// TRANS: %s is a link to the poll with the question as link description.
$rendered = sprintf(_m('Poll: %s'), $link);
$tags = array('poll');
$replies = array();
$options = array_merge(
array('urls' => array(),
'rendered' => $rendered,
'tags' => $tags,
'replies' => $replies,
'object_type' => PollPlugin::POLL_OBJECT),
$options
);
if (!array_key_exists('uri', $options)) {
$options['uri'] = $p->uri;
}
$saved = Notice::saveNew(
$profile->id,
$content,
array_key_exists('source', $options) ?
$options['source'] : 'web',
$options
);
return $saved;
}
*/
}

View File

@ -35,6 +35,7 @@ class PollResponse extends Entity
private ?int $gsactor_id;
private ?int $selection;
private DateTimeInterface $created;
private DateTimeInterface $modified;
public function setId(int $id): self
{
@ -102,6 +103,17 @@ class PollResponse extends Entity
return $this->created;
}
public function setModified(DateTimeInterface $modified): self
{
$this->modified = $modified;
return $this;
}
public function getModified(): DateTimeInterface
{
return $this->modified;
}
// }}} Autocode
/**
@ -121,7 +133,8 @@ class PollResponse extends Entity
'poll_id' => ['type' => 'int', '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],
'created' => ['type' => 'datetime', 'not null' => true, 'default' => 'CURRENT_TIMESTAMP', 'description' => 'date this record was created'],
'modified' => ['type' => 'timestamp', 'not null' => true, 'default' => 'CURRENT_TIMESTAMP', 'description' => 'date this record was modified'],
],
'primary key' => ['id'],
@ -129,6 +142,9 @@ class PollResponse extends Entity
//'poll_uri_key' => array('uri'),
//'poll_response_poll_id_gsactor_id_key' => ['poll_id', 'gsactor_id'], //doctrine bug?
],
'foreign keys' => [
'foreign_poll' => ['poll', ['poll_id' => 'id']],
],
'indexes' => [
'poll_response_gsactor_id_poll_id_index' => ['gsactor_id', 'poll_id'],
@ -149,7 +165,6 @@ class PollResponse extends Entity
$res = DB::dql('select pr from App\Entity\PollResponse pr
where pr.poll_id = :pollId and pr.gsactor_id = :gsactorId',
['pollId' => $pollId, 'gsactorId' => $gsactorId]);
//var_dump( $res);
return count($res) != 0;
}
}

View File

@ -205,4 +205,18 @@ abstract class Common
self::sizeStrToInt(ini_get('memory_limit'))
);
}
/**
* Clamps a value between 2 numbers
*
* @param int $current
* @param int $min
* @param int $max
*
* @return int clamped value
*/
public static function clamp(int $current,int $min,int $max): int
{
return min(max($current,$min),$max);
}
}

38
tests/Entity/PollTest.php Normal file
View File

@ -0,0 +1,38 @@
<?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 App\Tests\Util\Form\ActorArrayTransformer;
use App\Entity\Poll;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
class PollTest extends WebTestCase
{
public function testPoll()
{
$poll1 = Poll::create(['options' => implode("\n",['option 1', '2nd option'])]);
static::assertSame("option 1\n2nd option", $poll1->getOptions());
static::assertSame(['option 1', '2nd option'], $poll1->getOptionsArr());
static::assertTrue($poll1->isValidSelection(1));
static::assertTrue($poll1->isValidSelection(2));
static::assertFalse($poll1->isValidSelection(0));
static::assertFalse($poll1->isValidSelection(3));
}
}

View File

@ -0,0 +1,33 @@
<?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 App\Tests\Util\Common;
use App\Util\Common;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
class CommonTest extends WebTestCase
{
public function testClamp()
{
static::assertSame(2, Common::clamp(2,0,3));
static::assertSame(2, Common::clamp(2,2,3));
static::assertSame(1, Common::clamp(2,0,1));
static::assertSame(3, Common::clamp(2,3,5));
}
}