File size: 2,813 Bytes
e66cfb4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
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