<?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('../includes/db_config.php');
$userId = $_SESSION['user_id'];
// Check if course ID is provided
if (!isset($_GET['course_id']) || empty($_GET['course_id'])) {
echo json_encode([
'success' => false,
'message' => 'Course ID is required'
]);
exit();
}
$courseId = intval($_GET['course_id']);
try {
// Check if the user is enrolled in this course
$stmt = $conn->prepare("
SELECT e.*
FROM enrollments e
WHERE e.user_id = ? AND e.course_id = ?
");
$stmt->bind_param('ii', $userId, $courseId);
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows === 0) {
echo json_encode([
'success' => false,
'message' => 'You are not enrolled in this course'
]);
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) {
echo json_encode([
'success' => false,
'message' => 'Course not found'
]);
exit();
}
$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_id' => $course['instructor_id'] ?? '',
'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';
echo json_encode([
'success' => true,
'course' => $courseData,
'payment' => $paymentInfo
]);
} catch (Exception $e) {
echo json_encode([
'success' => false,
'message' => 'Error retrieving course details: ' . $e->getMessage()
]);
}
$conn->close();