Path : /home/vishqocm/pcib.in/admin/ajax/
File Upload :
Current File : /home/vishqocm//pcib.in/admin/ajax/update_enrollment_status.php

<?php
// Start session
session_start();

// Check if user has admin privileges
if (!isset($_SESSION['role']) || ($_SESSION['role'] !== 'admin' && $_SESSION['role'] !== 'director')) {
    echo 'Unauthorized access';
    exit;
}

// Include database configuration
require_once '../../admin/database/db_config.php';

// Check if enrollment ID is provided
if (!isset($_POST['enrollment_id']) || empty($_POST['enrollment_id'])) {
    echo 'Invalid enrollment ID provided';
    exit;
}

if (!isset($_POST['status']) || empty($_POST['status'])) {
    echo 'Invalid status provided';
    exit;
}

$enrollment_id = intval($_POST['enrollment_id']);
$status = $_POST['status'];

// Validate enrollment ID format
if ($enrollment_id <= 0) {
    echo 'Invalid enrollment ID format';
    exit;
}

// Check if enrollment exists
$check_query = "SELECT id, user_id, course_id FROM enrollments WHERE id = ?";
$stmt = $conn->prepare($check_query);
$stmt->bind_param("i", $enrollment_id);
$stmt->execute();
$result = $stmt->get_result();

if ($result->num_rows === 0) {
    echo 'Enrollment ID not found in database';
    exit;
}

$enrollment = $result->fetch_assoc();

// Validate status
if (!in_array($status, ['active', 'pending', 'suspended', 'completed'])) {
    echo 'Invalid status value';
    exit;
}

// Begin transaction
$conn->begin_transaction();

try {
    // Update enrollment status
    $update_query = "UPDATE enrollments SET status = ?, last_updated = NOW() WHERE id = ?";
    $stmt = $conn->prepare($update_query);
    $stmt->bind_param("si", $status, $enrollment_id);
    
    if (!$stmt->execute()) {
        throw new Exception("Failed to update enrollment status: " . $conn->error);
    }
    
    // Log the activity
    $admin_id = $_SESSION['user_id'];
    $activity_query = "INSERT INTO activities (user_id, user_type, activity_type, activity_description) 
                      VALUES (?, 'admin', 'enrollment_update', ?)";
    $stmt = $conn->prepare($activity_query);
    $activity_description = "Enrollment ID #" . $enrollment_id . " status changed to " . $status;
    $stmt->bind_param("is", $admin_id, $activity_description);
    
    if (!$stmt->execute() && $conn->errno != 1146) { // Ignore error if activities table doesn't exist
        throw new Exception("Failed to log activity: " . $conn->error);
    }
    
    // Commit transaction
    $conn->commit();
    echo 'success';
    
} catch (Exception $e) {
    // Rollback transaction on error
    $conn->rollback();
    echo 'Error: ' . $e->getMessage();
}
?>