Path : /home/vishqocm/pcib.in/backup_20250414/
File Upload :
Current File : /home/vishqocm//pcib.in/backup_20250414/index.php

<?php
session_start();
require_once 'database/db_config.php';

// Check if user has student privileges
require_student_privileges('../login.php');

// Get dashboard statistics with error handling
try {
    // Get student-specific statistics
    $user_id = $_SESSION['user_id'];
    
    // Get student information
    $student_query = "SELECT * FROM users WHERE id = ?";
    $stmt = $conn->prepare($student_query);
    $stmt->bind_param("i", $user_id);
    $stmt->execute();
    $student_result = $stmt->get_result();
    $student = $student_result->fetch_assoc();
    
    // Get enrolled courses count
    $enrolled_courses_query = "SELECT COUNT(*) as count FROM enrollments WHERE user_id = ?";
    $stmt = $conn->prepare($enrolled_courses_query);
    $stmt->bind_param("i", $user_id);
    $stmt->execute();
    $enrolled_courses = $stmt->get_result()->fetch_assoc()['count'];
    
    // Get completed courses count
    $completed_courses_query = "SELECT COUNT(*) as count FROM enrollments WHERE user_id = ? AND status = 'completed'";
    $stmt = $conn->prepare($completed_courses_query);
    $stmt->bind_param("i", $user_id);
    $stmt->execute();
    $completed_courses = $stmt->get_result()->fetch_assoc()['count'];
    
    // Get active courses
    $active_courses_query = "SELECT c.* FROM courses c 
                             JOIN enrollments e ON c.id = e.course_id 
                             WHERE e.user_id = ? AND e.status = 'active' 
                             ORDER BY e.enrollment_date DESC LIMIT 5";
    $stmt = $conn->prepare($active_courses_query);
    $stmt->bind_param("i", $user_id);
    $stmt->execute();
    $active_courses_result = $stmt->get_result();
    
    // Get recent activities - example query, adjust according to your database schema
    $activities_query = "SELECT * FROM activities 
                        WHERE user_id = ? 
                        ORDER BY created_at DESC LIMIT 10";
    $stmt = $conn->prepare($activities_query);
    $stmt->bind_param("i", $user_id);
    $stmt->execute();
    $activities_result = $stmt->get_result();
    
} catch (Exception $e) {
    // Log the error and show a user-friendly message
    error_log("Dashboard statistics error: " . $e->getMessage());
    
    // Initialize empty values if there's an error
    $enrolled_courses = 0;
    $completed_courses = 0;
    $active_courses_result = false;
    $activities_result = false;
}

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

<!-- Begin Page Content -->
<div class="container-fluid mt-4 pt-4">
    <!-- Page Heading -->
    <div class="d-sm-flex align-items-center justify-content-between mb-4">
        <h1 class="h3 mb-0 text-gray-800">Student Dashboard</h1>
        <a href="profile.php" class="d-none d-sm-inline-block btn btn-primary shadow-sm">
            <i class="fas fa-user fa-sm text-white-50"></i> View Profile
        </a>
    </div>

    <!-- Statistics Cards -->
    <div class="row">
        <!-- Enrolled Courses Card -->
        <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">
                                Enrolled Courses</div>
                            <div class="h5 mb-0 font-weight-bold text-gray-800"><?php echo number_format($enrolled_courses); ?></div>
                        </div>
                        <div class="col-auto">
                            <i class="fas fa-book fa-2x text-gray-300"></i>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <!-- Completed Courses Card -->
        <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 number_format($completed_courses); ?></div>
                        </div>
                        <div class="col-auto">
                            <i class="fas fa-graduation-cap fa-2x text-gray-300"></i>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <!-- Assignments Card -->
        <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">
                                Progress</div>
                            <div class="row no-gutters align-items-center">
                                <div class="col-auto">
                                    <div class="h5 mb-0 mr-3 font-weight-bold text-gray-800">
                                        <?php 
                                        if ($enrolled_courses > 0) {
                                            echo round(($completed_courses / $enrolled_courses) * 100) . '%';
                                        } else {
                                            echo '0%';
                                        }
                                        ?>
                                    </div>
                                </div>
                                <div class="col">
                                    <div class="progress progress-sm mr-2">
                                        <div class="progress-bar bg-info" role="progressbar" 
                                            style="width: <?php echo ($enrolled_courses > 0) ? ($completed_courses / $enrolled_courses) * 100 : 0; ?>%" 
                                            aria-valuenow="<?php echo ($enrolled_courses > 0) ? ($completed_courses / $enrolled_courses) * 100 : 0; ?>" 
                                            aria-valuemin="0" 
                                            aria-valuemax="100"></div>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="col-auto">
                            <i class="fas fa-clipboard-list fa-2x text-gray-300"></i>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <!-- Achievements Card -->
        <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">
                                Certificates</div>
                            <div class="h5 mb-0 font-weight-bold text-gray-800">
                                <?php 
                                // This is a placeholder - replace with actual count from your achievements system
                                echo '0';
                                ?>
                            </div>
                        </div>
                        <div class="col-auto">
                            <i class="fas fa-trophy fa-2x text-gray-300"></i>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Content Row -->
    <div class="row">
        <!-- Application Status Column -->
        <div class="col-lg-6 mb-4">
            <!-- Application Status Card -->
            <div class="card shadow mb-4">
                <div class="card-header py-3 bg-primary">
                    <h6 class="m-0 font-weight-bold text-white">My Application Status</h6>
                </div>
                <div class="card-body">
                   
                </div>
            </div>
        </div>

        <!-- Active Courses Column -->
        <div class="col-lg-6 mb-4">
            <!-- Active Courses Card -->
            <div class="card shadow mb-4">
                <div class="card-header py-3 d-flex flex-row align-items-center justify-content-between bg-primary">
                    <h6 class="m-0 font-weight-bold text-white">My Active Courses</h6>
                    <a href="my_courses.php" class="btn btn-sm btn-light shadow-sm">View All</a>
                </div>
                <div class="card-body">
                    <?php if ($active_courses_result && $active_courses_result->num_rows > 0): ?>
                        <div class="table-responsive">
                            <table class="table table-hover">
                                <thead>
                                    <tr>
                                        <th>Course</th>
                                        <th>Enrollment Date</th>
                                        <th>Status</th>
                                        <th>Action</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php while ($course = $active_courses_result->fetch_assoc()): ?>
                                        <tr>
                                            <td><?php echo htmlspecialchars($course['title']); ?></td>
                                            <td>
                                                <?php 
                                                // Get enrollment date
                                                $enrollment_query = "SELECT enrollment_date FROM enrollments WHERE user_id = ? AND course_id = ? LIMIT 1";
                                                $stmt = $conn->prepare($enrollment_query);
                                                $stmt->bind_param("ii", $user_id, $course['id']);
                                                $stmt->execute();
                                                $enrollment = $stmt->get_result()->fetch_assoc();
                                                echo $enrollment ? date('M d, Y', strtotime($enrollment['enrollment_date'])) : 'N/A';
                                                ?>
                                            </td>
                                            <td>
                                                <span class="badge bg-success">Active</span>
                                            </td>
                                            <td>
                                                <a href="course_details.php?id=<?php echo $course['id']; ?>" class="btn btn-sm btn-info">
                                                    <i class="fas fa-eye"></i> View
                                                </a>
                                            </td>
                                        </tr>
                                    <?php endwhile; ?>
                                </tbody>
                            </table>
                        </div>
                    <?php else: ?>
                        <div class="alert alert-info">
                            <p class="mb-0">You are not enrolled in any courses yet. <a href="../courses.php">Browse courses</a> to get started!</p>
                        </div>
                    <?php endif; ?>
                </div>
            </div>
        </div>
    </div>
</div>

<section class="container mt-4">
    <div class="row">
        <div class="col-lg-12">
            <!-- Enrollment Verification Tokens -->
            <div class="col-lg-12 mt-4">
                <div class="card shadow-sm">
                    <div class="card-header bg-primary text-white">
                        <h5 class="mb-0"><i class="fas fa-key me-2"></i> Enrollment Verification Tokens</h5>
                    </div>
                    <div class="card-body">
                        <p class="text-muted mb-3">Present these tokens at the institute to start your classes.</p>
                        
                        <div class="table-responsive">
                            <table class="table table-bordered table-hover">
                                <thead class="table-light">
                                    <tr>
                                        <th>Course</th>
                                        <th>Enrollment Date</th>
                                        <th>Status</th>
                                        <th>Verification Token</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php
                                    // Get all enrollments with verification tokens
                                    $tokens_query = "SELECT e.*, c.title as course_title, c.image 
                                                    FROM enrollments e 
                                                    JOIN courses c ON e.course_id = c.id 
                                                    WHERE e.user_id = ? 
                                                    ORDER BY e.enrollment_date DESC";
                                    $stmt = $conn->prepare($tokens_query);
                                    $stmt->bind_param("i", $user_id);
                                    $stmt->execute();
                                    $tokens_result = $stmt->get_result();
                                    
                                    if ($tokens_result && $tokens_result->num_rows > 0):
                                        while ($token = $tokens_result->fetch_assoc()):
                                    ?>
                                        <tr>
                                            <td>
                                                <div class="d-flex align-items-center">
                                                    <img src="<?php echo $token['image'] ? '../' . $token['image'] : '../assets/img/course-placeholder.jpg'; ?>" 
                                                         class="rounded" alt="Course Image" width="40" height="40">
                                                    <div class="ms-2">
                                                        <?php echo htmlspecialchars($token['course_title']); ?>
                                                    </div>
                                                </div>
                                            </td>
                                            <td><?php echo date('M d, Y', strtotime($token['enrollment_date'])); ?></td>
                                            <td>
                                                <span class="badge bg-<?php echo ($token['status'] === 'active') ? 'success' : 
                                                    (($token['status'] === 'completed') ? 'primary' : 'warning'); ?>">
                                                    <?php echo ucfirst($token['status']); ?>
                                                </span>
                                            </td>
                                            <td>
                                                <div class="input-group">
                                                    <input type="text" class="form-control bg-light" value="<?php echo $token['verification_token']; ?>" readonly>
                                                    <button class="btn btn-outline-primary copy-token-btn" type="button" data-token="<?php echo $token['verification_token']; ?>">
                                                        <i class="fas fa-copy"></i>
                                                    </button>
                                                </div>
                                            </td>
                                        </tr>
                                    <?php
                                        endwhile;
                                    else:
                                    ?>
                                        <tr>
                                            <td colspan="4" class="text-center">No enrollment tokens found.</td>
                                        </tr>
                                    <?php endif; ?>
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</section>

<script>
document.addEventListener('DOMContentLoaded', function() {
    // Load application status
    loadApplicationStatus();
    
    // Copy verification token to clipboard
    const copyButtons = document.querySelectorAll('.copy-token-btn');
    copyButtons.forEach(button => {
        button.addEventListener('click', function() {
            const token = this.getAttribute('data-token');
            
            navigator.clipboard.writeText(token).then(() => {
                // Change button text temporarily
                const originalHTML = this.innerHTML;
                this.innerHTML = '<i class="fas fa-check"></i>';
                
                setTimeout(() => {
                    this.innerHTML = originalHTML;
                }, 1500);
            }).catch(err => {
                console.error('Could not copy text: ', err);
            });
        });
    });
});

function loadApplicationStatus() {
    $.ajax({
        url: 'ajax/check_application_status.php',
        type: 'GET',
        success: function(response) {
            try {
                const result = typeof response === 'string' ? JSON.parse(response) : response;
                
                if (result.success) {
                    let html = '';
                    
                    if (result.has_application) {
                        // Application status
                        const app = result.application;
                        const docs = result.documents;
                        
                        // Determine badge color
                        let badgeClass = 'bg-secondary';
                        switch(app.status) {
                            case 'submitted':
                                badgeClass = 'bg-info';
                                break;
                            case 'document_verification':
                                badgeClass = 'bg-info';
                                break;
                            case 'payment_pending':
                                badgeClass = 'bg-warning text-dark';
                                break;
                            case 'enrollment_pending':
                                badgeClass = 'bg-warning text-dark';
                                break;
                            case 'enrolled':
                                badgeClass = 'bg-success';
                                break;
                            case 'completed':
                                badgeClass = 'bg-primary';
                                break;
                            case 'rejected':
                                badgeClass = 'bg-danger';
                                break;
                        }
                        
                        html += '<div class="card border-left-primary shadow h-100 mb-4">';
                        html += '<div class="card-body">';
                        html += '<div class="d-flex justify-content-between align-items-center mb-3">';
                        html += '<h5 class="card-title mb-0">Application Status</h5>';
                        html += `<span class="badge ${badgeClass}">${app.status.replace('_', ' ').toUpperCase()}</span>`;
                        html += '</div>';
                        html += `<p class="card-text">${app.status_message}</p>`;
                        
                        // Document verification progress
                        if (docs) {
                            html += '<div class="mt-3">';
                            html += '<h6>Document Verification</h6>';
                            
                            const docProgress = docs.verified_documents > 0 ? Math.round((docs.verified_documents / docs.total_documents) * 100) : 0;
                            
                            html += '<div class="progress mb-2">';
                            html += `<div class="progress-bar bg-success" role="progressbar" style="width: ${docProgress}%" `;
                            html += `aria-valuenow="${docProgress}" aria-valuemin="0" aria-valuemax="100">${docProgress}%</div>`;
                            html += '</div>';
                            
                            html += '<div class="small text-muted">';
                            html += `Verified: ${docs.verified_documents} | Pending: ${docs.pending_documents} | Rejected: ${docs.rejected_documents}`;
                            html += '</div>';
                            
                            if (docs.pending_documents > 0 || docs.total_documents < 4) {
                                html += '<a href="documents.php" class="btn btn-sm btn-primary mt-2">';
                                html += '<i class="fas fa-file-alt me-1"></i> Manage Documents';
                                html += '</a>';
                            }
                        }
                        
                        // Payment information if available
                        if (result.payment && app.status === 'payment_pending') {
                            html += '<div class="mt-3">';
                            html += '<h6>Payment</h6>';
                            
                            html += '<a href="payments.php" class="btn btn-sm btn-success mt-2">';
                            html += '<i class="fas fa-credit-card me-1"></i> Make Payment';
                            html += '</a>';
                        } else if (result.payment && result.payment.status === 'completed') {
                            html += '<div class="mt-3">';
                            html += '<h6>Payment</h6>';
                            html += '<div class="alert alert-success mb-0"><i class="fas fa-check-circle me-1"></i> Payment complete</div>';
                        }
                        
                        // Next steps
                        html += '<div class="mt-4">';
                        html += '<h6>Next Steps</h6>';
                        html += `<p class="mb-0">${app.next_step}</p>`;
                        html += '</div>';
                        
                        html += '</div>';
                        html += '</div>';
                        
                    } else {
                        html += '<div class="alert alert-info mb-0">';
                        html += '<p class="mb-0">You have not submitted any applications yet. ';
                        html += '<a href="../courses.php">Browse courses</a> to apply!</p>';
                        html += '</div>';
                    }
                    
                    $('#applicationStatus').html(html);
                    
                } else {
                    $('#applicationStatus').html(`<div class="alert alert-danger mb-0">${result.message}</div>`);
                }
                
            } catch (e) {
                console.error(e);
                $('#applicationStatus').html('<div class="alert alert-danger mb-0">An error occurred while loading application status.</div>');
            }
        },
        error: function() {
            $('#applicationStatus').html('<div class="alert alert-danger mb-0">Failed to connect to the server.</div>');
        }
    });
}
</script>

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