Path : /home/vishqocm/pcib.in/student/
File Upload :
Current File : /home/vishqocm//pcib.in/student/previous-exams.php

<?php
$pageTitle = "Previous Exams";
include_once('includes/header.php');

// Get student ID
$student_id = $_SESSION['user_id'];

// Check if a specific course_id is provided
$course_filter = isset($_GET['course_id']) ? intval($_GET['course_id']) : 0;

// Get current date
$current_date = date('Y-m-d');

// Add course filter clause if specified
$course_filter_clause = $course_filter > 0 ? " AND es.course_id = " . $course_filter : "";

// Get past exams for courses that the student is enrolled in
$query = "
    SELECT es.*, c.title as course_title, c.image as course_image,
           CONCAT(i.first_name, ' ', i.last_name) as instructor_name,
           se.id as student_exam_id, se.percentage as score, se.status as attempt_status,
           se.attempt_date,
           (SELECT COUNT(*) FROM exam_questions WHERE exam_id = es.id) as question_count,
           (SELECT COUNT(*) FROM student_exams WHERE user_id = ? AND exam_id = es.id) as attempt_count
    FROM exam_schedules es
    JOIN courses c ON es.course_id = c.id
    LEFT JOIN users i ON c.instructor_id = i.id
    JOIN enrollments e ON e.course_id = c.id AND e.user_id = ?
    LEFT JOIN student_exams se ON se.exam_id = es.id AND se.user_id = ?
    WHERE es.exam_date < ? AND es.is_active = 1
    AND e.status = 'completed'" . $course_filter_clause . "
    GROUP BY es.id
    ORDER BY es.exam_date DESC
";

$stmt = $conn->prepare($query);
$stmt->bind_param("iiis", $student_id, $student_id, $student_id, $current_date);
$stmt->execute();
$exams = $stmt->get_result();

// Filter and organize exams by status
$attempted_exams = [];
$not_attempted_exams = [];

while ($exam = $exams->fetch_assoc()) {
    if ($exam['student_exam_id']) {
        $attempted_exams[] = $exam;
    } else {
        $not_attempted_exams[] = $exam;
    }
}

// Get course details if a specific course is filtered
$course_details = null;
if ($course_filter > 0) {
    $course_query = "
        SELECT c.*, CONCAT(i.first_name, ' ', i.last_name) as instructor_name
        FROM courses c
        LEFT JOIN users i ON c.instructor_id = i.id
        WHERE c.id = ?
    ";
    $course_stmt = $conn->prepare($course_query);
    $course_stmt->bind_param("i", $course_filter);
    $course_stmt->execute();
    $course_result = $course_stmt->get_result();
    if ($course_result->num_rows > 0) {
        $course_details = $course_result->fetch_assoc();
    }
}
?>

<div class="container py-4">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h2><?php echo $course_details ? "Previous Exams: " . htmlspecialchars($course_details['title']) : $pageTitle; ?></h2>
        <div>
            <?php if ($course_filter > 0): ?>
                <a href="previous-exams.php" class="btn btn-outline-primary me-2">
                    <i class="fas fa-list me-2"></i>View All Previous Exams
                </a>
                <a href="scheduled-exams.php<?php echo $course_filter > 0 ? '?course_id='.$course_filter : ''; ?>" class="btn btn-outline-primary me-2">
                    <i class="fas fa-calendar-alt me-2"></i>View Upcoming Exams
                </a>
                <a href="my_courses.php" class="btn btn-outline-secondary">
                    <i class="fas fa-arrow-left me-2"></i>Back to My Courses
                </a>
            <?php else: ?>
                <a href="scheduled-exams.php" class="btn btn-outline-primary">
                    <i class="fas fa-calendar-alt me-2"></i>View Upcoming Exams
                </a>
            <?php endif; ?>
        </div>
    </div>
    
    <?php if (isset($_SESSION['success_message'])): ?>
    <div class="alert alert-success alert-dismissible fade show" role="alert">
        <i class="fas fa-check-circle me-2"></i> <?php echo $_SESSION['success_message']; ?>
        <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
    </div>
    <?php unset($_SESSION['success_message']); endif; ?>
    
    <?php if (isset($_SESSION['error_message'])): ?>
    <div class="alert alert-danger alert-dismissible fade show" role="alert">
        <i class="fas fa-exclamation-circle me-2"></i> <?php echo $_SESSION['error_message']; ?>
        <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
    </div>
    <?php unset($_SESSION['error_message']); endif; ?>

    <?php if ($course_details): ?>
    <!-- Course Information -->
    <div class="card mb-4">
        <div class="card-header bg-secondary text-white">
            <h5 class="mb-0"><i class="fas fa-book me-2"></i>Course Information</h5>
        </div>
        <div class="card-body">
            <div class="row">
                <div class="col-md-3">
                    <img src="<?php echo !empty($course_details['image']) ? '../' . $course_details['image'] : '../assets/img/course-placeholder.jpg'; ?>" 
                         class="img-fluid rounded" alt="<?php echo htmlspecialchars($course_details['title']); ?>">
                </div>
                <div class="col-md-9">
                    <h4><?php echo htmlspecialchars($course_details['title']); ?></h4>
                    <p class="text-muted">Instructor: <?php echo htmlspecialchars($course_details['instructor_name']); ?></p>
                    <p><?php echo htmlspecialchars(substr($course_details['description'], 0, 300)) . (strlen($course_details['description']) > 300 ? '...' : ''); ?></p>
                    <a href="course_details.php?id=<?php echo $course_details['id']; ?>" class="btn btn-outline-primary btn-sm">
                        <i class="fas fa-info-circle me-1"></i> View Course Details
                    </a>
                </div>
            </div>
        </div>
    </div>
    <?php endif; ?>

    <!-- Navigation Tabs -->
    <ul class="nav nav-tabs mb-4">
        <li class="nav-item">
            <a class="nav-link active" id="attempted-tab" data-bs-toggle="tab" href="#attempted" role="tab" aria-controls="attempted" aria-selected="true">
                <i class="fas fa-check-circle me-2"></i>Attempted Exams
                <?php if (count($attempted_exams) > 0): ?>
                <span class="badge bg-primary ms-1"><?php echo count($attempted_exams); ?></span>
                <?php endif; ?>
            </a>
        </li>
        <li class="nav-item">
            <a class="nav-link" id="not-attempted-tab" data-bs-toggle="tab" href="#not-attempted" role="tab" aria-controls="not-attempted" aria-selected="false">
                <i class="fas fa-history me-2"></i>Missed Exams
                <?php if (count($not_attempted_exams) > 0): ?>
                <span class="badge bg-secondary ms-1"><?php echo count($not_attempted_exams); ?></span>
                <?php endif; ?>
            </a>
        </li>
    </ul>

    <!-- Tab Content -->
    <div class="tab-content">
        <!-- Attempted Exams Tab -->
        <div class="tab-pane fade show active" id="attempted" role="tabpanel" aria-labelledby="attempted-tab">
            <?php if (count($attempted_exams) > 0): ?>
                <div class="row">
                    <?php foreach ($attempted_exams as $exam): ?>
                        <div class="col-md-6 col-lg-4 mb-4">
                            <div class="card h-100">
                                <div class="card-header">
                                    <div class="d-flex justify-content-between align-items-center">
                                        <h5 class="mb-0 text-truncate" title="<?php echo htmlspecialchars($exam['title']); ?>">
                                            <?php echo htmlspecialchars($exam['title']); ?>
                                        </h5>
                                        <span class="badge <?php echo $exam['attempt_status'] == 'passed' ? 'bg-success' : 'bg-danger'; ?>">
                                            <?php echo ucfirst($exam['attempt_status']); ?>
                                        </span>
                                    </div>
                                </div>
                                <div class="card-body">
                                    <div class="d-flex align-items-center mb-3">
                                        <?php if (!empty($exam['course_image'])): ?>
                                            <img src="../<?php echo htmlspecialchars($exam['course_image']); ?>" alt="Course Image" class="me-3 rounded" style="width: 60px; height: 60px; object-fit: cover;">
                                        <?php else: ?>
                                            <div class="me-3 rounded bg-light d-flex align-items-center justify-content-center" style="width: 60px; height: 60px;">
                                                <i class="fas fa-book fa-2x text-primary"></i>
                                            </div>
                                        <?php endif; ?>
                                        <div>
                                            <h6 class="mb-1"><?php echo htmlspecialchars($exam['course_title']); ?></h6>
                                            <p class="small text-muted mb-0">
                                                <i class="far fa-calendar-alt me-1"></i> 
                                                <?php echo date('d M Y', strtotime($exam['exam_date'])); ?>
                                            </p>
                                        </div>
                                    </div>
                                    
                                    <div class="mb-3">
                                        <div class="progress" style="height: 10px;">
                                            <div class="progress-bar <?php echo $exam['attempt_status'] == 'passed' ? 'bg-success' : 'bg-danger'; ?>" 
                                                 role="progressbar" 
                                                 style="width: <?php echo $exam['score']; ?>%;" 
                                                 aria-valuenow="<?php echo $exam['score']; ?>" 
                                                 aria-valuemin="0" 
                                                 aria-valuemax="100">
                                            </div>
                                        </div>
                                        <div class="d-flex justify-content-between mt-1">
                                            <small>Score: <?php echo $exam['score']; ?>%</small>
                                            <small>Passing: <?php echo $exam['passing_percentage']; ?>%</small>
                                        </div>
                                    </div>
                                    
                                    <div class="mb-3">
                                        <p class="small mb-1"><strong>Questions:</strong> <?php echo $exam['question_count']; ?></p>
                                        <p class="small mb-1"><strong>Duration:</strong> <?php echo $exam['duration_minutes']; ?> minutes</p>
                                        <p class="small mb-1"><strong>Attempted On:</strong> <?php echo date('d M Y', strtotime($exam['attempt_date'])); ?></p>
                                        <p class="small mb-0"><strong>Attempt Count:</strong> <?php echo $exam['attempt_count']; ?>/<?php echo $exam['attempts_allowed'] ?: 'Unlimited'; ?></p>
                                    </div>
                                    
                                    <div class="mt-3 text-center">
                                        <a href="exam-results.php?id=<?php echo $exam['id']; ?>" class="btn btn-primary">
                                            <i class="fas fa-eye me-1"></i> View Results
                                        </a>
                                        <?php if ($exam['attempt_status'] == 'passed'): ?>
                                            <a href="certificates.php?exam_id=<?php echo $exam['id']; ?>" class="btn btn-outline-success ms-2">
                                                <i class="fas fa-certificate me-1"></i> Certificate
                                            </a>
                                        <?php endif; ?>
                                    </div>
                                </div>
                            </div>
                        </div>
                    <?php endforeach; ?>
                </div>
            <?php else: ?>
                <div class="alert alert-info">
                    <i class="fas fa-info-circle me-2"></i> 
                    <?php if ($course_filter > 0): ?>
                        You haven't attempted any exams for this course.
                    <?php else: ?>
                        You haven't attempted any past exams.
                    <?php endif; ?>
                </div>
            <?php endif; ?>
        </div>
        
        <!-- Not Attempted Exams Tab -->
        <div class="tab-pane fade" id="not-attempted" role="tabpanel" aria-labelledby="not-attempted-tab">
            <?php if (count($not_attempted_exams) > 0): ?>
                <div class="row">
                    <?php foreach ($not_attempted_exams as $exam): ?>
                        <div class="col-md-6 col-lg-4 mb-4">
                            <div class="card h-100">
                                <div class="card-header">
                                    <div class="d-flex justify-content-between align-items-center">
                                        <h5 class="mb-0 text-truncate" title="<?php echo htmlspecialchars($exam['title']); ?>">
                                            <?php echo htmlspecialchars($exam['title']); ?>
                                        </h5>
                                        <span class="badge bg-secondary">Missed</span>
                                    </div>
                                </div>
                                <div class="card-body">
                                    <div class="d-flex align-items-center mb-3">
                                        <?php if (!empty($exam['course_image'])): ?>
                                            <img src="../<?php echo htmlspecialchars($exam['course_image']); ?>" alt="Course Image" class="me-3 rounded" style="width: 60px; height: 60px; object-fit: cover;">
                                        <?php else: ?>
                                            <div class="me-3 rounded bg-light d-flex align-items-center justify-content-center" style="width: 60px; height: 60px;">
                                                <i class="fas fa-book fa-2x text-primary"></i>
                                            </div>
                                        <?php endif; ?>
                                        <div>
                                            <h6 class="mb-1"><?php echo htmlspecialchars($exam['course_title']); ?></h6>
                                            <p class="small text-muted mb-0">
                                                <i class="far fa-calendar-alt me-1"></i> 
                                                <?php echo date('d M Y', strtotime($exam['exam_date'])); ?>
                                            </p>
                                        </div>
                                    </div>
                                    
                                    <div class="mb-3">
                                        <p class="small mb-1"><strong>Questions:</strong> <?php echo $exam['question_count']; ?></p>
                                        <p class="small mb-1"><strong>Duration:</strong> <?php echo $exam['duration_minutes']; ?> minutes</p>
                                        <p class="small mb-0"><strong>Time:</strong> <?php echo date('h:i A', strtotime($exam['start_time'])) . ' - ' . date('h:i A', strtotime($exam['end_time'])); ?></p>
                                    </div>
                                    
                                    <div class="mt-3 text-center">
                                        <a href="scheduled-exams.php" class="btn btn-primary">
                                            <i class="fas fa-calendar-alt me-1"></i> View Upcoming Exams
                                        </a>
                                    </div>
                                </div>
                            </div>
                        </div>
                    <?php endforeach; ?>
                </div>
            <?php else: ?>
                <div class="alert alert-info">
                    <i class="fas fa-info-circle me-2"></i> 
                    <?php if ($course_filter > 0): ?>
                        There are no missed exams for this course.
                    <?php else: ?>
                        There are no missed exams.
                    <?php endif; ?>
                </div>
            <?php endif; ?>
        </div>
    </div>
</div>

<?php include_once('includes/footer.php'); ?>