36646-vm/student.php
Flatlogic Bot e64eb23c43 bhuddi2.0
2025-12-04 12:25:23 +00:00

820 lines
42 KiB
PHP

<?php
session_start();
// If the user is not logged in, redirect to the login page...
if (!isset($_SESSION['loggedin'])) {
header('Location: login.php');
exit;
}
$is_viewer = $_SESSION['role'] === 'viewer';
// student.php
// 1. Get student ID from URL
$student_id = isset($_GET['id']) ? (int)$_GET['id'] : 0;
if (!$student_id) {
header("Location: index.php");
exit();
}
// 2. Database connection
require_once 'db/config.php';
try {
$pdo = db();
$stmt = $pdo->prepare("SELECT * FROM students WHERE id = ?");
$stmt->execute([$student_id]);
$student = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$student) {
// Optional: Show a "not found" message or redirect
header("Location: index.php?error=not_found");
exit();
}
// -- Behavior Insights Logic --
// 2.1. Create behaviors table if it doesn't exist and add rating column
$pdo->exec("CREATE TABLE IF NOT EXISTS behaviors (
id INT AUTO_INCREMENT PRIMARY KEY,
student_id INT NOT NULL,
behavior_description TEXT NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (student_id) REFERENCES students(id) ON DELETE CASCADE
);");
try {
$pdo->exec("ALTER TABLE behaviors ADD COLUMN rating INT NOT NULL DEFAULT 5");
} catch (PDOException $e) {
// Ignore errors, especially if the column already exists.
}
// 2.2. Handle form submission for adding a new behavior
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] === 'add_behavior') {
$behavior_description = trim($_POST['behavior_description'] ?? '');
$behavior_rating = isset($_POST['behavior_rating']) ? (int)$_POST['behavior_rating'] : 5;
if (!empty($behavior_description)) {
$stmt = $pdo->prepare("INSERT INTO behaviors (student_id, behavior_description, rating) VALUES (?, ?, ?)");
$stmt->execute([$student_id, $behavior_description, $behavior_rating]);
// Redirect to the same page to prevent form resubmission
header("Location: student.php?id=" . $student_id . "&success=behavior_logged");
exit();
}
}
// 2.3. Handle form submission for editing a behavior
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] === 'edit_behavior') {
$behavior_id = isset($_POST['behavior_id']) ? (int)$_POST['behavior_id'] : 0;
$behavior_description = trim($_POST['behavior_description'] ?? '');
$behavior_rating = isset($_POST['behavior_rating']) ? (int)$_POST['behavior_rating'] : 5;
if ($behavior_id && !empty($behavior_description)) {
$stmt = $pdo->prepare("UPDATE behaviors SET behavior_description = ?, rating = ? WHERE id = ? AND student_id = ?");
$stmt->execute([$behavior_description, $behavior_rating, $behavior_id, $student_id]);
header("Location: student.php?id=" . $student_id . "&success=behavior_updated");
exit();
}
}
// 2.3. Handle form submission for deleting a behavior
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] === 'delete_behavior') {
$behavior_id = isset($_POST['behavior_id']) ? (int)$_POST['behavior_id'] : 0;
if ($behavior_id) {
$stmt = $pdo->prepare("DELETE FROM behaviors WHERE id = ? AND student_id = ?");
$stmt->execute([$behavior_id, $student_id]);
header("Location: student.php?id=" . $student_id . "&success=behavior_deleted");
exit();
}
}
// 2.4. Fetch all behaviors for this student
$stmt = $pdo->prepare("SELECT * FROM behaviors WHERE student_id = ? ORDER BY created_at DESC");
$stmt->execute([$student_id]);
$behaviors = $stmt->fetchAll(PDO::FETCH_ASSOC);
// -- Academic Insights Logic --
// 2.4. Create academics table if it doesn't exist
$pdo->exec("CREATE TABLE IF NOT EXISTS academics (
id INT AUTO_INCREMENT PRIMARY KEY,
student_id INT NOT NULL,
subject VARCHAR(100) NOT NULL,
grade VARCHAR(2) NOT NULL,
comments TEXT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (student_id) REFERENCES students(id) ON DELETE CASCADE
);");
// 2.5. Handle form submission for adding a new academic record
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] === 'add_academic') {
$subject = trim($_POST['subject'] ?? '');
$grade = trim($_POST['grade'] ?? '');
$comments = trim($_POST['comments'] ?? '');
if (!empty($subject) && !empty($grade)) {
$stmt = $pdo->prepare("INSERT INTO academics (student_id, subject, grade, comments) VALUES (?, ?, ?, ?)");
$stmt->execute([$student_id, $subject, $grade, $comments]);
header("Location: student.php?id=" . $student_id . "&success=academic_logged");
exit();
}
}
// Handle form submission for editing an academic record
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] === 'edit_academic') {
$academic_id = isset($_POST['academic_id']) ? (int)$_POST['academic_id'] : 0;
$subject = trim($_POST['subject'] ?? '');
$grade = trim($_POST['grade'] ?? '');
$comments = trim($_POST['comments'] ?? '');
if ($academic_id && !empty($subject) && !empty($grade)) {
$stmt = $pdo->prepare("UPDATE academics SET subject = ?, grade = ?, comments = ? WHERE id = ? AND student_id = ?");
$stmt->execute([$subject, $grade, $comments, $academic_id, $student_id]);
header("Location: student.php?id=" . $student_id . "&success=academic_updated");
exit();
}
}
// Handle form submission for deleting an academic record
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] === 'delete_academic') {
$academic_id = isset($_POST['academic_id']) ? (int)$_POST['academic_id'] : 0;
if ($academic_id) {
$stmt = $pdo->prepare("DELETE FROM academics WHERE id = ? AND student_id = ?");
$stmt->execute([$academic_id, $student_id]);
header("Location: student.php?id=" . $student_id . "&success=academic_deleted");
exit();
}
}
// 2.6. Fetch all academic records for this student
$stmt = $pdo->prepare("SELECT * FROM academics WHERE student_id = ? ORDER BY created_at DESC");
$stmt->execute([$student_id]);
$academics = $stmt->fetchAll(PDO::FETCH_ASSOC);
// -- Student Details Logic --
// Create student_details table
$pdo->exec("CREATE TABLE IF NOT EXISTS student_details (
id INT AUTO_INCREMENT PRIMARY KEY,
student_id INT NOT NULL,
title VARCHAR(255) NOT NULL,
details TEXT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (student_id) REFERENCES students(id) ON DELETE CASCADE
);")
// Fetch all details for this student
$stmt = $pdo->prepare("SELECT * FROM student_details WHERE student_id = ? ORDER BY created_at DESC");
$stmt->execute([$student_id]);
$student_details = $stmt->fetchAll(PDO::FETCH_ASSOC);
// Handle form submission for adding a new student detail
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] === 'add_student_detail') {
$title = trim($_POST['detail_title'] ?? '');
$details = trim($_POST['detail_content'] ?? '');
if (!empty($title)) {
$stmt = $pdo->prepare("INSERT INTO student_details (student_id, title, details) VALUES (?, ?, ?)");
$stmt->execute([$student_id, $title, $details]);
header("Location: student.php?id=" . $student_id . "&success=detail_added");
exit();
}
}
// Handle form submission for deleting a student detail
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] === 'delete_student_detail') {
$detail_id = isset($_POST['detail_id']) ? (int)$_POST['detail_id'] : 0;
if ($detail_id) {
$stmt = $pdo->prepare("DELETE FROM student_details WHERE id = ? AND student_id = ?");
$stmt->execute([$detail_id, $student_id]);
header("Location: student.php?id=" . $student_id . "&success=detail_deleted");
exit();
}
}
// Handle form submission for editing a student detail
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] === 'edit_student_detail') {
$detail_id = isset($_POST['detail_id']) ? (int)$_POST['detail_id'] : 0;
$title = trim($_POST['detail_title'] ?? '');
$details = trim($_POST['detail_content'] ?? '');
if ($detail_id && !empty($title)) {
$stmt = $pdo->prepare("UPDATE student_details SET title = ?, details = ? WHERE id = ? AND student_id = ?");
$stmt->execute([$title, $details, $detail_id, $student_id]);
header("Location: student.php?id=" . $student_id . "&success=detail_updated");
exit();
}
}
} catch (PDOException $e) {
// For development, you might want to log this error.
// For production, show a generic error message.
die("Database error: Could not retrieve student data.");
}
$pageTitle = htmlspecialchars($student['first_name'] . ' ' . $student['last_name']);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title><?php echo $pageTitle; ?> - Student Details</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.min.css">
<link rel="stylesheet" href="assets/css/custom.css?v=<?php echo time(); ?>">
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;500;600;700&display=swap" rel="stylesheet">
</head>
<body class="bg-light">
<nav class="navbar navbar-expand-lg navbar-light bg-white shadow-sm">
<div class="container-fluid">
<a class="navbar-brand" href="index.php">Bhuddi School</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav ms-auto">
<li class="nav-item">
<a class="nav-link" href="#">Welcome, <?= htmlspecialchars($_SESSION['username']) ?>!</a>
</li>
<li class="nav-item">
<a class="nav-link" href="logout.php">Logout</a>
</li>
<?php if ($_SESSION['role'] === 'admin'): ?>
<li class="nav-item">
<a class="nav-link" href="admin.php">Admin</a>
</li>
<?php endif; ?>
</ul>
</div>
</div>
</nav>
<main class="container mt-5">
<div class="card p-4 shadow-sm border-0 rounded-3">
<div class="card-body">
<div class="d-flex align-items-center mb-4">
<i class="bi bi-person-circle text-primary me-3" style="font-size: 3rem;"></i>
<div>
<h1 class="h3 mb-0"><?php echo htmlspecialchars($student['first_name'] . ' ' . $student['last_name']); ?></h1>
<p class="text-muted mb-0">Student Profile</p>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="mb-3">
<strong class="text-secondary">First Name:</strong>
<p class="lead"><?php echo htmlspecialchars($student['first_name']); ?></p>
</div>
</div>
<div class="col-md-6">
<div class="mb-3">
<strong class="text-secondary">Last Name:</strong>
<p class="lead"><?php echo htmlspecialchars($student['last_name']); ?></p>
</div>
</div>
<div class="col-md-6">
<div class="mb-3">
<strong class="text-secondary">Date of Birth:</strong>
<p class="lead"><?php echo htmlspecialchars(date('F j, Y', strtotime($student['date_of_birth']))); ?></p>
</div>
</div>
<div class="col-md-6">
<div class="mb-3">
<strong class="text-secondary">Admission Date:</strong>
<p class="lead"><?php echo htmlspecialchars(date('F j, Y, g:i a', strtotime($student['created_at']))); ?></p>
</div>
</div>
</div>
<div class="mt-4">
<a href="index.php" class="btn btn-outline-secondary">
<i class="bi bi-arrow-left"></i> Back to Dashboard
</a>
</div>
</div>
</div>
<!-- Behavior Insights Section -->
<div class="card p-4 mt-4 shadow-sm border-0 rounded-3">
<div class="card-body">
<h2 class="h4 mb-4">Behavior Insights</h2>
<!-- Log New Behavior Form -->
<?php if (!$is_viewer): ?>
<div class="mb-5">
<h3 class="h5 mb-3">Log New Behavior</h3>
<form method="POST" action="student.php?id=<?php echo $student_id; ?>">
<input type="hidden" name="action" value="add_behavior">
<div class="mb-3">
<label for="behavior_description" class="form-label">Description</label>
<textarea class="form-control" id="behavior_description" name="behavior_description" rows="3" required></textarea>
</div>
<div class="mb-3">
<label for="behavior_rating" class="form-label">Severity Scale (<span id="rating_value">5</span>/10)</label>
<input type="range" class="form-range" id="behavior_rating" name="behavior_rating" min="1" max="10" value="5" oninput="document.getElementById('rating_value').innerText = this.value;">
</div>
<button type="submit" class="btn btn-primary">
<i class="bi bi-plus-circle"></i> Log Behavior
</button>
</form>
</div>
<?php endif; ?>
<!-- Behavior History -->
<div>
<h3 class="h5 mb-3">Behavior History</h3>
<?php if (empty($behaviors)): ?>
<p class="text-muted">No behavior entries have been logged for this student yet.</p>
<?php else: ?>
<ul class="list-group">
<?php foreach ($behaviors as $behavior): ?>
<li class="list-group-item d-flex justify-content-between align-items-start">
<div class="ms-2 me-auto">
<div class="fw-bold"><?php echo htmlspecialchars($behavior['behavior_description']); ?></div>
<small class="text-muted">Logged on: <?php echo htmlspecialchars(date('F j, Y, g:i a', strtotime($behavior['created_at']))); ?></small>
</div>
<span class="badge bg-primary rounded-pill me-3">Rating: <?php echo htmlspecialchars($behavior['rating']); ?></span>
<?php if (!$is_viewer): ?>
<div>
<button class="btn btn-sm btn-outline-primary me-1 btn-edit-behavior"
data-bs-toggle="modal"
data-bs-target="#editBehaviorModal"
data-behavior-id="<?php echo $behavior['id']; ?>"
data-behavior-description="<?php echo htmlspecialchars($behavior['behavior_description']); ?>"
data-behavior-rating="<?php echo htmlspecialchars($behavior['rating']); ?>">
<i class="bi bi-pencil"></i>
</button>
<button class="btn btn-sm btn-outline-danger btn-delete-behavior" data-behavior-id="<?php echo $behavior['id']; ?>"><i class="bi bi-trash"></i></button>
</div>
<?php endif; ?>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
</div>
</div>
</div>
<!-- Academic Insights Section -->
<div class="card p-4 mt-4 shadow-sm border-0 rounded-3">
<div class="card-body">
<h2 class="h4 mb-4">Academic Insights</h2>
<!-- Log New Academic Record Form -->
<?php if (!$is_viewer): ?>
<div class="mb-5">
<h3 class="h5 mb-3">Log New Academic Record</h3>
<form method="POST">
<input type="hidden" name="action" value="add_academic">
<div class="row">
<div class="col-md-6 mb-3">
<label for="subject" class="form-label">Subject</label>
<input type="text" class="form-control" id="subject" name="subject" required>
</div>
<div class="col-md-6 mb-3">
<label for="grade" class="form-label">Grade</label>
<select class="form-select" id="grade" name="grade" required>
<option value="" disabled selected>Select a grade</option>
<option value="A">A</option>
<option value="B">B</option>
<option value="C">C</option>
<option value="D">D</option>
<option value="F">F</option>
</select>
</div>
</div>
<div class="mb-3">
<label for="comments" class="form-label">Comments</label>
<textarea class="form-control" id="comments" name="comments" rows="3"></textarea>
</div>
<button type="submit" class="btn btn-primary">
<i class="bi bi-plus-circle"></i> Log Academic Record
</button>
</form>
</div>
<?php endif; ?>
<!-- Academic History -->
<div>
<h3 class="h5 mb-3">Academic History</h3>
<?php if (empty($academics)): ?>
<p class="text-muted">No academic records have been logged for this student yet.</p>
<?php else: ?>
<div class="table-responsive">
<table class="table table-striped table-hover">
<thead class="table-light">
<tr>
<th>Subject</th>
<th>Grade</th>
<th>Comments</th>
<th>Date</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php foreach ($academics as $academic): ?>
<tr>
<td><?php echo htmlspecialchars($academic['subject']); ?></td>
<td><span class="badge bg-primary"><?php echo htmlspecialchars($academic['grade']); ?></span></td>
<td><?php echo nl2br(htmlspecialchars($academic['comments'])); ?></td>
<td><small class="text-muted"><?php echo htmlspecialchars(date('F j, Y', strtotime($academic['created_at']))); ?></small></td>
<?php if (!$is_viewer): ?>
<td>
<button class="btn btn-sm btn-outline-primary me-1 btn-edit-academic"
data-bs-toggle="modal"
data-bs-target="#editAcademicModal"
data-academic-id="<?php echo $academic['id']; ?>"
data-subject="<?php echo htmlspecialchars($academic['subject']); ?>"
data-grade="<?php echo htmlspecialchars($academic['grade']); ?>"
data-comments="<?php echo htmlspecialchars($academic['comments']); ?>">
<i class="bi bi-pencil"></i>
</button>
<button class="btn btn-sm btn-outline-danger btn-delete-academic" data-academic-id="<?php echo $academic['id']; ?>"><i class="bi bi-trash"></i></button>
</td>
<?php endif; ?>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
<?php endif; ?>
</div>
</div>
</div>
<!-- Student Details Section -->
<div class="card p-4 mt-4 shadow-sm border-0 rounded-3">
<div class="card-body">
<h2 class="h4 mb-4">Additional Student Details</h2>
<!-- Add New Detail Form -->
<?php if (!$is_viewer): ?>
<div class="mb-5">
<h3 class="h5 mb-3">Add New Detail</h3>
<form method="POST">
<input type="hidden" name="action" value="add_student_detail">
<div class="row">
<div class="col-md-6 mb-3">
<label for="detail_title" class="form-label">Title</label>
<input type="text" class="form-control" id="detail_title" name="detail_title" required>
</div>
</div>
<div class="mb-3">
<label for="detail_content" class="form-label">Details</label>
<textarea class="form-control" id="detail_content" name="detail_content" rows="3"></textarea>
</div>
<button type="submit" class="btn btn-primary">
<i class="bi bi-plus-circle"></i> Add Detail
</button>
</form>
</div>
<?php endif; ?>
<!-- Details History -->
<div>
<h3 class="h5 mb-3">Details History</h3>
<?php if (empty($student_details)): ?>
<p class="text-muted">No additional details have been logged for this student yet.</p>
<?php else: ?>
<div class="table-responsive">
<table class="table table-striped table-hover">
<thead class="table-light">
<tr>
<th>Title</th>
<th>Details</th>
<th>Date</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php foreach ($student_details as $detail): ?>
<tr>
<td><?php echo htmlspecialchars($detail['title']); ?></td>
<td><?php echo nl2br(htmlspecialchars($detail['details'])); ?></td>
<td><small class="text-muted"><?php echo htmlspecialchars(date('F j, Y', strtotime($detail['created_at']))); ?></small></td>
<?php if (!$is_viewer): ?>
<td>
<button class="btn btn-sm btn-outline-primary me-1 btn-edit-detail"
data-bs-toggle="modal"
data-bs-target="#editDetailModal"
data-detail-id="<?php echo $detail['id']; ?>"
data-detail-title="<?php echo htmlspecialchars($detail['title']); ?>"
data-detail-content="<?php echo htmlspecialchars($detail['details']); ?>">
<i class="bi bi-pencil"></i>
</button>
<button class="btn btn-sm btn-outline-danger btn-delete-detail" data-detail-id="<?php echo $detail['id']; ?>"><i class="bi bi-trash"></i></button>
</td>
<?php endif; ?>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
<?php endif; ?>
</div>
</div>
</div>
</main>
<!-- Edit Behavior Modal -->
<div class="modal fade" id="editBehaviorModal" tabindex="-1" aria-labelledby="editBehaviorModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="editBehaviorModalLabel">Edit Behavior Entry</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<form id="editBehaviorForm" method="POST">
<div class="modal-body">
<input type="hidden" name="action" value="edit_behavior">
<input type="hidden" id="edit_behavior_id" name="behavior_id">
<div class="mb-3">
<label for="edit_behavior_description" class="form-label">Description</label>
<textarea class="form-control" id="edit_behavior_description" name="behavior_description" rows="3" required></textarea>
</div>
<div class="mb-3">
<label for="edit_behavior_rating" class="form-label">Severity Scale (<span id="edit_rating_value">5</span>/10)</label>
<input type="range" class="form-range" id="edit_behavior_rating" name="behavior_rating" min="1" max="10" oninput="document.getElementById('edit_rating_value').innerText = this.value;">
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Save changes</button>
</div>
</form>
</div>
</div>
</div>
<!-- Edit Academic Modal -->
<div class="modal fade" id="editAcademicModal" tabindex="-1" aria-labelledby="editAcademicModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="editAcademicModalLabel">Edit Academic Entry</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<form id="editAcademicForm" method="POST">
<div class="modal-body">
<input type="hidden" name="action" value="edit_academic">
<input type="hidden" id="edit_academic_id" name="academic_id">
<div class="mb-3">
<label for="edit_subject" class="form-label">Subject</label>
<input type="text" class="form-control" id="edit_subject" name="subject" required>
</div>
<div class="mb-3">
<label for="edit_grade" class="form-label">Grade</label>
<select class="form-select" id="edit_grade" name="grade" required>
<option value="A">A</option>
<option value="B">B</option>
<option value="C">C</option>
<option value="D">D</option>
<option value="F">F</option>
</select>
</div>
<div class="mb-3">
<label for="edit_comments" class="form-label">Comments</label>
<textarea class="form-control" id="edit_comments" name="comments" rows="3"></textarea>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Save changes</button>
</div>
</form>
</div>
</div>
</div>
<!-- Edit Student Detail Modal -->
<div class="modal fade" id="editDetailModal" tabindex="-1" aria-labelledby="editDetailModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="editDetailModalLabel">Edit Student Detail</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<form id="editDetailForm" method="POST">
<div class="modal-body">
<input type="hidden" name="action" value="edit_student_detail">
<input type="hidden" id="edit_detail_id" name="detail_id">
<div class="mb-3">
<label for="edit_detail_title" class="form-label">Title</label>
<input type="text" class="form-control" id="edit_detail_title" name="detail_title" required>
</div>
<div class="mb-3">
<label for="edit_detail_content" class="form-label">Details</label>
<textarea class="form-control" id="edit_detail_content" name="detail_content" rows="3"></textarea>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Save changes</button>
</div>
</form>
</div>
</div>
</div>
<!-- Toast container -->
<div class="position-fixed bottom-0 end-0 p-3" style="z-index: 11">
<!-- Behavior Toast -->
<div id="behaviorToast" class="toast align-items-center text-white bg-success border-0" role="alert" aria-live="assertive" aria-atomic="true">
<div class="d-flex">
<div class="toast-body">
<i class="bi bi-check-circle-fill me-2"></i>
New behavior logged successfully!
</div>
<button type="button" class="btn-close btn-close-white me-2 m-auto" data-bs-dismiss="toast" aria-label="Close"></button>
</div>
</div>
<!-- Academic Toast -->
<div id="academicToast" class="toast align-items-center text-white bg-success border-0" role="alert" aria-live="assertive" aria-atomic="true">
<div class="d-flex">
<div class="toast-body">
<i class="bi bi-check-circle-fill me-2"></i>
New academic record logged successfully!
</div>
<button type="button" class="btn-close btn-close-white me-2 m-auto" data-bs-dismiss="toast" aria-label="Close"></button>
</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
<script>
document.addEventListener('DOMContentLoaded', function () {
const urlParams = new URLSearchParams(window.location.search);
const studentId = '<?php echo $student_id; ?>';
let toastToShow = null;
if (urlParams.has('success')) {
if (urlParams.get('success') === 'behavior_logged') {
toastToShow = document.getElementById('behaviorToast');
} else if (urlParams.get('success') === 'academic_logged') {
toastToShow = document.getElementById('academicToast');
}
}
if (toastToShow) {
const toast = new bootstrap.Toast(toastToShow);
toast.show();
// Remove the query parameter from the URL without reloading the page
const newUrl = window.location.pathname + '?id=' + studentId;
window.history.replaceState({}, document.title, newUrl);
}
document.querySelectorAll('.btn-delete-behavior').forEach(button => {
button.addEventListener('click', function() {
if (confirm('Are you sure you want to delete this behavior entry?')) {
const behaviorId = this.dataset.behaviorId;
const form = document.createElement('form');
form.method = 'POST';
form.action = 'student.php?id=<?php echo $student_id; ?>';
const actionInput = document.createElement('input');
actionInput.type = 'hidden';
actionInput.name = 'action';
actionInput.value = 'delete_behavior';
form.appendChild(actionInput);
const idInput = document.createElement('input');
idInput.type = 'hidden';
idInput.name = 'behavior_id';
idInput.value = behaviorId;
form.appendChild(idInput);
document.body.appendChild(form);
form.submit();
}
});
});
const editBehaviorModal = document.getElementById('editBehaviorModal');
editBehaviorModal.addEventListener('show.bs.modal', function (event) {
const button = event.relatedTarget;
const behaviorId = button.dataset.behaviorId;
const description = button.dataset.behaviorDescription;
const rating = button.dataset.behaviorRating;
const modalForm = document.getElementById('editBehaviorForm');
modalForm.action = 'student.php?id=<?php echo $student_id; ?>';
const behaviorIdInput = document.getElementById('edit_behavior_id');
const descriptionInput = document.getElementById('edit_behavior_description');
const ratingInput = document.getElementById('edit_behavior_rating');
const ratingValue = document.getElementById('edit_rating_value');
behaviorIdInput.value = behaviorId;
descriptionInput.value = description;
ratingInput.value = rating;
ratingValue.innerText = rating;
});
document.querySelectorAll('.btn-delete-academic').forEach(button => {
button.addEventListener('click', function() {
if (confirm('Are you sure you want to delete this academic entry?')) {
const academicId = this.dataset.academicId;
const form = document.createElement('form');
form.method = 'POST';
form.action = 'student.php?id=<?php echo $student_id; ?>';
const actionInput = document.createElement('input');
actionInput.type = 'hidden';
actionInput.name = 'action';
actionInput.value = 'delete_academic';
form.appendChild(actionInput);
const idInput = document.createElement('input');
idInput.type = 'hidden';
idInput.name = 'academic_id';
idInput.value = academicId;
form.appendChild(idInput);
document.body.appendChild(form);
form.submit();
}
});
});
const editAcademicModal = document.getElementById('editAcademicModal');
editAcademicModal.addEventListener('show.bs.modal', function (event) {
const button = event.relatedTarget;
const academicId = button.dataset.academicId;
const subject = button.dataset.subject;
const grade = button.dataset.grade;
const comments = button.dataset.comments;
const modalForm = document.getElementById('editAcademicForm');
modalForm.action = 'student.php?id=<?php echo $student_id; ?>';
document.getElementById('edit_academic_id').value = academicId;
document.getElementById('edit_subject').value = subject;
document.getElementById('edit_grade').value = grade;
document.getElementById('edit_comments').value = comments;
});
document.querySelectorAll('.btn-delete-detail').forEach(button => {
button.addEventListener('click', function() {
if (confirm('Are you sure you want to delete this detail?')) {
const detailId = this.dataset.detailId;
const form = document.createElement('form');
form.method = 'POST';
form.action = 'student.php?id=<?php echo $student_id; ?>';
const actionInput = document.createElement('input');
actionInput.type = 'hidden';
actionInput.name = 'action';
actionInput.value = 'delete_student_detail';
form.appendChild(actionInput);
const idInput = document.createElement('input');
idInput.type = 'hidden';
idInput.name = 'detail_id';
idInput.value = detailId;
form.appendChild(idInput);
document.body.appendChild(form);
form.submit();
}
});
});
const editDetailModal = document.getElementById('editDetailModal');
editDetailModal.addEventListener('show.bs.modal', function (event) {
const button = event.relatedTarget;
const detailId = button.dataset.detailId;
const title = button.dataset.detailTitle;
const content = button.dataset.detailContent;
const modalForm = document.getElementById('editDetailForm');
modalForm.action = 'student.php?id=<?php echo $student_id; ?>';
document.getElementById('edit_detail_id').value = detailId;
document.getElementById('edit_detail_title').value = title;
document.getElementById('edit_detail_content').value = content;
});
});
</script>
</body>
</html>