Fixed the way polls are displayed in certain situations.

This commit is contained in:
flash 2019-05-05 17:46:57 +02:00
parent f5d693f24b
commit d33f7614d0
6 changed files with 34 additions and 12 deletions

View file

@ -70,6 +70,10 @@
opacity: .2; opacity: .2;
} }
&--voted &__background {
opacity: .4;
}
&__container { &__container {
display: flex; display: flex;
justify-content: center; justify-content: center;
@ -80,6 +84,10 @@
padding: 5px; padding: 5px;
} }
&--voted &__text {
font-weight: 700;
}
&__votes { &__votes {
flex: 0 0 auto; flex: 0 0 auto;
padding: 5px; padding: 5px;

View file

@ -48,6 +48,11 @@ if (empty($poll)) {
$topicInfo = forum_poll_get_topic($poll['poll_id']); $topicInfo = forum_poll_get_topic($poll['poll_id']);
if (!is_null($topicInfo['topic_locked'])) {
echo "The topic associated with this poll has been locked.<br>";
return;
}
if (!forum_perms_check_user( if (!forum_perms_check_user(
MSZ_FORUM_PERMS_GENERAL, $topicInfo['forum_id'], MSZ_FORUM_PERMS_GENERAL, $topicInfo['forum_id'],
$currentUserId, MSZ_FORUM_PERM_SET_READ $currentUserId, MSZ_FORUM_PERM_SET_READ

View file

@ -202,7 +202,7 @@ function forum_poll_get_topic(int $poll): array
} }
$getTopic = db_prepare(" $getTopic = db_prepare("
SELECT `forum_id`, `topic_id` SELECT `forum_id`, `topic_id`, `topic_locked`
FROM `msz_forum_topics` FROM `msz_forum_topics`
WHERE `poll_id` = :poll WHERE `poll_id` = :poll
"); ");

View file

@ -467,24 +467,27 @@
</div> </div>
{% endmacro %} {% endmacro %}
{% macro forum_poll(poll, options, user_answers, topic_id) %} {% macro forum_poll(poll, options, user_answers, topic_id, can_vote, preview_results) %}
{% from '_layout/input.twig' import input_csrf, input_hidden, input_checkbox, input_checkbox_raw %} {% from '_layout/input.twig' import input_csrf, input_hidden, input_checkbox, input_checkbox_raw %}
{% set user_answers = user_answers is empty or user_answers is not iterable ? [] : user_answers %} {% set user_answers = user_answers is empty or user_answers is not iterable ? [] : user_answers %}
{% set user_answered = user_answers|length > 0 %} {% set user_answered = user_answers|length > 0 %}
{% set results_available = user_answered or poll.poll_expired or poll.poll_preview_results %} {% set results_available = preview_results or user_answered or poll.poll_expired or poll.poll_preview_results %}
{% set can_vote = not poll.poll_expired and (poll.poll_change_vote or not user_answered) %} {% set options_available = not poll.poll_expired and (poll.poll_change_vote or not user_answered) %}
{% set display_results = user_answered or poll.poll_expired %} {% set display_results = user_answered or poll.poll_expired %}
{% if options is iterable and options|length > 0 %} {% if options is iterable and options|length > 0 %}
<div class="forum__poll"> <div class="forum__poll">
{% if results_available %} {% if results_available %}
{{ input_checkbox_raw('', display_results, 'forum__poll__toggle', '', false, {'id':'forum-poll-toggle'}) }} {% if options_available %}
{{ input_checkbox_raw('', display_results, 'forum__poll__toggle', '', false, {'id':'forum-poll-toggle'}) }}
{% endif %}
<div class="container forum__poll__container forum__poll__container--results"> <div class="container forum__poll__container forum__poll__container--results">
<div class="forum__poll__results"> <div class="forum__poll__results">
{% for option in options %} {% for option in options %}
{% set percent = poll.poll_votes < 1 ? 0 : (option.option_votes / poll.poll_votes) * 100 %} {% set percent = poll.poll_votes < 1 ? 0 : (option.option_votes / poll.poll_votes) * 100 %}
<div class="forum__poll__result"> <div class="forum__poll__result{% if option.option_id in user_answers %} forum__poll__result--voted{% endif %}">
<div class="forum__poll__result__background" style="width: {{ percent }}%"> <div class="forum__poll__result__background" style="width: {{ percent }}%">
</div> </div>
<div class="forum__poll__result__container"> <div class="forum__poll__result__container">
@ -506,7 +509,7 @@
</div> </div>
{% endif %} {% endif %}
{% if can_vote %} {% if options_available %}
<div class="forum__poll__buttons"> <div class="forum__poll__buttons">
<label class="input__button forum__poll__button" for="forum-poll-toggle">Vote</label> <label class="input__button forum__poll__button" for="forum-poll-toggle">Vote</label>
</div> </div>
@ -514,7 +517,7 @@
</div> </div>
{% endif %} {% endif %}
{% if can_vote %} {% if options_available %}
<form method="post" action="{{ url('forum-poll-vote') }}" class="container forum__poll__container forum__poll__container--poll js-forum-poll" <form method="post" action="{{ url('forum-poll-vote') }}" class="container forum__poll__container forum__poll__container--poll js-forum-poll"
data-poll-id="{{ poll.poll_id }}" data-poll-max-votes="{{ poll.poll_max_votes }}"> data-poll-id="{{ poll.poll_id }}" data-poll-max-votes="{{ poll.poll_max_votes }}">
{{ input_csrf('forum_poll') }} {{ input_csrf('forum_poll') }}
@ -525,12 +528,13 @@
{{ input_checkbox( {{ input_checkbox(
'poll[answers][]', 'poll[answers][]',
option.option_text, option.option_id in user_answers, 'forum__poll__option', option.option_text, option.option_id in user_answers, 'forum__poll__option',
option.option_id, poll.poll_max_votes <= 1 option.option_id, poll.poll_max_votes <= 1,
null, not can_vote
) }} ) }}
{% endfor %} {% endfor %}
</div> </div>
{% if poll.poll_max_votes > 1 %} {% if can_vote and poll.poll_max_votes > 1 %}
<div class="forum__poll__remaining js-forum-poll-remaining"> <div class="forum__poll__remaining js-forum-poll-remaining">
You have <span class="forum__poll__remaining__num"> You have <span class="forum__poll__remaining__num">
<span class="js-forum-poll-remaining-count">{{ poll.poll_max_votes }}</span> vote<span class="js-forum-poll-remaining-plural">s</span> <span class="js-forum-poll-remaining-count">{{ poll.poll_max_votes }}</span> vote<span class="js-forum-poll-remaining-plural">s</span>
@ -545,7 +549,9 @@
{% endif %} {% endif %}
<div class="forum__poll__buttons"> <div class="forum__poll__buttons">
<button class="input__button forum__poll__button">Vote</button> {% if can_vote %}
<button class="input__button forum__poll__button">Vote</button>
{% endif %}
{% if results_available %} {% if results_available %}
<label class="input__button forum__poll__button" for="forum-poll-toggle">Results</label> <label class="input__button forum__poll__button" for="forum-poll-toggle">Results</label>
{% endif %} {% endif %}

View file

@ -56,7 +56,7 @@
{% block content %} {% block content %}
{{ forum_header(topic_info.topic_title, topic_breadcrumbs, false, canonical_url, topic_actions) }} {{ forum_header(topic_info.topic_title, topic_breadcrumbs, false, canonical_url, topic_actions) }}
{{ topic_notice }} {{ topic_notice }}
{{ forum_poll(topic_info, topic_poll_options, topic_poll_user_answers, topic_info.topic_id) }} {{ forum_poll(topic_info, topic_poll_options, topic_poll_user_answers, topic_info.topic_id, current_user.user_id|default(0) > 0, topic_info.author_user_id == current_user.user_id|default(0)) }}
{{ topic_tools }} {{ topic_tools }}
{{ forum_post_listing(topic_posts, current_user.user_id|default(0), topic_perms) }} {{ forum_post_listing(topic_posts, current_user.user_id|default(0), topic_perms) }}
{{ topic_tools }} {{ topic_tools }}

View file

@ -64,6 +64,9 @@ function pdo_prepare_array(array $keys, bool $useKeys = false, string $format =
return implode(', ', $parts); return implode(', ', $parts);
} }
// render_error, render_info and render_info_or_json should be redone a bit better
// following a uniform format so there can be a global handler for em
function render_error(int $code, string $template = 'errors.%d'): string function render_error(int $code, string $template = 'errors.%d'): string
{ {
return render_info(null, $code, $template); return render_info(null, $code, $template);