Your IP : 216.73.216.93


Current Path : /home/users/unlimited/www/nigeria.codeskitter.site/student/
Upload File :
Current File : /home/users/unlimited/www/nigeria.codeskitter.site/student/course-player.php

<?php
include '../includes/config.php';

if(!isset($_SESSION['user_id']) || $_SESSION['user_role'] != 'student') {
    header("Location: ../login.php");
    exit();
}

$user_id = $_SESSION['user_id'];
$course_id = $_GET['course_id'] ?? 0;

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

if(!$enrollment) {
    header("Location: my-courses.php");
    exit();
}

// Get course details
$course_sql = "SELECT c.*, i.name as instructor_name 
               FROM courses c 
               LEFT JOIN instructors i ON c.instructor_id = i.id 
               WHERE c.id = ?";
$stmt = $conn->prepare($course_sql);
$stmt->bind_param("i", $course_id);
$stmt->execute();
$course = $stmt->get_result()->fetch_assoc();

// Get course modules
$modules_sql = "SELECT cm.*, 
                COALESCE(sp.progress_percentage, 0) as progress,
                COALESCE(sp.is_completed, 0) as is_completed,
                sp.time_spent
                FROM course_modules cm 
                LEFT JOIN student_progress sp ON cm.id = sp.module_id AND sp.user_id = ?
                WHERE cm.course_id = ? 
                ORDER BY cm.sort_order ASC";
$stmt_modules = $conn->prepare($modules_sql);
$stmt_modules->bind_param("ii", $user_id, $course_id);
$stmt_modules->execute();
$modules = $stmt_modules->get_result();

// Get current module if specified
$current_module_id = $_GET['module_id'] ?? 0;
if($current_module_id) {
    $current_module_sql = "SELECT * FROM course_modules WHERE id = ?";
    $stmt_current = $conn->prepare($current_module_sql);
    $stmt_current->bind_param("i", $current_module_id);
    $stmt_current->execute();
    $current_module = $stmt_current->get_result()->fetch_assoc();
} else {
    // Get first module if no module specified
    $first_module_sql = "SELECT * FROM course_modules WHERE course_id = ? ORDER BY sort_order ASC LIMIT 1";
    $stmt_first = $conn->prepare($first_module_sql);
    $stmt_first->bind_param("i", $course_id);
    $stmt_first->execute();
    $current_module = $stmt_first->get_result()->fetch_assoc();
    $current_module_id = $current_module['id'] ?? 0;
}

// Update last accessed time
if($current_module_id) {
    $update_access_sql = "INSERT INTO student_progress (user_id, module_id, course_id, last_accessed) 
                         VALUES (?, ?, ?, NOW()) 
                         ON DUPLICATE KEY UPDATE last_accessed = NOW()";
    $stmt_update = $conn->prepare($update_access_sql);
    $stmt_update->bind_param("iii", $user_id, $current_module_id, $course_id);
    $stmt_update->execute();
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?php echo $course['title']; ?> - eLearning</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.7.2/font/bootstrap-icons.css">
    <link rel="stylesheet" href="../assets/css/style.css">
</head>
<body>
    <?php include 'navigation.php'; ?>

    <div class="container-fluid">
        <div class="row">
            <!-- Sidebar - Course Modules -->
            <div class="col-lg-3 bg-light border-end">
                <div class="sidebar-sticky pt-3">
                    <div class="p-3">
                        <h5><?php echo $course['title']; ?></h5>
                        <div class="progress mb-3">
                            <div class="progress-bar bg-success" style="width: <?php echo $enrollment['progress']; ?>%"></div>
                        </div>
                        <small class="text-muted"><?php echo $enrollment['progress']; ?>% Complete</small>
                    </div>
                    
                    <nav class="nav flex-column">
                        <?php while($module = $modules->fetch_assoc()): ?>
                        <a class="nav-link border-bottom py-3 <?php echo $module['id'] == $current_module_id ? 'bg-primary text-white' : ''; ?>" 
                           href="course-player.php?course_id=<?php echo $course_id; ?>&module_id=<?php echo $module['id']; ?>">
                            <div class="d-flex justify-content-between align-items-center">
                                <div>
                                    <i class="bi bi-<?php echo getModuleIcon($module['content_type']); ?> me-2"></i>
                                    <?php echo $module['title']; ?>
                                </div>
                                <?php if($module['is_completed']): ?>
                                <i class="bi bi-check-circle-fill text-success"></i>
                                <?php endif; ?>
                            </div>
                            <?php if($module['duration']): ?>
                            <small class="text-muted"><?php echo $module['duration']; ?> min</small>
                            <?php endif; ?>
                        </a>
                        <?php endwhile; ?>
                    </nav>
                </div>
            </div>

            <!-- Main Content - Module Player -->
            <div class="col-lg-9">
                <div class="p-4">
                    <?php if($current_module): ?>
                    <div class="d-flex justify-content-between align-items-center mb-4">
                        <div>
                            <h2><?php echo $current_module['title']; ?></h2>
                            <p class="text-muted"><?php echo $course['title']; ?></p>
                        </div>
                        <div>
                            <?php if($current_module['is_completed']): ?>
                            <span class="badge bg-success">Completed</span>
                            <?php else: ?>
                            <button class="btn btn-success mark-complete" data-module-id="<?php echo $current_module['id']; ?>">
                                Mark as Complete
                            </button>
                            <?php endif; ?>
                        </div>
                    </div>

                    <div class="module-content mb-4">
                        <?php if($current_module['content_type'] == 'video'): ?>
                        <div class="ratio ratio-16x9 mb-4">
                            <iframe src="<?php echo $current_module['content_url']; ?>" 
                                    allowfullscreen></iframe>
                        </div>
                        <?php elseif($current_module['content_type'] == 'article'): ?>
                        <div class="card">
                            <div class="card-body">
                                <?php echo nl2br($current_module['description']); ?>
                            </div>
                        </div>
                        <?php elseif($current_module['content_type'] == 'quiz'): ?>
                        <div class="card">
                            <div class="card-body text-center py-5">
                                <i class="bi bi-question-circle fs-1 text-primary mb-3"></i>
                                <h4>Quiz: <?php echo $current_module['title']; ?></h4>
                                <p class="text-muted">Complete the quiz to test your knowledge</p>
                                <button class="btn btn-primary">Start Quiz</button>
                            </div>
                        </div>
                        <?php endif; ?>
                    </div>

                    <div class="module-description">
                        <h5>About this module</h5>
                        <p><?php echo $current_module['description']; ?></p>
                    </div>

                    <!-- Navigation Buttons -->
                    <div class="d-flex justify-content-between mt-5">
                        <?php
                        // Get previous and next modules
                        $nav_sql = "SELECT id, title FROM course_modules 
                                   WHERE course_id = ? AND sort_order < (SELECT sort_order FROM course_modules WHERE id = ?) 
                                   ORDER BY sort_order DESC LIMIT 1";
                        $stmt_prev = $conn->prepare($nav_sql);
                        $stmt_prev->bind_param("ii", $course_id, $current_module_id);
                        $stmt_prev->execute();
                        $prev_module = $stmt_prev->get_result()->fetch_assoc();

                        $nav_sql = "SELECT id, title FROM course_modules 
                                   WHERE course_id = ? AND sort_order > (SELECT sort_order FROM course_modules WHERE id = ?) 
                                   ORDER BY sort_order ASC LIMIT 1";
                        $stmt_next = $conn->prepare($nav_sql);
                        $stmt_next->bind_param("ii", $course_id, $current_module_id);
                        $stmt_next->execute();
                        $next_module = $stmt_next->get_result()->fetch_assoc();
                        ?>
                        
                        <?php if($prev_module): ?>
                        <a href="course-player.php?course_id=<?php echo $course_id; ?>&module_id=<?php echo $prev_module['id']; ?>" 
                           class="btn btn-outline-primary">
                            <i class="bi bi-arrow-left"></i> Previous
                        </a>
                        <?php else: ?>
                        <span></span>
                        <?php endif; ?>

                        <?php if($next_module): ?>
                        <a href="course-player.php?course_id=<?php echo $course_id; ?>&module_id=<?php echo $next_module['id']; ?>" 
                           class="btn btn-primary">
                            Next <i class="bi bi-arrow-right"></i>
                        </a>
                        <?php else: ?>
                        <a href="course-progress.php?course_id=<?php echo $course_id; ?>" class="btn btn-success">
                            Complete Course <i class="bi bi-check-circle"></i>
                        </a>
                        <?php endif; ?>
                    </div>

                    <?php else: ?>
                    <div class="text-center py-5">
                        <i class="bi bi-exclamation-circle fs-1 text-muted"></i>
                        <h3 class="text-muted">No Modules Available</h3>
                        <p class="text-muted">This course doesn't have any learning modules yet.</p>
                    </div>
                    <?php endif; ?>
                </div>
            </div>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
    <script>
        // Mark module as complete
        document.querySelectorAll('.mark-complete').forEach(button => {
            button.addEventListener('click', function() {
                const moduleId = this.getAttribute('data-module-id');
                markModuleComplete(moduleId);
            });
        });

        function markModuleComplete(moduleId) {
            fetch('mark-complete.php', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded',
                },
                body: `module_id=${moduleId}&action=complete`
            })
            .then(response => response.json())
            .then(data => {
                if(data.success) {
                    location.reload();
                } else {
                    alert('Error marking module as complete');
                }
            });
        }
    </script>
</body>
</html>

<?php
function getModuleIcon($contentType) {
    switch($contentType) {
        case 'video': return 'play-circle';
        case 'article': return 'file-text';
        case 'quiz': return 'question-circle';
        case 'assignment': return 'pencil-square';
        default: return 'file-earmark';
    }
}
?>