37970-vm/learners.php
Flatlogic Bot e29ef2dff4 v2
2026-01-30 14:21:32 +00:00

135 lines
5.4 KiB
PHP

<?php
require_once __DIR__ . '/db/config.php';
$db = db();
$current_role = 'Admin';
$pageTitle = 'Manage Learners | Township Schools Platform';
$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) VALUES (?, ?, ?)");
$stmt->execute([
$_POST['full_name'],
$_POST['grade'],
$_POST['student_id']
]);
$message = "Learner registered successfully.";
} catch (Exception $e) {
$message = "Error: " . $e->getMessage();
}
} elseif ($_POST['action'] === 'delete') {
try {
$stmt = $db->prepare("DELETE FROM learners WHERE id = ?");
$stmt->execute([$_POST['id']]);
$message = "Learner record deleted.";
} catch (Exception $e) {
$message = "Error: " . $e->getMessage();
}
}
}
// Fetch Learners
$learners = $db->query("SELECT * FROM learners ORDER BY full_name ASC")->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></p>
</div>
<div class="col-auto">
<button class="btn btn-primary" 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" role="alert">
<?= htmlspecialchars($message) ?>
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
<?php endif; ?>
<div class="card">
<div class="table-responsive">
<table class="table table-hover mb-0">
<thead>
<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 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"><?= 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">
<div class="modal-header">
<h5 class="modal-title">Register New Learner</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal"></button>
</div>
<div class="modal-body">
<input type="hidden" name="action" value="add">
<div class="mb-3">
<label class="form-label">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">Grade</label>
<select name="grade" class="form-select" required>
<option value="">Select Grade</option>
<option value="Grade 8">Grade 8</option>
<option value="Grade 9">Grade 9</option>
<option value="Grade 10">Grade 10</option>
<option value="Grade 11">Grade 11</option>
<option value="Grade 12">Grade 12</option>
</select>
</div>
<div class="mb-3">
<label class="form-label">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">
<button type="button" class="btn btn-light" data-bs-dismiss="modal">Cancel</button>
<button type="submit" class="btn btn-primary">Register Learner</button>
</div>
</form>
</div>
</div>
<?php include 'includes/footer.php'; ?>