<?php
// Database configuration
require_once 'db_config.php';
// Create exam_schedules table
$create_exam_schedules = "CREATE TABLE IF NOT EXISTS `exam_schedules` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(255) NOT NULL,
`description` text NULL,
`course_id` int(11) NOT NULL,
`exam_date` date NOT NULL,
`start_time` time NOT NULL,
`end_time` time NOT NULL,
`duration_minutes` int(11) NOT NULL DEFAULT 60,
`location` varchar(255) NULL,
`passing_percentage` int(11) NOT NULL DEFAULT 40,
`is_active` tinyint(1) NOT NULL DEFAULT 1,
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
KEY `course_id` (`course_id`),
CONSTRAINT `exam_schedules_ibfk_1` FOREIGN KEY (`course_id`) REFERENCES `courses` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;";
// Create question_banks table
$create_question_banks = "CREATE TABLE IF NOT EXISTS `question_banks` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(255) NOT NULL,
`course_id` int(11) NOT NULL,
`description` text NULL,
`is_active` tinyint(1) NOT NULL DEFAULT 1,
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
KEY `course_id` (`course_id`),
CONSTRAINT `question_banks_ibfk_1` FOREIGN KEY (`course_id`) REFERENCES `courses` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;";
// Create questions table
$create_questions = "CREATE TABLE IF NOT EXISTS `questions` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`question_bank_id` int(11) NOT NULL,
`question_text` text NOT NULL,
`question_type` enum('multiple_choice', 'true_false', 'short_answer', 'essay') NOT NULL DEFAULT 'multiple_choice',
`difficulty_level` enum('easy', 'medium', 'hard') NOT NULL DEFAULT 'medium',
`marks` int(11) NOT NULL DEFAULT 1,
`is_practice` tinyint(1) NOT NULL DEFAULT 0,
`is_active` tinyint(1) NOT NULL DEFAULT 1,
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
KEY `question_bank_id` (`question_bank_id`),
CONSTRAINT `questions_ibfk_1` FOREIGN KEY (`question_bank_id`) REFERENCES `question_banks` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;";
// Create question_options table
$create_question_options = "CREATE TABLE IF NOT EXISTS `question_options` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`question_id` int(11) NOT NULL,
`option_text` text NOT NULL,
`is_correct` tinyint(1) NOT NULL DEFAULT 0,
PRIMARY KEY (`id`),
KEY `question_id` (`question_id`),
CONSTRAINT `question_options_ibfk_1` FOREIGN KEY (`question_id`) REFERENCES `questions` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;";
// Create exam_question_maps table (to map questions to exams)
$create_exam_question_maps = "CREATE TABLE IF NOT EXISTS `exam_question_maps` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`exam_id` int(11) NOT NULL,
`question_id` int(11) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `exam_question_unique` (`exam_id`, `question_id`),
KEY `exam_id` (`exam_id`),
KEY `question_id` (`question_id`),
CONSTRAINT `exam_question_maps_ibfk_1` FOREIGN KEY (`exam_id`) REFERENCES `exam_schedules` (`id`) ON DELETE CASCADE,
CONSTRAINT `exam_question_maps_ibfk_2` FOREIGN KEY (`question_id`) REFERENCES `questions` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;";
// Create student_exams table (records of student exam attempts)
$create_student_exams = "CREATE TABLE IF NOT EXISTS `student_exams` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) NOT NULL,
`exam_id` int(11) NOT NULL,
`start_time` datetime NULL,
`end_time` datetime NULL,
`status` enum('pending', 'in_progress', 'completed', 'graded', 'failed', 'passed') NOT NULL DEFAULT 'pending',
`total_score` decimal(5,2) NULL,
`percentage` decimal(5,2) NULL,
`admin_remarks` text NULL,
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
KEY `user_id` (`user_id`),
KEY `exam_id` (`exam_id`),
CONSTRAINT `student_exams_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE,
CONSTRAINT `student_exams_ibfk_2` FOREIGN KEY (`exam_id`) REFERENCES `exam_schedules` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;";
// Create student_answers table
$create_student_answers = "CREATE TABLE IF NOT EXISTS `student_answers` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`student_exam_id` int(11) NOT NULL,
`question_id` int(11) NOT NULL,
`answer_text` text NULL,
`selected_option_id` int(11) NULL,
`is_correct` tinyint(1) NULL,
`marks_obtained` decimal(5,2) NULL DEFAULT 0.00,
`admin_remarks` text NULL,
PRIMARY KEY (`id`),
KEY `student_exam_id` (`student_exam_id`),
KEY `question_id` (`question_id`),
KEY `selected_option_id` (`selected_option_id`),
CONSTRAINT `student_answers_ibfk_1` FOREIGN KEY (`student_exam_id`) REFERENCES `student_exams` (`id`) ON DELETE CASCADE,
CONSTRAINT `student_answers_ibfk_2` FOREIGN KEY (`question_id`) REFERENCES `questions` (`id`) ON DELETE CASCADE,
CONSTRAINT `student_answers_ibfk_3` FOREIGN KEY (`selected_option_id`) REFERENCES `question_options` (`id`) ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;";
// Create practice_sessions table
$create_practice_sessions = "CREATE TABLE IF NOT EXISTS `practice_sessions` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) NOT NULL,
`course_id` int(11) NOT NULL,
`start_time` datetime NOT NULL,
`end_time` datetime NULL,
`total_questions` int(11) NOT NULL DEFAULT 0,
`correct_answers` int(11) NOT NULL DEFAULT 0,
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
KEY `user_id` (`user_id`),
KEY `course_id` (`course_id`),
CONSTRAINT `practice_sessions_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE,
CONSTRAINT `practice_sessions_ibfk_2` FOREIGN KEY (`course_id`) REFERENCES `courses` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;";
// Create practice_answers table
$create_practice_answers = "CREATE TABLE IF NOT EXISTS `practice_answers` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`practice_session_id` int(11) NOT NULL,
`question_id` int(11) NOT NULL,
`selected_option_id` int(11) NULL,
`is_correct` tinyint(1) NOT NULL DEFAULT 0,
PRIMARY KEY (`id`),
KEY `practice_session_id` (`practice_session_id`),
KEY `question_id` (`question_id`),
KEY `selected_option_id` (`selected_option_id`),
CONSTRAINT `practice_answers_ibfk_1` FOREIGN KEY (`practice_session_id`) REFERENCES `practice_sessions` (`id`) ON DELETE CASCADE,
CONSTRAINT `practice_answers_ibfk_2` FOREIGN KEY (`question_id`) REFERENCES `questions` (`id`) ON DELETE CASCADE,
CONSTRAINT `practice_answers_ibfk_3` FOREIGN KEY (`selected_option_id`) REFERENCES `question_options` (`id`) ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;";
// Execute the SQL statements
try {
$conn->query($create_exam_schedules);
echo "Table 'exam_schedules' created successfully.<br>";
$conn->query($create_question_banks);
echo "Table 'question_banks' created successfully.<br>";
$conn->query($create_questions);
echo "Table 'questions' created successfully.<br>";
$conn->query($create_question_options);
echo "Table 'question_options' created successfully.<br>";
$conn->query($create_exam_question_maps);
echo "Table 'exam_question_maps' created successfully.<br>";
$conn->query($create_student_exams);
echo "Table 'student_exams' created successfully.<br>";
$conn->query($create_student_answers);
echo "Table 'student_answers' created successfully.<br>";
$conn->query($create_practice_sessions);
echo "Table 'practice_sessions' created successfully.<br>";
$conn->query($create_practice_answers);
echo "Table 'practice_answers' created successfully.<br>";
echo "All exam-related tables have been created successfully.";
} catch (Exception $e) {
echo "Error: " . $e->getMessage();
}
?>