forked from GNUsocial/gnu-social
[Poll] Added templates, response counting
This commit is contained in:
@@ -24,6 +24,7 @@ namespace App\Entity;
|
||||
use App\Core\DB\DB;
|
||||
use App\Core\Entity;
|
||||
use DateTimeInterface;
|
||||
use function Functional\id;
|
||||
|
||||
class Poll extends Entity
|
||||
{
|
||||
@@ -162,6 +163,21 @@ class Poll extends Entity
|
||||
return true;
|
||||
}
|
||||
|
||||
public function countResponses(): array
|
||||
{
|
||||
$responses = [];
|
||||
$options = $this->getOptionsArr();
|
||||
for ($i = 1; $i <= count($options); ++$i) {
|
||||
$responses[$options[$i - 1]] = $count = 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])[0][1] / $this->id; //todo: fix
|
||||
}
|
||||
|
||||
//echo var_dump($count);
|
||||
//var_dump($responses);
|
||||
return $responses;
|
||||
}
|
||||
|
||||
//from old version
|
||||
/**
|
||||
* Get a bookmark based on a notice
|
||||
|
@@ -21,6 +21,7 @@
|
||||
|
||||
namespace App\Entity;
|
||||
|
||||
use App\Core\DB\DB;
|
||||
use App\Core\Entity;
|
||||
use DateTimeInterface;
|
||||
|
||||
@@ -30,7 +31,7 @@ class PollResponse extends Entity
|
||||
|
||||
private int $id;
|
||||
private ?string $uri;
|
||||
private string $poll_id;
|
||||
private int $poll_id;
|
||||
private ?int $gsactor_id;
|
||||
private ?int $selection;
|
||||
private DateTimeInterface $created;
|
||||
@@ -57,13 +58,13 @@ class PollResponse extends Entity
|
||||
return $this->uri;
|
||||
}
|
||||
|
||||
public function setPollId(string $poll_id): self
|
||||
public function setPollId(int $poll_id): self
|
||||
{
|
||||
$this->poll_id = $poll_id;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getPollId(): string
|
||||
public function getPollId(): int
|
||||
{
|
||||
return $this->poll_id;
|
||||
}
|
||||
@@ -115,22 +116,30 @@ class PollResponse extends Entity
|
||||
'id' => ['type' => 'serial', 'not null' => true],
|
||||
//'uri' => array('type' => 'varchar', 'length' => 191, 'not null' => true, 'description' => 'UUID to the response notice'),
|
||||
'uri' => ['type' => 'varchar', 'length' => 191, 'description' => 'UUID to the response notice'],
|
||||
'poll_id' => ['type' => 'char', 'length' => 36, 'not null' => true, 'description' => 'UUID of poll being responded to'],
|
||||
'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],
|
||||
],
|
||||
'primary key' => ['id'],
|
||||
/*
|
||||
'unique keys' => array(
|
||||
'poll_uri_key' => array('uri'),
|
||||
'poll_response_poll_id_profile_id_key' => array('poll_id', 'profile_id'),
|
||||
),
|
||||
|
||||
'indexes' => array(
|
||||
'poll_response_profile_id_poll_id_index' => array('profile_id', 'poll_id'),
|
||||
)
|
||||
*/
|
||||
'unique keys' => [
|
||||
//'poll_uri_key' => array('uri'),
|
||||
//'poll_response_poll_id_gsactor_id_key' => ['poll_id', 'gsactor_id'], //doctrine bug?
|
||||
],
|
||||
|
||||
'indexes' => [
|
||||
'poll_response_gsactor_id_poll_id_index' => ['gsactor_id', 'poll_id'],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
public static function exits(int $pollId, int $gsactorId): bool
|
||||
{
|
||||
$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;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user