File size: 9,925 Bytes
7ba3b4e
c9ca893
bad6a07
e0f928e
d924141
c9ca893
e0f928e
 
 
e9df8e8
 
 
 
 
 
795b9ba
 
169cec0
c9ca893
169cec0
c9ca893
9d5f38a
 
be0b9ff
 
a810341
 
c274a12
 
9d5f38a
 
 
32f2e18
795b9ba
 
 
 
169cec0
795b9ba
 
bad6a07
c9ca893
7ef2742
 
169cec0
 
7ef2742
 
169cec0
e9df8e8
169cec0
81e422f
169cec0
 
a26033f
81e422f
 
 
169cec0
7ef2742
169cec0
 
9d5f38a
7ef2742
4b264b4
9d5f38a
7ef2742
169cec0
9d5f38a
7ef2742
 
 
bbc7ac2
7ef2742
d3582b2
7ef2742
169cec0
 
 
32f2e18
8e6cd67
32f2e18
c9ca893
 
 
 
 
 
58956eb
7ef2742
c9ca893
 
 
58956eb
c9ca893
 
 
 
 
f1969d5
81e422f
2a4a631
f1969d5
c9ca893
 
 
1fe6266
 
 
 
81e422f
ee53acf
7ef2742
 
 
a810341
 
 
 
7ef2742
c9ca893
 
 
e9df8e8
 
58956eb
 
 
 
 
 
 
7b6a611
58956eb
 
e9df8e8
58956eb
 
 
d6d844d
a810341
e9df8e8
81e422f
c9ca893
e9df8e8
054ca33
58956eb
 
 
 
 
 
 
7ef2742
58956eb
a810341
7ef2742
58956eb
 
 
 
 
 
 
 
 
 
 
5866ecf
 
c274a12
 
 
5866ecf
 
 
 
 
a810341
 
5866ecf
a810341
 
5866ecf
 
26df557
edfbd88
 
 
5efa76f
 
c274a12
edfbd88
a810341
 
 
 
 
 
 
 
 
 
 
 
 
edfbd88
 
 
 
 
 
5866ecf
 
 
 
 
 
4b264b4
5866ecf
516f536
5866ecf
 
a810341
5866ecf
a810341
 
 
 
 
 
5866ecf
a810341
 
 
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
import streamlit as st
import os
from fpdf import FPDF
import uuid

# Initialize session state variables
if 'session_id' not in st.session_state:
    st.session_state.session_id = str(uuid.uuid4())

if 'questions' not in st.session_state:
    st.session_state.questions = []
if 'current_index' not in st.session_state:
    st.session_state.current_index = 0
if 'current_module' not in st.session_state:
    st.session_state.current_module = None
if 'correct_count' not in st.session_state:
    st.session_state.correct_count = 0
if 'module_correct_count' not in st.session_state:
    st.session_state.module_correct_count = {}
if 'module_question_count' not in st.session_state:
    st.session_state.module_question_count = {}
if 'pdf_data' not in st.session_state:
    st.session_state.pdf_data = None
if 'selected_answer' not in st.session_state:
    st.session_state.selected_answer = None
if 'answered' not in st.session_state:
    st.session_state.answered = False
if 'submit_click_count' not in st.session_state:
    st.session_state.submit_click_count = 0  # Initialize click counter

def reset_pdf_cache():
    st.session_state.pdf_data = 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 module, data in st.session_state.module_question_count.items():
        correct_count = st.session_state.module_correct_count.get(module, 0)
        total_count = data
        pdf.cell(200, 10, txt=f"Module: {module}", ln=True, align="L")
        pdf.ln(5)
        pdf.cell(200, 10, txt=f"Correct Answers: {correct_count}/{total_count}", ln=True, align="L")
        pdf.ln(5)

        for entry in st.session_state.questions:
            if entry['module'] == module:
                question, options, selected, correct, explanation, step_by_step_solution = (
                    entry['question'], 
                    entry['options'], 
                    entry['selected'] if entry['selected'] is not None else "Not Answered",
                    entry['correct_answer'], 
                    entry['explanation'],
                    entry['step_by_step_solution']
                )
                pdf.multi_cell(0, 10, f"Q: {question}")
                for option in options:
                    if option == correct:
                        pdf.set_text_color(0, 128, 0)  # Green for correct
                        pdf.multi_cell(0, 10, f"{option}")
                    elif option == selected:
                        pdf.set_text_color(255, 0, 0)  # Red for incorrect
                        pdf.multi_cell(0, 10, f"{option}")
                    else:
                        pdf.set_text_color(0, 0, 0)  # Default color for others
                        pdf.multi_cell(0, 10, f"   {option}")
                pdf.set_text_color(0, 0, 0)  # Reset color
                pdf.multi_cell(0, 10, f"Explanation: {explanation}")
                pdf.ln(5)
                pdf.multi_cell(0, 10, "Step-by-Step Solution:")
                for step in step_by_step_solution:
                    pdf.multi_cell(0, 10, step)
                pdf.ln(10)

        pdf.ln(10)  # Add space after each module

    return pdf.output(dest='S').encode('latin1', 'replace')

def load_modules():
    modules = {}
    module_dir = "modules"
    for filename in os.listdir(module_dir):
        if filename.endswith(".py") and filename != "__init__.py":
            module_name = filename[:-3]
            # Dynamically import the module only when needed
            module = __import__(f"{module_dir}.{module_name}", fromlist=[''])
            modules[module_name] = {
                "title": getattr(module, "title", module_name),
                "description": getattr(module, "description", "No description available."),
                "generate_question": module.generate_question  # Access the generate_question function
            }
    return modules

def generate_new_question(module_name, module):
    question_data = module['generate_question']()
    # Ensure 'answered' is initialized to False and add the 'module' and 'selected' keys
    question_data['answered'] = False
    question_data['module'] = module_name  # Add the module name to the question data
    question_data['selected'] = None  # Initialize 'selected' to None
    # Ensure there are exactly 4 options
    if len(question_data['options']) != 4:
        st.warning(f"Question in module '{module_name}' does not have 4 options. Found {len(question_data['options'])}.")
    
    # Reset the Submit/Next click counter when generating a new question
    st.session_state.submit_click_count = 0

    return question_data

def navigate_question(direction):
    if direction == "prev" and st.session_state.current_index > 0:
        st.session_state.current_index -= 1
        st.session_state.answered = True
    elif direction == "next" and st.session_state.current_index < len(st.session_state.questions) - 1:
        st.session_state.current_index += 1
        st.session_state.answered = True

# Load all modules dynamically
modules = load_modules()

# Streamlit interface
st.sidebar.title("Quiz Modules")
module_name = st.sidebar.radio("Choose a module:", [modules[module]["title"] for module in modules], index=0)

selected_module = None
for module in modules:
    if modules[module]["title"] == module_name:
        selected_module = module
        break

if selected_module != st.session_state.current_module:
    st.session_state.current_module = selected_module
    st.session_state.current_index = 0
    st.session_state.questions = [generate_new_question(selected_module, modules[selected_module])]
    st.session_state.module_question_count[selected_module] = 0
    st.session_state.module_correct_count[selected_module] = 0
    st.session_state.selected_answer = None
    st.session_state.answered = False

# Load the current module's question
current_question = st.session_state.questions[st.session_state.current_index]

# Display module title and description
st.title(modules[selected_module]["title"])
st.write(modules[selected_module]["description"])

# Navigation and PDF report buttons
col1, col2, col3 = st.columns([1, 1, 2])
with col1:
    if st.button("⬅️ Prev", disabled=st.session_state.current_index == 0):
        navigate_question("prev")
with col2:
    if st.button("➑️ Next", disabled=st.session_state.current_index >= len(st.session_state.questions) - 1):
        navigate_question("next")
with col3:
    if len(st.session_state.questions) > 0:
        pdf = generate_pdf_report()
        st.session_state.pdf_data = pdf  # Reset PDF cache
        st.download_button(
            label="Download PDF Report πŸ“„",
            data=st.session_state.pdf_data,
            file_name="quiz_report.pdf",
            mime="application/pdf"
        )

st.write(current_question["question"])

# Display the Submit/Next click count
st.write(f"Submit/Next button clicked {st.session_state.submit_click_count} time(s).")

# Create the form for the question
with st.form(key=f'question_form_{st.session_state.current_index}'):
    selected_answer = st.radio(
        "Choose an answer:", 
        options=current_question['options'], 
        index=current_question['options'].index(current_question['selected']) if current_question['answered'] else None, 
        key=f"question_{st.session_state.current_index}_options"
    )
    
    submit_button = st.form_submit_button(label="Submit/Next")

# Handle button state and answer submission
if submit_button:
    if selected_answer is None:
        st.warning("Please select an option before submitting.")
    else:
        # Increment the click counter as soon as the button is clicked
        st.session_state.submit_click_count += 1

        if not current_question['answered']:
            # Process the answer
            current_question['selected'] = selected_answer
            current_question['answered'] = True
            st.session_state.module_question_count[selected_module] += 1

            if selected_answer == current_question['correct_answer']:
                st.session_state.correct_count += 1
                st.session_state.module_correct_count[selected_module] += 1

            # Set answered to true to disable options
            st.session_state.questions[st.session_state.current_index]['answered'] = True
            st.session_state.selected_answer = selected_answer

        else:
            # If already answered, move to the next question
            new_question = generate_new_question(selected_module, modules[selected_module])
            st.session_state.questions.append(new_question)
            st.session_state.current_index = len(st.session_state.questions) - 1
            st.session_state.answered = False

# Show correct/incorrect feedback after submission
if current_question.get('answered', False):
    for option in current_question['options']:
        if option == current_question['correct_answer']:
            st.markdown(f"<span style='color:green;'>{option} βœ…</span>", unsafe_allow_html=True)
        elif option == current_question['selected']:
            st.markdown(f"<span style='color:red;'>{option} ❌</span>", unsafe_allow_html=True)
        else:
            st.markdown(f"{option}")
    
    # Show explanation and step-by-step solution with improved visuals
    st.write(f"**Explanation:** {current_question['explanation']}")
    st.markdown("""
    <div style='border: 2px solid #4CAF50; padding: 10px; margin-top: 20px; background-color: #f9f9f9;'>
    <h4 style='color: #4CAF50;'>Step-by-Step Solution:</h4>
    <ol>
    """, unsafe_allow_html=True)
    
    for step in current_question['step_by_step_solution']:
        st.markdown(f"<li style='margin-bottom: 10px;'>{step}</li>", unsafe_allow_html=True)
    
    st.markdown("</ol></div>", unsafe_allow_html=True)