ruslanmv commited on
Commit
c17c125
1 Parent(s): fa6e217
Files changed (3) hide show
  1. app.py +15 -2
  2. static/script.js +23 -5
  3. templates/host.html +4 -4
app.py CHANGED
@@ -52,8 +52,8 @@ 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(data):
57
  global selected_questions
58
  exam_name = data['exam_name']
59
  start_question = data['start_question'] - 1 # Adjust for 0-based indexing
@@ -65,6 +65,19 @@ def load_quiz(data):
65
  else:
66
  emit('quiz_loaded', {"success": False}, room=request.sid)
67
 
 
 
 
 
 
 
 
 
 
 
 
 
 
68
  @socketio.on('start_quiz')
69
  def start_quiz():
70
  if participants and selected_questions:
 
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
 
65
  else:
66
  emit('quiz_loaded', {"success": False}, room=request.sid)
67
 
68
+ @socketio.on('load_quiz')
69
+ def load_quiz(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
+
80
+
81
  @socketio.on('start_quiz')
82
  def start_quiz():
83
  if participants and selected_questions:
static/script.js CHANGED
@@ -31,10 +31,15 @@ function selectExam() {
31
 
32
  function loadQuiz() {
33
  const examName = document.getElementById('exam-selector').value;
34
- const startQuestion = document.getElementById('start-question-number').value;
35
- socket.emit('load_quiz', { exam_name: examName, start_question: parseInt(startQuestion) });
36
- }
 
 
 
37
 
 
 
38
  function updateSliderValue(value) {
39
  document.getElementById('start-question').value = value;
40
  document.getElementById('start-question-number').value = value;
@@ -63,12 +68,25 @@ function restartQuiz() {
63
 
64
  socket.on('quiz_loaded', (data) => {
65
  if (data.success) {
66
- alert(`Quiz loaded with ${data.num_questions} questions, starting from question ${data.start_question}.`);
 
 
 
 
 
 
 
 
 
 
 
 
67
  } else {
68
- alert(`Failed to load quiz.`);
69
  }
70
  });
71
 
 
72
  socket.on('new_question', (data) => {
73
  document.getElementById('waiting-message').style.display = 'none';
74
  document.getElementById('question-text').innerText = data.question;
 
31
 
32
  function loadQuiz() {
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 updateSliderValue(value) {
44
  document.getElementById('start-question').value = value;
45
  document.getElementById('start-question-number').value = value;
 
68
 
69
  socket.on('quiz_loaded', (data) => {
70
  if (data.success) {
71
+ alert(`Quiz loaded with ${data.num_questions} questions.`);
72
+
73
+ // Update the range and number input max values dynamically
74
+ const startQuestionInput = document.getElementById('start-question');
75
+ const startQuestionNumber = document.getElementById('start-question-number');
76
+ startQuestionInput.max = data.num_questions;
77
+ startQuestionNumber.max = data.num_questions;
78
+
79
+ // Show the start question controls
80
+ document.getElementById('start-question-label').style.display = 'block';
81
+ startQuestionInput.style.display = 'block';
82
+ startQuestionNumber.style.display = 'block';
83
+ document.getElementById('question-start-display').style.display = 'block';
84
  } else {
85
+ alert("Failed to load quiz. Please try again.");
86
  }
87
  });
88
 
89
+
90
  socket.on('new_question', (data) => {
91
  document.getElementById('waiting-message').style.display = 'none';
92
  document.getElementById('question-text').innerText = data.question;
templates/host.html CHANGED
@@ -17,10 +17,10 @@
17
  <option value="{{ exam }}">{{ exam }}</option>
18
  {% endfor %}
19
  </select>
20
- <label for="start-question" class="mt-3">Select starting question:</label>
21
- <input type="range" id="start-question" min="1" max="10" value="1" class="form-range mt-2 mb-2" oninput="updateSliderValue(this.value)">
22
- <input type="number" id="start-question-number" min="1" max="10" value="1" class="form-control" oninput="updateSliderValue(this.value)">
23
- <p id="question-start-display" class="mt-2">Starting from question 1.</p>
24
  <button onclick="loadQuiz()" class="btn btn-info mt-3">Load Quiz</button><br><br>
25
  <button onclick="startQuiz()" class="btn btn-success mt-3">Start Quiz</button><br><br>
26
  <button onclick="restartQuiz()" class="btn btn-warning mt-3">Restart</button><br><br>
 
17
  <option value="{{ exam }}">{{ exam }}</option>
18
  {% endfor %}
19
  </select>
20
+ <label for="start-question" id="start-question-label" class="mt-3" style="display: none;">Select starting question:</label>
21
+ <input type="range" id="start-question" min="1" max="10" value="1" class="form-range mt-2 mb-2" style="display: none;" oninput="updateSliderValue(this.value)">
22
+ <input type="number" id="start-question-number" min="1" max="10" value="1" class="form-control" style="display: none;" oninput="updateSliderValue(this.value)">
23
+ <p id="question-start-display" class="mt-2" style="display: none;">Starting from question 1.</p>
24
  <button onclick="loadQuiz()" class="btn btn-info mt-3">Load Quiz</button><br><br>
25
  <button onclick="startQuiz()" class="btn btn-success mt-3">Start Quiz</button><br><br>
26
  <button onclick="restartQuiz()" class="btn btn-warning mt-3">Restart</button><br><br>