2020-11-04 18:58:20 +00:00
|
|
|
<?php
|
|
|
|
|
2021-10-10 09:26:18 +01:00
|
|
|
declare(strict_types = 1);
|
|
|
|
|
2020-11-04 18:58:20 +00: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/>.
|
|
|
|
|
|
|
|
// }}}
|
|
|
|
|
2021-04-16 16:57:25 +01:00
|
|
|
namespace Plugin\Poll\Entity;
|
2020-11-04 18:58:20 +00:00
|
|
|
|
2022-03-27 15:19:09 +01:00
|
|
|
use App\Core\DB;
|
2020-11-04 18:58:20 +00:00
|
|
|
use App\Core\Entity;
|
|
|
|
use DateTimeInterface;
|
|
|
|
|
2020-11-10 02:01:31 +00:00
|
|
|
/**
|
|
|
|
* For storing a poll response
|
|
|
|
*
|
|
|
|
* @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-11-04 18:58:20 +00:00
|
|
|
class PollResponse extends Entity
|
|
|
|
{
|
|
|
|
// {{{ Autocode
|
2021-05-05 17:03:03 +01:00
|
|
|
// @codeCoverageIgnoreStart
|
2020-11-04 18:58:20 +00:00
|
|
|
private int $id;
|
2021-12-26 21:32:09 +00:00
|
|
|
private ?string $uri = null;
|
2020-11-08 16:36:06 +00:00
|
|
|
private int $poll_id;
|
2021-12-26 21:32:09 +00:00
|
|
|
private ?int $actor_id = null;
|
|
|
|
private ?int $selection = null;
|
2021-10-10 09:26:18 +01:00
|
|
|
private DateTimeInterface $created;
|
|
|
|
private DateTimeInterface $modified;
|
2020-11-04 18:58:20 +00:00
|
|
|
|
|
|
|
public function setId(int $id): self
|
|
|
|
{
|
|
|
|
$this->id = $id;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getId(): int
|
|
|
|
{
|
|
|
|
return $this->id;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setUri(?string $uri): self
|
|
|
|
{
|
2021-12-26 21:32:09 +00:00
|
|
|
$this->uri = \is_null($uri) ? null : mb_substr($uri, 0, 191);
|
2020-11-04 18:58:20 +00:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getUri(): ?string
|
|
|
|
{
|
|
|
|
return $this->uri;
|
|
|
|
}
|
|
|
|
|
2020-11-08 16:36:06 +00:00
|
|
|
public function setPollId(int $poll_id): self
|
2020-11-04 18:58:20 +00:00
|
|
|
{
|
|
|
|
$this->poll_id = $poll_id;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2020-11-08 16:36:06 +00:00
|
|
|
public function getPollId(): int
|
2020-11-04 18:58:20 +00:00
|
|
|
{
|
|
|
|
return $this->poll_id;
|
|
|
|
}
|
|
|
|
|
2021-09-18 03:22:27 +01:00
|
|
|
public function setActorId(?int $actor_id): self
|
2020-11-04 18:58:20 +00:00
|
|
|
{
|
2021-09-18 03:22:27 +01:00
|
|
|
$this->actor_id = $actor_id;
|
2020-11-04 18:58:20 +00:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2021-09-18 03:22:27 +01:00
|
|
|
public function getActorId(): ?int
|
2020-11-04 18:58:20 +00:00
|
|
|
{
|
2021-09-18 03:22:27 +01:00
|
|
|
return $this->actor_id;
|
2020-11-04 18:58:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function setSelection(?int $selection): self
|
|
|
|
{
|
|
|
|
$this->selection = $selection;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getSelection(): ?int
|
|
|
|
{
|
|
|
|
return $this->selection;
|
|
|
|
}
|
|
|
|
|
2021-05-05 17:03:03 +01:00
|
|
|
public function setCreated(DateTimeInterface $created): self
|
2020-11-04 18:58:20 +00:00
|
|
|
{
|
|
|
|
$this->created = $created;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2021-05-05 17:03:03 +01:00
|
|
|
public function getCreated(): DateTimeInterface
|
2020-11-04 18:58:20 +00:00
|
|
|
{
|
|
|
|
return $this->created;
|
|
|
|
}
|
|
|
|
|
2021-05-05 17:03:03 +01:00
|
|
|
public function setModified(DateTimeInterface $modified): self
|
2020-11-09 21:25:57 +00:00
|
|
|
{
|
|
|
|
$this->modified = $modified;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2021-05-05 17:03:03 +01:00
|
|
|
public function getModified(): DateTimeInterface
|
2020-11-09 21:25:57 +00:00
|
|
|
{
|
|
|
|
return $this->modified;
|
|
|
|
}
|
|
|
|
|
2021-05-05 17:03:03 +01:00
|
|
|
// @codeCoverageIgnoreEnd
|
2020-11-04 18:58:20 +00:00
|
|
|
// }}} Autocode
|
|
|
|
|
|
|
|
/**
|
2020-11-09 17:43:10 +00:00
|
|
|
* Entity schema definition
|
|
|
|
*
|
|
|
|
* @return array schema definition
|
2020-11-04 18:58:20 +00:00
|
|
|
*/
|
2021-10-10 09:26:18 +01:00
|
|
|
public static function schemaDef(): array
|
2020-11-04 18:58:20 +00:00
|
|
|
{
|
|
|
|
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'),
|
2021-09-18 03:22:27 +01:00
|
|
|
'uri' => ['type' => 'varchar', 'length' => 191, 'description' => 'UUID to the response notice'],
|
2021-12-26 15:12:06 +00:00
|
|
|
'poll_id' => ['type' => 'int', 'not null' => true, 'description' => 'UUID of poll being responded to'],
|
2021-09-18 03:22:27 +01:00
|
|
|
'actor_id' => ['type' => 'int'],
|
|
|
|
'selection' => ['type' => 'int'],
|
|
|
|
'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'],
|
2020-11-04 18:58:20 +00:00
|
|
|
],
|
|
|
|
'primary key' => ['id'],
|
2020-11-08 16:36:06 +00:00
|
|
|
|
|
|
|
'unique keys' => [
|
|
|
|
//'poll_uri_key' => array('uri'),
|
2021-09-18 03:22:27 +01:00
|
|
|
//'poll_response_poll_id_actor_id_key' => ['poll_id', 'actor_id'], //doctrine bug?
|
2020-11-08 16:36:06 +00:00
|
|
|
],
|
2020-11-09 21:25:57 +00:00
|
|
|
'foreign keys' => [
|
|
|
|
'foreign_poll' => ['poll', ['poll_id' => 'id']],
|
|
|
|
],
|
2020-11-08 16:36:06 +00:00
|
|
|
|
|
|
|
'indexes' => [
|
2021-09-18 03:22:27 +01:00
|
|
|
'poll_response_actor_id_poll_id_index' => ['actor_id', 'poll_id'],
|
2020-11-08 16:36:06 +00:00
|
|
|
],
|
2020-11-04 18:58:20 +00:00
|
|
|
];
|
|
|
|
}
|
2020-11-08 16:36:06 +00:00
|
|
|
|
2020-11-09 17:43:10 +00:00
|
|
|
/**
|
|
|
|
* Checks if a user already responded to the poll
|
|
|
|
*
|
2021-09-18 03:22:27 +01:00
|
|
|
* @param int $actorId user
|
2020-11-09 17:43:10 +00:00
|
|
|
*/
|
2021-09-18 03:22:27 +01:00
|
|
|
public static function exits(int $pollId, int $actorId): bool
|
2020-11-08 16:36:06 +00:00
|
|
|
{
|
2021-10-10 09:26:18 +01:00
|
|
|
$res = DB::dql(
|
|
|
|
'select pr from App\Entity\PollResponse pr
|
2021-09-18 03:22:27 +01:00
|
|
|
where pr.poll_id = :pollId and pr.actor_id = :actorId',
|
2021-10-10 09:26:18 +01:00
|
|
|
['pollId' => $pollId, 'actorId' => $actorId],
|
|
|
|
);
|
|
|
|
return \count($res) != 0;
|
2020-11-08 16:36:06 +00:00
|
|
|
}
|
2020-11-04 18:58:20 +00:00
|
|
|
}
|