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

<?php
// Database connection
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "popularcomputer";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

echo "<h1>Exam System Tables Check</h1>";

// 1. Check exam_questions table
$examQuestionsCheck = $conn->query("SHOW TABLES LIKE 'exam_questions'");
if ($examQuestionsCheck->num_rows == 0) {
    echo "<p>The exam_questions table does not exist. Creating it...</p>";
    
    $createExamQuestions = "
    CREATE TABLE `exam_questions` (
        `id` int(11) NOT NULL AUTO_INCREMENT,
        `exam_id` int(11) NOT NULL,
        `question_text` text NOT NULL,
        `question_type` enum('mcq','descriptive') NOT NULL DEFAULT 'mcq',
        `option1` text,
        `option2` text,
        `option3` text,
        `option4` text,
        `correct_option` int(11) DEFAULT NULL,
        `correct_answer` text,
        `mark` float NOT NULL DEFAULT '1',
        `question_order` int(11) NOT NULL DEFAULT '0',
        `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
        PRIMARY KEY (`id`),
        KEY `exam_id` (`exam_id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
    ";
    
    if ($conn->query($createExamQuestions) === TRUE) {
        echo "<p style='color:green'>Table exam_questions created successfully</p>";
    } else {
        echo "<p style='color:red'>Error creating table: " . $conn->error . "</p>";
    }
} else {
    echo "<p>The exam_questions table exists.</p>";
}

// 2. Check student_exams table
$studentExamsCheck = $conn->query("SHOW TABLES LIKE 'student_exams'");
if ($studentExamsCheck->num_rows == 0) {
    echo "<p>The student_exams table does not exist. Creating it...</p>";
    
    $createStudentExams = "
    CREATE TABLE `student_exams` (
        `id` int(11) NOT NULL AUTO_INCREMENT,
        `user_id` int(11) NOT NULL,
        `exam_id` int(11) NOT NULL,
        `start_time` datetime NOT NULL,
        `end_time` datetime DEFAULT NULL,
        `time_taken` int(11) DEFAULT NULL COMMENT 'in minutes',
        `total_questions` int(11) NOT NULL DEFAULT '0',
        `correct_answers` int(11) NOT NULL DEFAULT '0',
        `incorrect_answers` int(11) NOT NULL DEFAULT '0',
        `percentage` float DEFAULT NULL,
        `status` enum('in_progress','completed','passed','failed') NOT NULL DEFAULT 'in_progress',
        `attempt_count` int(11) NOT NULL DEFAULT '1',
        `attempt_date` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
        PRIMARY KEY (`id`),
        KEY `user_id` (`user_id`),
        KEY `exam_id` (`exam_id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
    ";
    
    if ($conn->query($createStudentExams) === TRUE) {
        echo "<p style='color:green'>Table student_exams created successfully</p>";
    } else {
        echo "<p style='color:red'>Error creating table: " . $conn->error . "</p>";
    }
} else {
    echo "<p>The student_exams table exists.</p>";
}

// 3. Check student_exam_answers table
$answersCheck = $conn->query("SHOW TABLES LIKE 'student_exam_answers'");
if ($answersCheck->num_rows == 0) {
    echo "<p>The student_exam_answers table does not exist. Creating it...</p>";
    
    $createAnswers = "
    CREATE TABLE `student_exam_answers` (
        `id` int(11) NOT NULL AUTO_INCREMENT,
        `student_exam_id` int(11) NOT NULL,
        `question_id` int(11) NOT NULL,
        `selected_option` int(11) DEFAULT NULL,
        `answer_text` text,
        `marks_awarded` float DEFAULT NULL,
        `is_correct` tinyint(1) DEFAULT NULL,
        `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
        PRIMARY KEY (`id`),
        KEY `student_exam_id` (`student_exam_id`),
        KEY `question_id` (`question_id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
    ";
    
    if ($conn->query($createAnswers) === TRUE) {
        echo "<p style='color:green'>Table student_exam_answers created successfully</p>";
    } else {
        echo "<p style='color:red'>Error creating table: " . $conn->error . "</p>";
    }
} else {
    echo "<p>The student_exam_answers table exists.</p>";
}

// 4. Check exam_certificates table
$certificatesCheck = $conn->query("SHOW TABLES LIKE 'exam_certificates'");
if ($certificatesCheck->num_rows == 0) {
    echo "<p>The exam_certificates table does not exist. Creating it...</p>";
    
    $createCertificates = "
    CREATE TABLE `exam_certificates` (
        `id` int(11) NOT NULL AUTO_INCREMENT,
        `student_exam_id` int(11) NOT NULL,
        `certificate_number` varchar(50) NOT NULL,
        `verification_code` varchar(50) NOT NULL,
        `certificate_data` text,
        `issue_date` date NOT NULL,
        `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
        PRIMARY KEY (`id`),
        UNIQUE KEY `certificate_number` (`certificate_number`),
        UNIQUE KEY `verification_code` (`verification_code`),
        KEY `student_exam_id` (`student_exam_id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
    ";
    
    if ($conn->query($createCertificates) === TRUE) {
        echo "<p style='color:green'>Table exam_certificates created successfully</p>";
    } else {
        echo "<p style='color:red'>Error creating table: " . $conn->error . "</p>";
    }
} else {
    echo "<p>The exam_certificates table exists.</p>";
}

// Create sample exam data
$examDataCheck = $conn->query("SELECT COUNT(*) as count FROM exam_schedules");
$examData = $examDataCheck->fetch_assoc();

if ($examData['count'] == 0) {
    echo "<p>Creating sample exam data...</p>";
    
    // First, check if we have any courses
    $courseCheck = $conn->query("SELECT id FROM courses LIMIT 1");
    
    if ($courseCheck->num_rows > 0) {
        $course = $courseCheck->fetch_assoc();
        $course_id = $course['id'];
        
        // Create a sample exam
        $currentDate = date('Y-m-d');
        $futureDate = date('Y-m-d', strtotime('+7 days'));
        $pastDate = date('Y-m-d', strtotime('-7 days'));
        
        // Future exam
        $insertFutureExam = "
        INSERT INTO exam_schedules (course_id, title, description, exam_date, start_time, end_time, duration, passing_percentage, attempts_allowed, allow_late_attempts, is_active)
        VALUES (?, 'Final Examination', 'This is the final examination for the course.', ?, '10:00:00', '12:00:00', 120, 60, 2, 0, 1)
        ";
        
        $stmt = $conn->prepare($insertFutureExam);
        $stmt->bind_param("is", $course_id, $futureDate);
        
        if ($stmt->execute()) {
            echo "<p style='color:green'>Created a future exam</p>";
            $futureExamId = $conn->insert_id;
            
            // Add sample questions to the future exam
            for ($i = 1; $i <= 5; $i++) {
                $insertQuestion = "
                INSERT INTO exam_questions (exam_id, question_text, question_type, option1, option2, option3, option4, correct_option, mark, question_order)
                VALUES (?, ?, 'mcq', ?, ?, ?, ?, ?, 1, ?)
                ";
                
                $questionText = "Sample Question $i for the exam";
                $option1 = "Option 1 for question $i";
                $option2 = "Option 2 for question $i";
                $option3 = "Option 3 for question $i";
                $option4 = "Option 4 for question $i";
                $correctOption = rand(1, 4);
                
                $stmt = $conn->prepare($insertQuestion);
                $stmt->bind_param("isssssii", $futureExamId, $questionText, $option1, $option2, $option3, $option4, $correctOption, $i);
                $stmt->execute();
            }
        }
        
        // Past exam
        $insertPastExam = "
        INSERT INTO exam_schedules (course_id, title, description, exam_date, start_time, end_time, duration, passing_percentage, attempts_allowed, allow_late_attempts, is_active)
        VALUES (?, 'Mid-term Examination', 'This is the mid-term examination for the course.', ?, '14:00:00', '16:00:00', 120, 50, 1, 1, 1)
        ";
        
        $stmt = $conn->prepare($insertPastExam);
        $stmt->bind_param("is", $course_id, $pastDate);
        
        if ($stmt->execute()) {
            echo "<p style='color:green'>Created a past exam</p>";
            $pastExamId = $conn->insert_id;
            
            // Add sample questions to the past exam
            for ($i = 1; $i <= 5; $i++) {
                $insertQuestion = "
                INSERT INTO exam_questions (exam_id, question_text, question_type, option1, option2, option3, option4, correct_option, mark, question_order)
                VALUES (?, ?, 'mcq', ?, ?, ?, ?, ?, 1, ?)
                ";
                
                $questionText = "Mid-term Question $i";
                $option1 = "Option 1 for mid-term question $i";
                $option2 = "Option 2 for mid-term question $i";
                $option3 = "Option 3 for mid-term question $i";
                $option4 = "Option 4 for mid-term question $i";
                $correctOption = rand(1, 4);
                
                $stmt = $conn->prepare($insertQuestion);
                $stmt->bind_param("isssssii", $pastExamId, $questionText, $option1, $option2, $option3, $option4, $correctOption, $i);
                $stmt->execute();
            }
        }
    } else {
        echo "<p style='color:orange'>No courses found in the database. Please create courses first before creating exams.</p>";
    }
}

// Close connection
$conn->close();

echo "<h3>Table setup complete. You can now navigate to:</h3>";
echo "<ul>
    <li><a href='student/scheduled-exams.php'>Scheduled Exams</a></li>
    <li><a href='student/exam-results.php'>Exam Results</a></li>
    <li><a href='student/previous-exams.php'>Previous Exams</a></li>
</ul>";
?>