Skip to content
Snippets Groups Projects
Commit c016d3b8 authored by Kris Hu's avatar Kris Hu
Browse files

update attemptFormative logic, enable to storet data to TestAttempt and QuestionAttempt tables

parent 1fe028c2
Branches
No related tags found
No related merge requests found
......@@ -844,9 +844,9 @@ def attemptTest():
@routes_bp.route('/attemptFormative/<int:id>', methods=['GET', 'POST'])
def attemptFormative(id):
student_id = session["user_id"]
test_id = id
if request.method == 'POST':
# answer snet from front end
# answer sent from front end
answers = request.form.to_dict(flat=True)
result = {
......@@ -854,6 +854,9 @@ def attemptFormative(id):
"fib": []
}
total_questions = 0
correct_answers = 0
for key, user_answer in answers.items():
if '_' in key:
qtype, qid = key.split('_')
......@@ -862,7 +865,6 @@ def attemptFormative(id):
if qtype == 'mcq':
row = MCQ.query.get(qid)
if row:
# Corresponding A/B/C/D to the actual option content
options_map = {
'A': row.option_a,
'B': row.option_b,
......@@ -870,30 +872,73 @@ def attemptFormative(id):
'D': row.option_d
}
# Get the content actually selected by the user
user_option_value = options_map.get(user_answer.strip().upper(), '')
is_correct = user_answer.strip().upper() == row.answer.strip().upper()
result["mcq"].append({
"question_id": qid,
"your_answer": user_option_value,
"answer": options_map.get(row.answer.strip().upper(), ''),
"is_correct": user_answer.strip().upper() == row.answer.strip().upper(),
"is_correct": is_correct,
"feedback": row.feedback if row.feedback != '' else ''
})
total_questions += 1
if is_correct:
correct_answers += 1
elif qtype == 'fib':
# sql = text("SELECT * FROM FIB WHERE id = :id")
# row = db.engine.execute(sql, {"id": qid}).fetchone()
row = FIB.query.get(qid)
if row:
is_correct = user_answer.strip().lower() == row.answer.strip().lower()
result["fib"].append({
"question_id": qid,
"your_answer": user_answer,
"answer": row.answer,
"is_correct": user_answer.strip().lower() == row.answer.strip().lower(),
"is_correct": is_correct,
"feedback": row.feedback if row.feedback != '' else ''
})
total_questions += 1
if is_correct:
correct_answers += 1
# Calculate score
score = round((correct_answers / total_questions) * 100, 2) if total_questions > 0 else 0.0
# Save to TestAttempt
new_attempt = TestAttempt(
student_id=student_id,
test_id=test_id,
score=score,
time_taken=0 # Set time_taken to 0, you can modify this if needed
)
new_attempt.attempt_date = datetime.now()
db.session.add(new_attempt)
db.session.commit()
# Get the latest TestAttempt (top 1 by id descending)
latest_attempt = TestAttempt.query.filter_by(student_id=student_id, test_id=test_id).order_by(TestAttempt.id.desc()).first()
# Insert QuestionAttempt records
for mcq_data in result["mcq"]:
qa = QuestionAttempt(
test_attempt_id=latest_attempt.id,
student_answer=mcq_data["your_answer"],
mcq_id=mcq_data["question_id"]
)
db.session.add(qa)
for fib_data in result["fib"]:
qa = QuestionAttempt(
test_attempt_id=latest_attempt.id,
student_answer=fib_data["your_answer"],
fib_id=fib_data["question_id"]
)
db.session.add(qa)
db.session.commit()
# Save to session, redirect to feedback page
session["formative_result"] = result
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment