File size: 4,387 Bytes
50b7a69
4556c83
50b7a69
 
 
de7c6d8
9a82c5c
 
50b7a69
9a82c5c
4556c83
 
 
 
0d1bedf
 
9a82c5c
50b7a69
0d1bedf
 
50b7a69
 
 
 
4cd0f3c
50b7a69
de7c6d8
50b7a69
0d1bedf
3a7f5ab
0d1bedf
 
50b7a69
 
 
4cd0f3c
0d1bedf
50b7a69
 
 
 
 
 
 
d77ef80
 
5b846f1
96884ca
83297c3
5b846f1
96884ca
5b846f1
77212e3
5b846f1
 
77212e3
d77ef80
 
 
 
 
 
 
 
50b7a69
 
83297c3
96884ca
de7c6d8
 
83297c3
4cd0f3c
 
83297c3
4556c83
d77ef80
3a7f5ab
83297c3
de7c6d8
13d8d3a
4cd0f3c
50b7a69
 
 
 
 
 
 
1666eb3
 
 
 
 
 
 
 
50b7a69
de7c6d8
 
50b7a69
83297c3
 
de7c6d8
9a82c5c
 
b4443a9
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
from flask import Flask, render_template, request, redirect, url_for, session
from flask_session import Session
import json
import random
import os
import time

app = Flask(__name__)
app.secret_key = 'supersecretkey'

# Configure Flask-Session
app.config['SESSION_TYPE'] = 'filesystem'
Session(app)

QUESTIONS_FOLDER = 'questions'

@app.route('/')
def index():
    files = [f for f in os.listdir(QUESTIONS_FOLDER) if f.endswith('.json')]
    return render_template('index.html', files=files)

@app.route('/start', methods=['POST'])
def start():
    session['questions'] = []
    session['answers'] = []
    session['current_question'] = 0
    session['start_time'] = time.time()

    selected_file = request.form['file']
    session['selected_file'] = os.path.splitext(selected_file)[0]  # Remove file extension
    file_path = os.path.join(QUESTIONS_FOLDER, selected_file)
    with open(file_path, 'r') as file:
        questions = json.load(file)
        random.shuffle(questions)
        session['questions'] = questions
        session['answers'] = [[] for _ in questions]  # Initialize answers list for each question
    return redirect(url_for('quiz'))

@app.route('/quiz', methods=['GET', 'POST'])
def quiz():
    if 'questions' not in session or 'current_question' not in session:
        return redirect(url_for('index'))

    if request.method == 'POST':
        action = request.form.get('action')
        if action == 'next':
            question = session['questions'][session['current_question']]
            multiple_selection = 'Choose two' in question['question'] or 'Select TWO' in question['question']
            answers = request.form.getlist('answer')
            if multiple_selection:
                required_answers = 2
                if len(answers) == required_answers:
                    session['answers'][session['current_question']] = answers
            else:
                if answers:
                    session['answers'][session['current_question']] = answers
            session['current_question'] += 1
            if session['current_question'] >= len(session['questions']):
                return redirect(url_for('results'))
        elif action == 'previous':
            session['current_question'] -= 1
            if session['current_question'] < 0:
                session['current_question'] = 0
        elif action == 'end':
            return redirect(url_for('results'))

    question = session['questions'][session['current_question']]
    multiple_selection = 'Choose two' in question['question'] or 'Select TWO' in question['question']
    elapsed_time = time.time() - session['start_time']
    elapsed_time_str = time.strftime('%H:%M:%S', time.gmtime(elapsed_time))

    previous_answers = session['answers'][session['current_question']] if session['current_question'] < len(session['answers']) else []

    return render_template('quiz.html', question=question,
                           question_number=session['current_question'] + 1,
                           total_questions=len(session['questions']),
                           selected_file=session['selected_file'],
                           show_previous=session['current_question'] > 0,
                           multiple_selection=multiple_selection,
                           elapsed_time=elapsed_time_str,
                           previous_answers=previous_answers)

@app.route('/results')
def results():
    if 'questions' not in session or 'answers' not in session:
        return redirect(url_for('index'))

    total_questions = len(session['questions'])
    session['score'] = 0  # Initialize score

    for i, question in enumerate(session['questions']):
        correct_answers = [str(ord(x) - ord('A')) for x in question['correct']]
        user_answers = session['answers'][i]
        if set(user_answers) == set(correct_answers):
            session['score'] += 1

    score_percentage = (session['score'] / total_questions) * 100
    elapsed_time = time.time() - session['start_time']
    elapsed_time_str = time.strftime('%H:%M:%S', time.gmtime(elapsed_time))

    return render_template('results.html', score=session['score'],
                           total_questions=total_questions, score_percentage=score_percentage,
                           elapsed_time=elapsed_time_str)

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=7860)