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-03 15:53:46 +00:00
|
|
|
namespace App\Entity;
|
2020-10-22 23:13:39 +01:00
|
|
|
|
2020-10-29 18:32:35 +00:00
|
|
|
use App\Core\DB\DB;
|
2020-10-22 23:13:39 +01:00
|
|
|
use App\Core\Entity;
|
|
|
|
use DateTimeInterface;
|
2020-11-08 16:36:06 +00:00
|
|
|
use function Functional\id;
|
2020-10-22 23:13:39 +01:00
|
|
|
|
|
|
|
class Poll extends Entity
|
|
|
|
{
|
2020-10-29 18:32:35 +00:00
|
|
|
// {{{ Autocode
|
2020-10-22 23:13:39 +01:00
|
|
|
|
2020-10-29 18:32:35 +00:00
|
|
|
private int $id;
|
2020-11-04 18:58:20 +00:00
|
|
|
private ?string $uri;
|
|
|
|
private ?int $gsactor_id;
|
2020-10-29 18:32:35 +00:00
|
|
|
private ?string $question;
|
|
|
|
private ?string $options;
|
|
|
|
private DateTimeInterface $created;
|
2020-10-22 23:13:39 +01:00
|
|
|
|
2020-10-29 18:32:35 +00:00
|
|
|
public function setId(int $id): self
|
2020-10-22 23:13:39 +01:00
|
|
|
{
|
|
|
|
$this->id = $id;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2020-10-29 18:32:35 +00:00
|
|
|
public function getId(): int
|
|
|
|
{
|
|
|
|
return $this->id;
|
|
|
|
}
|
|
|
|
|
2020-11-04 18:58:20 +00:00
|
|
|
public function setUri(?string $uri): self
|
2020-10-29 18:32:35 +00:00
|
|
|
{
|
|
|
|
$this->uri = $uri;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2020-11-04 18:58:20 +00:00
|
|
|
public function getUri(): ?string
|
2020-10-29 18:32:35 +00:00
|
|
|
{
|
|
|
|
return $this->uri;
|
|
|
|
}
|
|
|
|
|
2020-11-04 18:58:20 +00:00
|
|
|
public function setGsactorId(?int $gsactor_id): self
|
2020-10-29 18:32:35 +00:00
|
|
|
{
|
2020-11-04 18:58:20 +00:00
|
|
|
$this->gsactor_id = $gsactor_id;
|
2020-10-29 18:32:35 +00:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2020-11-04 18:58:20 +00:00
|
|
|
public function getGsactorId(): ?int
|
2020-10-29 18:32:35 +00:00
|
|
|
{
|
2020-11-04 18:58:20 +00:00
|
|
|
return $this->gsactor_id;
|
2020-10-29 18:32:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function setQuestion(?string $question): self
|
|
|
|
{
|
|
|
|
$this->question = $question;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getQuestion(): ?string
|
|
|
|
{
|
|
|
|
return $this->question;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setOptions(?string $options): self
|
|
|
|
{
|
|
|
|
$this->options = $options;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getOptions(): ?string
|
|
|
|
{
|
|
|
|
return $this->options;
|
|
|
|
}
|
|
|
|
|
2020-10-22 23:13:39 +01:00
|
|
|
public function setCreated(DateTimeInterface $created): self
|
|
|
|
{
|
|
|
|
$this->created = $created;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2020-10-29 18:32:35 +00:00
|
|
|
public function getCreated(): DateTimeInterface
|
|
|
|
{
|
|
|
|
return $this->created;
|
|
|
|
}
|
|
|
|
|
|
|
|
// }}} Autocode
|
|
|
|
|
2020-10-22 23:13:39 +01:00
|
|
|
/**
|
2020-11-09 17:43:10 +00:00
|
|
|
* Entity schema definition
|
|
|
|
*
|
|
|
|
* @return array schema definition
|
2020-10-22 23:13:39 +01:00
|
|
|
*/
|
|
|
|
public static function schemaDef(): array
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'name' => 'poll',
|
|
|
|
'description' => 'Per-notice poll data for Poll plugin',
|
|
|
|
'fields' => [
|
2020-11-04 18:58:20 +00:00
|
|
|
'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?
|
2020-10-22 23:13:39 +01:00
|
|
|
'question' => ['type' => 'text'],
|
|
|
|
'options' => ['type' => 'text'],
|
|
|
|
'created' => ['type' => 'datetime', 'not null' => true],
|
|
|
|
],
|
|
|
|
'primary key' => ['id'],
|
2020-11-04 18:58:20 +00:00
|
|
|
// 'unique keys' => [
|
|
|
|
// 'poll_uri_key' => ['uri'],
|
|
|
|
// ],
|
2020-10-22 23:13:39 +01:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2020-11-09 17:43:10 +00:00
|
|
|
/**
|
|
|
|
* Get poll object from its id
|
|
|
|
*
|
|
|
|
* @param int $id
|
|
|
|
*
|
|
|
|
* @return null|static
|
|
|
|
*/
|
2020-10-29 18:32:35 +00:00
|
|
|
public static function getFromId(int $id): ?self
|
|
|
|
{
|
2020-11-03 15:53:46 +00:00
|
|
|
return DB::find('poll', ['id' => $id]);
|
2020-10-29 18:32:35 +00:00
|
|
|
}
|
|
|
|
|
2020-11-09 17:43:10 +00:00
|
|
|
/**
|
|
|
|
* 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
|
2020-11-04 18:58:20 +00:00
|
|
|
{
|
|
|
|
$options = implode("\n",$opt);
|
2020-11-09 17:43:10 +00:00
|
|
|
return self::create(['gsactor_id' => $gsactorId, 'question' => $question, 'options' => $options]);
|
2020-11-04 18:58:20 +00:00
|
|
|
}
|
|
|
|
|
2020-11-09 17:43:10 +00:00
|
|
|
/**
|
|
|
|
* Gets options in array format
|
|
|
|
*
|
|
|
|
* @return array of options
|
|
|
|
*/
|
2020-11-04 19:43:36 +00:00
|
|
|
public function getOptionsArr(): array
|
2020-11-04 18:58:20 +00:00
|
|
|
{
|
|
|
|
return explode("\n", $this->options);
|
|
|
|
}
|
|
|
|
|
2020-10-22 23:13:39 +01:00
|
|
|
/**
|
|
|
|
* Is this a valid selection index?
|
|
|
|
*
|
|
|
|
* @param int $selection (1-based)
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2020-11-04 19:43:36 +00:00
|
|
|
public function isValidSelection($selection): bool
|
2020-10-22 23:13:39 +01:00
|
|
|
{
|
2020-11-04 19:43:36 +00:00
|
|
|
if ($selection != (int) $selection) {
|
2020-10-22 23:13:39 +01:00
|
|
|
return false;
|
|
|
|
}
|
2020-11-04 19:43:36 +00:00
|
|
|
if ($selection < 1 || $selection > count($this->getOptionsArr())) {
|
2020-10-22 23:13:39 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2020-11-04 19:43:36 +00:00
|
|
|
|
2020-11-09 17:43:10 +00:00
|
|
|
/**
|
|
|
|
* Counts responses from each option from a poll object, stores them into an array
|
|
|
|
*
|
|
|
|
* @return array with question and num of responses
|
|
|
|
*/
|
2020-11-08 16:36:06 +00:00
|
|
|
public function countResponses(): array
|
|
|
|
{
|
|
|
|
$responses = [];
|
|
|
|
$options = $this->getOptionsArr();
|
2020-11-09 17:43:10 +00:00
|
|
|
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
|
2020-11-08 16:36:06 +00:00
|
|
|
where pr.poll_id = :id and pr.selection = :selection',
|
2020-11-09 17:43:10 +00:00
|
|
|
['id' => $this->id, 'selection' => $i + 1])[0][1]);
|
|
|
|
*/
|
2020-11-08 16:36:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $responses;
|
|
|
|
}
|
|
|
|
|
2020-11-04 19:43:36 +00:00
|
|
|
//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);
|
|
|
|
}
|
2020-10-22 23:13:39 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
}
|