37970-vm/learners.php
2026-01-30 14:32:19 +00:00

153 lines
6.5 KiB
PHP

<?php
require_once __DIR__ . '/db/config.php';
session_start();
// Auth Check
if (!isset($_SESSION['user_id']) || $_SESSION['role'] !== 'Admin') {
header('Location: login.php');
exit;
}
$db = db();
$school_id = $_SESSION['school_id'];
$pageTitle = 'Manage Learners | SOMS';
$message = '';
// Handle Add Learner
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action'])) {
if ($_POST['action'] === 'add') {
try {
$stmt = $db->prepare("INSERT INTO learners (full_name, grade, student_id, school_id) VALUES (?, ?, ?, ?)");
$stmt->execute([
$_POST['full_name'],
$_POST['grade'],
$_POST['student_id'],
$school_id
]);
$message = "Learner registered successfully.";
} catch (Exception $e) {
$message = "Error: " . $e->getMessage();
}
} elseif ($_POST['action'] === 'delete') {
try {
// Ensure the learner belongs to this school before deleting
$stmt = $db->prepare("DELETE FROM learners WHERE id = ? AND school_id = ?");
$stmt->execute([$_POST['id'], $school_id]);
$message = "Learner record deleted.";
} catch (Exception $e) {
$message = "Error: " . $e->getMessage();
}
}
}
// Fetch Learners for this specific school
$stmt = $db->prepare("SELECT * FROM learners WHERE school_id = ? ORDER BY full_name ASC");
$stmt->execute([$school_id]);
$learners = $stmt->fetchAll();
include 'includes/header.php';
?>
<div class="container pb-5">
<div class="row mb-4 align-items-center">
<div class="col">
<h2 class="h4 mb-1">Learner Management</h2>
<p class="text-muted small">Total: <strong><?= count($learners) ?> learners</strong> in this school</p>
</div>
<div class="col-auto">
<button class="btn btn-primary shadow-sm" data-bs-toggle="modal" data-bs-target="#addLearnerModal">
<i class="bi bi-person-plus me-2"></i> Register New Learner
</button>
</div>
</div>
<?php if ($message): ?>
<div class="alert alert-info alert-dismissible fade show shadow-sm" role="alert">
<?= htmlspecialchars($message) ?>
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
<?php endif; ?>
<div class="card shadow-sm border-0">
<div class="table-responsive">
<table class="table table-hover mb-0">
<thead class="bg-light">
<tr>
<th class="ps-4">Full Name</th>
<th>Grade</th>
<th>Student ID</th>
<th class="text-end pe-4">Actions</th>
</tr>
</thead>
<tbody>
<?php if (empty($learners)): ?>
<tr>
<td colspan="4" class="text-center py-5 text-muted">
<i class="bi bi-people display-1 mb-3 d-block"></i>
No learners registered yet.
</td>
</tr>
<?php endif; ?>
<?php foreach ($learners as $l): ?>
<tr>
<td class="ps-4">
<strong><?= htmlspecialchars($l['full_name']) ?></strong>
</td>
<td><?= htmlspecialchars($l['grade']) ?></td>
<td><span class="badge bg-light text-dark border"><?= htmlspecialchars($l['student_id']) ?></span></td>
<td class="text-end pe-4">
<form method="POST" class="d-inline" onsubmit="return confirm('Are you sure you want to delete this learner?');">
<input type="hidden" name="action" value="delete">
<input type="hidden" name="id" value="<?= $l['id'] ?>">
<button type="submit" class="btn btn-sm btn-outline-danger">
<i class="bi bi-trash"></i>
</button>
</form>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
</div>
</div>
<!-- Add Learner Modal -->
<div class="modal fade" id="addLearnerModal" tabindex="-1">
<div class="modal-dialog">
<form method="POST" class="modal-content border-0 shadow-lg">
<div class="modal-header bg-primary text-white">
<h5 class="modal-title fw-bold">Register New Learner</h5>
<button type="button" class="btn-close btn-close-white" data-bs-modal="modal" data-bs-target="#addLearnerModal" aria-label="Close" onclick="bootstrap.Modal.getInstance(document.getElementById('addLearnerModal')).hide()"></button>
</div>
<div class="modal-body p-4">
<input type="hidden" name="action" value="add">
<div class="mb-3">
<label class="form-label small fw-bold">Full Name</label>
<input type="text" name="full_name" class="form-control" required placeholder="e.g. Sipho Zulu">
</div>
<div class="mb-3">
<label class="form-label small fw-bold">Grade</label>
<select name="grade" class="form-select" required>
<option value="">Select Grade</option>
<option value="8">Grade 8</option>
<option value="9">Grade 9</option>
<option value="10">Grade 10</option>
<option value="11">Grade 11</option>
<option value="12">Grade 12</option>
</select>
</div>
<div class="mb-3">
<label class="form-label small fw-bold">Student ID / National ID</label>
<input type="text" name="student_id" class="form-control" required placeholder="e.g. STU10023">
</div>
</div>
<div class="modal-footer bg-light">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
<button type="submit" class="btn btn-primary px-4 fw-bold">Register Learner</button>
</div>
</form>
</div>
</div>
<?php include 'includes/footer.php'; ?>