Harshal Vhatkar
commited on
Commit
·
0162dc3
1
Parent(s):
9987aed
fix practice quiz
Browse files- gen_mcqs.py +1 -0
- session_page.py +193 -100
gen_mcqs.py
CHANGED
@@ -337,6 +337,7 @@ def save_quiz(course_id, session_id, title, questions, user_id):
|
|
337 |
"session_id": session_id,
|
338 |
"title": title,
|
339 |
"questions": questions,
|
|
|
340 |
"created_at": datetime.utcnow(),
|
341 |
"status": "active",
|
342 |
"submissions": []
|
|
|
337 |
"session_id": session_id,
|
338 |
"title": title,
|
339 |
"questions": questions,
|
340 |
+
"duration_minutes": 15,
|
341 |
"created_at": datetime.utcnow(),
|
342 |
"status": "active",
|
343 |
"submissions": []
|
session_page.py
CHANGED
@@ -568,7 +568,6 @@ def display_preclass_content(session, student_id, course_id):
|
|
568 |
st.session_state.messages = []
|
569 |
|
570 |
if st.session_state.user_type == 'student':
|
571 |
-
display_pre_class_quiz_tab(student_id, course_id, session["session_id"])
|
572 |
display_pre_subjective_test_tab(student_id, course_id, session["session_id"])
|
573 |
|
574 |
st.subheader("Create a Practice Quiz")
|
@@ -601,128 +600,222 @@ def display_preclass_content(session, student_id, course_id):
|
|
601 |
else:
|
602 |
st.error("Error generating questions.")
|
603 |
|
604 |
-
# if st.button("Attempt Practice Quizzes "):
|
605 |
-
|
606 |
|
607 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
608 |
if getattr(st.session_state, 'show_quizzes', False):
|
609 |
-
# quiz = quizzes_collection.find_one({"course_id": course_id, "session_id": session['session_id'], "user_id": student_id})
|
610 |
quiz = quizzes_collection.find_one(
|
611 |
-
{
|
|
|
|
|
|
|
|
|
612 |
sort=[("created_at", -1)]
|
613 |
)
|
614 |
if not quiz:
|
615 |
st.info("No practice quizzes created.")
|
616 |
else:
|
617 |
-
|
618 |
-
|
619 |
-
|
|
|
|
|
620 |
|
621 |
-
|
622 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
623 |
|
624 |
-
# Display correct answers after submission
|
625 |
-
st.subheader("Quiz Review")
|
626 |
for i, question in enumerate(quiz['questions']):
|
627 |
-
st.markdown(f"**
|
628 |
-
|
629 |
-
|
630 |
-
|
631 |
-
|
632 |
-
|
633 |
-
|
634 |
-
|
635 |
-
|
636 |
-
|
637 |
-
if quiz_key not in st.session_state:
|
638 |
-
st.session_state[quiz_key] = {
|
639 |
-
'submitted': False,
|
640 |
-
'score': None,
|
641 |
-
'answers': {}
|
642 |
-
}
|
643 |
-
|
644 |
-
# If quiz was just submitted, show the results
|
645 |
-
if st.session_state[quiz_key]['submitted']:
|
646 |
-
st.success(f"Quiz submitted successfully! Your score: {st.session_state[quiz_key]['score']:.1f}%")
|
647 |
-
# Reset the quiz state
|
648 |
-
st.session_state[quiz_key]['submitted'] = False
|
649 |
|
|
|
650 |
|
651 |
-
|
652 |
-
|
653 |
-
|
654 |
-
# Create a form for quiz submission
|
655 |
-
form_key = f"quiz_form_{quiz['_id']}_student_{student_id}"
|
656 |
-
with st.form(key=form_key):
|
657 |
-
student_answers = {}
|
658 |
-
|
659 |
-
for i, question in enumerate(quiz['questions']):
|
660 |
-
st.markdown(f"**Question {i+1}:** {question['question']}")
|
661 |
-
options = [opt for opt in question['options']]
|
662 |
-
# student_answers[str(i)] = st.radio(
|
663 |
-
# f"Select answer for question {i+1}:",
|
664 |
-
# options=options,
|
665 |
-
# key=f"q_{i}",
|
666 |
-
# index=None
|
667 |
-
# )
|
668 |
-
answer = st.radio(
|
669 |
-
f"Select answer for question {i+1}:",
|
670 |
-
options=options,
|
671 |
-
key=f"{quiz['_id']}_{i}", # Simplify the radio button key
|
672 |
-
index=None
|
673 |
-
)
|
674 |
-
if answer: # Only add to answers if a selection was made
|
675 |
-
student_answers[str(i)] = answer
|
676 |
-
|
677 |
-
# Submit button
|
678 |
-
# submitted = st.form_submit_button("Submit Quiz")
|
679 |
-
print("Before the submit button")
|
680 |
-
submit_button = st.form_submit_button("Submit Quiz")
|
681 |
-
print("After the submit button")
|
682 |
-
if submit_button and student_answers:
|
683 |
-
print("Clicked the button")
|
684 |
-
print(student_answers)
|
685 |
-
correct_answers = 0
|
686 |
-
for i, question in enumerate(quiz['questions']):
|
687 |
-
if student_answers[str(i)] == question['correct_option']:
|
688 |
-
correct_answers += 1
|
689 |
-
score = (correct_answers / len(quiz['questions'])) * 100
|
690 |
-
|
691 |
-
if score is not None:
|
692 |
-
st.success(f"Quiz submitted successfully! Your score: {score:.1f}%")
|
693 |
-
st.session_state[quiz_key]['submitted'] = True
|
694 |
-
st.session_state[quiz_key]['score'] = score
|
695 |
-
st.session_state[quiz_key]['answers'] = student_answers
|
696 |
-
# This will trigger a rerun, but now we'll handle it properly
|
697 |
-
st.rerun()
|
698 |
-
|
699 |
else:
|
700 |
-
|
701 |
-
|
702 |
-
|
703 |
-
|
704 |
-
|
705 |
-
|
706 |
-
|
707 |
-
|
708 |
-
|
709 |
-
|
710 |
-
|
711 |
-
|
712 |
-
|
713 |
-
|
714 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
715 |
|
|
|
716 |
def display_pre_class_quiz_tab(student_id, course_id, session_id):
|
717 |
"""Display pre-class quizzes for students"""
|
718 |
st.markdown("### Pre-class Quizzes")
|
719 |
-
|
720 |
-
# Get available quizzes
|
721 |
quizzes = quizzes_collection.find({
|
722 |
"course_id": course_id,
|
723 |
"session_id": session_id,
|
724 |
-
"status": "active"
|
|
|
725 |
})
|
|
|
|
|
|
|
|
|
|
|
|
|
726 |
|
727 |
quizzes = list(quizzes)
|
728 |
if not quizzes:
|
|
|
568 |
st.session_state.messages = []
|
569 |
|
570 |
if st.session_state.user_type == 'student':
|
|
|
571 |
display_pre_subjective_test_tab(student_id, course_id, session["session_id"])
|
572 |
|
573 |
st.subheader("Create a Practice Quiz")
|
|
|
600 |
else:
|
601 |
st.error("Error generating questions.")
|
602 |
|
603 |
+
# # if st.button("Attempt Practice Quizzes "):
|
604 |
+
# # quizzes = list(quizzes_collection.find({"course_id": course_id, "session_id": session['session_id'], "user_id": student_id}))
|
605 |
|
606 |
|
607 |
+
# if getattr(st.session_state, 'show_quizzes', False):
|
608 |
+
# # quiz = quizzes_collection.find_one({"course_id": course_id, "session_id": session['session_id'], "user_id": student_id})
|
609 |
+
# quiz = quizzes_collection.find_one(
|
610 |
+
# {"course_id": course_id, "session_id": session['session_id'], "user_id": student_id},
|
611 |
+
# sort=[("created_at", -1)]
|
612 |
+
# )
|
613 |
+
# if not quiz:
|
614 |
+
# st.info("No practice quizzes created.")
|
615 |
+
# else:
|
616 |
+
# with st.expander(f"📝 Practice Quiz", expanded=False):
|
617 |
+
# # Check if student has already taken this quiz
|
618 |
+
# existing_score = get_student_quiz_score(quiz['_id'], student_id)
|
619 |
+
|
620 |
+
# if existing_score is not None:
|
621 |
+
# st.success(f"Quiz completed! Your score: {existing_score:.1f}%")
|
622 |
+
|
623 |
+
# # Display correct answers after submission
|
624 |
+
# st.subheader("Quiz Review")
|
625 |
+
# for i, question in enumerate(quiz['questions']):
|
626 |
+
# st.markdown(f"**Question {i+1}:** {question['question']}")
|
627 |
+
# for opt in question['options']:
|
628 |
+
# if opt.startswith(question['correct_option']):
|
629 |
+
# st.markdown(f"✅ {opt}")
|
630 |
+
# else:
|
631 |
+
# st.markdown(f"- {opt}")
|
632 |
+
|
633 |
+
# else:
|
634 |
+
# # Initialize quiz state for this specific quiz
|
635 |
+
# quiz_key = f"quiz_{quiz['_id']}_student_{student_id}"
|
636 |
+
# if quiz_key not in st.session_state:
|
637 |
+
# st.session_state[quiz_key] = {
|
638 |
+
# 'submitted': False,
|
639 |
+
# 'score': None,
|
640 |
+
# 'answers': {}
|
641 |
+
# }
|
642 |
+
|
643 |
+
# # If quiz was just submitted, show the results
|
644 |
+
# if st.session_state[quiz_key]['submitted']:
|
645 |
+
# st.success(f"Quiz submitted successfully! Your score: {st.session_state[quiz_key]['score']:.1f}%")
|
646 |
+
# # Reset the quiz state
|
647 |
+
# st.session_state[quiz_key]['submitted'] = False
|
648 |
+
|
649 |
+
|
650 |
+
# # Display quiz questions
|
651 |
+
# st.write("Please select your answers:")
|
652 |
+
|
653 |
+
# # Create a form for quiz submission
|
654 |
+
# form_key = f"quiz_form_{quiz['_id']}_student_{student_id}"
|
655 |
+
# with st.form(key=form_key):
|
656 |
+
# student_answers = {}
|
657 |
+
|
658 |
+
# for i, question in enumerate(quiz['questions']):
|
659 |
+
# st.markdown(f"**Question {i+1}:** {question['question']}")
|
660 |
+
# options = [opt for opt in question['options']]
|
661 |
+
# # student_answers[str(i)] = st.radio(
|
662 |
+
# # f"Select answer for question {i+1}:",
|
663 |
+
# # options=options,
|
664 |
+
# # key=f"q_{i}",
|
665 |
+
# # index=None
|
666 |
+
# # )
|
667 |
+
# answer = st.radio(
|
668 |
+
# f"Select answer for question {i+1}:",
|
669 |
+
# options=options,
|
670 |
+
# key=f"{quiz['_id']}_{i}", # Simplify the radio button key
|
671 |
+
# index=None
|
672 |
+
# )
|
673 |
+
# if answer: # Only add to answers if a selection was made
|
674 |
+
# student_answers[str(i)] = answer
|
675 |
+
|
676 |
+
# # Submit button
|
677 |
+
# # submitted = st.form_submit_button("Submit Quiz")
|
678 |
+
# print("Before the submit button")
|
679 |
+
# submit_button = st.form_submit_button("Submit Quiz")
|
680 |
+
# print("After the submit button")
|
681 |
+
# if submit_button and student_answers:
|
682 |
+
# print("Clicked the button")
|
683 |
+
# print(student_answers)
|
684 |
+
# correct_answers = 0
|
685 |
+
# for i, question in enumerate(quiz['questions']):
|
686 |
+
# if student_answers[str(i)] == question['correct_option']:
|
687 |
+
# correct_answers += 1
|
688 |
+
# score = (correct_answers / len(quiz['questions'])) * 100
|
689 |
+
|
690 |
+
# if score is not None:
|
691 |
+
# st.success(f"Quiz submitted successfully! Your score: {score:.1f}%")
|
692 |
+
# st.session_state[quiz_key]['submitted'] = True
|
693 |
+
# st.session_state[quiz_key]['score'] = score
|
694 |
+
# st.session_state[quiz_key]['answers'] = student_answers
|
695 |
+
# # This will trigger a rerun, but now we'll handle it properly
|
696 |
+
# st.rerun()
|
697 |
+
|
698 |
+
# else:
|
699 |
+
# st.error("Error submitting quiz. Please try again.")
|
700 |
+
# # correct_answers = 0
|
701 |
+
# # for i, question in enumerate(quiz['questions']):
|
702 |
+
# # if student_answers[str(i)] == question['correct_option']:
|
703 |
+
# # correct_answers += 1
|
704 |
+
# # score = (correct_answers / len(quiz['questions'])) * 100
|
705 |
+
# # print(score)
|
706 |
+
# # try:
|
707 |
+
# # quizzes_collection.update_one(
|
708 |
+
# # {"_id": quiz['_id']},
|
709 |
+
# # {"$push": {"submissions": {"student_id": student_id, "score": score}}}
|
710 |
+
# # )
|
711 |
+
# # st.success(f"Quiz submitted successfully! Your score: {score:.1f}%")
|
712 |
+
# # except Exception as db_error:
|
713 |
+
# # st.error(f"Error saving submission: {str(db_error)}")
|
714 |
+
|
715 |
if getattr(st.session_state, 'show_quizzes', False):
|
|
|
716 |
quiz = quizzes_collection.find_one(
|
717 |
+
{
|
718 |
+
"course_id": course_id,
|
719 |
+
"session_id": session['session_id'],
|
720 |
+
"user_id": student_id
|
721 |
+
},
|
722 |
sort=[("created_at", -1)]
|
723 |
)
|
724 |
if not quiz:
|
725 |
st.info("No practice quizzes created.")
|
726 |
else:
|
727 |
+
with st.expander(f"📝 Practice Quiz", expanded=True):
|
728 |
+
existing_score = get_student_quiz_score(quiz['_id'], student_id)
|
729 |
+
|
730 |
+
if existing_score is not None:
|
731 |
+
st.success(f"Quiz completed! Your score: {existing_score:.1f}%")
|
732 |
|
733 |
+
# Show review
|
734 |
+
st.subheader("Quiz Review")
|
735 |
+
for i, question in enumerate(quiz['questions']):
|
736 |
+
st.markdown(f"**Q{i+1}:** {question['question']}")
|
737 |
+
for opt in question['options']:
|
738 |
+
if opt == question['correct_option']:
|
739 |
+
st.markdown(f"✅ {opt}")
|
740 |
+
else:
|
741 |
+
st.markdown(f"- {opt}")
|
742 |
+
else:
|
743 |
+
with st.form(key=f"quiz_form_{quiz['_id']}"):
|
744 |
+
student_answers = {}
|
745 |
|
|
|
|
|
746 |
for i, question in enumerate(quiz['questions']):
|
747 |
+
st.markdown(f"**Q{i+1}:** {question['question']}")
|
748 |
+
options = question['options']
|
749 |
+
answer = st.radio(
|
750 |
+
f"Select answer:",
|
751 |
+
options=options,
|
752 |
+
key=f"q_{quiz['_id']}_{i}",
|
753 |
+
index=None
|
754 |
+
)
|
755 |
+
if answer:
|
756 |
+
student_answers[str(i)] = answer
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
757 |
|
758 |
+
submit_button = st.form_submit_button("Submit Quiz")
|
759 |
|
760 |
+
if submit_button:
|
761 |
+
if len(student_answers) != len(quiz['questions']):
|
762 |
+
st.error("Please answer all questions before submitting.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
763 |
else:
|
764 |
+
# Calculate score
|
765 |
+
correct_answers = 0
|
766 |
+
total_questions = len(quiz['questions'])
|
767 |
+
|
768 |
+
for q_idx, question in enumerate(quiz['questions']):
|
769 |
+
student_answer = student_answers.get(str(q_idx))
|
770 |
+
correct_option = question['correct_option']
|
771 |
+
|
772 |
+
if student_answer == correct_option:
|
773 |
+
correct_answers += 1
|
774 |
+
|
775 |
+
score = (correct_answers / total_questions) * 100
|
776 |
+
|
777 |
+
# Save submission to database
|
778 |
+
try:
|
779 |
+
result = quizzes_collection.update_one(
|
780 |
+
{"_id": quiz['_id']},
|
781 |
+
{
|
782 |
+
"$push": {
|
783 |
+
"submissions": {
|
784 |
+
"student_id": student_id,
|
785 |
+
"answers": student_answers,
|
786 |
+
"score": score,
|
787 |
+
"submitted_at": datetime.utcnow()
|
788 |
+
}
|
789 |
+
}
|
790 |
+
}
|
791 |
+
)
|
792 |
+
|
793 |
+
if result.modified_count > 0:
|
794 |
+
st.success(f"Quiz submitted successfully! Score: {score:.1f}%")
|
795 |
+
st.rerun()
|
796 |
+
else:
|
797 |
+
st.error("Error saving submission. Please try again.")
|
798 |
+
|
799 |
+
except Exception as e:
|
800 |
+
st.error(f"Database error: {str(e)}")
|
801 |
+
|
802 |
|
803 |
+
display_pre_class_quiz_tab(student_id, course_id, session["session_id"])
|
804 |
def display_pre_class_quiz_tab(student_id, course_id, session_id):
|
805 |
"""Display pre-class quizzes for students"""
|
806 |
st.markdown("### Pre-class Quizzes")
|
|
|
|
|
807 |
quizzes = quizzes_collection.find({
|
808 |
"course_id": course_id,
|
809 |
"session_id": session_id,
|
810 |
+
"status": "active",
|
811 |
+
"quiz_type": "pre_class"
|
812 |
})
|
813 |
+
# Get available quizzes
|
814 |
+
# quizzes = quizzes_collection.find({
|
815 |
+
# "course_id": course_id,
|
816 |
+
# "session_id": session_id,
|
817 |
+
# "status": "active"
|
818 |
+
# })
|
819 |
|
820 |
quizzes = list(quizzes)
|
821 |
if not quizzes:
|