elkay frontend quiz.py
Browse files- phase/Student_view/quiz.py +36 -13
phase/Student_view/quiz.py
CHANGED
|
@@ -1,35 +1,60 @@
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
from utils.quizdata import quizzes_data
|
| 3 |
import datetime
|
| 4 |
import json
|
| 5 |
from utils import db as dbapi
|
|
|
|
| 6 |
|
|
|
|
| 7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
def _load_quiz_obj(quiz_id):
|
| 11 |
"""
|
| 12 |
Return a normalized quiz object from either quizzes_data (built-in)
|
| 13 |
-
or the DB
|
| 14 |
{"title": str, "questions": [{"question","options","answer","points"}...]}
|
| 15 |
-
For DB rows, convert answer_key letters (e.g., "A") into the option text.
|
| 16 |
"""
|
| 17 |
-
#
|
| 18 |
if quiz_id in quizzes_data:
|
| 19 |
q = quizzes_data[quiz_id]
|
| 20 |
-
# ensure each question has points
|
| 21 |
for qq in q.get("questions", []):
|
| 22 |
qq.setdefault("points", 1)
|
| 23 |
return q
|
| 24 |
|
| 25 |
-
#
|
| 26 |
-
data =
|
| 27 |
if not data:
|
| 28 |
return {"title": f"Quiz {quiz_id}", "questions": []}
|
| 29 |
|
| 30 |
items_out = []
|
| 31 |
for it in (data.get("items") or []):
|
| 32 |
-
# decode JSON if needed
|
| 33 |
opts = it.get("options")
|
| 34 |
if isinstance(opts, (str, bytes)):
|
| 35 |
try:
|
|
@@ -41,15 +66,13 @@ def _load_quiz_obj(quiz_id):
|
|
| 41 |
ans = it.get("answer_key")
|
| 42 |
if isinstance(ans, (str, bytes)):
|
| 43 |
try:
|
| 44 |
-
ans = json.loads(ans)
|
| 45 |
except Exception:
|
| 46 |
-
# allow
|
| 47 |
-
pass
|
| 48 |
|
| 49 |
-
# convert letter(s) -> option text
|
| 50 |
def letter_to_text(letter):
|
| 51 |
if isinstance(letter, str):
|
| 52 |
-
idx = ord(letter.upper()) - 65
|
| 53 |
return opts[idx] if 0 <= idx < len(opts) else letter
|
| 54 |
return letter
|
| 55 |
|
|
@@ -297,7 +320,7 @@ def show_results(quiz_id):
|
|
| 297 |
if isinstance(quiz_id, int):
|
| 298 |
assignment_id = st.session_state.get("current_assignment")
|
| 299 |
if assignment_id:
|
| 300 |
-
|
| 301 |
student_id=st.session_state.user["user_id"],
|
| 302 |
assignment_id=assignment_id,
|
| 303 |
quiz_id=quiz_id,
|
|
|
|
| 1 |
+
import os
|
| 2 |
import streamlit as st
|
| 3 |
from utils.quizdata import quizzes_data
|
| 4 |
import datetime
|
| 5 |
import json
|
| 6 |
from utils import db as dbapi
|
| 7 |
+
import utils.api as api
|
| 8 |
|
| 9 |
+
USE_LOCAL_DB = os.getenv("DISABLE_DB", "1") != "1"
|
| 10 |
|
| 11 |
+
def _get_quiz_from_source(quiz_id: int):
|
| 12 |
+
"""
|
| 13 |
+
Fetch a quiz payload from the local DB (if enabled) or from the backend API.
|
| 14 |
+
Expected backend shape: {'quiz': {...}, 'items': [...]}
|
| 15 |
+
"""
|
| 16 |
+
if USE_LOCAL_DB and hasattr(dbapi, "get_quiz"):
|
| 17 |
+
return dbapi.get_quiz(quiz_id)
|
| 18 |
+
# backend: expose GET /quizzes/{quiz_id}
|
| 19 |
+
return api.get_quiz(quiz_id)
|
| 20 |
|
| 21 |
+
def _submit_quiz_result(student_id: int, assignment_id: int, quiz_id: int,
|
| 22 |
+
score: int, total: int, details: dict):
|
| 23 |
+
"""
|
| 24 |
+
Submit a quiz result either to the local DB or to the backend API.
|
| 25 |
+
"""
|
| 26 |
+
if USE_LOCAL_DB and hasattr(dbapi, "submit_quiz"):
|
| 27 |
+
return dbapi.submit_quiz(student_id=student_id,
|
| 28 |
+
assignment_id=assignment_id,
|
| 29 |
+
quiz_id=quiz_id,
|
| 30 |
+
score=score, total=total, details=details)
|
| 31 |
+
# backend: POST /quizzes/submit (or your route of choice)
|
| 32 |
+
# utils.api should wrap that route; below assumes api.submit_quiz exists.
|
| 33 |
+
return api.submit_quiz(student_id=student_id,
|
| 34 |
+
assignment_id=assignment_id,
|
| 35 |
+
quiz_id=quiz_id,
|
| 36 |
+
score=score, total=total, details=details)
|
| 37 |
|
| 38 |
def _load_quiz_obj(quiz_id):
|
| 39 |
"""
|
| 40 |
Return a normalized quiz object from either quizzes_data (built-in)
|
| 41 |
+
or the backend/DB. Normalized shape:
|
| 42 |
{"title": str, "questions": [{"question","options","answer","points"}...]}
|
|
|
|
| 43 |
"""
|
| 44 |
+
# Built-ins first
|
| 45 |
if quiz_id in quizzes_data:
|
| 46 |
q = quizzes_data[quiz_id]
|
|
|
|
| 47 |
for qq in q.get("questions", []):
|
| 48 |
qq.setdefault("points", 1)
|
| 49 |
return q
|
| 50 |
|
| 51 |
+
# Teacher-assigned (DB/backend)
|
| 52 |
+
data = _get_quiz_from_source(int(quiz_id)) # <-- uses API when DISABLE_DB=1
|
| 53 |
if not data:
|
| 54 |
return {"title": f"Quiz {quiz_id}", "questions": []}
|
| 55 |
|
| 56 |
items_out = []
|
| 57 |
for it in (data.get("items") or []):
|
|
|
|
| 58 |
opts = it.get("options")
|
| 59 |
if isinstance(opts, (str, bytes)):
|
| 60 |
try:
|
|
|
|
| 66 |
ans = it.get("answer_key")
|
| 67 |
if isinstance(ans, (str, bytes)):
|
| 68 |
try:
|
| 69 |
+
ans = json.loads(ans) # support '["A","C"]'
|
| 70 |
except Exception:
|
| 71 |
+
pass # allow "A"
|
|
|
|
| 72 |
|
|
|
|
| 73 |
def letter_to_text(letter):
|
| 74 |
if isinstance(letter, str):
|
| 75 |
+
idx = ord(letter.upper()) - 65
|
| 76 |
return opts[idx] if 0 <= idx < len(opts) else letter
|
| 77 |
return letter
|
| 78 |
|
|
|
|
| 320 |
if isinstance(quiz_id, int):
|
| 321 |
assignment_id = st.session_state.get("current_assignment")
|
| 322 |
if assignment_id:
|
| 323 |
+
_submit_quiz_result(
|
| 324 |
student_id=st.session_state.user["user_id"],
|
| 325 |
assignment_id=assignment_id,
|
| 326 |
quiz_id=quiz_id,
|