Skip to content
This repository has been archived by the owner on Oct 17, 2024. It is now read-only.

Commit

Permalink
✨ Add deleting and renaming years
Browse files Browse the repository at this point in the history
  • Loading branch information
CuzImBisonratte committed Sep 23, 2023
1 parent 33da219 commit 2e15d69
Show file tree
Hide file tree
Showing 6 changed files with 284 additions and 1 deletion.
10 changes: 9 additions & 1 deletion settings/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,12 +180,20 @@
<h1 class="overlay-title">School-Years</h1>
<div class="dropdown_container container_item">
<div class="dropdown_container-name" onclick="location.assign('add-year')">
Add School-Year
Add Year
</div>
<div class="dropdown_container-dropdown_icon">
<i class="fa-solid fa-calendar-plus"></i>
</div>
</div>
<div class="dropdown_container container_item">
<div class="dropdown_container-name" onclick="location.assign('manage-year')">
Manage Years
</div>
<div class="dropdown_container-dropdown_icon">
<i class="fas fa-calendar-day"></i>
</div>
</div>
<?php
foreach ($school_years as $year) {
echo '<div class="dropdown_container container_item"';
Expand Down
51 changes: 51 additions & 0 deletions settings/manage-year/delete.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

// Check login state
require($_SERVER["DOCUMENT_ROOT"] . "/res/php/session.php");
start_session();
require($_SERVER["DOCUMENT_ROOT"] . "/res/php/checkLogin.php");
if (!checkLogin()) header("Location: https://account.noten-app.de");

// Get config
require($_SERVER["DOCUMENT_ROOT"] . "/config.php");

// DB Connection
$con = mysqli_connect(
config_db_host,
config_db_user,
config_db_password,
config_db_name
);
if (mysqli_connect_errno()) exit("Error with the Database");

// Delete all grades
if ($stmt = $con->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");
125 changes: 125 additions & 0 deletions settings/manage-year/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
<?php

// Check login state
require($_SERVER["DOCUMENT_ROOT"] . "/res/php/session.php");
start_session();
require($_SERVER["DOCUMENT_ROOT"] . "/res/php/checkLogin.php");
if (!checkLogin()) header("Location: https://account.noten-app.de");

// Get config
require($_SERVER["DOCUMENT_ROOT"] . "/config.php");

// DB Connection
$con = mysqli_connect(
config_db_host,
config_db_user,
config_db_password,
config_db_name
);
if (mysqli_connect_errno()) exit("Error with the Database");

// Get years
if ($stmt = $con->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();
?>

<!DOCTYPE html>
<html lang="de">

<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Settings | Noten-App</title>
<link rel="icon" type="image/x-icon" href="/res/img/favicon.ico" />
<link rel="stylesheet" href="/res/fontawesome/css/fontawesome.min.css">
<link rel="stylesheet" href="/res/fontawesome/css/solid.min.css">
<link rel="stylesheet" href="/res/css/fonts.css">
<link rel="stylesheet" href="/res/css/main.css">
<link rel="stylesheet" href="/res/css/navbar.css">
<link rel="stylesheet" href="../style.css">
<link rel="stylesheet" href="../overlays.css">
</head>

<body>
<nav>
<a href="/" class="nav-link">
<div class="navbar_icon">
<i class="fas fa-home"></i>
</div>
</a>
<a href="/settings/" class="nav-link">
<div class="navbar_icon">
<i class="fa-solid fa-arrow-left"></i>
</div>
</a>
</nav>
<div class="overlays" id="overlay_container">
<div class="overlay" id="overlay_schoolyears">
<h1 class="overlay-title">School-Years</h1>
<?php
foreach ($school_years as $year) {
echo '<div class="dropdown_container container_item"';
echo 'onclick="loadYear(\'' . $year["id"] . '\');"';
if ($_SESSION["setting_years"] == $year["id"]) echo 'style="background-color: var(--background3-color);"';
echo '><div class="dropdown_container-name">';
echo "<span>" . htmlspecialchars($year["name"]) . "</span>";
echo '</div></div>';
}
?>
</div>
</div>
<main id="main">
<div class="group_container" id="current-year" onclick="open_overlay('overlay_schoolyears');">
<div class="current-year">
<span id="current-year-title">Current School Year</span><br>
<span id="current-year-name"><?php foreach ($school_years as $year) if ($_SESSION["setting_years"] == $year["id"]) echo htmlspecialchars($year["name"]); ?></span>
</div>
<div class="calendar-icon">
<i class="fas fa-calendar-day"></i>
</div>
</div>
<div class="group_container" id="year-delete">
<div class="year-name">
<span id="year-name-title">New School Year</span><br>
<input type="text" id="year-name-input" placeholder="Name" maxlength="20">
</div>
</div>
<div id="submit_button">Update Year</div>
<div id="delete_button">Delete this Year</div>
<div class="group_container" id="info">
<p>Warning: When you click on delete, all classes, grades and homework entries for the current chosen year will be deleted.</p>
</div>
<div id="nextyear" style="display:none;visibility:hidden;" hidden>
<?php
foreach ($school_years as $year) if ($_SESSION["setting_years"] != $year["id"]) {
echo htmlspecialchars($year["id"]);
break;
}
?></div>
<footer>
<div class="footer_container">
<p>Made with ❤️ in Germany.</p>
<p><?php echo config_version_name; ?><?php if ($_SESSION["beta_tester"] == 1) echo " Beta" ?></p>
</div>
</footer>
</main>
<script src="https://assets.noten-app.de/js/jquery/jquery-3.6.1.min.js"></script>
<script src="https://assets.noten-app.de/js/themes/themes.js"></script>
<script src="../overlays.js"></script>
<script src="../school_years.js"></script>
<script src="script.js"></script>
</body>

</html>
44 changes: 44 additions & 0 deletions settings/manage-year/script.js
Original file line number Diff line number Diff line change
@@ -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);
}
}
});
}
});
45 changes: 45 additions & 0 deletions settings/manage-year/update.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

// Check login state
require($_SERVER["DOCUMENT_ROOT"] . "/res/php/session.php");
start_session();
require($_SERVER["DOCUMENT_ROOT"] . "/res/php/checkLogin.php");
if (!checkLogin()) header("Location: https://account.noten-app.de");

// Get config
require($_SERVER["DOCUMENT_ROOT"] . "/config.php");

// DB Connection
$con = mysqli_connect(
config_db_host,
config_db_user,
config_db_password,
config_db_name
);
if (mysqli_connect_errno()) exit("Error with the Database");

// Validate input
if (!isset($_POST["year_name"])) {
echo "Error: Missing input";
exit();
}
if (strlen($_POST["year_name"]) > 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");
10 changes: 10 additions & 0 deletions settings/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

0 comments on commit 2e15d69

Please sign in to comment.