144 lines
5.5 KiB
PHP
144 lines
5.5 KiB
PHP
<?php
|
|
include 'templates/header.php';
|
|
require_once 'db/config.php';
|
|
|
|
// Admin-only page
|
|
if (!isset($_SESSION['role_id']) || $_SESSION['role_id'] != 1) {
|
|
header('Location: dashboard.php');
|
|
exit;
|
|
}
|
|
|
|
$pdo = db();
|
|
$action = $_GET['action'] ?? 'list';
|
|
$id = $_GET['id'] ?? null;
|
|
|
|
// Fetch programs for the dropdown
|
|
$programs_stmt = $pdo->query('SELECT id, name FROM programs ORDER BY name');
|
|
$programs = $programs_stmt->fetchAll();
|
|
|
|
// Handle form submissions
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
$name = trim($_POST['name']);
|
|
$program_id = $_POST['program_id'];
|
|
|
|
if (isset($_POST['add_course'])) {
|
|
if (!empty($name) && !empty($program_id)) {
|
|
$stmt = $pdo->prepare('INSERT INTO courses (name, program_id) VALUES (?, ?)');
|
|
$stmt->execute([$name, $program_id]);
|
|
}
|
|
} elseif (isset($_POST['update_course'])) {
|
|
if (!empty($name) && !empty($program_id) && !empty($id)) {
|
|
$stmt = $pdo->prepare('UPDATE courses SET name = ?, program_id = ? WHERE id = ?');
|
|
$stmt->execute([$name, $program_id, $id]);
|
|
}
|
|
header('Location: admin_courses.php');
|
|
exit;
|
|
} elseif (isset($_POST['delete_course'])) {
|
|
if (!empty($id)) {
|
|
$stmt = $pdo->prepare('DELETE FROM courses WHERE id = ?');
|
|
$stmt->execute([$id]);
|
|
}
|
|
header('Location: admin_courses.php');
|
|
exit;
|
|
}
|
|
}
|
|
|
|
?>
|
|
|
|
<h2>Assessment Planning: Courses</h2>
|
|
|
|
<p><a href="admin.php"> ← Back to Admin Dashboard</a></p>
|
|
|
|
<?php if ($action === 'edit' && $id): ?>
|
|
<?php
|
|
$stmt = $pdo->prepare('SELECT * FROM courses WHERE id = ?');
|
|
$stmt->execute([$id]);
|
|
$course = $stmt->fetch();
|
|
?>
|
|
<h3>Edit Course</h3>
|
|
<form action="admin_courses.php?action=edit&id=<?php echo $id; ?>" method="post">
|
|
<div class="mb-3">
|
|
<label for="name" class="form-label">Course Name</label>
|
|
<input type="text" class="form-control" id="name" name="name" value="<?php echo htmlspecialchars($course['name']); ?>" required>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label for="program_id" class="form-label">Program</label>
|
|
<select class="form-select" id="program_id" name="program_id" required>
|
|
<?php foreach ($programs as $program): ?>
|
|
<option value="<?php echo $program['id']; ?>" <?php echo ($program['id'] == $course['program_id']) ? 'selected' : ''; ?>><?php echo htmlspecialchars($program['name']); ?></option>
|
|
<?php endforeach; ?>
|
|
</select>
|
|
</div>
|
|
<input type="hidden" name="id" value="<?php echo $id; ?>">
|
|
<button type="submit" name="update_course" class="btn btn-primary">Update</button>
|
|
<a href="admin_courses.php" class="btn btn-secondary">Cancel</a>
|
|
</form>
|
|
|
|
<?php elseif ($action === 'delete' && $id): ?>
|
|
<?php
|
|
$stmt = $pdo->prepare('SELECT * FROM courses WHERE id = ?');
|
|
$stmt->execute([$id]);
|
|
$course = $stmt->fetch();
|
|
?>
|
|
<h3>Delete Course</h3>
|
|
<p>Are you sure you want to delete the course "<?php echo htmlspecialchars($course['name']); ?>"?</p>
|
|
<form action="admin_courses.php?action=delete&id=<?php echo $id; ?>" method="post">
|
|
<input type="hidden" name="id" value="<?php echo $id; ?>">
|
|
<button type="submit" name="delete_course" class="btn btn-danger">Delete</button>
|
|
<a href="admin_courses.php" class="btn btn-secondary">Cancel</a>
|
|
</form>
|
|
|
|
<?php else: ?>
|
|
|
|
<h3>Add New Course</h3>
|
|
<form action="admin_courses.php" method="post">
|
|
<div class="mb-3">
|
|
<label for="name" class="form-label">Course Name</label>
|
|
<input type="text" class="form-control" id="name" name="name" required>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label for="program_id" class="form-label">Program</label>
|
|
<select class="form-select" id="program_id" name="program_id" required>
|
|
<option value="">Select a Program</option>
|
|
<?php foreach ($programs as $program): ?>
|
|
<option value="<?php echo $program['id']; ?>"><?php echo htmlspecialchars($program['name']); ?></option>
|
|
<?php endforeach; ?>
|
|
</select>
|
|
</div>
|
|
<button type="submit" name="add_course" class="btn btn-primary">Add Course</button>
|
|
</form>
|
|
|
|
<hr>
|
|
|
|
<h3>Existing Courses</h3>
|
|
<table class="table table-striped">
|
|
<thead>
|
|
<tr>
|
|
<th>ID</th>
|
|
<th>Name</th>
|
|
<th>Program</th>
|
|
<th>Actions</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<?php
|
|
$stmt = $pdo->query('SELECT c.id, c.name, p.name AS program_name FROM courses c JOIN programs p ON c.program_id = p.id ORDER BY c.id');
|
|
while ($row = $stmt->fetch()) {
|
|
echo "<tr>";
|
|
echo "<td>" . htmlspecialchars($row['id']) . "</td>";
|
|
echo "<td>" . htmlspecialchars($row['name']) . "</td>";
|
|
echo "<td>" . htmlspecialchars($row['program_name']) . "</td>";
|
|
echo '<td>
|
|
<a href="admin_courses.php?action=edit&id=' . $row['id'] . '" class="btn btn-sm btn-outline-primary">Edit</a>
|
|
<a href="admin_courses.php?action=delete&id=' . $row['id'] . '" class="btn btn-sm btn-outline-danger">Delete</a>
|
|
</td>';
|
|
echo "</tr>";
|
|
}
|
|
?>
|
|
</tbody>
|
|
</table>
|
|
|
|
<?php endif; ?>
|
|
|
|
<?php include 'templates/footer.php'; ?>
|