<?php
// Include database configuration
require_once '../admin/database/db_config.php';
// Include header
include_once 'includes/header.php';
// Get students with pagination
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
$per_page = 10;
$offset = ($page - 1) * $per_page;
$search = isset($_GET['search']) ? $_GET['search'] : '';
$status_filter = isset($_GET['status']) ? $_GET['status'] : '';
// Build the query
$where_conditions = ["role = 'student'"];
$params = [];
$types = '';
if (!empty($search)) {
$where_conditions[] = "(username LIKE ? OR email LIKE ? OR first_name LIKE ? OR last_name LIKE ?)";
$search_param = "%$search%";
$params = array_merge($params, [$search_param, $search_param, $search_param, $search_param]);
$types .= 'ssss';
}
if (!empty($status_filter)) {
$where_conditions[] = "status = ?";
$params[] = $status_filter;
$types .= 's';
}
$where_clause = !empty($where_conditions) ? "WHERE " . implode(" AND ", $where_conditions) : "";
// Count total students for pagination
$count_query = "SELECT COUNT(*) as total FROM users $where_clause";
$count_stmt = $conn->prepare($count_query);
if (!empty($params)) {
$count_stmt->bind_param($types, ...$params);
}
$count_stmt->execute();
$count_result = $count_stmt->get_result();
$total_students = $count_result->fetch_assoc()['total'];
$total_pages = ceil($total_students / $per_page);
// Get students
$query = "SELECT * FROM users $where_clause ORDER BY created_at DESC LIMIT ?, ?";
$stmt = $conn->prepare($query);
// Add pagination parameters
$params[] = $offset;
$params[] = $per_page;
$types .= 'ii';
if (!empty($params)) {
$stmt->bind_param($types, ...$params);
}
$stmt->execute();
$result = $stmt->get_result();
// Delete student
if (isset($_GET['action']) && $_GET['action'] === 'delete' && isset($_GET['id'])) {
$student_id = intval($_GET['id']);
$course_id = isset($_GET['course_id']) ? intval($_GET['course_id']) : 0;
if ($course_id > 0) {
// Only unenroll the student from this specific course
$delete_enrollment_query = "DELETE FROM enrollments WHERE user_id = ? AND course_id = ?";
$stmt = $conn->prepare($delete_enrollment_query);
$stmt->bind_param("ii", $student_id, $course_id);
// Execute query and check for errors
if ($stmt->execute()) {
// Log the action
$log_action = "Unenrolled student ID $student_id from course ID $course_id";
$log_query = "INSERT INTO admin_logs (admin_id, action, ip_address) VALUES (?, ?, ?)";
$log_stmt = $conn->prepare($log_query);
$log_stmt->bind_param("iss", $_SESSION['user_id'], $log_action, $_SERVER['REMOTE_ADDR']);
$log_stmt->execute();
header("Location: students.php?deleted=1");
exit;
} else {
$error_message = "Error: " . $stmt->error;
}
} else {
// Unenroll the student from all courses but keep the user account
$delete_enrollments_query = "DELETE FROM enrollments WHERE user_id = ?";
$stmt = $conn->prepare($delete_enrollments_query);
$stmt->bind_param("i", $student_id);
// Execute query and check for errors
if ($stmt->execute()) {
// Log the action
$log_action = "Unenrolled student ID $student_id from all courses";
$log_query = "INSERT INTO admin_logs (admin_id, action, ip_address) VALUES (?, ?, ?)";
$log_stmt = $conn->prepare($log_query);
$log_stmt->bind_param("iss", $_SESSION['user_id'], $log_action, $_SERVER['REMOTE_ADDR']);
$log_stmt->execute();
header("Location: students.php?deleted=1");
exit;
} else {
$error_message = "Error: " . $stmt->error;
}
}
}
?>
<!-- Breadcrumb -->
<nav aria-label="breadcrumb" class="mb-4">
<ol class="breadcrumb">
<li class="breadcrumb-item"><a href="index.php">Dashboard</a></li>
<li class="breadcrumb-item active">Student Management</li>
</ol>
</nav>
<!-- Page Title and Action Buttons -->
<div class="d-flex justify-content-between align-items-center mb-4">
<h1 class="h3 mb-0 text-gray-800">Student Management</h1>
<button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#addStudentModal">
<i class="fas fa-plus me-1"></i> Add New Student
</button>
</div>
<!-- Alerts for success and error messages -->
<?php if (isset($msg)): ?>
<div class="alert alert-success alert-dismissible fade show mb-4 animate__animated animate__fadeIn" role="alert">
<?php echo $msg; ?>
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
<?php endif; ?>
<?php if (isset($error)): ?>
<div class="alert alert-danger alert-dismissible fade show mb-4 animate__animated animate__fadeIn" role="alert">
<?php echo $error; ?>
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
<?php endif; ?>
<!-- Main Content -->
<div class="card mb-4 shadow-sm">
<div class="card-header d-flex justify-content-between align-items-center">
<h5 class="mb-0">Students</h5>
<div class="d-flex">
<!-- Status Filter -->
<?php if (isset($status_filter)): // Only show if status column exists ?>
<form method="get" class="me-2">
<input type="hidden" name="search" value="<?php echo htmlspecialchars($search); ?>">
<select class="form-select form-select-sm" name="status" onchange="this.form.submit()">
<option value="">All Status</option>
<option value="active" <?php echo $status_filter === 'active' ? 'selected' : ''; ?>>Active</option>
<option value="inactive" <?php echo $status_filter === 'inactive' ? 'selected' : ''; ?>>Inactive</option>
</select>
</form>
<?php endif; ?>
<!-- Search Form -->
<form class="d-flex" method="get">
<?php if (isset($status_filter)): // Only include if status column exists ?>
<input type="hidden" name="status" value="<?php echo htmlspecialchars($status_filter); ?>">
<?php endif; ?>
<div class="input-group">
<input type="text" class="form-control form-control-sm" placeholder="Search students..." name="search"
value="<?php echo htmlspecialchars($search); ?>">
<button class="btn btn-outline-primary btn-sm" type="submit">
<i class="fas fa-search"></i>
</button>
</div>
</form>
</div>
</div>
<div class="card-body p-0">
<div class="table-responsive">
<table class="table table-hover align-middle mb-0">
<thead>
<tr>
<th>ID</th>
<th>Student</th>
<th>Email</th>
<th>Enrolled Courses</th>
<th>Joined</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php if ($result->num_rows > 0): ?>
<?php while ($student = $result->fetch_assoc()): ?>
<tr>
<td><?php echo $student['id']; ?></td>
<td>
<div class="d-flex align-items-center">
<?php if (!empty($student['profile_image'])): ?>
<img src="<?php echo formatUrl($student['profile_image']); ?>" class="rounded-circle me-2" width="40" height="40" alt="Profile">
<?php else: ?>
<div class="bg-light rounded-circle d-flex align-items-center justify-content-center me-2" style="width:40px;height:40px">
<i class="fas fa-user text-secondary"></i>
</div>
<?php endif; ?>
<div>
<div class="fw-bold"><?php echo htmlspecialchars($student['first_name'] . ' ' . $student['last_name']); ?></div>
<small class="text-muted">@<?php echo htmlspecialchars($student['username']); ?></small>
</div>
</div>
</td>
<td><?php echo htmlspecialchars($student['email']); ?></td>
<td>
<?php
// Get enrolled courses count
$enrolled_query = "SELECT COUNT(*) as total FROM enrollments WHERE user_id = ?";
$enrolled_stmt = $conn->prepare($enrolled_query);
$enrolled_stmt->bind_param("i", $student['id']);
$enrolled_stmt->execute();
$enrolled_count = $enrolled_stmt->get_result()->fetch_assoc()['total'];
if ($enrolled_count > 0) {
echo '<span class="badge bg-success">' . $enrolled_count . ' courses</span>';
} else {
echo '<span class="badge bg-light text-dark">No courses</span>';
}
?>
</td>
<td><?php echo date('M d, Y', strtotime($student['created_at'])); ?></td>
<td>
<div class="btn-group btn-group-sm">
<button type="button" class="btn btn-info" data-bs-toggle="modal" data-bs-target="#viewStudentModal<?php echo $student['id']; ?>" title="View">
<i class="fas fa-eye"></i>
</button>
<button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#editStudentModal<?php echo $student['id']; ?>" title="Edit">
<i class="fas fa-edit"></i>
</button>
<button type="button" class="btn btn-danger delete-student-btn"
data-id="<?php echo $student['id']; ?>"
data-name="<?php echo htmlspecialchars($student['first_name'] . ' ' . $student['last_name']); ?>"
title="Delete">
<i class="fas fa-trash"></i>
</button>
</div>
<!-- View Student Modal -->
<div class="modal fade" id="viewStudentModal<?php echo $student['id']; ?>" tabindex="-1">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Student Details</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal"></button>
</div>
<div class="modal-body">
<div class="text-center mb-4">
<?php if (!empty($student['profile_image'])): ?>
<img src="../uploads/profiles/<?php echo $student['profile_image']; ?>" class="rounded-circle" width="100" height="100" alt="Profile">
<?php else: ?>
<div class="bg-light rounded-circle d-flex align-items-center justify-content-center mx-auto" style="width:100px;height:100px">
<i class="fas fa-user fa-3x text-secondary"></i>
</div>
<?php endif; ?>
<h5 class="mt-3 mb-0"><?php echo htmlspecialchars($student['first_name'] . ' ' . $student['last_name']); ?></h5>
<p class="text-muted">@<?php echo htmlspecialchars($student['username']); ?></p>
</div>
<div class="row g-3">
<div class="col-md-6">
<h6>Email</h6>
<p><?php echo htmlspecialchars($student['email']); ?></p>
</div>
<div class="col-md-6">
<h6>Joined</h6>
<p><?php echo date('F d, Y', strtotime($student['created_at'])); ?></p>
</div>
</div>
<h6 class="mt-4">Enrolled Courses</h6>
<?php
// Get enrolled courses
$courses_query = "
SELECT c.id, c.title, e.id as enrollment_id
FROM enrollments e
JOIN courses c ON e.course_id = c.id
WHERE e.user_id = ?
ORDER BY e.id DESC
";
$courses_stmt = $conn->prepare($courses_query);
$courses_stmt->bind_param("i", $student['id']);
$courses_stmt->execute();
$courses_result = $courses_stmt->get_result();
if ($courses_result->num_rows > 0):
?>
<div class="list-group">
<?php while ($course = $courses_result->fetch_assoc()): ?>
<a href="../course.php?id=<?php echo $course['id']; ?>" class="list-group-item list-group-item-action" target="_blank">
<div class="d-flex justify-content-between align-items-center">
<div>
<?php echo htmlspecialchars($course['title']); ?>
</div>
<small class="text-muted">Enrollment #<?php echo $course['enrollment_id']; ?></small>
</div>
</a>
<?php endwhile; ?>
</div>
<?php else: ?>
<div class="alert alert-light">
<i class="fas fa-info-circle me-2"></i> This student is not enrolled in any courses yet.
</div>
<?php endif; ?>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<!-- Edit Student Modal -->
<div class="modal fade" id="editStudentModal<?php echo $student['id']; ?>" tabindex="-1">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Edit Student</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal"></button>
</div>
<form method="POST" action="update_user.php">
<div class="modal-body">
<input type="hidden" name="user_id" value="<?php echo $student['id']; ?>">
<div class="mb-3">
<label class="form-label">Username</label>
<input type="text" class="form-control" name="username" value="<?php echo htmlspecialchars($student['username']); ?>" required>
</div>
<div class="mb-3">
<label class="form-label">Email</label>
<input type="email" class="form-control" name="email" value="<?php echo htmlspecialchars($student['email']); ?>" required>
</div>
<div class="mb-3">
<label class="form-label">First Name</label>
<input type="text" class="form-control" name="first_name" value="<?php echo htmlspecialchars($student['first_name']); ?>">
</div>
<div class="mb-3">
<label class="form-label">Last Name</label>
<input type="text" class="form-control" name="last_name" value="<?php echo htmlspecialchars($student['last_name']); ?>">
</div>
<input type="hidden" name="role" value="student">
<div class="mb-3">
<label class="form-label">New Password (leave blank to keep current)</label>
<input type="password" class="form-control" name="password">
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
<button type="submit" class="btn btn-primary">Save Changes</button>
</div>
</form>
</div>
</div>
</div>
</td>
</tr>
<?php endwhile; ?>
<?php else: ?>
<tr>
<td colspan="6" class="text-center py-4">
<div class="d-flex flex-column align-items-center">
<i class="fas fa-user-graduate fa-3x text-muted mb-3"></i>
<h5>No students found</h5>
<p class="text-muted">Try adjusting your search criteria</p>
<button type="button" class="btn btn-primary btn-sm mt-2" data-bs-toggle="modal" data-bs-target="#addStudentModal">
<i class="fas fa-plus me-1"></i> Add New Student
</button>
</div>
</td>
</tr>
<?php endif; ?>
</tbody>
</table>
</div>
</div>
<div class="card-footer">
<!-- Pagination -->
<?php if ($total_pages > 1): ?>
<nav aria-label="Page navigation">
<ul class="pagination justify-content-center mb-0">
<?php if ($page > 1): ?>
<li class="page-item">
<a class="page-link" href="?page=<?php echo $page - 1; ?>&search=<?php echo urlencode($search); ?>&status=<?php echo urlencode($status_filter); ?>">
<i class="fas fa-angle-left"></i>
</a>
</li>
<?php endif; ?>
<?php
// Determine range of page numbers to show
$range = 2; // Show 2 pages before and after current page
$start_page = max(1, $page - $range);
$end_page = min($total_pages, $page + $range);
// Always show first page
if ($start_page > 1) {
echo '<li class="page-item"><a class="page-link" href="?page=1&search=' . urlencode($search) . '&status=' . urlencode($status_filter) . '">1</a></li>';
if ($start_page > 2) {
echo '<li class="page-item disabled"><span class="page-link">...</span></li>';
}
}
// Show page numbers
for ($i = $start_page; $i <= $end_page; $i++) {
echo '<li class="page-item ' . ($i === $page ? 'active' : '') . '">
<a class="page-link" href="?page=' . $i . '&search=' . urlencode($search) . '&status=' . urlencode($status_filter) . '">' . $i . '</a>
</li>';
}
// Always show last page
if ($end_page < $total_pages) {
if ($end_page < $total_pages - 1) {
echo '<li class="page-item disabled"><span class="page-link">...</span></li>';
}
echo '<li class="page-item"><a class="page-link" href="?page=' . $total_pages . '&search=' . urlencode($search) . '&status=' . urlencode($status_filter) . '">' . $total_pages . '</a></li>';
}
?>
<?php if ($page < $total_pages): ?>
<li class="page-item">
<a class="page-link" href="?page=<?php echo $page + 1; ?>&search=<?php echo urlencode($search); ?>&status=<?php echo urlencode($status_filter); ?>">
<i class="fas fa-angle-right"></i>
</a>
</li>
<?php endif; ?>
</ul>
</nav>
<?php endif; ?>
</div>
</div>
<!-- Add Student Modal -->
<div class="modal fade" id="addStudentModal" tabindex="-1">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Add New Student</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal"></button>
</div>
<form method="POST" action="add_user.php">
<div class="modal-body">
<div class="mb-3">
<label class="form-label">Username</label>
<input type="text" class="form-control" name="username" required>
</div>
<div class="mb-3">
<label class="form-label">Email</label>
<input type="email" class="form-control" name="email" required>
</div>
<div class="mb-3">
<label class="form-label">Password</label>
<input type="password" class="form-control" name="password" required>
</div>
<div class="mb-3">
<label class="form-label">First Name</label>
<input type="text" class="form-control" name="first_name">
</div>
<div class="mb-3">
<label class="form-label">Last Name</label>
<input type="text" class="form-control" name="last_name">
</div>
<input type="hidden" name="role" value="student">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
<button type="submit" class="btn btn-primary">Add Student</button>
</div>
</form>
</div>
</div>
</div>
<!-- Delete Student Modal -->
<div class="modal fade" id="deleteStudentModal" tabindex="-1" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Confirm Deletion</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<p>Are you sure you want to delete the student account for: <span id="studentToDelete"></span>?</p>
<p class="text-danger">This action cannot be undone. All enrollment records for this student will also be deleted.</p>
</div>
<div class="modal-footer">
<form method="post">
<input type="hidden" name="student_id" id="deleteStudentId">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
<button type="submit" name="delete_student" class="btn btn-danger">Delete Student</button>
</form>
</div>
</div>
</div>
</div>
<script>
document.addEventListener('DOMContentLoaded', function() {
// Delete student modal functionality
const deleteBtns = document.querySelectorAll('.delete-student-btn');
const deleteStudentModal = document.getElementById('deleteStudentModal');
const studentToDelete = document.getElementById('studentToDelete');
const deleteStudentId = document.getElementById('deleteStudentId');
if (deleteBtns.length > 0) {
deleteBtns.forEach(btn => {
btn.addEventListener('click', function() {
const studentId = this.getAttribute('data-id');
const studentName = this.getAttribute('data-name');
studentToDelete.textContent = studentName;
deleteStudentId.value = studentId;
const modal = new bootstrap.Modal(deleteStudentModal);
modal.show();
});
});
}
});
</script>
<?php
// Include footer
include_once 'includes/footer.php';
?>