ruslanmv commited on
Commit
d72d0a0
1 Parent(s): ab240d1
Files changed (2) hide show
  1. app.py +29 -40
  2. static/script.js +6 -16
app.py CHANGED
@@ -52,30 +52,6 @@ def on_leave():
52
  emit('update_participants', {"participants": participants, "count": len(participants)}, room='quiz')
53
  print(f"{username} left the quiz.")
54
 
55
- #@socketio.on('load_quiz')
56
- def load_quiz_old(data):
57
- global selected_questions
58
- exam_name = data['exam_name']
59
- start_question = data['start_question'] - 1 # Adjust for 0-based indexing
60
- selected_questions = backend.select_exam(exam_name)
61
- if selected_questions:
62
- num_questions = len(selected_questions)
63
- current_question['index'] = start_question
64
- emit('quiz_loaded', {"success": True, "num_questions": num_questions, "start_question": start_question + 1}, room=request.sid)
65
- else:
66
- emit('quiz_loaded', {"success": False}, room=request.sid)
67
-
68
- #@socketio.on('load_quiz')
69
- def load_quiz_new1(data):
70
- global selected_questions
71
- exam_name = data['exam_name']
72
- selected_questions = backend.select_exam(exam_name)
73
- if selected_questions:
74
- num_questions = len(selected_questions)
75
- emit('quiz_loaded', {"success": True, "num_questions": num_questions}, room=request.sid)
76
- else:
77
- emit('quiz_loaded', {"success": False}, room=request.sid)
78
-
79
  @socketio.on('load_quiz')
80
  def load_quiz(data):
81
  global selected_questions, current_question
@@ -85,29 +61,29 @@ def load_quiz(data):
85
  if selected_questions:
86
  num_questions = len(selected_questions)
87
  current_question['index'] = start_question # Set the starting question index
88
- emit('quiz_loaded', {"success": True, "num_questions": num_questions}, room=request.sid)
 
 
89
  else:
90
  emit('quiz_loaded', {"success": False}, room=request.sid)
91
 
92
-
93
-
94
  #@socketio.on('start_quiz')
95
- def start_quiz_old():
96
- if participants and selected_questions:
97
- current_question['started'] = True
98
- emit('new_question', selected_questions[current_question['index']], room='quiz')
99
- # Also emit the question to the host
100
- emit('new_question', selected_questions[current_question['index']], room=request.sid)
101
- emit('enable_end_quiz', room=request.sid) # Enable "End Quiz" for the host
 
102
 
103
  @socketio.on('start_quiz')
104
  def start_quiz():
105
  if participants and selected_questions:
106
  current_question['started'] = True
107
  index = current_question['index']
108
- emit('new_question', selected_questions[index], room='quiz')
109
- # Also emit the question to the host
110
- emit('new_question', selected_questions[index], room=request.sid)
111
  emit('enable_end_quiz', room=request.sid) # Enable "End Quiz" for the host
112
 
113
 
@@ -143,6 +119,20 @@ def check_answers():
143
  if current_question['answers'].get(participant["username"]) == correct_answer:
144
  participants[sid]["score"] += 1
145
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
146
  @socketio.on('next_question')
147
  def next_question():
148
  current_question['index'] += 1
@@ -150,13 +140,12 @@ def next_question():
150
  if current_question['index'] < len(selected_questions):
151
  question = selected_questions[current_question['index']]
152
  emit('clear_results', room='quiz')
153
- emit('new_question', question, room='quiz')
154
- # Also emit the question to the host
155
- emit('new_question', question, room=request.sid)
156
  else:
157
  final_results = calculate_final_results()
158
  emit('display_final_results', final_results, room='quiz')
159
 
 
160
  @socketio.on('end_quiz')
161
  def end_quiz():
162
  if current_question['started']: # Ensure the quiz has started before ending it
 
52
  emit('update_participants', {"participants": participants, "count": len(participants)}, room='quiz')
53
  print(f"{username} left the quiz.")
54
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55
  @socketio.on('load_quiz')
56
  def load_quiz(data):
57
  global selected_questions, current_question
 
61
  if selected_questions:
62
  num_questions = len(selected_questions)
63
  current_question['index'] = start_question # Set the starting question index
64
+ #emit('quiz_loaded', {"success": True, "num_questions": num_questions}, room=request.sid)
65
+ emit('quiz_loaded', {"success": True, "num_questions": num_questions, "start_question": start_question + 1}, room=request.sid)
66
+
67
  else:
68
  emit('quiz_loaded', {"success": False}, room=request.sid)
69
 
 
 
70
  #@socketio.on('start_quiz')
71
+ #def start_quiz():
72
+ # if participants and selected_questions:
73
+ # current_question['started'] = True
74
+ # index = current_question['index']
75
+ # emit('new_question', selected_questions[index], room='quiz')
76
+ # # Also emit the question to the host
77
+ # emit('new_question', selected_questions[index], room=request.sid)
78
+ # emit('enable_end_quiz', room=request.sid) # Enable "End Quiz" for the host
79
 
80
  @socketio.on('start_quiz')
81
  def start_quiz():
82
  if participants and selected_questions:
83
  current_question['started'] = True
84
  index = current_question['index']
85
+ question = selected_questions[index]
86
+ emit('new_question', {"question": question["question"], "options": question["options"], "index": index + 1}, room='quiz')
 
87
  emit('enable_end_quiz', room=request.sid) # Enable "End Quiz" for the host
88
 
89
 
 
119
  if current_question['answers'].get(participant["username"]) == correct_answer:
120
  participants[sid]["score"] += 1
121
 
122
+ #@socketio.on('next_question')
123
+ #def next_question():
124
+ # current_question['index'] += 1
125
+ # current_question['answers'] = {}
126
+ # if current_question['index'] < len(selected_questions):
127
+ # question = selected_questions[current_question['index']]
128
+ # emit('clear_results', room='quiz')
129
+ # emit('new_question', question, room='quiz')
130
+ # # Also emit the question to the host
131
+ # emit('new_question', question, room=request.sid)
132
+ # else:
133
+ # final_results = calculate_final_results()
134
+ # emit('display_final_results', final_results, room='quiz')
135
+
136
  @socketio.on('next_question')
137
  def next_question():
138
  current_question['index'] += 1
 
140
  if current_question['index'] < len(selected_questions):
141
  question = selected_questions[current_question['index']]
142
  emit('clear_results', room='quiz')
143
+ emit('new_question', {"question": question["question"], "options": question["options"], "index": current_question['index'] + 1}, room='quiz')
 
 
144
  else:
145
  final_results = calculate_final_results()
146
  emit('display_final_results', final_results, room='quiz')
147
 
148
+
149
  @socketio.on('end_quiz')
150
  def end_quiz():
151
  if current_question['started']: # Ensure the quiz has started before ending it
static/script.js CHANGED
@@ -29,17 +29,7 @@ function selectExam() {
29
  document.getElementById('question-start-display').textContent = `Starting from question ${startQuestion}.`;
30
  }
31
 
32
- function loadQuiz_old() {
33
- const examName = document.getElementById('exam-selector').value;
34
- const startQuestion = parseInt(document.getElementById('start-question-number').value, 10);
35
-
36
- if (!examName) {
37
- alert("Please select an exam first.");
38
- return;
39
- }
40
 
41
- socket.emit('load_quiz', { exam_name: examName, start_question: startQuestion });
42
- }
43
  function loadQuiz() {
44
  const examName = document.getElementById('exam-selector').value;
45
  const startQuestion = parseInt(document.getElementById('start-question-number').value, 10);
@@ -83,14 +73,13 @@ function restartQuiz() {
83
  socket.on('quiz_loaded', (data) => {
84
  if (data.success) {
85
  alert(`Quiz loaded with ${data.num_questions} questions.`);
86
-
87
- // Update the range and number input max values dynamically
88
  const startQuestionInput = document.getElementById('start-question');
89
  const startQuestionNumber = document.getElementById('start-question-number');
90
  startQuestionInput.max = data.num_questions;
91
  startQuestionNumber.max = data.num_questions;
92
-
93
- // Show the start question controls
 
94
  document.getElementById('start-question-label').style.display = 'block';
95
  startQuestionInput.style.display = 'block';
96
  startQuestionNumber.style.display = 'block';
@@ -103,8 +92,7 @@ socket.on('quiz_loaded', (data) => {
103
 
104
  socket.on('new_question', (data) => {
105
  document.getElementById('waiting-message').style.display = 'none';
106
- document.getElementById('question-text').innerText = data.question;
107
- // Dynamically generate letters for options (up to 'h')
108
  const letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'];
109
  const options = data.options.map((opt, index) =>
110
  `<input type="radio" id="${letters[index]}" name="answer" value="${opt}">
@@ -113,6 +101,8 @@ socket.on('new_question', (data) => {
113
  document.getElementById('options').innerHTML = options;
114
  });
115
 
 
 
116
  socket.on('display_results', (data) => {
117
  const img = `<img src="data:image/png;base64,${data.chart}" alt="Results Chart" />`;
118
  const resultText = `<p>Correct Answer: ${data.results.correct_answer}</p>`;
 
29
  document.getElementById('question-start-display').textContent = `Starting from question ${startQuestion}.`;
30
  }
31
 
 
 
 
 
 
 
 
 
32
 
 
 
33
  function loadQuiz() {
34
  const examName = document.getElementById('exam-selector').value;
35
  const startQuestion = parseInt(document.getElementById('start-question-number').value, 10);
 
73
  socket.on('quiz_loaded', (data) => {
74
  if (data.success) {
75
  alert(`Quiz loaded with ${data.num_questions} questions.`);
 
 
76
  const startQuestionInput = document.getElementById('start-question');
77
  const startQuestionNumber = document.getElementById('start-question-number');
78
  startQuestionInput.max = data.num_questions;
79
  startQuestionNumber.max = data.num_questions;
80
+ startQuestionInput.value = data.start_question;
81
+ startQuestionNumber.value = data.start_question;
82
+ document.getElementById('question-start-display').textContent = `Starting from question ${data.start_question}.`;
83
  document.getElementById('start-question-label').style.display = 'block';
84
  startQuestionInput.style.display = 'block';
85
  startQuestionNumber.style.display = 'block';
 
92
 
93
  socket.on('new_question', (data) => {
94
  document.getElementById('waiting-message').style.display = 'none';
95
+ document.getElementById('question-text').innerText = `Question ${data.index}: ${data.question}`;
 
96
  const letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'];
97
  const options = data.options.map((opt, index) =>
98
  `<input type="radio" id="${letters[index]}" name="answer" value="${opt}">
 
101
  document.getElementById('options').innerHTML = options;
102
  });
103
 
104
+
105
+
106
  socket.on('display_results', (data) => {
107
  const img = `<img src="data:image/png;base64,${data.chart}" alt="Results Chart" />`;
108
  const resultText = `<p>Correct Answer: ${data.results.correct_answer}</p>`;