File size: 5,396 Bytes
7ba3b4e
795b9ba
bad6a07
d924141
795b9ba
 
 
 
 
 
 
 
 
 
 
2a56f14
7b17e7d
795b9ba
 
 
 
 
 
 
 
 
 
97a8822
 
 
 
 
 
 
 
9124e0f
 
32f2e18
795b9ba
 
 
 
d924141
795b9ba
 
bad6a07
795b9ba
97a8822
795b9ba
97a8822
 
7b17e7d
97a8822
7b17e7d
97a8822
 
795b9ba
 
32f2e18
7b17e7d
32f2e18
795b9ba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32f2e18
97a8822
 
795b9ba
97a8822
9124e0f
795b9ba
 
 
 
7b17e7d
97a8822
7b17e7d
 
bad6a07
7b17e7d
 
 
9124e0f
 
 
97a8822
 
 
 
 
 
 
 
 
 
 
bad6a07
97a8822
 
 
 
 
 
 
a1b650a
97a8822
 
7b17e7d
 
 
 
 
 
 
9124e0f
795b9ba
 
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
import streamlit as st
import importlib
from fpdf import FPDF

# List of available modules with shorter names and icons
module_names = {
    "Bases": "presentation_bases",
    "Validity": "valid_invalid_numbers",
    "Conversion": "conversion_bases",
    "Grouping": "grouping_techniques",
    "Addition": "addition_bases",
    "2's Complement": "twos_complement",
    "Negative Numbers": "negative_binary",
    "Subtraction": "subtraction_bases",
}

# Initialize session state variables
if 'correct_count' not in st.session_state:
    st.session_state.correct_count = 0
if 'question_count' not in st.session_state:
    st.session_state.question_count = 0
if 'current_module' not in st.session_state:
    st.session_state.current_module = None
if 'question_queue' not in st.session_state:
    st.session_state.question_queue = []
if 'submitted' not in st.session_state:
    st.session_state.submitted = False
if 'current_question' not in st.session_state:
    st.session_state.current_question = None
if 'options' not in st.session_state:
    st.session_state.options = []
if 'correct_answer' not in st.session_state:
    st.session_state.correct_answer = None
if 'explanation' not in st.session_state:
    st.session_state.explanation = None
if 'selected_answer' not in st.session_state:
    st.session_state.selected_answer = None

def generate_pdf_report():
    pdf = FPDF()
    pdf.add_page()
    pdf.set_font("Arial", size=12)

    pdf.cell(200, 10, txt="Quiz Report", ln=True, align="C")
    pdf.ln(10)

    for idx, entry in enumerate(st.session_state.question_queue):
        question, options, selected, correct, explanation = entry
        pdf.multi_cell(0, 10, f"Q{idx+1}: {question}")
        for option in options:
            if option == correct:
                pdf.multi_cell(0, 10, f"{option} (Correct)")
            elif option == selected:
                pdf.multi_cell(0, 10, f"{option} (Your Choice)")
            else:
                pdf.multi_cell(0, 10, f"   {option}")
        pdf.multi_cell(0, 10, f"Explanation: {explanation}")
        pdf.ln(10)

    return pdf.output(dest='S').encode('utf-8')

# Streamlit interface
st.sidebar.title("Quiz Modules")
module_name = st.sidebar.radio("Choose a module:", list(module_names.keys()), index=0)

if module_name:
    module_file = module_names[module_name]
    try:
        module = importlib.import_module(f'modules.{module_file}')
        generate_question = module.generate_question
        title = getattr(module, 'title', f"{module_name} Module")
        description = getattr(module, 'description', "Description is not available")
        
        # Show module title and description
        st.title(title)
        st.write(description)

        if st.session_state.current_question is None or st.session_state.submitted:
            st.session_state.current_question, st.session_state.options, st.session_state.correct_answer, st.session_state.explanation = generate_question()
            st.session_state.submitted = False
            st.session_state.selected_answer = None

        if st.session_state.question_count > 0:
            correct_percentage = (st.session_state.correct_count / st.session_state.question_count) * 100
            st.write(f"**Correct Answers:** {st.session_state.correct_count}/{st.session_state.question_count} ({correct_percentage:.2f}%)")

        st.write(f"<div style='border: 2px solid #ccc; padding: 15px; background-color: #f9f9f9;'>", unsafe_allow_html=True)
        st.write(f"**Question {st.session_state.question_count + 1}:** {st.session_state.current_question}")
        st.session_state.selected_answer = st.radio("Choose an answer:", st.session_state.options)
        st.write("</div>", unsafe_allow_html=True)

        col1, col2 = st.columns([1, 4])
        with col1:
            if st.session_state.question_count > 0:
                if st.button("⬅️ Prev"):
                    # Logic for previous question (if applicable)
                    pass
        with col2:
            if st.button("Submit"):
                st.session_state.submitted = True
                st.session_state.question_count += 1
                st.session_state.question_queue.append((
                    st.session_state.current_question, 
                    st.session_state.options, 
                    st.session_state.selected_answer, 
                    st.session_state.correct_answer, 
                    st.session_state.explanation
                ))

                if st.session_state.selected_answer == st.session_state.correct_answer:
                    st.session_state.correct_count += 1
                    st.success("βœ… Correct!")
                else:
                    st.error("❌ Incorrect.")
                st.write(f"**Explanation:** {st.session_state.explanation}")
                st.session_state.current_question = None
                st.experimental_set_query_params(dummy_param=True)

        if st.session_state.submitted and st.session_state.question_count > 0:
            pdf = generate_pdf_report()
            st.download_button(
                label="Download PDF Report πŸ“„",
                data=pdf,
                file_name="quiz_report.pdf",
                mime="application/pdf"
            )

    except ModuleNotFoundError:
        st.error(f"The module '{module_name}' was not found. Please select another module.")