-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
237 lines (212 loc) · 10.7 KB
/
script.js
File metadata and controls
237 lines (212 loc) · 10.7 KB
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
// script.js
document.addEventListener('DOMContentLoaded', function () {
// ──────────────────────────────────────────────
// Quiz data
// ──────────────────────────────────────────────
const quizQuestions = [
{
question: "Which AWS service is used to store objects in the cloud?",
options: ["EC2", "S3", "RDS", "Lambda"],
answer: "S3"
},
{
question: "What does CPU stand for?",
options: ["Central Processing Unit", "Core Processing Unit", "Central Program Utility", "Compute Processing Unit"],
answer: "Central Processing Unit"
},
{
question: "Which protocol is used to securely transfer files?",
options: ["FTP", "HTTP", "SFTP", "SMTP"],
answer: "SFTP"
},
{
question: "What is 2 + 2?",
options: ["3", "4", "5", "6"],
answer: "4"
},
{
question: "What is the default port for HTTPS?",
options: ["80", "443", "8080", "22"],
answer: "443"
}
];
// ──────────────────────────────────────────────
// Timer configuration (seconds)
// ──────────────────────────────────────────────
const EXAM_DURATION_SECONDS = 30 * 60; // 30 minutes
let timerInterval = null;
let secondsRemaining = EXAM_DURATION_SECONDS;
// ──────────────────────────────────────────────
// Element references
// ──────────────────────────────────────────────
const signinContainer = document.getElementById('signin-container');
const quizView = document.getElementById('quiz-view');
const signinForm = document.getElementById('signin-form');
const signinError = document.getElementById('signin-error');
const guestBtn = document.getElementById('guest-btn');
const signoutBtn = document.getElementById('signout-btn');
const userDisplay = document.getElementById('user-display');
const timerDisplay = document.getElementById('timer-display');
const quizForm = document.getElementById('quiz-form');
const startPaymentBtn = document.getElementById('start-payment');
const paymentContainer = document.getElementById('payment-container');
const resultsContainer = document.getElementById('results-container');
const resultsScore = document.getElementById('results-score');
const retakeBtn = document.getElementById('retake-btn');
const proceedPaymentBtn = document.getElementById('proceed-payment-btn');
// ──────────────────────────────────────────────
// Sign-In logic
// ──────────────────────────────────────────────
signinForm.addEventListener('submit', function (e) {
e.preventDefault();
const email = document.getElementById('signin-email').value.trim();
const password = document.getElementById('signin-password').value;
if (!email || !password) {
showSigninError('Please enter your email and password.');
return;
}
// Demo authentication: accept any non-empty credentials
startQuiz(email);
});
guestBtn.addEventListener('click', function () {
startQuiz('Guest');
});
signoutBtn.addEventListener('click', function () {
stopTimer();
secondsRemaining = EXAM_DURATION_SECONDS;
quizView.style.display = 'none';
signinContainer.style.display = 'flex';
paymentContainer.style.display = 'none';
resultsContainer.style.display = 'none';
document.getElementById('quiz-container').style.display = 'block';
});
function showSigninError(msg) {
signinError.textContent = msg;
signinError.style.display = 'block';
}
// ──────────────────────────────────────────────
// Start quiz session
// ──────────────────────────────────────────────
function startQuiz(username) {
signinContainer.style.display = 'none';
quizView.style.display = 'block';
userDisplay.textContent = username === 'Guest' ? 'Guest' : username;
signinError.style.display = 'none';
renderQuiz();
startTimer();
}
// ──────────────────────────────────────────────
// Render quiz questions
// ──────────────────────────────────────────────
function renderQuiz() {
quizForm.innerHTML = '';
quizQuestions.forEach(function (q, index) {
const questionDiv = document.createElement('div');
questionDiv.classList.add('question');
questionDiv.innerHTML =
'<p class="question-text"><strong>' + (index + 1) + '.</strong> ' + q.question + '</p>' +
q.options.map(function (option) {
return '<label class="option-label">' +
'<input type="radio" name="question' + index + '" value="' + option + '" required>' +
'<span>' + option + '</span>' +
'</label>';
}).join('');
quizForm.appendChild(questionDiv);
});
}
// ──────────────────────────────────────────────
// Timer logic
// ──────────────────────────────────────────────
function startTimer() {
secondsRemaining = EXAM_DURATION_SECONDS;
updateTimerDisplay();
timerInterval = setInterval(function () {
secondsRemaining--;
updateTimerDisplay();
if (secondsRemaining <= 0) {
stopTimer();
alert('\u23F0 Time is up! Your quiz has been automatically submitted.');
submitQuiz();
}
}, 1000);
}
function stopTimer() {
if (timerInterval) {
clearInterval(timerInterval);
timerInterval = null;
}
}
function updateTimerDisplay() {
const mins = Math.floor(secondsRemaining / 60);
const secs = secondsRemaining % 60;
timerDisplay.textContent =
String(mins).padStart(2, '0') + ':' + String(secs).padStart(2, '0');
if (secondsRemaining <= 300) {
timerDisplay.classList.add('timer-warning');
} else {
timerDisplay.classList.remove('timer-warning');
}
}
// ──────────────────────────────────────────────
// Quiz submission & scoring
// ──────────────────────────────────────────────
startPaymentBtn.addEventListener('click', function () {
submitQuiz();
});
function submitQuiz() {
stopTimer();
var score = 0;
quizQuestions.forEach(function (q, index) {
var selected = quizForm.querySelector('input[name="question' + index + '"]:checked');
if (selected && selected.value === q.answer) {
score++;
}
});
var total = quizQuestions.length;
var pct = Math.round((score / total) * 100);
document.getElementById('quiz-container').style.display = 'none';
resultsContainer.style.display = 'block';
resultsScore.innerHTML =
'You scored <strong>' + score + ' / ' + total + '</strong> (' + pct + '%)';
}
retakeBtn.addEventListener('click', function () {
resultsContainer.style.display = 'none';
document.getElementById('quiz-container').style.display = 'block';
renderQuiz();
startTimer();
});
proceedPaymentBtn.addEventListener('click', function () {
resultsContainer.style.display = 'none';
paymentContainer.style.display = 'block';
});
// ──────────────────────────────────────────────
// Stripe payment integration
// ──────────────────────────────────────────────
var stripe = Stripe('your-publishable-key-here'); // Replace with your Stripe publishable key
var elements = stripe.elements();
var card = elements.create('card');
card.mount('#card-element');
document.getElementById('payment-form').addEventListener('submit', function (event) {
event.preventDefault();
stripe.createToken(card).then(function (result) {
if (result.error) {
console.error(result.error);
alert('Payment failed: ' + result.error.message);
} else {
fetch('/charge', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ token: result.token.id })
})
.then(function (response) { return response.json(); })
.then(function (data) {
if (data.success) {
alert('\uD83C\uDF89 Payment successful! You now have full access.');
} else {
alert('Payment failed. Please try again.');
}
});
}
});
});
});