File size: 3,566 Bytes
7eea7cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Import necessary libraries
import streamlit as st
import sqlite3
import random
from datetime import datetime

# Create a connection to the SQLite database
conn = sqlite3.connect('student_exams.db')

# Create a cursor object
cur = conn.cursor()

# Create tables if they do not exist
cur.execute('''
    CREATE TABLE IF NOT EXISTS students
    (id_number INTEGER PRIMARY KEY, name TEXT, year INTEGER, section TEXT)
''')

cur.execute('''
    CREATE TABLE IF NOT EXISTS exams
    (id INTEGER PRIMARY KEY, timestamp_create TIMESTAMP, subject TEXT, topic TEXT, type TEXT, num_items INTEGER)
''')

cur.execute('''
    CREATE TABLE IF NOT EXISTS attempts
    (id INTEGER PRIMARY KEY, date_taken TIMESTAMP, exam_id INTEGER, student_id INTEGER, answers TEXT, results TEXT, total_score INTEGER)
''')

# Insert dummy data
cur.execute("INSERT OR IGNORE INTO students (id_number, name, year, section) VALUES (1, 'John Doe', 1, 'A')")
cur.execute("INSERT OR IGNORE INTO exams (id, timestamp_create, subject, topic, type, num_items) VALUES (1, '2022-01-01 00:00:00', 'Math', 'Algebra', 'Multiple Choice', 5)")

# Commit the transaction
conn.commit()

# Function to get the exam questions
def get_exam_questions(exam_id):
    questions = []
    for i in range(5):  # Assuming 5 questions for simplicity
        question = {
            'question': f'Question {i+1}',
            'options': [f'Option {j+1}' for j in range(4)],
            'correct_answer': random.choice([f'Option {j+1}' for j in range(4)])
        }
        questions.append(question)
    return questions

# Function to evaluate the answers
def evaluate_answers(questions, answers):
    score = 0
    results = []
    for i, question in enumerate(questions):
        if answers[i] == question['correct_answer']:
            score += 1
            results.append('Correct')
        else:
            results.append('Incorrect')
    return score, results

# Streamlit app
st.title('Quiz Interface')

# Get the exam questions
exam_id = 1
questions = get_exam_questions(exam_id)

# Initialize the answers
answers = []

# Display the questions
for i, question in enumerate(questions):
    st.write(question['question'])
    answer = st.selectbox('Select an answer', question['options'], key=i)
    answers.append(answer)

# Evaluate the answers
if st.button('Submit'):
    score, results = evaluate_answers(questions, answers)
    
    # Store the attempt data
    cur.execute("INSERT INTO attempts (date_taken, exam_id, student_id, answers, results, total_score) VALUES (?, ?, ?, ?, ?, ?)",
                 (datetime.now().strftime("%Y-%m-%d %H:%M:%S"), exam_id, 1, ', '.join(answers), ', '.join(results), score))
    conn.commit()
    
    # Display the summary
    st.write('Summary:')
    st.write(f'Total Score: {score}/{len(questions)}')
    st.write('Results:')
    for i, result in enumerate(results):
        st.write(f'Question {i+1}: {result}')
    
    # Display the history of previous attempts
    st.write('History of Previous Attempts:')
    cur.execute("SELECT date_taken, total_score FROM attempts WHERE exam_id = ? AND student_id = ?", (exam_id, 1))
    attempts = cur.fetchall()
    for attempt in attempts:
        st.write(f'Date: {attempt[0]}, Score: {attempt[1]}/{len(questions)}')
    
    # Provide an option to retake the quiz
    if st.button('Retake Quiz'):
        answers = []
        for i, question in enumerate(questions):
            st.write(question['question'])
            answer = st.selectbox('Select an answer', question['options'], key=i)
            answers.append(answer)