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

<?php
session_start();

// Check if student is logged in
if (!isset($_SESSION['user_id']) || $_SESSION['role'] !== 'student') {
    echo json_encode([
        'success' => false,
        'message' => 'Unauthorized access'
    ]);
    exit();
}

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

$userId = $_SESSION['user_id'];

try {
    // Get the latest application
    $stmt = $conn->prepare("
        SELECT a.id, a.application_date, a.status, c.course_id
        FROM enrollment_applications a
        LEFT JOIN enrollments ca ON a.id = ca.application_id
        LEFT JOIN courses c ON ca.course_id = c.id
        WHERE a.user_id = ? 
        ORDER BY a.application_date DESC 
        LIMIT 1
    ");
    $stmt->bind_param('i', $userId);
    $stmt->execute();
    $result = $stmt->get_result();
    
    if ($result->num_rows === 0) {
        echo json_encode([
            'success' => true,
            'has_application' => false,
            'message' => 'No applications found'
        ]);
        exit();
    }
    
    $application = $result->fetch_assoc();
    
    // Get document counts
    $stmt = $conn->prepare("
        SELECT 
            COUNT(*) as total_documents,
            SUM(CASE WHEN status = 'verified' THEN 1 ELSE 0 END) as verified_documents,
            SUM(CASE WHEN status = 'rejected' THEN 1 ELSE 0 END) as rejected_documents,
            SUM(CASE WHEN status = 'pending' THEN 1 ELSE 0 END) as pending_documents
        FROM student_documents
        WHERE user_id = ?
    ");
    $stmt->bind_param('i', $userId);
    $stmt->execute();
    $documentCounts = $stmt->get_result()->fetch_assoc();
    
    // Get course information if available
    $courseInfo = null;
    if (!empty($application['course_id'])) {
        $stmt = $conn->prepare("
            SELECT id, title, price, discount
            FROM courses
            WHERE id = ?
        ");
        $stmt->bind_param('i', $application['course_id']);
        $stmt->execute();
        $courseResult = $stmt->get_result();
        if ($courseResult->num_rows > 0) {
            $courseInfo = $courseResult->fetch_assoc();
            
            // Calculate discounted price
            if ($courseInfo['discount'] > 0) {
                $courseInfo['original_price'] = $courseInfo['price'];
                $courseInfo['discounted_price'] = $courseInfo['price'] - ($courseInfo['price'] * $courseInfo['discount'] / 100);
            } else {
                $courseInfo['original_price'] = $courseInfo['price'];
                $courseInfo['discounted_price'] = $courseInfo['price'];
            }
        }
    }
    
    // Get enrollment information if exists
    $enrollmentInfo = null;
    if (!empty($application['course_id'])) {
        $stmt = $conn->prepare("
            SELECT id, enrollment_date, status, verification_token
            FROM enrollments
            WHERE user_id = ? AND course_id = ?
            ORDER BY enrollment_date DESC
            LIMIT 1
        ");
        $stmt->bind_param('ii', $userId, $application['course_id']);
        $stmt->execute();
        $enrollmentResult = $stmt->get_result();
        if ($enrollmentResult->num_rows > 0) {
            $enrollmentInfo = $enrollmentResult->fetch_assoc();
            $enrollmentInfo['enrollment_date'] = date('M d, Y', strtotime($enrollmentInfo['enrollment_date']));
        }
    }
    
    // Get payment information if exists
    $paymentInfo = null;
    if (!empty($application['course_id'])) {
        $stmt = $conn->prepare("
            SELECT id, amount, payment_date, status
            FROM payments
            WHERE user_id = ? AND course_id = ?
            ORDER BY payment_date DESC
            LIMIT 1
        ");
        $stmt->bind_param('ii', $userId, $application['course_id']);
        $stmt->execute();
        $paymentResult = $stmt->get_result();
        if ($paymentResult->num_rows > 0) {
            $paymentInfo = $paymentResult->fetch_assoc();
            $paymentInfo['payment_date'] = date('M d, Y', strtotime($paymentInfo['payment_date']));
        }
    }
    
    // Define status messages and next steps
    $statusMessages = [
        'submitted' => 'Your application has been submitted. Please upload all required documents for verification.',
        'document_verification' => 'Your application is being reviewed. Please wait for document verification.',
        'payment_pending' => 'Your documents have been verified. Please proceed to make payment.',
        'enrollment_pending' => 'Your payment has been received. Enrollment is being processed.',
        'enrolled' => 'Congratulations! You are enrolled in the course.',
        'completed' => 'You have completed this course.',
        'rejected' => 'Unfortunately, your application has been rejected. Please contact the administrator for more information.'
    ];
    
    $nextSteps = [
        'submitted' => 'Upload all required documents through the Documents page.',
        'document_verification' => 'Wait for admin to verify your documents. You will be notified once verification is complete.',
        'payment_pending' => 'Proceed to the Payments page to make your payment.',
        'enrollment_pending' => 'Your enrollment is being processed. You will be notified once it is complete.',
        'enrolled' => 'You can now access your course from the My Courses page.',
        'completed' => 'Check your certificates or enroll in another course.',
        'rejected' => 'Contact the administrator for more information or submit a new application.'
    ];
    
    echo json_encode([
        'success' => true,
        'has_application' => true,
        'application' => [
            'id' => $application['id'],
            'date' => date('M d, Y', strtotime($application['application_date'])),
            'status' => $application['status'],
            'status_message' => $statusMessages[$application['status']] ?? 'Your application is being processed.',
            'next_step' => $nextSteps[$application['status']] ?? 'Please wait for further instructions.'
        ],
        'documents' => $documentCounts,
        'course' => $courseInfo,
        'enrollment' => $enrollmentInfo,
        'payment' => $paymentInfo
    ]);
    
} catch (Exception $e) {
    echo json_encode([
        'success' => false,
        'message' => 'Error checking application status: ' . $e->getMessage()
    ]);
}

$conn->close();