$qid = $q['qid'];
// Create options (splitting input into lines)
- foreach(preg_split("/((\r?\n)|(\r\n?))/", $options) as $o)
+ foreach(preg_split("/((\r?\n)|(\r\n?))/", trim($options)) as $o)
{
query_db(
"INSERT INTO options(qid, name) VALUES (?, ?)",
}
$ended = false;
-if (isset($qid)) {
+if (isset($qid))
+{
$enddate = get_end_date($qid);
- $nowdate = gmdate("Y-m-d\TH:i");
- $ended = $nowdate > $enddate;
+ if ($enddate)
+ {
+ $nowdate = gmdate("Y-m-d\TH:i");
+ $ended = $nowdate > $enddate;
+ }
}
?>
input, textarea {
font-family: sans-serif;
font-size: 1em;
+ box-sizing: border-box;
+ width: 100%;
+ margin-bottom: 0.5em;
}
input[type=submit] {
background: #fdf;
border: none;
color: #434;
}
+ input[type=number] {
+ width: unset;
+ }
+ form {
+ line-height: 1.5em;
+ }
ul {
list-style-type: none;
padding: 0px;
margin: 10px 0px;
line-height: 1.1em;
}
+ li[draggable=true] {
+ cursor: grab;
+ padding: 0.8em;
+ border: 1px solid #cca;
+ }
hr {
border: none;
border-top: 1px solid #cca;
if (isset($qid) && $view == "vote" && !$ended): ?>
<h2><?= get_title($qid) ?></h2>
-<?php if (isset($enddate)): ?>
+<?php if ($enddate): ?>
<p>Closes: <input type="datetime-local" value="<?= $enddate ?>" disabled> (UTC)</p>
<?php endif ?>
+ <!--
<p>
The option with the most points wins!
Give your favorite option 5 points, second favorite 4 points, etc.
Or give them all 5 points if you really can't decide.
</p>
+ -->
<hr>
<form method="post">
<input name="qid" type="hidden" value="<?= $qid ?>">
<ul>
- <?php foreach (get_options($qid) as $opt): ?>
- <li><input name="opt-<?= $opt ?>" type="number" min="0" max="5">
- <b><?= $opt ?></b></li>
+ <?php
+ $options = get_options($qid);
+ $num_opts = count($options);
+ foreach ($options as $index => $opt):
+ $points = $num_opts - $index;
+ ?>
+ <li draggable="true">
+ <input hidden name="opt-<?= $opt ?>" type="number"
+ min="1" max="<?= $num_opts ?>" value="<?= $points ?>">
+ <b><?= $opt ?></b>
+ </li>
<?php endforeach ?>
</ul>
<input type="submit" value="Cast Vote!">
</form>
+ <script>
+ // Drag-and-drop
+ var draggedElement;
+ document.querySelectorAll("li[draggable='true']").forEach((item) => {
+ item.addEventListener("dragstart", (e) => {
+ draggedElement = item;
+ });
+
+ item.addEventListener("dragenter", (e) => {
+
+ // No need to replace item with itself
+ if (draggedElement === item) return;
+
+ const list = draggedElement.parentElement;
+ var listItems = Array.from(list.children)
+ .filter((i) => (i.tagName == "LI"));
+
+ // Move draggedElement to position of the currently hovered one
+ var oldIndex = listItems.indexOf(draggedElement);
+ var newIndex = listItems.indexOf(item);
+ listItems.splice(oldIndex, 1);
+ listItems.splice(newIndex, 0, draggedElement);
+ list.replaceChildren(...listItems);
+
+ // Update point values based on new positions
+ var points = listItems.length;
+ listItems.forEach((i) => {
+ var input = i.querySelector("input");
+ input.value = points.toString();
+ points --;
+ });
+ });
+ });
+ </script>
+
<hr>
<br><a href="<?= poll_url($qid, "results") ?>">Show Results</a>
<br><br><a href="/">New Poll</a>