<?php
require '../config/db_connect.php';
require '../vendor/autoload.php'; // Assuming PhpSpreadsheet is installed via Composer
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
// Check if user is logged in and has appropriate role
session_start();
if (!isset($_SESSION['role']) || ($_SESSION['role'] != 'admin' && $_SESSION['role'] != 'director')) {
header('Location: ../login.php');
exit();
}
// Create a new spreadsheet
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
// Set the headers
$headers = ['question_text', 'question_type', 'options', 'correct_answer', 'marks'];
foreach ($headers as $index => $header) {
$column = chr(65 + $index); // Convert index to column letter (A, B, C, etc.)
$sheet->setCellValue($column . '1', $header);
// Set bold style for headers
$sheet->getStyle($column . '1')->getFont()->setBold(true);
}
// Sample data
$sampleData = [
[
'What is the capital of France?',
'multiple_choice',
'London|Paris|Berlin|Madrid',
'Paris',
1
],
[
'The Pacific Ocean is the largest ocean on Earth.',
'true_false',
'True|False',
'True',
1
],
[
'What is the chemical symbol for gold?',
'short_answer',
'',
'Au',
2
],
[
'Explain the water cycle and its importance to Earth\'s ecosystem.',
'essay',
'',
'',
5
]
];
// Add sample data
foreach ($sampleData as $rowIndex => $rowData) {
foreach ($rowData as $columnIndex => $cellValue) {
$column = chr(65 + $columnIndex); // Convert index to column letter
$row = $rowIndex + 2; // Start from row 2
$sheet->setCellValue($column . $row, $cellValue);
}
}
// Auto size columns
foreach (range('A', 'E') as $column) {
$sheet->getColumnDimension($column)->setAutoSize(true);
}
// Add instruction sheet
$instructionSheet = $spreadsheet->createSheet();
$instructionSheet->setTitle('Instructions');
$instructionSheet->setCellValue('A1', 'Instructions for Question Import Template');
$instructionSheet->getStyle('A1')->getFont()->setBold(true)->setSize(14);
$instructions = [
'A3' => 'This template is designed to help you import questions into the question bank.',
'A5' => 'Column Descriptions:',
'A6' => '1. question_text: The text of your question',
'A7' => '2. question_type: Type of question - must be one of: multiple_choice, true_false, short_answer, essay',
'A8' => '3. options: For multiple_choice - list options separated by pipe (|) character; for true_false - use "True|False"',
'A9' => '4. correct_answer: The correct answer for the question. For multiple_choice, it must match one of the options exactly',
'A10' => '5. marks: The number of marks/points for this question (integer)',
'A12' => 'Notes:',
'A13' => '- For short_answer questions, leave the options column empty',
'A14' => '- For essay questions, leave both options and correct_answer columns empty',
'A15' => '- Be sure to remove the sample questions before uploading your own questions',
'A16' => '- Maximum 100 questions per import'
];
foreach ($instructions as $cell => $text) {
$instructionSheet->setCellValue($cell, $text);
}
// Auto size columns for instruction sheet
$instructionSheet->getColumnDimension('A')->setWidth(100);
for ($row = 3; $row <= 16; $row++) {
$instructionSheet->getRowDimension($row)->setRowHeight(-1);
}
// Set the first sheet as active again
$spreadsheet->setActiveSheetIndex(0);
// Set response headers
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="question_import_template.xlsx"');
header('Cache-Control: max-age=0');
// Create Excel file
$writer = new Xlsx($spreadsheet);
$writer->save('php://output');
exit;
?>