From a064eae86bfa1240ba4e50ebb67bea849ad60b0b Mon Sep 17 00:00:00 2001 From: aymm Date: Sun, 28 Mar 2021 21:19:04 +0200 Subject: [PATCH] Poll voting --- globals.php | 7 +++++++ scripts/poll.js | 29 +++++++++++++++++++++++++++++ styles/style.css | 22 +++++++++++++++++++++- view_poll.php | 13 +++++++++---- vote_poll.php | 17 +++++++++++++++++ 5 files changed, 83 insertions(+), 5 deletions(-) create mode 100644 vote_poll.php diff --git a/globals.php b/globals.php index e7c504c..939b980 100644 --- a/globals.php +++ b/globals.php @@ -47,3 +47,10 @@ function get_page_header( . $logout_link . ''; } + +function redirect($to) +{ + header('Location: '.$to); + die('' + .''); +} diff --git a/scripts/poll.js b/scripts/poll.js index 750cfb7..b43a3d8 100644 --- a/scripts/poll.js +++ b/scripts/poll.js @@ -3,6 +3,7 @@ window.addEventListener('DOMContentLoaded', () => { for (const el of document.querySelectorAll('.option input[type=checkbox]')) { el.onclick = updateVotesRemaining; } + document.querySelector('.success-banner').onclick = hideBanner; }); function displayLocalTimestamps() { @@ -36,4 +37,32 @@ function updateVotesRemaining() { const voteButton = document.querySelector('button[name=submit_vote]'); const canVoteInitial = voteButton.dataset.canVote === 'true'; voteButton.disabled = !canVoteInitial || (total - numChecked) < 0 || numChecked === 0; +} + +function hideBanner() { + const banner = document.querySelector('.success-banner'); + const animationEnd = findTransitionEnd(); + banner.addEventListener(animationEnd, animationEnded); + banner.classList.add('hiding'); +} + +function animationEnded(event) { + event.target.removeEventListener(findTransitionEnd(), animationEnded); + event.target.classList.add('hidden'); + event.target.classList.remove('hiding'); +} + +function findTransitionEnd() { + const e = document.createElement('div'); + const animations = { + 'transition': 'transitionend', + 'OTransition': 'oTransitionEnd', + 'MozTransition': 'transitionend', + 'WebkitTransition': 'webkitTransitionEnd' + }; + for (const a in animations) { + if (e.style[a] !== undefined) { + return animations[a]; + } + } } \ No newline at end of file diff --git a/styles/style.css b/styles/style.css index 8acb4d8..87b23a4 100644 --- a/styles/style.css +++ b/styles/style.css @@ -5,6 +5,7 @@ --main-fg-color: white; --main-accent-color: rgb(253, 122, 0); --disabled-color: gray; + --green: green; } @supports (color: color(display-p3 1 1 1)) { :root { @@ -49,7 +50,7 @@ button:disabled { } .hidden { - display: none; + display: none !important; } /* Header */ @@ -100,6 +101,7 @@ header { .datewrapper { display: grid; grid-column-gap: 8px; + margin-left: 8px; } .created_at { @@ -121,3 +123,21 @@ datewrapper time { .votes-remaining { font-size: small; } + +/* Success banner */ +.success-banner { + background-color: var(--green); + padding: 8px; + display: inline-block; + margin-bottom: 8px; + cursor: pointer; + transition: all 0.5s ease-in; +} + +.success-banner.hiding { + transform: translateX(-150%); +} + +.success-banner span { + font-size: x-large; +} \ No newline at end of file diff --git a/view_poll.php b/view_poll.php index 8538636..ffd3615 100644 --- a/view_poll.php +++ b/view_poll.php @@ -51,8 +51,11 @@ $votes_remaining_hidden = $poll->canVote() ? '' : ' hidden'; $data_can_vote = $poll->canVote() ? 'true' : 'false'; $disabled_button = ($poll->canVote() && count($user_votes) > 0) ? '' : 'disabled'; - -?> +if (array_key_exists('success', $_GET) && $_GET['success'] == 1) { ?> +
+
Your vote has been saved, thank you!
+
+
@@ -82,7 +85,9 @@ $disabled_button = ($poll->canVote() && count($user_votes) > 0) ? '' : 'disabled
-
+ + + canVote() && count($user_votes) > 0) ? '' : 'disabled foreach ($poll->options as $option) { $checked = $option->is_your_response ? 'checked' : ''; ?>
- /> + value="position ?>" name="options[]"/> text . ' (' . $option->respondents . ')'?>
diff --git a/vote_poll.php b/vote_poll.php new file mode 100644 index 0000000..2a7ac7f --- /dev/null +++ b/vote_poll.php @@ -0,0 +1,17 @@ +voteInPoll((int)$_POST['pollid'], $voted_options, $_POST['polltoken']); +} catch (\Exception $e) { + get_page_header('Voting error'); + $str = 'Sorry, something went wrong while voting!' + . 'Please yell at @hutattedonmyarm>
' + . 'Go back to the poll'; + die($str); +} +redirect('view_poll.php?id='.$_POST['pollid'].'&success=1');