<?php
// Start session
session_start();
// Include database configuration
require_once '../../config/database.php';
// Check if user is logged in and has admin role
if (!isset($_SESSION['user_id']) || $_SESSION['role'] !== 'admin') {
header('Location: ../login.php');
exit;
}
// Initialize messages array
$messages = [];
$success = true;
// Create exam_schedules table if it doesn't exist
$create_exam_schedules = "CREATE TABLE IF NOT EXISTS exam_schedules (
id INT(11) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(255) NOT NULL,
description TEXT,
course_id INT(11) NOT NULL,
exam_date DATE NOT NULL,
start_time TIME NOT NULL,
end_time TIME NOT NULL,
duration INT(11) NOT NULL COMMENT 'Duration in minutes',
passing_percentage INT(11) NOT NULL DEFAULT 60,
status ENUM('active', 'inactive', 'completed') NOT NULL DEFAULT 'active',
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
FOREIGN KEY (course_id) REFERENCES courses(id) ON DELETE CASCADE
)";
if ($conn->query($create_exam_schedules)) {
$messages[] = "exam_schedules table created or already exists.";
} else {
$messages[] = "Error creating exam_schedules table: " . $conn->error;
$success = false;
}
// Create question_banks table if it doesn't exist
$create_question_banks = "CREATE TABLE IF NOT EXISTS question_banks (
id INT(11) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(255) NOT NULL,
description TEXT,
course_id INT(11) NULL,
status ENUM('active', 'inactive') NOT NULL DEFAULT 'active',
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
FOREIGN KEY (course_id) REFERENCES courses(id) ON DELETE SET NULL
)";
if ($conn->query($create_question_banks)) {
$messages[] = "question_banks table created or already exists.";
} else {
$messages[] = "Error creating question_banks table: " . $conn->error;
$success = false;
}
// Create questions table if it doesn't exist
$create_questions = "CREATE TABLE IF NOT EXISTS questions (
id INT(11) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
question_text TEXT NOT NULL,
question_type ENUM('multiple_choice', 'true_false', 'short_answer') NOT NULL DEFAULT 'multiple_choice',
question_bank_id INT(11) UNSIGNED NULL,
difficulty ENUM('easy', 'medium', 'hard') NOT NULL DEFAULT 'medium',
marks INT(11) NOT NULL DEFAULT 1,
correct_answer TEXT NOT NULL,
option_a TEXT,
option_b TEXT,
option_c TEXT,
option_d TEXT,
explanation TEXT,
status ENUM('active', 'inactive') NOT NULL DEFAULT 'active',
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
FOREIGN KEY (question_bank_id) REFERENCES question_banks(id) ON DELETE SET NULL
)";
if ($conn->query($create_questions)) {
$messages[] = "questions table created or already exists.";
} else {
$messages[] = "Error creating questions table: " . $conn->error;
$success = false;
}
// Create exam_question_maps table if it doesn't exist
$create_exam_question_maps = "CREATE TABLE IF NOT EXISTS exam_question_maps (
id INT(11) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
exam_id INT(11) UNSIGNED NOT NULL,
question_id INT(11) UNSIGNED NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (exam_id) REFERENCES exam_schedules(id) ON DELETE CASCADE,
FOREIGN KEY (question_id) REFERENCES questions(id) ON DELETE CASCADE,
UNIQUE KEY unique_exam_question (exam_id, question_id)
)";
if ($conn->query($create_exam_question_maps)) {
$messages[] = "exam_question_maps table created or already exists.";
} else {
$messages[] = "Error creating exam_question_maps table: " . $conn->error;
$success = false;
}
// Create student_exams table if it doesn't exist
$create_student_exams = "CREATE TABLE IF NOT EXISTS student_exams (
id INT(11) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
student_id INT(11) NOT NULL,
exam_id INT(11) UNSIGNED NOT NULL,
start_time DATETIME,
end_time DATETIME,
status ENUM('not_started', 'in_progress', 'completed', 'abandoned') NOT NULL DEFAULT 'not_started',
score DECIMAL(5,2) DEFAULT NULL,
percentage DECIMAL(5,2) DEFAULT NULL,
pass_status ENUM('pass', 'fail') DEFAULT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
FOREIGN KEY (student_id) REFERENCES users(id) ON DELETE CASCADE,
FOREIGN KEY (exam_id) REFERENCES exam_schedules(id) ON DELETE CASCADE
)";
if ($conn->query($create_student_exams)) {
$messages[] = "student_exams table created or already exists.";
} else {
$messages[] = "Error creating student_exams table: " . $conn->error;
$success = false;
}
// Create student_answers table if it doesn't exist
$create_student_answers = "CREATE TABLE IF NOT EXISTS student_answers (
id INT(11) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
student_exam_id INT(11) UNSIGNED NOT NULL,
question_id INT(11) UNSIGNED NOT NULL,
answer TEXT,
is_correct TINYINT(1) DEFAULT NULL,
marks_awarded DECIMAL(5,2) DEFAULT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (student_exam_id) REFERENCES student_exams(id) ON DELETE CASCADE,
FOREIGN KEY (question_id) REFERENCES questions(id) ON DELETE CASCADE
)";
if ($conn->query($create_student_answers)) {
$messages[] = "student_answers table created or already exists.";
} else {
$messages[] = "Error creating student_answers table: " . $conn->error;
$success = false;
}
// Check if we need to add question_bank_id column to questions table
// This can happen if the questions table was created before question_banks
$add_question_bank_column = false;
$check_column = $conn->query("SHOW COLUMNS FROM questions LIKE 'question_bank_id'");
if ($check_column && $check_column->num_rows == 0) {
$add_question_bank_column = true;
}
if ($add_question_bank_column) {
$alter_query = "ALTER TABLE questions ADD COLUMN question_bank_id INT(11) UNSIGNED NULL,
ADD CONSTRAINT fk_question_bank FOREIGN KEY (question_bank_id) REFERENCES question_banks(id) ON DELETE SET NULL";
if ($conn->query($alter_query)) {
$messages[] = "question_bank_id column added to questions table.";
} else {
$messages[] = "Error adding question_bank_id column: " . $conn->error;
$success = false;
}
}
// Insert a default question bank if none exists
$check_banks = $conn->query("SELECT COUNT(*) as count FROM question_banks");
$bank_count = 0;
if ($check_banks) {
$bank_count = $check_banks->fetch_assoc()['count'];
}
if ($bank_count == 0) {
$insert_default_bank = "INSERT INTO question_banks (title, description, status)
VALUES ('General Question Bank', 'Default question bank for general questions', 'active')";
if ($conn->query($insert_default_bank)) {
$messages[] = "Created default question bank.";
} else {
$messages[] = "Error creating default question bank: " . $conn->error;
$success = false;
}
}
// Set session messages
$_SESSION['setup_messages'] = $messages;
$_SESSION['setup_success'] = $success;
// Redirect back to the diagnostic page
header('Location: ../check_exam_mapping.php');
exit;
?>