Dash-math_Database / firebase_quiz_hooks.py
Mochi0622's picture
initial deploy
e66cfb4
Raw
History Blame Contribute Delete
2.81 kB
"""
firebase_quiz_hooks.py
----------------------
答題記錄已移至 dt_callbacks.py 直接處理。
這裡只保留解法展開和 AI 提示的記錄。
"""
from __future__ import annotations
from dash import callback, Input, Output, State, ALL, no_update, ctx, dcc
def create_dummy_stores():
return [dcc.Store(id="_fq-log", data={})]
@callback(
Output("_fq-log", "data"),
Input({"type": "solution-toggle", "key": ALL}, "n_clicks"),
State({"type": "solution-toggle", "key": ALL}, "id"),
State("student-id-store", "data"),
State("page-url", "pathname"),
prevent_initial_call=True,
)
def record_solution(n_clicks_list, ids, student_id, pathname):
if not student_id:
return no_update
trigger = ctx.triggered_id
if not trigger or not isinstance(trigger, dict):
return no_update
key = trigger.get("key", "")
unit = (pathname or "").strip("/").replace("/", "_") or "unknown"
for i, pid in enumerate(ids):
if isinstance(pid, dict) and pid.get("key") == key:
if (n_clicks_list[i] or 0) == 1:
try:
from firebase_progress import log_solution_viewed
log_solution_viewed(student_id, unit, key)
except Exception as e:
print(f"[Firebase] log_solution_viewed 失敗:{e}")
break
return no_update
@callback(
Output("_fq-log", "data", allow_duplicate=True),
Input({"type": "quiz-hint", "qid": ALL}, "n_clicks"),
Input({"type": "quiz-hint-choice", "qid": ALL}, "n_clicks"),
State({"type": "quiz-hint", "qid": ALL}, "id"),
State({"type": "quiz-hint-choice", "qid": ALL}, "id"),
State("student-id-store", "data"),
State("page-url", "pathname"),
prevent_initial_call=True,
)
def record_hint(fill_clicks, choice_clicks, fill_ids, choice_ids, student_id, pathname):
if not student_id:
return no_update
trigger = ctx.triggered_id
if not trigger or not isinstance(trigger, dict):
return no_update
qid = trigger.get("qid", "")
qtype = trigger.get("type", "")
if not qid or "hint" not in qtype:
return no_update
unit = (pathname or "").strip("/").replace("/", "_") or "unknown"
ids = fill_ids if qtype == "quiz-hint" else choice_ids
clicks = fill_clicks if qtype == "quiz-hint" else choice_clicks
for i, pid in enumerate(ids):
if isinstance(pid, dict) and pid.get("qid") == qid:
if (clicks[i] or 0) == 1:
try:
from firebase_progress import log_hint_used
log_hint_used(student_id, unit, qid)
except Exception as e:
print(f"[Firebase] log_hint_used 失敗:{e}")
break
return no_update