diff options
| author | ilotterytea <iltsu@alright.party> | 2025-04-22 14:16:52 +0500 |
|---|---|---|
| committer | ilotterytea <iltsu@alright.party> | 2025-04-22 14:18:19 +0500 |
| commit | f3bc9fd394e9efc8cc9c6408c6e94d539f05c452 (patch) | |
| tree | c1e6ca85ef14007944b2d06c968139963157a2a2 | |
| parent | aad9ffc2c02ccce9098558503e130e4cc9fc35b3 (diff) | |
feat: ratings
| -rw-r--r-- | database.sql | 8 | ||||
| -rw-r--r-- | public/emotes/index.php | 51 | ||||
| -rw-r--r-- | public/emotes/rate.php | 53 | ||||
| -rw-r--r-- | public/static/img/icons/ratings/-1.png (renamed from public/static/img/icons/coal.png) | bin | 573 -> 573 bytes | |||
| -rw-r--r-- | public/static/img/icons/ratings/1.png (renamed from public/static/img/icons/gem.png) | bin | 883 -> 883 bytes | |||
| -rw-r--r-- | src/config.php | 8 |
6 files changed, 96 insertions, 24 deletions
diff --git a/database.sql b/database.sql index 5585a93..ec59cf8 100644 --- a/database.sql +++ b/database.sql @@ -47,4 +47,12 @@ CREATE TABLE IF NOT EXISTS acquired_emote_sets ( user_id INTEGER NOT NULL, emote_set_id INTEGER NOT NULL, is_default BOOLEAN NOT NULL DEFAULT false +); + +CREATE TABLE IF NOT EXISTS ratings ( + id INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT, + user_id INTEGER NOT NULL REFERENCES users(id), + emote_id INTEGER NOT NULL REFERENCES emotes(id), + rate INTEGER NOT NULL, + rated_at TIMESTAMP NOT NULL DEFAULT UTC_TIMESTAMP );
\ No newline at end of file diff --git a/public/emotes/index.php b/public/emotes/index.php index 5224433..4cddc47 100644 --- a/public/emotes/index.php +++ b/public/emotes/index.php @@ -8,12 +8,13 @@ include "../../src/alert.php"; authorize_user(); -function display_list_emotes(int $page, int $limit): array +$db = new PDO(DB_URL, DB_USER, DB_PASS); + +function display_list_emotes(PDO &$db, int $page, int $limit): array { $search = $_GET["q"] ?? ""; $user_id = $_SESSION["user_id"] ?? "-1"; $offset = $page * $limit; - $db = new PDO(DB_URL, DB_USER, DB_PASS); $stmt = $db->prepare("SELECT e.*, CASE WHEN EXISTS ( SELECT 1 @@ -61,9 +62,8 @@ function display_list_emotes(int $page, int $limit): array return $emotes; } -function display_emote(int $id) +function display_emote(PDO &$db, int $id) { - $db = new PDO(DB_URL, DB_USER, DB_PASS); $stmt = $db->prepare("SELECT * FROM emotes WHERE id = ?"); $stmt->execute([$id]); @@ -93,12 +93,14 @@ $emote = null; $id = $_GET["id"] ?? ""; +$db = new PDO(DB_URL, DB_USER, DB_PASS); + if ($id == "" || !is_numeric($id)) { $page = intval($_GET["p"] ?? "0"); $limit = 50; - $emotes = display_list_emotes($page, $limit); + $emotes = display_list_emotes($db, $page, $limit); } else { - $emote = display_emote(intval($id)); + $emote = display_emote($db, intval($id)); } ?> @@ -144,7 +146,6 @@ if ($id == "" || !is_numeric($id)) { echo '' ?> <div class="items row left full"> <?php - $db = new PDO(DB_URL, DB_USER, DB_PASS); $added = false; if (isset($_SESSION["user_emote_set_id"])) { @@ -152,8 +153,6 @@ if ($id == "" || !is_numeric($id)) { $stmt->execute([$_SESSION["user_emote_set_id"], $emote->get_id()]); $added = $stmt->rowCount() > 0; } - - $db = null; ?> <form action="/emotes/setmanip.php" method="POST"> <input type="text" name="id" value="<?php echo $emote->get_id() ?>" @@ -172,20 +171,26 @@ if ($id == "" || !is_numeric($id)) { </form> </div> <div class="items row right full"> - <form action="/emotes/rate.php" method="POST"> - <input type="text" name="id" value="<?php echo $emote->get_id() ?>" - style="display: none;"> - <input type="text" name="rate" value="5" style="display:none;"> - <button type="submit" class="transparent gem"><img src="/static/img/icons/gem.png" - alt="GEM!" title="IT'S A GEM!"></button> - </form> - <form action="/emotes/rate.php" method="POST"> - <input type="text" name="id" value="<?php echo $emote->get_id() ?>" - style="display: none;"> - <input type="text" name="rate" value="1" style="display:none;"> - <button type="submit" class="transparent coal"><img src="/static/img/icons/coal.png" - alt="COAL!" title="IT'S A COAL!"></button> - </form> + <?php + $stmt = $db->prepare("SELECT rate FROM ratings WHERE user_id = ? AND emote_id = ?"); + $stmt->execute([$_SESSION["user_id"], $id]); + + if ($row = $stmt->fetch()) { + echo 'You gave <img src="/static/img/icons/ratings/' . $row["rate"] . '.png" width="16" height="16"'; + echo 'title="' . RATING_NAMES[$row["rate"]] . '">'; + } else { + foreach (RATING_NAMES as $key => $value) { + echo '<form action="/emotes/rate.php" method="POST">'; + echo '<input type="text" name="id" value="' . $emote->get_id() . '"style="display: none;">'; + echo "<input type=\"text\" name=\"rate\" value=\"$key\" style=\"display:none;\">"; + echo '<button type="submit" class="transparent">'; + echo "<img + src=\"/static/img/icons/ratings/$key.png\" alt=\"$value!\" + title=\"IT'S A $value!\">"; + echo '</button></form>'; + } + } + ?> <a class="button red" href="/emotes/report.php?id=<?php echo $emote->get_id() ?>">Report emote</a> </div> diff --git a/public/emotes/rate.php b/public/emotes/rate.php new file mode 100644 index 0000000..3cc3e01 --- /dev/null +++ b/public/emotes/rate.php @@ -0,0 +1,53 @@ +<?php +include_once "../../src/alert.php"; +include_once "../../src/utils.php"; +include_once "../../src/config.php"; +include_once "../../src/accounts.php"; + +if (!authorize_user(true)) { + exit; +} + +$id = intval(str_safe($_POST["id"] ?? "0", 10)); +$rate = intval(str_safe($_POST["rate"] ?? "0", 2)); + +if ($id == 0 || $rate == 0) { + generate_alert("/emotes" . (isset($_POST["id"]) ? "?id=" . $_POST["id"] : ""), "Not enough POST fields"); + exit; +} + +$db = new PDO(DB_URL, DB_USER, DB_PASS); + +// checking if emote exists +$stmt = $db->prepare("SELECT id FROM emotes WHERE id = ?"); +$stmt->execute([$id]); +if ($stmt->rowCount() != 1) { + generate_alert("/emotes", "Emote ID $id does not exist", 404); + exit; +} + +// checking if user has already given a rate +$stmt = $db->prepare("SELECT id FROM ratings WHERE user_id = ? AND emote_id = ?"); +$stmt->execute([$_SESSION["user_id"], $id]); +if ($stmt->rowCount() != 0) { + generate_alert("/emotes?id=$id", "You have already given a rate for this emote!", 403); + exit; +} + +// giving a rate +$stmt = $db->prepare("INSERT INTO ratings(user_id, emote_id, rate) VALUES (?, ?, ?)"); +$stmt->execute([$_SESSION["user_id"], $id, clamp($rate, -2, 2)]); + +if (CLIENT_REQUIRES_JSON) { + $stmt = $db->prepare("SELECT * FROM ratings WHERE id = ?"); + $stmt->execute([$db->lastInsertId()]); + + json_response([ + "status_code" => 200, + "message" => "Rated!", + "data" => $stmt->fetch(PDO::FETCH_ASSOC) + ]); + exit; +} + +generate_alert("/emotes?id=$id", "Rated!", 200); diff --git a/public/static/img/icons/coal.png b/public/static/img/icons/ratings/-1.png Binary files differindex 38f492a..38f492a 100644 --- a/public/static/img/icons/coal.png +++ b/public/static/img/icons/ratings/-1.png diff --git a/public/static/img/icons/gem.png b/public/static/img/icons/ratings/1.png Binary files differindex 0b01c2b..0b01c2b 100644 --- a/public/static/img/icons/gem.png +++ b/public/static/img/icons/ratings/1.png diff --git a/src/config.php b/src/config.php index 188b7bb..2752566 100644 --- a/src/config.php +++ b/src/config.php @@ -2,4 +2,10 @@ // DATABASE define("DB_USER", "kochan"); define("DB_PASS", "kochan"); -define("DB_URL", "mysql:host=localhost;dbname=tinyemotes;port=3306");
\ No newline at end of file +define("DB_URL", "mysql:host=localhost;dbname=tinyemotes;port=3306"); + +// RATINGS +define("RATING_NAMES", [ + "-1" => "COAL", + "1" => "GEM", +]);
\ No newline at end of file |
