Path : /home/vishqocm/pcib.in/faculty/
File Upload :
Current File : /home/vishqocm/pcib.in/faculty/students.php

<?php
// Include database configuration
require_once 'database/db_config.php';

// Check if user has faculty privileges
// require_faculty_privileges('../login.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 first_name 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();

?>

<!-- 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</h1>
</div>



<!-- 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-control" name="status" onchange="this.form.submit()">
                    <option value="active" <?php echo $status_filter === 'active' ? 'selected' : ''; ?>>Active</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>
                    </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 $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>
                            </tr>
                        <?php endwhile; ?>
                    <?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>



<?php
// Include footer
include_once 'includes/footer.php';
?>