From 2e15d6983916759ba70c0da4bcdc166dacb40064 Mon Sep 17 00:00:00 2001 From: Konstantin Protzen Date: Sat, 23 Sep 2023 23:46:04 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Add=20deleting=20and=20renaming=20y?= =?UTF-8?q?ears?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- settings/index.php | 10 ++- settings/manage-year/delete.php | 51 +++++++++++++ settings/manage-year/index.php | 125 ++++++++++++++++++++++++++++++++ settings/manage-year/script.js | 44 +++++++++++ settings/manage-year/update.php | 45 ++++++++++++ settings/style.css | 10 +++ 6 files changed, 284 insertions(+), 1 deletion(-) create mode 100644 settings/manage-year/delete.php create mode 100644 settings/manage-year/index.php create mode 100644 settings/manage-year/script.js create mode 100644 settings/manage-year/update.php diff --git a/settings/index.php b/settings/index.php index ea0706c..4cff2d2 100644 --- a/settings/index.php +++ b/settings/index.php @@ -180,12 +180,20 @@

School-Years

+ prepare("DELETE FROM " . config_table_name_grades . " WHERE user_id = ? AND year = ?")) { + $stmt->bind_param("ss", $_SESSION["user_id"], $_SESSION["setting_years"]); + $stmt->execute(); + $stmt->close(); +} + +// Delete all classes +if ($stmt = $con->prepare("DELETE FROM " . config_table_name_classes . " WHERE user_id = ? AND year = ?")) { + $stmt->bind_param("ss", $_SESSION["user_id"], $_SESSION["setting_years"]); + $stmt->execute(); + $stmt->close(); +} + +// Delete homework +if ($stmt = $con->prepare("DELETE FROM " . config_table_name_homework . " WHERE user_id = ? AND year = ?")) { + $stmt->bind_param("ss", $_SESSION["user_id"], $_SESSION["setting_years"]); + $stmt->execute(); + $stmt->close(); +} + +// Delete year +if ($stmt = $con->prepare("DELETE FROM " . config_table_name_school_years . " WHERE id = ? AND owner = ?")) { + $stmt->bind_param("ss", $_SESSION["setting_years"], $_SESSION["user_id"]); + $stmt->execute(); + $stmt->close(); +} + +$con->close(); + +exit("success"); diff --git a/settings/manage-year/index.php b/settings/manage-year/index.php new file mode 100644 index 0000000..2f4c018 --- /dev/null +++ b/settings/manage-year/index.php @@ -0,0 +1,125 @@ +prepare("SELECT id, name FROM " . config_table_name_school_years . " WHERE owner = ?")) { + $stmt->bind_param("s", $_SESSION["user_id"]); + $stmt->execute(); + $stmt->bind_result($year, $name); + $school_years = []; + while ($stmt->fetch()) { + array_push($school_years, ["id" => $year, "name" => $name]); + } + $stmt->close(); +} + +// DB Con close +$con->close(); +?> + + + + + + + + + Settings | Noten-App + + + + + + + + + + + + +
+
+

School-Years

+
'; + } + ?> +
+ +
+
+
+ Current School Year
+ +
+
+ +
+
+
+
+ New School Year
+ +
+
+
Update Year
+
Delete this Year
+
+

Warning: When you click on delete, all classes, grades and homework entries for the current chosen year will be deleted.

+
+ +
+ +
+
+ + + + + + + + \ No newline at end of file diff --git a/settings/manage-year/script.js b/settings/manage-year/script.js new file mode 100644 index 0000000..f4cbb63 --- /dev/null +++ b/settings/manage-year/script.js @@ -0,0 +1,44 @@ +const submit_button = document.getElementById("submit_button"); + +submit_button.addEventListener("click", function () { + const year_name = document.getElementById("year-name-input").value; + if (year_name == "") { + alert("Please enter a year name"); + return; + } + $.ajax({ + type: "POST", + url: "update.php", + data: { + year_name: year_name + }, + success: function (response) { + if (response == "success") { + window.location.href = "/"; + } else { + alert(response); + } + } + }); +}); + +const delete_button = document.getElementById("delete_button"); + +delete_button.addEventListener("click", function () { + if (confirm("Are you sure you want to delete this year?")) { + $.ajax({ + type: "POST", + url: "delete.php", + data: { + next: document.getElementById("nextyear").innerText + }, + success: function (response) { + if (response == "success") { + window.location.href = "/"; + } else { + alert(response); + } + } + }); + } +}); \ No newline at end of file diff --git a/settings/manage-year/update.php b/settings/manage-year/update.php new file mode 100644 index 0000000..88ecf89 --- /dev/null +++ b/settings/manage-year/update.php @@ -0,0 +1,45 @@ + 20) { + echo "Error: Year name too long"; + exit(); +} +if (strlen($_POST["year_name"]) < 1) { + echo "Error: Year name too short"; + exit(); +} + +// Update year +if ($stmt = $con->prepare("UPDATE " . config_table_name_school_years . " SET name = ? WHERE id = ? AND owner = ?")) { + $stmt->bind_param("sss", $_POST["year_name"], $_SESSION["setting_years"], $_SESSION["user_id"]); + $stmt->execute(); + $stmt->close(); +} + +// Set new year +$_SESSION["setting_years"] = isset($_POST["next"]) ? $_POST["next"] : ""; + +exit("success"); diff --git a/settings/style.css b/settings/style.css index 4fe6be1..228884d 100644 --- a/settings/style.css +++ b/settings/style.css @@ -221,4 +221,14 @@ Add year text-align: center; color: var(--background-color); margin-bottom: 0.5rem; +} + +#delete_button { + background-color: #f8312f; + padding: 1rem; + border-radius: 1rem; + cursor: pointer; + text-align: center; + color: var(--background-color); + margin-bottom: 0.5rem; } \ No newline at end of file