Path : /home/vishqocm/pcib.in/enroll/
File Upload :
Current File : /home/vishqocm//pcib.in/enroll/create_enrollment_tables.php

<?php
// Enable error reporting for debugging
error_reporting(E_ALL);
ini_set('display_errors', 1);

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

// Create new tables for enrollment process
$enrollment_tables = [
    // Student Documents table
    "CREATE TABLE IF NOT EXISTS student_documents (
        id INT AUTO_INCREMENT PRIMARY KEY,
        user_id INT NOT NULL,
        document_type VARCHAR(100) NOT NULL,
        document_path VARCHAR(255) NOT NULL,
        upload_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
        status ENUM('pending', 'verified', 'rejected') DEFAULT 'pending',
        admin_notes TEXT,
        FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
    )",
    
    // Enrollment Applications table
    "CREATE TABLE IF NOT EXISTS enrollment_applications (
        id INT AUTO_INCREMENT PRIMARY KEY,
        user_id INT NOT NULL,
        course_id INT NOT NULL,
        application_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
        status ENUM('pending', 'approved', 'rejected', 'payment_pending', 'completed') DEFAULT 'pending',
        verification_token VARCHAR(100),
        personal_details TEXT,
        admin_notes TEXT,
        verification_date TIMESTAMP NULL,
        verified_by INT NULL,
        FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE,
        FOREIGN KEY (course_id) REFERENCES courses(id) ON DELETE CASCADE,
        FOREIGN KEY (verified_by) REFERENCES users(id) ON DELETE SET NULL
    )",
    
    // Payments table
    "CREATE TABLE IF NOT EXISTS payments (
        id INT(11) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
        user_id INT(11) NOT NULL,
        course_id INT(11) NULL,
        amount DECIMAL(10,2) NOT NULL,
        payment_method VARCHAR(50) NOT NULL,
        transaction_id VARCHAR(100) NOT NULL,
        status VARCHAR(20) NOT NULL DEFAULT 'pending',
        payment_details TEXT NULL,
        payment_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
        marked_by INT(11) NULL,
        UNIQUE KEY (transaction_id),
        INDEX (user_id),
        INDEX (course_id)
    )",
    
    // Add a verification_token field to existing enrollments table if it doesn't exist
    "ALTER TABLE enrollments ADD COLUMN IF NOT EXISTS verification_token VARCHAR(100) AFTER status"
];

// Execute the queries
$successful = true;

foreach ($enrollment_tables as $table_query) {
    if (!$conn->query($table_query)) {
        echo "Error creating table: " . $conn->error . "<br>";
        $successful = false;
    }
}

if ($successful) {
    echo "<div style='background-color: #d4edda; color: #155724; padding: 15px; border-radius: 5px; margin-bottom: 20px;'>";
    echo "<h4>Success!</h4>";
    echo "<p>All enrollment tables were created successfully.</p>";
    echo "</div>";
} else {
    echo "<div style='background-color: #f8d7da; color: #721c24; padding: 15px; border-radius: 5px; margin-bottom: 20px;'>";
    echo "<h4>Error</h4>";
    echo "<p>There were errors while creating the enrollment tables. Please check the error messages above.</p>";
    echo "</div>";
}

// Add document_required field to courses table if it doesn't exist
$alter_courses = "ALTER TABLE courses ADD COLUMN IF NOT EXISTS document_required TINYINT(1) NOT NULL DEFAULT 1 AFTER status";
if (!$conn->query($alter_courses)) {
    echo "Error altering courses table: " . $conn->error . "<br>";
}

// Add enrollment_limit field to courses table if it doesn't exist
$alter_courses_limit = "ALTER TABLE courses ADD COLUMN IF NOT EXISTS enrollment_limit INT DEFAULT NULL AFTER document_required";
if (!$conn->query($alter_courses_limit)) {
    echo "Error adding enrollment_limit to courses table: " . $conn->error . "<br>";
}

// Add payment_plan field to enrollments table if it doesn't exist
$check_payment_plan_column = "SHOW COLUMNS FROM enrollments LIKE 'payment_plan'";
$result = $conn->query($check_payment_plan_column);

if ($result->num_rows == 0) {
    $add_payment_plan_query = "ALTER TABLE enrollments ADD COLUMN payment_plan ENUM('monthly', 'quarterly', 'full') DEFAULT 'full'";
    if ($conn->query($add_payment_plan_query) === TRUE) {
        echo "Added payment_plan field to enrollments table<br>";
    } else {
        echo "Error adding payment_plan field: " . $conn->error . "<br>";
    }
}

// Check and create payments table if it doesn't exist
$check_payments_table = "SHOW TABLES LIKE 'payments'";
$result = $conn->query($check_payments_table);

if ($result->num_rows == 0) {
    $create_payments_table = "CREATE TABLE payments (
        id INT(11) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
        user_id INT(11) NOT NULL,
        course_id INT(11) NULL,
        amount DECIMAL(10,2) NOT NULL,
        payment_method VARCHAR(50) NOT NULL,
        transaction_id VARCHAR(100) NOT NULL,
        status VARCHAR(20) NOT NULL DEFAULT 'pending',
        payment_details TEXT NULL,
        payment_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
        marked_by INT(11) NULL,
        UNIQUE KEY (transaction_id),
        INDEX (user_id),
        INDEX (course_id)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;";
    
    if ($conn->query($create_payments_table) === TRUE) {
        echo "Created payments table<br>";
    } else {
        echo "Error creating payments table: " . $conn->error . "<br>";
    }
}

// Check and create activities table if it doesn't exist
$check_activities_table = "SHOW TABLES LIKE 'activities'";
$result = $conn->query($check_activities_table);

if ($result->num_rows == 0) {
    $create_activities_table = "CREATE TABLE activities (
        id INT(11) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
        user_id INT(11) NOT NULL,
        user_type VARCHAR(20) NOT NULL,
        activity_type VARCHAR(50) NOT NULL,
        activity_description TEXT NOT NULL,
        ip_address VARCHAR(50) NULL,
        created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
        INDEX (user_id),
        INDEX (activity_type)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;";
    
    if ($conn->query($create_activities_table) === TRUE) {
        echo "Created activities table<br>";
    } else {
        echo "Error creating activities table: " . $conn->error . "<br>";
    }
}
?>