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

<?php
// Start session
session_start();

// Check if user is logged in
if (!isset($_SESSION['user_id']) || $_SESSION['role'] !== 'student') {
    header('Location: ../login.php');
    exit();
}

// Include database connection and header 
require_once('../config/database.php');

// Get course ID from URL
if (!isset($_GET['id']) || empty($_GET['id'])) {
    echo "<script>window.location.href = 'index.php';</script>";
    exit();
}

$courseId = intval($_GET['id']);
$userId = $_SESSION['user_id'];

// Check if the user is enrolled in this course
$stmt = $conn->prepare("
    SELECT * FROM enrollments 
    WHERE user_id = ? AND course_id = ?
");
$stmt->bind_param('ii', $userId, $courseId);
$stmt->execute();
$result = $stmt->get_result();

if ($result->num_rows === 0) {
    echo "<script>window.location.href = 'my_courses.php';</script>";
    exit();
}

$enrollment = $result->fetch_assoc();

// Get course details with instructor information
$stmt = $conn->prepare("
    SELECT c.*, 
           CONCAT(u.first_name, ' ', u.last_name) as instructor_name
    FROM courses c
    LEFT JOIN users u ON c.instructor_id = u.id
    WHERE c.id = ?
");
$stmt->bind_param('i', $courseId);
$stmt->execute();
$result = $stmt->get_result();

if ($result->num_rows === 0) {
    $error_message = 'Course not found';
} else {
    $course = $result->fetch_assoc();
    
    // Format course data
    $courseData = [
        'id' => $course['id'],
        'title' => $course['title'],
        'description' => $course['description'] ?? '',
        'duration' => $course['duration'] ?? '',
        'price' => $course['price'] ?? 0,
        'discount_price' => $course['discount_price'] ?? 0,
        'image' => $course['image'] ?? '',
        'status' => $course['status'] ?? 'active',
        'instructor_name' => $course['instructor_name'] ?? 'Not Assigned',
        'enrollment_date' => date('M d, Y', strtotime($enrollment['enrollment_date'])),
        'enrollment_status' => $enrollment['status'],
        'completion_date' => $enrollment['completion_date'] ? date('M d, Y', strtotime($enrollment['completion_date'])) : null,
        'certificate_number' => $enrollment['certificate_number'] ?? null,
        'certificate_path' => $enrollment['certificate_path'] ?? null
    ];
    
    // Get payment information if exists
    $stmt = $conn->prepare("
        SELECT 
            SUM(amount) as total_paid,
            MAX(payment_date) as last_payment_date,
            COUNT(*) as payment_count
        FROM payments
        WHERE user_id = ? AND course_id = ? AND status IN ('completed', 'verified')
    ");
    $stmt->bind_param('ii', $userId, $courseId);
    $stmt->execute();
    $result = $stmt->get_result();
    
    $paymentInfo = [
        'total_paid' => 0,
        'payment_count' => 0,
        'last_payment_date' => null
    ];
    
    if ($result->num_rows > 0) {
        $payment = $result->fetch_assoc();
        $paymentInfo['total_paid'] = floatval($payment['total_paid'] ?? 0);
        $paymentInfo['payment_count'] = intval($payment['payment_count'] ?? 0);
        $paymentInfo['last_payment_date'] = $payment['last_payment_date'] 
            ? date('M d, Y', strtotime($payment['last_payment_date'])) 
            : null;
    }
    
    // Get course fee based on payment plan
    $course_fee = $courseData['discount_price'] > 0 && $courseData['discount_price'] < $courseData['price'] 
        ? $courseData['discount_price'] 
        : $courseData['price'];
    
    if ($enrollment['payment_plan'] === 'monthly') {
        $paymentInfo['fee_amount'] = round($course_fee / 12, 2);
        $paymentInfo['total_fee'] = $course_fee;
        $paymentInfo['payments_count'] = 12;
        $paymentInfo['payment_type'] = 'Monthly';
    } else if ($enrollment['payment_plan'] === 'six_month') {
        $paymentInfo['fee_amount'] = round($course_fee / 6, 2);
        $paymentInfo['total_fee'] = $course_fee;
        $paymentInfo['payments_count'] = 6;
        $paymentInfo['payment_type'] = 'Six-Month';
    } else {
        $paymentInfo['fee_amount'] = $course_fee;
        $paymentInfo['total_fee'] = $course_fee;
        $paymentInfo['payments_count'] = 1;
        $paymentInfo['payment_type'] = 'Full Payment';
    }
    
    // Calculate remaining amount
    $paymentInfo['remaining_amount'] = max(0, $paymentInfo['total_fee'] - $paymentInfo['total_paid']);
    
    // Ensure payment progress is properly calculated and capped at 100%
    if ($paymentInfo['total_fee'] > 0) {
        $paymentInfo['payment_progress'] = min(100, round(($paymentInfo['total_paid'] / $paymentInfo['total_fee']) * 100));
    } else {
        $paymentInfo['payment_progress'] = 0;
    }
    
    // If total paid is greater than or equal to total fee, set progress to 100%
    if ($paymentInfo['total_paid'] >= $paymentInfo['total_fee']) {
        $paymentInfo['payment_progress'] = 100;
        $paymentInfo['remaining_amount'] = 0;
    }
    
    // Get next payment amount
    $paymentInfo['next_payment'] = min($paymentInfo['remaining_amount'], $paymentInfo['fee_amount']);
    
    // Add additional payment status information
    $paymentInfo['is_paid_in_full'] = $paymentInfo['total_paid'] >= $paymentInfo['total_fee'];
    $paymentInfo['payment_status'] = $paymentInfo['is_paid_in_full'] ? 'Completed' : 'In Progress';
}

// Include header file
require_once('includes/header.php');
?>

<!-- Content Wrapper -->
<div id="content-wrapper" class="d-flex flex-column">
    <!-- Main Content -->
    <div id="content">
        <?php require_once('includes/topbar.php'); ?>
        
        <!-- Begin Page Content -->
        <div class="container-fluid">
            <!-- Page Heading -->
            <div class="d-sm-flex align-items-center justify-content-between mb-4">
                <h1 class="h3 mb-0 text-gray-800">Course Details</h1>
                <a href="enrolled_courses.php" class="d-none d-sm-inline-block btn btn-sm btn-primary shadow-sm">
                    <i class="fas fa-arrow-left fa-sm text-white-50"></i> Back to Courses
                </a>
            </div>

            <?php if (isset($error_message)): ?>
            <!-- Error Alert -->
            <div class="alert alert-danger" role="alert">
                <?php echo $error_message; ?>
            </div>
            <?php else: ?>
            <!-- Course Details Section -->
            <div class="row">
                <!-- Course Information Card -->
                <div class="col-xl-4 col-md-6 mb-4">
                    <div class="card border-left-primary 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">Course Information</h6>
                        </div>
                        <div class="card-body">
                            <div class="text-center mb-3">
                                <?php if ($courseData['image'] && $courseData['image'] !== ''): ?>
                                    <img src="../uploads/courses/<?php echo $courseData['image']; ?>" class="img-fluid rounded" style="max-height: 150px;" alt="Course Image">
                                <?php else: ?>
                                    <img src="../assets/img/course-placeholder.jpg" class="img-fluid rounded" style="max-height: 150px;" alt="Course Image">
                                <?php endif; ?>
                            </div>
                            <h4 class="font-weight-bold text-center mb-3"><?php echo $courseData['title']; ?></h4>
                            <div class="mb-2">
                                <strong><i class="fas fa-user-tie mr-2"></i>Instructor:</strong>
                                <span class="ml-2"><?php echo $courseData['instructor_name']; ?></span>
                            </div>
                            <div class="mb-2">
                                <strong><i class="fas fa-calendar-alt mr-2"></i>Enrolled:</strong>
                                <span class="ml-2"><?php echo $courseData['enrollment_date']; ?></span>
                            </div>
                            <div class="mb-2">
                                <strong><i class="fas fa-clock mr-2"></i>Duration:</strong>
                                <span class="ml-2"><?php echo $courseData['duration'] ?: 'Not specified'; ?></span>
                            </div>
                            <div class="mb-2">
                                <strong><i class="fas fa-tag mr-2"></i>Status:</strong>
                                <?php 
                                    $statusBadgeClass = 'badge-success';
                                    if ($courseData['enrollment_status'] === 'pending') {
                                        $statusBadgeClass = 'badge-warning';
                                    } else if ($courseData['enrollment_status'] === 'completed') {
                                        $statusBadgeClass = 'badge-primary';
                                    } else if ($courseData['enrollment_status'] === 'suspended') {
                                        $statusBadgeClass = 'badge-danger';
                                    }
                                ?>
                                <span class="badge badge-pill ml-2 <?php echo $statusBadgeClass; ?>">
                                    <?php echo ucfirst($courseData['enrollment_status']); ?>
                                </span>
                            </div>
                        </div>
                    </div>
                </div>

                <!-- Payment Information Card -->
                <div class="col-xl-4 col-md-6 mb-4">
                    <div class="card border-left-success 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">Payment Information</h6>
                            <?php 
                                $paymentBadgeClass = 'badge-warning';
                                if ($paymentInfo['is_paid_in_full']) {
                                    $paymentBadgeClass = 'badge-success';
                                } else if ($paymentInfo['payment_progress'] >= 50) {
                                    $paymentBadgeClass = 'badge-info';
                                }
                            ?>
                            <div class="badge badge-pill <?php echo $paymentBadgeClass; ?>">
                                <?php echo $paymentInfo['payment_status']; ?>
                            </div>
                        </div>
                        <div class="card-body">
                            <div class="mb-3">
                                <label class="font-weight-bold text-dark">Payment Progress</label>
                                <div class="progress mb-2" style="height: 15px;">
                                    <?php
                                        $progressBarClass = 'bg-danger';
                                        if ($paymentInfo['payment_progress'] >= 100) {
                                            $progressBarClass = 'bg-success';
                                        } else if ($paymentInfo['payment_progress'] >= 75) {
                                            $progressBarClass = 'bg-info';
                                        } else if ($paymentInfo['payment_progress'] >= 50) {
                                            $progressBarClass = 'bg-primary';
                                        } else if ($paymentInfo['payment_progress'] >= 25) {
                                            $progressBarClass = 'bg-warning';
                                        }
                                    ?>
                                    <div class="progress-bar progress-bar-striped <?php echo $progressBarClass; ?>" 
                                         role="progressbar" 
                                         style="width: <?php echo $paymentInfo['payment_progress']; ?>%;" 
                                         aria-valuenow="<?php echo $paymentInfo['payment_progress']; ?>" 
                                         aria-valuemin="0" 
                                         aria-valuemax="100">
                                        <?php echo $paymentInfo['payment_progress']; ?>%
                                    </div>
                                </div>
                            </div>
                            
                            <table class="table table-sm table-borderless">
                                <tr>
                                    <td><strong>Payment Plan:</strong></td>
                                    <td class="text-right"><?php echo $paymentInfo['payment_type']; ?></td>
                                </tr>
                                <tr>
                                    <td><strong>Total Fee:</strong></td>
                                    <td class="text-right">₱<?php echo number_format($paymentInfo['total_fee'], 2); ?></td>
                                </tr>
                                <tr>
                                    <td><strong>Total Paid:</strong></td>
                                    <td class="text-right">₱<?php echo number_format($paymentInfo['total_paid'], 2); ?></td>
                                </tr>
                                <tr>
                                    <td><strong>Remaining:</strong></td>
                                    <td class="text-right font-weight-bold">₱<?php echo number_format($paymentInfo['remaining_amount'], 2); ?></td>
                                </tr>
                                <tr>
                                    <td><strong>Last Payment:</strong></td>
                                    <td class="text-right"><?php echo $paymentInfo['last_payment_date'] ?: 'No payments yet'; ?></td>
                                </tr>
                                <?php if (!$paymentInfo['is_paid_in_full']): ?>
                                <tr id="next-payment-row">
                                    <td><strong>Next Payment:</strong></td>
                                    <td class="text-right">₱<?php echo number_format($paymentInfo['next_payment'], 2); ?></td>
                                </tr>
                                <?php endif; ?>
                            </table>
                            
                            <div class="text-center mt-3">
                                <a href="payments.php?course_id=<?php echo $courseId; ?>" class="btn btn-success btn-sm">
                                    <i class="fas fa-money-bill-wave mr-1"></i> Manage Payments
                                </a>
                                <?php if (!$paymentInfo['is_paid_in_full']): ?>
                                <a href="make_payment.php?course_id=<?php echo $courseId; ?>" class="btn btn-primary btn-sm ml-2">
                                    <i class="fas fa-credit-card mr-1"></i> Make Payment
                                </a>
                                <?php endif; ?>
                            </div>
                        </div>
                    </div>
                </div>

                <!-- Course Content Card -->
                <div class="col-xl-4 col-md-12 mb-4">
                    <div class="card border-left-info 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-info">Course Resources</h6>
                        </div>
                        <div class="card-body">
                            <div class="mb-4">
                                <h6 class="font-weight-bold">Course Description</h6>
                                <p class="text-gray-800">
                                    <?php echo $courseData['description'] ?: 'No description available for this course.'; ?>
                                </p>
                            </div>
                            
                            <div class="resource-links">
                                <h6 class="font-weight-bold">Quick Links</h6>
                                <div class="list-group">
                                    <a href="course_content.php?id=<?php echo $courseId; ?>" class="list-group-item list-group-item-action">
                                        <i class="fas fa-book mr-2"></i> Course Content
                                    </a>
                                    <a href="assignments.php?course_id=<?php echo $courseId; ?>" class="list-group-item list-group-item-action">
                                        <i class="fas fa-tasks mr-2"></i> Assignments
                                    </a>
                                    <a href="attendance.php?course_id=<?php echo $courseId; ?>" class="list-group-item list-group-item-action">
                                        <i class="fas fa-calendar-check mr-2"></i> Attendance
                                    </a>
                                    <a href="download_resources.php?course_id=<?php echo $courseId; ?>" class="list-group-item list-group-item-action">
                                        <i class="fas fa-download mr-2"></i> Download Resources
                                    </a>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <?php endif; ?>
        </div>
        <!-- /.container-fluid -->
    </div>
    <!-- End of Main Content -->

    <?php require_once('includes/footer.php'); ?>
</div>
<!-- End of Content Wrapper -->