<?php
// Test script to check if exam results can be retrieved
require_once 'config/database.php';

$online_exam_id = 1;
$student_id = 5642;

try {
    // Fetch student's responses for this online exam (same query as in exam_results.php)
    echo "Fetching responses for exam_id: $online_exam_id, student_id: $student_id\n";
    $stmt = $pdo->prepare("SELECT sr.*, q.question_text, q.correct_answer, q.marks as question_marks FROM online_exam_student_responses sr JOIN online_exam_questions q ON sr.question_id = q.id WHERE sr.online_exam_id = ? AND sr.student_id = ? ORDER BY q.id ASC");
    $stmt->execute([$online_exam_id, $student_id]);
    $student_responses = $stmt->fetchAll();
    
    echo "Found " . count($student_responses) . " responses\n";
    
    foreach ($student_responses as $index => $response) {
        echo "Question " . ($index + 1) . ": " . substr($response['question_text'], 0, 30) . "...\n";
        echo "  Your answer: " . ($response['selected_option'] ?? $response['written_answer']) . "\n";
        echo "  Correct answer: " . $response['correct_answer'] . "\n";
        echo "  Marks obtained: " . $response['marks_obtained'] . "/" . $response['question_marks'] . "\n\n";
    }
    
    // Test admin/teacher statistics query (same as in exam_results.php)
    echo "Testing admin statistics query:\n";
    $stmt = $pdo->prepare("SELECT COUNT(DISTINCT student_id) as participant_count FROM online_exam_student_responses WHERE online_exam_id = ?");
    $stmt->execute([$online_exam_id]);
    $participant_data = $stmt->fetch();
    $participant_count = $participant_data['participant_count'] ?? 0;
    echo "Participant count: $participant_count\n";
    
} catch (Exception $e) {
    echo "Error: " . $e->getMessage() . "\n";
}
?>