From: Chris Fulljames Date: Sun, 24 May 2026 16:13:16 +0000 (-0400) Subject: Functional X-Git-Url: https://littlesong.place/gitweb/?a=commitdiff_plain;h=4da351b8fd3cc146e9d44da0c8825387a755969a;p=poll.git Functional --- 4da351b8fd3cc146e9d44da0c8825387a755969a diff --git a/index.php b/index.php new file mode 100644 index 0000000..d63c944 --- /dev/null +++ b/index.php @@ -0,0 +1,294 @@ +prepare($query); + $statement->setFetchMode(PDO::FETCH_ASSOC); + $result = $statement->execute($params); + #$statement->debugDumpParams(); + if (!$result) + { + error(500, "Uh-oh, something went wrong. Sorry!"); + } + return $statement; +} + +function get_title($qid) +{ + $q = query_db( + "SELECT title FROM questions WHERE qid = ?", + [ $qid ])->fetch(); + + return $q['title']; +} + +function get_options($qid) +{ + $q = query_db( + "SELECT * FROM options WHERE qid = ?", + [ $qid ])->fetchAll(); + + $opts = array_map(fn($row) => $row['name'], $q); + shuffle($opts); + return $opts; +} + +function get_results($qid) +{ + $q = query_db( + "SELECT * FROM options WHERE qid = ?", + [ $qid ])->fetchAll(); + + $results = []; + foreach ($q as $opt) + { + // Get all points for option + $qq = query_db( + "SELECT SUM(points) FROM responses WHERE oid = ?", + [ $opt['oid'] ])->fetch(); + $results[$opt['name']] = $qq['SUM(points)'] ?? 0; + } + asort($results); + return array_reverse($results); +} + +function get_num_responses($qid) +{ + $q = query_db( + "SELECT COUNT(*) FROM responses INNER JOIN options USING (oid) WHERE qid = ?", + [ $qid ])->fetch(); + return $q["COUNT(*)"]; +} + +function get_end_date($qid) +{ + $q = query_db( + "SELECT enddate FROM questions WHERE qid = ?", + [ $qid ])->fetch(); + return $q['enddate']; +} + +function add_vote($qid, $opt, $points) +{ + $q = query_db( + "SELECT oid FROM options WHERE qid = ? AND name = ?", + [$qid, $opt])->fetch(); + + if ($q) + { + $oid = $q['oid']; + query_db( + "INSERT INTO responses(oid, points) VALUES (?, ?)", + [$oid, $points])->fetch(); + } + else + { + error_log($opt); + } +} + +function create_new_poll($title, $options, $enddate) +{ + // Create new question + $q = query_db( + "INSERT INTO questions(title, enddate) VALUES (?, ?) RETURNING qid", + [$title, $enddate])->fetch(); + $qid = $q['qid']; + + // Create options (splitting input into lines) + foreach(preg_split("/((\r?\n)|(\r\n?))/", $options) as $o) + { + query_db( + "INSERT INTO options(qid, name) VALUES (?, ?)", + [$qid, $o])->fetch(); + } + + return $qid; +} + +function poll_url($qid, $view="vote") +{ + return "?qid=$qid&view=$view"; +} + +// Creating new poll +if (isset($_POST['title']) and isset($_POST['options'])) +{ + $qid = create_new_poll($_POST['title'], $_POST['options'], $_POST['enddate'] ?? NULL); + // Redirect to poll + header("Location: ".poll_url($qid)); + exit; +} + +// Voting +if (isset($_POST['qid'])) +{ + foreach ($_POST as $key => $value) + { + $prefix = "opt-"; + if (str_starts_with($key, $prefix)) + { + $opt = substr($key, strlen($prefix)); + $opt = str_replace("_", " ", $opt); + add_vote($_POST['qid'], $opt, (int) $value); + } + } + + // Redirect to results + header("Location: ".poll_url($qid, "results")); + exit; +} + +// Background JSON check whether more people have voted +if (isset($qid) && isset($_GET['count'])) { + // Check of more users have joined + header('Content-type: application/json'); + if ($_GET['count'] != get_num_responses($qid)) echo '{"reload": true}'; + else echo '{"reload": false}'; + exit; +} + +$ended = false; +if (isset($qid)) { + $enddate = get_end_date($qid); + $nowdate = gmdate("Y-m-d\TH:i"); + $ended = $nowdate > $enddate; +} + +?> + + + + Poll! + + + + + + + + + + + + + + + + + +

+ +

Closes: (UTC)

+ +

+ 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. +

+
+ + + +
+
">Show Results +

New Poll + + +

+ + +

Poll has ended.

+ + + +
New Poll + + +

New Poll

+
+
+
+
+ +
+ + + + + diff --git a/schema.sql b/schema.sql new file mode 100644 index 0000000..152a911 --- /dev/null +++ b/schema.sql @@ -0,0 +1,25 @@ +DROP TABLE IF EXISTS questions; +CREATE TABLE questions ( + qid INTEGER NOT NULL PRIMARY KEY, + title STRING NOT NULL, + enddate STRING +); + +DROP TABLE IF EXISTS options; +CREATE TABLE options ( + oid INTEGER NOT NULL PRIMARY KEY, + qid INTEGER NOT NULL, + name TEXT NOT NULL, + + FOREIGN KEY(qid) REFERENCES questions(qid) ON DELETE CASCADE +); + +DROP TABLE IF EXISTS responses; +CREATE TABLE responses ( + rid INTEGER NOT NULL PRIMARY KEY, + oid INTEGER NOT NULL, + points INTEGER NOT NULL, + + FOREIGN KEY(oid) REFERENCES options(oid) ON DELETE CASCADE +); +