41 lines
1.3 KiB
Python
41 lines
1.3 KiB
Python
"""CSV export helpers."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import csv
|
|
from io import StringIO
|
|
|
|
from app.db import connect
|
|
|
|
|
|
async def export_session_csv(db_path: str, sid: str) -> str:
|
|
out = StringIO()
|
|
writer = csv.writer(out)
|
|
writer.writerow(["sid", "student_id", "name", "question_idx", "answer", "elapsed_ms", "score", "status"])
|
|
async with connect(db_path) as db:
|
|
cursor = await db.execute(
|
|
"""
|
|
SELECT p.sid, p.student_id, p.name, s.question_idx, s.answer, s.elapsed_ms, s.score, s.status
|
|
FROM participants p
|
|
LEFT JOIN submissions s ON s.sid = p.sid AND s.student_id = p.student_id
|
|
WHERE p.sid = ?
|
|
ORDER BY p.student_id, s.question_idx
|
|
""",
|
|
(sid,),
|
|
)
|
|
rows = await cursor.fetchall()
|
|
for row in rows:
|
|
writer.writerow(
|
|
[
|
|
row["sid"],
|
|
row["student_id"],
|
|
row["name"],
|
|
"" if row["question_idx"] is None else row["question_idx"],
|
|
row["answer"] or "",
|
|
"" if row["elapsed_ms"] is None else row["elapsed_ms"],
|
|
"" if row["score"] is None else row["score"],
|
|
row["status"] or "",
|
|
]
|
|
)
|
|
return out.getvalue()
|