Kiruthikaramalingam commited on
Commit
55afd23
·
verified ·
1 Parent(s): 87fdf15

Update templates/tempor.html

Browse files
Files changed (1) hide show
  1. templates/tempor.html +45 -10
templates/tempor.html CHANGED
@@ -6,6 +6,11 @@
6
  <title>Interactive Python Chatbot</title>
7
  <link rel="stylesheet" type="text/css" href="/static/tempor.css">
8
  <script>
 
 
 
 
 
9
  async function sendMessage(event) {
10
  event.preventDefault();
11
  const userInput = document.getElementById("user_input").value.trim();
@@ -35,6 +40,10 @@
35
  async function startQuiz() {
36
  const difficulty = document.getElementById("difficulty").value;
37
 
 
 
 
 
38
  try {
39
  const response = await fetch("/start_quiz", {
40
  method: "POST",
@@ -45,11 +54,9 @@
45
  const data = await response.json();
46
  const quizBox = document.getElementById("quiz-box");
47
 
48
-
49
- quizBox.innerHTML = `<p class="quiz-feedback">${data.reply}</p>`;
50
-
51
-
52
  if (data.question && data.options) {
 
 
53
  quizBox.innerHTML = `
54
  <p class="quiz-question"><strong>Question:</strong> ${data.question}</p>
55
  <form id="quiz-form">
@@ -59,13 +66,13 @@
59
  <label for="option${index}">${option}</label>
60
  </div>
61
  `).join('')}
62
-
63
  </form>
64
-
65
  <button type="button" class="btn btn-primary" onclick="submitAnswer()">Submit Answer</button>
66
  `;
 
67
  } else {
68
  quizBox.innerHTML = `<p class="quiz-end">Quiz finished! Great job!</p>`;
 
69
  }
70
  } catch (error) {
71
  alert("Error: Unable to start the quiz.");
@@ -73,6 +80,7 @@
73
  }
74
 
75
  async function submitAnswer() {
 
76
  const form = document.getElementById("quiz-form");
77
  const answer = form.querySelector('input[name="answer"]:checked')?.value;
78
 
@@ -85,19 +93,19 @@
85
  const response = await fetch("/submit_answer", {
86
  method: "POST",
87
  headers: { "Content-Type": "application/json" },
88
- body: JSON.stringify({ answer, user_id: "default" }),
89
  });
90
 
91
  const data = await response.json();
92
  const quizBox = document.getElementById("quiz-box");
93
  const scoreBox = document.getElementById("score-box");
94
 
95
- // Display feedback for the answer
96
  quizBox.innerHTML = `<p class="quiz-feedback">${data.reply}</p>`;
97
  scoreBox.innerHTML = `Score: ${data.score}`;
98
 
99
- // Check if there's a next question
100
  if (data.next_question && data.options) {
 
 
101
  quizBox.innerHTML += `
102
  <p class="quiz-question"><strong>Next Question:</strong> ${data.next_question}</p>
103
  <form id="quiz-form">
@@ -110,6 +118,7 @@
110
  <button type="button" class="btn btn-primary" onclick="submitAnswer()">Submit Answer</button>
111
  </form>
112
  `;
 
113
  } else {
114
  quizBox.innerHTML += `<p class="quiz-end">Quiz Completed! Great job!</p>`;
115
  }
@@ -118,6 +127,33 @@
118
  }
119
  }
120
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
121
  async function clearHistory() {
122
  try {
123
  const response = await fetch("/clear_history", { method: "POST" });
@@ -146,7 +182,6 @@
146
  <button type="submit">Send</button>
147
  </form>
148
  <button class="clear-history-btn" onclick="clearHistory()">Clear History</button>
149
-
150
  </div>
151
 
152
  <!-- Quiz Section -->
 
6
  <title>Interactive Python Chatbot</title>
7
  <link rel="stylesheet" type="text/css" href="/static/tempor.css">
8
  <script>
9
+ let timer; // Timer variable
10
+ let timeRemaining = 30; // Default time for each question
11
+ let isTimeUp = false; // Flag to track if the timer expired
12
+ const timerElement = document.createElement("div"); // Timer element
13
+
14
  async function sendMessage(event) {
15
  event.preventDefault();
16
  const userInput = document.getElementById("user_input").value.trim();
 
40
  async function startQuiz() {
41
  const difficulty = document.getElementById("difficulty").value;
42
 
43
+ // Reset score to 0 when starting a new quiz
44
+ document.getElementById("score-box").innerHTML = `Score: 0`;
45
+ isTimeUp = false; // Reset timeUp flag
46
+
47
  try {
48
  const response = await fetch("/start_quiz", {
49
  method: "POST",
 
54
  const data = await response.json();
55
  const quizBox = document.getElementById("quiz-box");
56
 
 
 
 
 
57
  if (data.question && data.options) {
58
+ timeRemaining = 30; // Reset timer for the first question
59
+
60
  quizBox.innerHTML = `
61
  <p class="quiz-question"><strong>Question:</strong> ${data.question}</p>
62
  <form id="quiz-form">
 
66
  <label for="option${index}">${option}</label>
67
  </div>
68
  `).join('')}
 
69
  </form>
 
70
  <button type="button" class="btn btn-primary" onclick="submitAnswer()">Submit Answer</button>
71
  `;
72
+ startTimer(); // Start the timer after rendering the first question
73
  } else {
74
  quizBox.innerHTML = `<p class="quiz-end">Quiz finished! Great job!</p>`;
75
+ clearTimer();
76
  }
77
  } catch (error) {
78
  alert("Error: Unable to start the quiz.");
 
80
  }
81
 
82
  async function submitAnswer() {
83
+ clearTimer(); // Clear the timer when submitting an answer
84
  const form = document.getElementById("quiz-form");
85
  const answer = form.querySelector('input[name="answer"]:checked')?.value;
86
 
 
93
  const response = await fetch("/submit_answer", {
94
  method: "POST",
95
  headers: { "Content-Type": "application/json" },
96
+ body: JSON.stringify({ answer, user_id: "default", timeUp: isTimeUp }),
97
  });
98
 
99
  const data = await response.json();
100
  const quizBox = document.getElementById("quiz-box");
101
  const scoreBox = document.getElementById("score-box");
102
 
 
103
  quizBox.innerHTML = `<p class="quiz-feedback">${data.reply}</p>`;
104
  scoreBox.innerHTML = `Score: ${data.score}`;
105
 
 
106
  if (data.next_question && data.options) {
107
+ timeRemaining = 30; // Reset the timer for the next question
108
+ isTimeUp = false; // Reset the flag for the next question
109
  quizBox.innerHTML += `
110
  <p class="quiz-question"><strong>Next Question:</strong> ${data.next_question}</p>
111
  <form id="quiz-form">
 
118
  <button type="button" class="btn btn-primary" onclick="submitAnswer()">Submit Answer</button>
119
  </form>
120
  `;
121
+ startTimer(); // Start the timer for the next question
122
  } else {
123
  quizBox.innerHTML += `<p class="quiz-end">Quiz Completed! Great job!</p>`;
124
  }
 
127
  }
128
  }
129
 
130
+ function startTimer() {
131
+ clearTimer(); // Clear any existing timer
132
+ const quizBox = document.getElementById("quiz-box");
133
+ timerElement.textContent = `Time remaining: ${timeRemaining}s`;
134
+ timerElement.className = "timer-display";
135
+ quizBox.appendChild(timerElement);
136
+
137
+ timer = setInterval(() => {
138
+ timeRemaining--;
139
+ timerElement.textContent = `Time remaining: ${timeRemaining}s`;
140
+
141
+ if (timeRemaining <= 0) {
142
+ clearTimer();
143
+ isTimeUp = true; // Mark the timer as expired
144
+ alert("Time's up! Moving to the next question.");
145
+ submitAnswer(); // Automatically submit an empty answer
146
+ }
147
+ }, 1000);
148
+ }
149
+
150
+ function clearTimer() {
151
+ clearInterval(timer);
152
+ if (timerElement.parentNode) {
153
+ timerElement.parentNode.removeChild(timerElement);
154
+ }
155
+ }
156
+
157
  async function clearHistory() {
158
  try {
159
  const response = await fetch("/clear_history", { method: "POST" });
 
182
  <button type="submit">Send</button>
183
  </form>
184
  <button class="clear-history-btn" onclick="clearHistory()">Clear History</button>
 
185
  </div>
186
 
187
  <!-- Quiz Section -->