Add poll 8

Add display of poll results and it status.
This commit is contained in:
Visman 2020-11-21 21:05:04 +07:00
parent d1e883d81b
commit 2ee3fd6f8c
5 changed files with 154 additions and 7 deletions

View file

@ -9,6 +9,7 @@ use ForkBB\Models\DataModel;
use ForkBB\Models\Topic\Model as Topic;
use PDO;
use RuntimeException;
use function \ForkBB\__;
class Model extends DataModel
{
@ -136,4 +137,86 @@ class Model extends DataModel
|| 60 * $this->c->config->i_poll_time > \time() - $this->parent->poll_time
);
}
/**
* Возвращает максимум голосов за один ответ по каждому вопросу
*/
protected function getmaxVote(): array
{
$result = [];
foreach (\array_keys($this->question) as $q) {
$result[$q] = \min(\max($this->vote[$q]), $this->total[$q]);
}
return $result;
}
/**
* Возвращает процент голосов по каждому ответу кажого вопроса
*/
protected function getpercVote(): array
{
$result = [];
foreach (\array_keys($this->question) as $q) {
if ($this->total[$q] > 0) {
$total = $this->total[$q] / 100;
foreach (\array_keys($this->answer[$q]) as $a) {
$result[$q][$a] = \min(100, \round($this->vote[$q][$a] / $total, 2));
}
} else {
foreach (\array_keys($this->answer[$q]) as $a) {
$result[$q][$a] = 0;
}
}
}
return $result;
}
/**
* Возвращает ширину ответа в процентах
*/
protected function getwidthVote(): array
{
$result = [];
foreach (\array_keys($this->question) as $q) {
if ($this->maxVote[$q] > 0) {
$max = $this->maxVote[$q] / 100;
foreach (\array_keys($this->answer[$q]) as $a) {
$result[$q][$a] = \min(100, \round($this->vote[$q][$a] / $max));
}
} else {
foreach (\array_keys($this->answer[$q]) as $a) {
$result[$q][$a] = 0;
}
}
}
return $result;
}
protected function getstatus(): ?string
{
if ($this->tid < 1) {
return null;
} elseif (
$this->c->user->isGuest
&& '1' != $this->c->config->b_poll_guest
) {
return __('Poll results are hidden from the guests');
} elseif (! $this->isOpen) {
return __('This poll is closed');
} elseif (! $this->canSeeResult) {
return __('Poll results are hidden up to %s voters', $this->parent->poll_term);
} elseif ($this->userVoted) {
return __('You voted');
} else {
return __('Poll status is undefined');
}
}
}

View file

@ -68,3 +68,21 @@ msgstr "Duration"
msgid "Poll duration help"
msgstr "Duration of the poll (in days). Set to 0 to disable."
msgid "(%1$s [%2$s%%])"
msgstr "(%1$s [%2$s%%])"
msgid "Poll results are hidden from the guests"
msgstr "Poll results are hidden from the guests"
msgid "This poll is closed"
msgstr "This poll is closed"
msgid "Poll results are hidden up to %s voters"
msgstr "Poll results are hidden up to %s voters"
msgid "You voted"
msgstr "You voted"
msgid "Poll status is undefined"
msgstr "Статус опроса неопределен"

View file

@ -58,7 +58,7 @@ msgid "Question %s legend"
msgstr "Вопрос %s"
msgid "You can choose up to %s answers"
msgstr "Можете выбрать до %s ответов."
msgstr "Можно выбрать до %s ответов."
msgid "In total voted: %s"
msgstr "Проголосовало: %s"
@ -68,3 +68,21 @@ msgstr "Длительность"
msgid "Poll duration help"
msgstr "Продолжительность опроса (в днях). 0 снимает ограничения."
msgid "(%1$s [%2$s%%])"
msgstr "(%1$s [%2$s%%])"
msgid "Poll results are hidden from the guests"
msgstr "Результаты опроса скрыты от гостей"
msgid "This poll is closed"
msgstr "Опрос закрыт"
msgid "Poll results are hidden up to %s voters"
msgstr "Результаты опроса скрыты до %s проголосовавших"
msgid "You voted"
msgstr "Вы проголосовали"
msgid "Poll status is undefined"
msgstr "Статус опроса неопределен"

View file

@ -6,7 +6,7 @@
<fieldset id="id-question-{!! $q !!}" class="f-poll-q">
<legend class="f-poll-ql">{!! __('Question %s legend', $q) !!}</legend>
<h3 class="f-poll-qt">{{ $question }}</h3>
@if ($poll->type[$q] > 1)
@if (($poll->canVote || ! $poll->tid) && $poll->type[$q] > 1)
<p class="f-poll-mult">{!! __('You can choose up to %s answers', $poll->type[$q]) !!}</p>
@endif
<ol class="f-poll-as">
@ -23,8 +23,8 @@
</label>
@elseif ($poll->canSeeResult)
<span class="f-poll-at">{{ $answer }}</span>
<span class="f-poll-ap">(votes 1 [100%])</span>
<p class="f-poll-ab"><span style="width: 100%;"><span>100%</span></span></p>
<span class="f-poll-ap">{!! __('(%1$s [%2$s%%])', $poll->vote[$q][$a], $poll->percVote[$q][$a]) !!}</span>
<p class="f-poll-ab"><span class="f-poll-ab-s1" style="width:{{ $poll->widthVote[$q][$a] }}%;"><span class="f-poll-ab-s2">{{ $poll->widthVote[$q][$a] }}%</span></span></p>
@else
<label class="f-poll-al">
<span class="f-poll-at">{{ $answer }}</span>
@ -33,10 +33,14 @@
</li>
@endforeach
</ol>
<p class="f-poll-total">{!! __('In total voted: %s', $poll->total[$q]) !!}</p>
<p class="f-poll-total">{!! __('In total voted: %s', $poll->total[$q] ?? 0) !!}</p>
</fieldset>
@endforeach
@if ($poll->canVote)
</form>
@else
@if (null !== $poll->status)
<p class="f-poll-status"><span>{!! $poll->status !!}</span></p>
@endif
@endif
</div>

View file

@ -1959,7 +1959,8 @@ body,
padding-top: 0.625rem;
}
#fork .f-poll-ql {
#fork .f-poll-ql,
#fork .f-poll-ab-s2 {
display: none;
}
@ -1981,7 +1982,8 @@ body,
}
#fork .f-poll-mult,
#fork .f-poll-total {
#fork .f-poll-total,
#fork .f-poll-ap {
font-size: 0.8rem;
font-style: italic;
}
@ -1990,6 +1992,28 @@ body,
text-decoration: underline;
}
#fork .f-poll-ab {
border: 0.0625rem solid #AA7939;
width: 60%;
height: 1rem;
}
#fork .f-poll-ab-s1 {
background-color: #AA7939;
height: 1rem;
display: block;
font-size: 0;
overflow: hidden;
}
#fork .f-poll-status {
font-size: 0.8rem;
font-style: italic;
text-align: center;
border-top: 0.0625rem solid #AA7939;
padding-top: 0.625rem;
}
/**********/
/* Репорт */
/**********/