Path : /home/vishqocm/pcib.in/student/
File Upload :
Current File : /home/vishqocm//pcib.in/student/my_courses.php

<?php
// Include header
include_once 'includes/header.php';

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

// Get enrolled courses for the current student
$enrolled_courses_query = "
    SELECT e.*, c.title as course_title, c.description, c.image, c.duration,
           c.price, c.discount_price, c.status as course_status,
           CASE 
                WHEN e.status = 'completed' THEN 1
                WHEN c.status = 'completed' THEN 1
                ELSE 0
           END as is_completed,
           e.completion_date, e.certificate_path, e.certificate_number
    FROM enrollments e
    INNER JOIN courses c ON e.course_id = c.id
    WHERE e.user_id = ? AND e.status IN ('active', 'completed')
    ORDER BY e.enrollment_date DESC
";

$stmt = $conn->prepare($enrolled_courses_query);
$stmt->bind_param("i", $student_id);
$stmt->execute();
$result = $stmt->get_result();
$enrolled_courses = [];
$active_courses = [];
$completed_courses = [];

while ($row = $result->fetch_assoc()) {
    // Mark course as completed if either enrollment status or course status is completed
    if ($row['status'] == 'completed' || $row['is_completed'] == 1) {
        // If course is marked completed by admin but enrollment is not yet updated
        if ($row['status'] != 'completed' && $row['is_completed'] == 1) {
            // Update enrollment status to completed
            $update_enrollment = "
                UPDATE enrollments 
                SET status = 'completed', completion_date = NOW() 
                WHERE id = ? AND status != 'completed'
            ";
            $update_stmt = $conn->prepare($update_enrollment);
            $update_stmt->bind_param("i", $row['id']);
            $update_stmt->execute();
            
            // Set completion date for display
            $row['completion_date'] = date('Y-m-d H:i:s');
            $row['status'] = 'completed';
            
            // Log this activity
            $activity_log = "
                INSERT INTO student_activities (user_id, activity_type, description, activity_date, related_id)
                VALUES (?, 'course_completed', ?, NOW(), ?)
            ";
            $activity_desc = "Course '{$row['course_title']}' marked as completed";
            $activity_stmt = $conn->prepare($activity_log);
            if ($activity_stmt) {
                $activity_stmt->bind_param("isi", $student_id, $activity_desc, $row['course_id']);
                $activity_stmt->execute();
            }
        }
        
        $completed_courses[] = $row;
    } else {
        $active_courses[] = $row;
    }
}

// Get applied courses (applications)
$applied_courses_query = "
    SELECT a.*, c.title as course_title, c.description, c.image, c.duration,
           c.price, c.discount_price,
           (
               SELECT COUNT(*)
               FROM student_documents sd
               WHERE sd.user_id = a.user_id AND sd.status = 'verified'
           ) as verified_docs,
           (
               SELECT COUNT(*)
               FROM student_documents sd
               WHERE sd.user_id = a.user_id
           ) as total_docs
    FROM enrollment_applications a
    INNER JOIN courses c ON a.course_id = c.id
    WHERE a.user_id = ? AND a.status NOT IN ('cancelled', 'rejected')
    ORDER BY a.application_date DESC
";

$stmt = $conn->prepare($applied_courses_query);
$stmt->bind_param("i", $student_id);
$stmt->execute();
$result = $stmt->get_result();
$applied_courses = [];

while ($row = $result->fetch_assoc()) {
    $applied_courses[] = $row;
}
?>

<div class="container-fluid py-4">
    <!-- Page Heading -->
    <div class="d-sm-flex align-items-center justify-content-between mb-4">
        <h1 class="h3 mb-0 text-gray-800">My Courses</h1>
        <a href="../courses.php" class="d-none d-sm-inline-block btn btn-sm btn-primary shadow-sm">
            <i class="fas fa-search fa-sm text-white-50"></i> Browse New Courses
        </a>
    </div>

    <!-- Content Row - Statistics -->
    <div class="row">
        <!-- Active Courses Count -->
        <div class="col-xl-3 col-md-6 mb-4">
            <div class="card border-left-primary shadow h-100 py-2">
                <div class="card-body">
                    <div class="row no-gutters align-items-center">
                        <div class="col mr-2">
                            <div class="text-xs font-weight-bold text-primary text-uppercase mb-1">
                                Active Courses</div>
                            <div class="h5 mb-0 font-weight-bold text-gray-800"><?php echo count($active_courses); ?></div>
                        </div>
                        <div class="col-auto">
                            <i class="fas fa-book-open fa-2x text-gray-300"></i>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <!-- Completed Courses Count -->
        <div class="col-xl-3 col-md-6 mb-4">
            <div class="card border-left-success shadow h-100 py-2">
                <div class="card-body">
                    <div class="row no-gutters align-items-center">
                        <div class="col mr-2">
                            <div class="text-xs font-weight-bold text-success text-uppercase mb-1">
                                Completed Courses</div>
                            <div class="h5 mb-0 font-weight-bold text-gray-800"><?php echo count($completed_courses); ?></div>
                        </div>
                        <div class="col-auto">
                            <i class="fas fa-check-circle fa-2x text-gray-300"></i>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <!-- Applications Count -->
        <div class="col-xl-3 col-md-6 mb-4">
            <div class="card border-left-warning shadow h-100 py-2">
                <div class="card-body">
                    <div class="row no-gutters align-items-center">
                        <div class="col mr-2">
                            <div class="text-xs font-weight-bold text-warning text-uppercase mb-1">
                                Pending Applications</div>
                            <div class="h5 mb-0 font-weight-bold text-gray-800"><?php echo count($applied_courses); ?></div>
                        </div>
                        <div class="col-auto">
                            <i class="fas fa-hourglass-half fa-2x text-gray-300"></i>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <!-- Total Courses -->
        <div class="col-xl-3 col-md-6 mb-4">
            <div class="card border-left-info shadow h-100 py-2">
                <div class="card-body">
                    <div class="row no-gutters align-items-center">
                        <div class="col mr-2">
                            <div class="text-xs font-weight-bold text-info text-uppercase mb-1">
                                Total Courses</div>
                            <div class="h5 mb-0 font-weight-bold text-gray-800"><?php echo count($active_courses) + count($completed_courses) + count($applied_courses); ?></div>
                        </div>
                        <div class="col-auto">
                            <i class="fas fa-graduation-cap fa-2x text-gray-300"></i>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Active Courses -->
    <div class="card shadow mb-4">
        <div class="card-header py-3 d-flex flex-row align-items-center justify-content-between">
            <h6 class="m-0 font-weight-bold text-primary">My Active Courses</h6>
            <?php if (!empty($active_courses)): ?>
            <div class="dropdown no-arrow">
                <a class="dropdown-toggle" href="#" role="button" id="dropdownMenuLink"
                    data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                    <i class="fas fa-ellipsis-v fa-sm fa-fw text-gray-400"></i>
                </a>
                <div class="dropdown-menu dropdown-menu-right shadow animated--fade-in"
                    aria-labelledby="dropdownMenuLink">
                    <div class="dropdown-header">Course Actions:</div>
                    <a class="dropdown-item" href="attendance_report.php">View Attendance</a>
                    <a class="dropdown-item" href="payment_history.php">View Payments</a>
                </div>
            </div>
            <?php endif; ?>
                </div>
                <div class="card-body">
                    <?php if (empty($active_courses)): ?>
            <div class="text-center py-4">
                <i class="fas fa-book-open fa-4x text-gray-300 mb-3"></i>
                <p class="text-gray-600 mb-0">You don't have any active courses.</p>
                <a href="../courses.php" class="btn btn-primary mt-3">
                    <i class="fas fa-search fa-sm"></i> Browse Available Courses
                </a>
                    </div>
                    <?php else: ?>
                    <div class="row">
                        <?php foreach ($active_courses as $course): ?>
                        <div class="col-md-6 col-lg-4 mb-4">
                    <div class="card shadow h-100">
                        <div class="card-header py-3 d-flex flex-row align-items-center justify-content-between">
                            <h6 class="m-0 font-weight-bold text-primary"><?php echo htmlspecialchars($course['course_title']); ?></h6>
                            <span class="badge bg-primary">Active</span>
                        </div>
                                <img src="<?php echo !empty($course['image']) ? '../' . $course['image'] : '../assets/img/course-placeholder.jpg'; ?>" 
                             class="card-img-top" alt="<?php echo htmlspecialchars($course['course_title']); ?>"
                             style="height: 140px; object-fit: cover;">
                                
                                <div class="card-body">
                            <div class="small text-gray-600 mb-3">
                                <i class="fas fa-calendar-alt fa-fw text-gray-400"></i> Enrolled: <?php echo date('M d, Y', strtotime($course['enrollment_date'])); ?>
                                    </div>

                            <p class="card-text mb-3"><?php echo substr(htmlspecialchars($course['description']), 0, 100) . '...'; ?></p>
                                
                            <div class="d-flex justify-content-between">
                                <a href="course_details.php?id=<?php echo $course['course_id']; ?>" class="btn btn-primary btn-sm">
                                    <i class="fas fa-book-open fa-sm fa-fw"></i> View Details
                                        </a>
                                <a href="attendance_report.php?course_id=<?php echo $course['course_id']; ?>" class="btn btn-info btn-sm">
                                    <i class="fas fa-calendar-check fa-sm fa-fw"></i> Attendance
                                </a>
                            </div>
                        </div>
                    </div>
                </div>
                <?php endforeach; ?>
            </div>
            <?php endif; ?>
        </div>
    </div>

    <!-- Applied Courses -->
    <div class="card shadow mb-4">
        <div class="card-header py-3">
            <h6 class="m-0 font-weight-bold text-warning">Applications</h6>
                </div>
                <div class="card-body">
                    <?php if (empty($applied_courses)): ?>
            <div class="text-center py-4">
                <i class="fas fa-clipboard-list fa-4x text-gray-300 mb-3"></i>
                <p class="text-gray-600 mb-0">You don't have any pending applications.</p>
                    </div>
                    <?php else: ?>
            <div class="table-responsive">
                <table class="table table-bordered" width="100%" cellspacing="0">
                    <thead>
                        <tr>
                            <th>Course</th>
                            <th>Applied On</th>
                            <th>Status</th>
                            <th>Documents</th>
                            <th>Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($applied_courses as $course): ?>
                        <tr>
                            <td><?php echo htmlspecialchars($course['course_title']); ?></td>
                            <td><?php echo date('M d, Y', strtotime($course['application_date'])); ?></td>
                            <td>
                                <span class="badge bg-<?php 
                                    echo $course['status'] === 'pending' ? 'warning' : 
                                        ($course['status'] === 'approved' ? 'success' : 'primary'); ?>">
                                    <?php echo ucfirst($course['status']); ?>
                                        </span>
                            </td>
                            <td>
                                <div class="progress" style="height: 10px;">
                                    <?php $doc_percent = $course['total_docs'] > 0 ? round(($course['verified_docs'] / $course['total_docs']) * 100) : 0; ?>
                                    <div class="progress-bar bg-success" role="progressbar" style="width: <?php echo $doc_percent; ?>%"></div>
                                </div>
                                <div class="small text-muted mt-1"><?php echo $course['verified_docs']; ?> of <?php echo $course['total_docs']; ?> verified</div>
                            </td>
                            <td>
                                <?php if ($course['status'] === 'pending'): ?>
                                <a href="documents.php" class="btn btn-primary btn-sm">
                                    <i class="fas fa-file-upload fa-sm"></i> Upload Docs
                                </a>
                                <?php else: ?>
                                <a href="documents.php" class="btn btn-info btn-sm">
                                    <i class="fas fa-file-alt fa-sm"></i> View Docs
                                </a>
                                <?php endif; ?>
                            </td>
                        </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
            <?php endif; ?>
        </div>
    </div>

    <!-- Completed Courses -->
    <?php if (!empty($completed_courses)): ?>
    <div class="card shadow mb-4">
        <div class="card-header py-3 d-flex flex-row align-items-center justify-content-between">
            <h6 class="m-0 font-weight-bold text-success">Completed Courses</h6>
            <a href="scheduled-exams.php" class="btn btn-sm btn-success">
                <i class="fas fa-edit fa-sm"></i> View All Exams
            </a>
                </div>
                <div class="card-body">
                    <div class="row">
                        <?php foreach ($completed_courses as $course): ?>
                        <div class="col-md-6 col-lg-4 mb-4">
                    <div class="card shadow h-100">
                        <div class="card-header py-3 d-flex flex-row align-items-center justify-content-between">
                            <h6 class="m-0 font-weight-bold text-success"><?php echo htmlspecialchars($course['course_title']); ?></h6>
                            <span class="badge bg-success">Completed</span>
                        </div>
                                    <img src="<?php echo !empty($course['image']) ? '../' . $course['image'] : '../assets/img/course-placeholder.jpg'; ?>" 
                             class="card-img-top" alt="<?php echo htmlspecialchars($course['course_title']); ?>"
                             style="height: 140px; object-fit: cover;">
                                
                                <div class="card-body">
                            <div class="small text-gray-600 mb-3">
                                <i class="fas fa-calendar-check fa-fw text-gray-400"></i> Completed: 
                                <?php echo !empty($course['completion_date']) ? date('M d, Y', strtotime($course['completion_date'])) : 'N/A'; ?>
                                </div>
                                
                            <div class="d-flex justify-content-between">
                                <a href="course_details.php?id=<?php echo $course['course_id']; ?>" class="btn btn-success btn-sm">
                                    <i class="fas fa-book-open fa-sm fa-fw"></i> Details
                                        </a>
                                <a href="scheduled-exams.php?course_id=<?php echo $course['course_id']; ?>" class="btn btn-primary btn-sm">
                                    <i class="fas fa-edit fa-sm fa-fw"></i> Take Exam
                                        </a>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <?php endforeach; ?>
            </div>
        </div>
    </div>
    <?php endif; ?>
</div>

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