<?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();
}
// Check if bank_id is provided
if (!isset($_GET['bank_id']) || empty($_GET['bank_id'])) {
$_SESSION['error'] = "Question bank ID is required.";
header("Location: banks.php");
exit();
}
$bank_id = intval($_GET['bank_id']);
// Validate question bank exists
$check_bank = $conn->prepare("SELECT title FROM question_banks WHERE id = ?");
$check_bank->bind_param("i", $bank_id);
$check_bank->execute();
$bank_result = $check_bank->get_result();
if ($bank_result->num_rows === 0) {
$_SESSION['error'] = "Question bank not found.";
header("Location: banks.php");
exit();
}
$bank = $bank_result->fetch_assoc();
$bank_title = $bank['title'];
// Get all questions for this bank
$questions_query = "SELECT * FROM questions WHERE question_bank_id = ? ORDER BY id";
$stmt = $conn->prepare($questions_query);
$stmt->bind_param("i", $bank_id);
$stmt->execute();
$questions_result = $stmt->get_result();
if ($questions_result->num_rows === 0) {
$_SESSION['error'] = "No questions found in this bank.";
header("Location: questions.php?bank_id=" . $bank_id);
exit();
}
// Set up CSV export
$filename = 'questions_export_' . preg_replace('/[^a-z0-9_-]/i', '_', $bank_title) . '_' . date('Y-m-d') . '.csv';
// Set headers for CSV download
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename="' . $filename . '"');
// Open output stream
$output = fopen('php://output', 'w');
// Define CSV headers
$csv_headers = [
'question_text',
'question_type',
'difficulty',
'marks',
'is_practice',
'explanation',
'answer_key'
];
// Add additional headers for multiple choice options (up to 10 options)
for ($i = 1; $i <= 10; $i++) {
$csv_headers[] = 'option_' . $i;
$csv_headers[] = 'correct_option_' . $i;
}
// Add header for true/false answer
$csv_headers[] = 'correct_answer';
// Write CSV header row
fputcsv($output, $csv_headers);
// Process each question
while ($question = $questions_result->fetch_assoc()) {
$row = [
'question_text' => $question['question_text'],
'question_type' => $question['question_type'],
'difficulty' => $question['difficulty'],
'marks' => $question['marks'],
'is_practice' => $question['is_practice'] ? 'Yes' : 'No',
'explanation' => $question['explanation'],
'answer_key' => '' // Will be filled for short answer questions
];
// Initialize option fields to empty
for ($i = 1; $i <= 10; $i++) {
$row['option_' . $i] = '';
$row['correct_option_' . $i] = '';
}
// Initialize true/false answer
$row['correct_answer'] = '';
// Get options for this question
$options_query = "SELECT * FROM question_options WHERE question_id = ? ORDER BY id";
$stmt = $conn->prepare($options_query);
$stmt->bind_param("i", $question['id']);
$stmt->execute();
$options_result = $stmt->get_result();
// Process based on question type
if ($question['question_type'] === 'multiple_choice') {
$option_index = 1;
while ($option = $options_result->fetch_assoc()) {
if ($option_index > 10) break; // Limit to 10 options
$row['option_' . $option_index] = $option['option_text'];
$row['correct_option_' . $option_index] = $option['is_correct'] ? 'Yes' : 'No';
$option_index++;
}
}
elseif ($question['question_type'] === 'true_false') {
// For true/false, get the correct answer
while ($option = $options_result->fetch_assoc()) {
if ($option['is_correct']) {
$row['correct_answer'] = $option['option_text']; // 'True' or 'False'
break;
}
}
}
elseif ($question['question_type'] === 'short_answer') {
// For short answer, get the answer key
if ($option = $options_result->fetch_assoc()) {
$row['answer_key'] = $option['option_text'];
}
}
// Write question row to CSV
fputcsv($output, $row);
}
// Close file handles
fclose($output);
exit();