File size: 8,660 Bytes
c7d096e 8b65a0f c7d096e 8b65a0f c7d096e 8b65a0f c7d096e 8b65a0f c7d096e 8b65a0f c7d096e 8b65a0f c7d096e 8b65a0f c7d096e 8b65a0f c7d096e 8b65a0f c7d096e 8b65a0f c7d096e 8b65a0f c7d096e 8b65a0f c7d096e 8b65a0f c7d096e 8b65a0f c7d096e 8b65a0f c7d096e 8b65a0f c7d096e 8b65a0f c7d096e 8b65a0f c7d096e 8b65a0f c7d096e 8b65a0f c7d096e 8b65a0f c7d096e 8b65a0f c7d096e 8b65a0f c7d096e 8b65a0f c7d096e 8b65a0f c7d096e 8b65a0f c7d096e 8b65a0f c7d096e 8b65a0f c7d096e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 | import { Document, Packer, Paragraph, TextRun, HeadingLevel, AlignmentType } from 'docx';
import { GeneratedQuestion } from '@/types/quiz';
export async function exportQuestionsToDocx(
questions: GeneratedQuestion[],
filename: string = 'quiz-questions.docx',
includeAnswers: boolean = true
) {
// Create document
const doc = new Document({
sections: [{
properties: {},
children: [
// Title
new Paragraph({
text: 'Quiz Questions',
heading: HeadingLevel.HEADING_1,
alignment: AlignmentType.CENTER,
spacing: {
after: 400,
}
}),
// Date created
new Paragraph({
text: `Generated: ${new Date().toLocaleDateString()}`,
alignment: AlignmentType.CENTER,
spacing: {
after: 200
}
}),
// Total questions
new Paragraph({
text: `Total Questions: ${questions.length}`,
alignment: AlignmentType.CENTER,
spacing: {
after: 300
}
}),
// Questions
...questions.map((question, index) =>
renderQuestion(question, index + 1, includeAnswers)
).flat()
]
}]
});
// Generate and download the document
const blob = await Packer.toBlob(doc);
const url = URL.createObjectURL(blob);
const link = document.createElement('a');
link.href = url;
link.download = filename;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
URL.revokeObjectURL(url);
}
function renderQuestion(question: GeneratedQuestion, questionNumber: number, includeAnswers: boolean = true): Paragraph[] {
const elements: Paragraph[] = [];
// Question number and type
elements.push(
new Paragraph({
children: [
new TextRun({
text: `Question ${questionNumber}`,
bold: true,
size: 24
}),
new TextRun({
text: ` (${question.type})`,
size: 20,
color: '666666'
})
],
spacing: {
before: 300,
after: 150
}
})
);
// Question stem
elements.push(
new Paragraph({
children: [
new TextRun({
text: question.stem,
size: 22
})
],
spacing: {
after: 200
}
})
);
// Points
elements.push(
new Paragraph({
children: [
new TextRun({
text: `Points: ${question.points}`,
size: 18,
italics: true,
color: '666666'
})
],
spacing: {
after: 150
}
})
);
// Question content
elements.push(...renderQuestionContent(question, includeAnswers));
// Separator
elements.push(
new Paragraph({
children: [
new TextRun({
text: '─'.repeat(50),
color: 'CCCCCC'
})
],
spacing: {
before: 200,
after: 200
}
})
);
return elements;
}
function renderQuestionContent(question: GeneratedQuestion, includeAnswers: boolean = true): Paragraph[] {
const elements: Paragraph[] = [];
// Handle the actual content structure for multiple choice questions
if (question.content && typeof question.content === 'object') {
// Handle multiple choice questions with the actual structure
if ('Options' in question.content && question.content.Options) {
// Options is an object with keys A, B, C, D
const options = question.content.Options as { A: string; B: string; C: string; D: string; };
const correctAnswer = (question.content as { Answer?: string }).Answer;
Object.entries(options).forEach(([key, value]) => {
const isCorrect = key === correctAnswer;
elements.push(
new Paragraph({
children: [
new TextRun({
text: `${key}. ${value}`,
size: 20,
color: includeAnswers && isCorrect ? '008000' : '000000',
bold: includeAnswers && isCorrect
})
],
spacing: {
after: 100
}
})
);
});
// Add answer key if including answers
if (includeAnswers && correctAnswer) {
elements.push(
new Paragraph({
children: [
new TextRun({
text: `Answer: ${correctAnswer}`,
bold: true,
size: 20,
color: '008000'
})
],
spacing: {
before: 200,
after: 200
}
})
);
}
}
}
return elements;
}
export async function exportQuestionsToJson(questions: GeneratedQuestion[], filename: string = 'quiz-questions.json') {
const data = {
metadata: {
exportDate: new Date().toISOString(),
totalQuestions: questions.length,
totalPoints: questions.reduce((sum, q) => sum + q.points, 0)
},
questions: questions.map(q => ({
id: q.id,
type: q.type,
stem: q.stem,
content: q.content,
points: q.points,
createdAt: q.createdAt
}))
};
const blob = new Blob([JSON.stringify(data, null, 2)], { type: 'application/json' });
const url = URL.createObjectURL(blob);
const link = document.createElement('a');
link.href = url;
link.download = filename;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
URL.revokeObjectURL(url);
}
export async function exportQuestionsToMarkdown(
questions: GeneratedQuestion[],
filename: string = 'quiz-questions.md',
includeAnswers: boolean = true
) {
let markdown = '# Quiz Questions\n\n';
markdown += `Generated: ${new Date().toLocaleDateString()}\n\n`;
markdown += `Total Questions: ${questions.length}\n\n`;
markdown += '---\n\n';
questions.forEach((question, index) => {
markdown += `## Question ${index + 1} (${question.type})\n\n`;
markdown += `**Points:** ${question.points}\n\n`;
markdown += `${question.stem}\n\n`;
if (question.content && typeof question.content === 'object') {
if ('Options' in question.content && question.content.Options) {
const options = question.content.Options as { A: string; B: string; C: string; D: string; };
const correctAnswer = (question.content as { Answer?: string }).Answer;
Object.entries(options).forEach(([key, value]) => {
const isCorrect = includeAnswers && key === correctAnswer;
markdown += `${key}. ${value}${isCorrect ? ' ✓' : ''}\n`;
});
if (includeAnswers && correctAnswer) {
markdown += `\n**Answer:** ${correctAnswer}\n`;
}
}
}
markdown += '\n---\n\n';
});
const blob = new Blob([markdown], { type: 'text/markdown' });
const url = URL.createObjectURL(blob);
const link = document.createElement('a');
link.href = url;
link.download = filename;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
URL.revokeObjectURL(url);
}
export async function exportQuestionsToCsv(questions: GeneratedQuestion[], filename: string = 'quiz-questions.csv') {
// CSV header
let csv = 'Question Number,Type,Points,Question,Option A,Option B,Option C,Option D,Correct Answer\n';
questions.forEach((question, index) => {
const row = [];
row.push(index + 1);
row.push(question.type);
row.push(question.points);
row.push(`"${question.stem.replace(/"/g, '""')}"`);
if (question.content && typeof question.content === 'object' && 'Options' in question.content) {
const options = question.content.Options as { A: string; B: string; C: string; D: string; };
const correctAnswer = (question.content as { Answer?: string }).Answer;
row.push(`"${options.A.replace(/"/g, '""')}"`);
row.push(`"${options.B.replace(/"/g, '""')}"`);
row.push(`"${options.C.replace(/"/g, '""')}"`);
row.push(`"${options.D.replace(/"/g, '""')}"`);
row.push(correctAnswer || '');
} else {
// Add empty cells for non-multiple choice questions
row.push('', '', '', '', '');
}
csv += row.join(',') + '\n';
});
const blob = new Blob([csv], { type: 'text/csv' });
const url = URL.createObjectURL(blob);
const link = document.createElement('a');
link.href = url;
link.download = filename;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
URL.revokeObjectURL(url);
} |