Skip to content

Commit b48933e

Browse files
Nimon77FreekBes
andauthored
Enable the login button only when exam has started (#10)
Also includes a countdown until the exam start --------- Co-authored-by: Freek Bes <freek.bes@codam.nl>
1 parent bf3019d commit b48933e

File tree

3 files changed

+48
-3
lines changed

3 files changed

+48
-3
lines changed

client/uis/screen.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export interface UIExamModeElements {
2323
examStartText: HTMLSpanElement;
2424
examEndText: HTMLSpanElement;
2525
examStartButton: HTMLButtonElement;
26+
examStartTimer: HTMLParagraphElement;
2627
}
2728

2829
export abstract class UIScreen {

client/uis/screens/examscreen.ts

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ export class ExamModeUI extends UIScreen {
1111
private _examMode: boolean = false;
1212
private _examIds: number[] = [];
1313
private _loginScreen: LoginScreenUI;
14+
private _examStartButtonEnableInterval: ReturnType<typeof setInterval> | null = null;
15+
private _examStartTime = new Date("2099-01-01T00:00:00Z"); // Set to a future date so that the button is disabled by default
1416
protected _events: AuthenticatorEvents = {
1517
authenticationStart: () => {
1618
this._disableForm();
@@ -43,6 +45,7 @@ export class ExamModeUI extends UIScreen {
4345
examStartText: document.getElementById('exam-mode-start') as HTMLSpanElement,
4446
examEndText: document.getElementById('exam-mode-end') as HTMLSpanElement,
4547
examStartButton: document.getElementById('exam-mode-start-button') as HTMLButtonElement,
48+
examStartTimer: document.getElementById('exam-mode-start-timer') as HTMLParagraphElement,
4649
} as UIExamModeElements;
4750

4851
this._initForm();
@@ -100,6 +103,16 @@ export class ExamModeUI extends UIScreen {
100103
});
101104
}
102105

106+
private _clearExamStartTimer(): void {
107+
const form = this._form as UIExamModeElements;
108+
if (this._examStartButtonEnableInterval) {
109+
clearTimeout(this._examStartButtonEnableInterval);
110+
this._examStartButtonEnableInterval = null;
111+
}
112+
form.examStartTimer.innerText = "Click the arrow below to start your exam.";
113+
this._enableOrDisableSubmitButton();
114+
}
115+
103116
private _populateData(examsToPopulate: ExamForHost[]): void {
104117
const form = this._form as UIExamModeElements;
105118

@@ -108,6 +121,9 @@ export class ExamModeUI extends UIScreen {
108121
form.examProjectsText.innerText = '';
109122
form.examStartText.innerText = 'unknown';
110123
form.examEndText.innerText = 'unknown';
124+
// Clear the timeout for the exam start button and disable it
125+
this._examStartTime = new Date("2099-01-01T00:00:00Z");
126+
this._clearExamStartTimer();
111127
}
112128
else {
113129
// Find all exams in the data.json file that match the ids in the exams variable
@@ -144,14 +160,42 @@ export class ExamModeUI extends UIScreen {
144160
form.examProjectsText.innerText = projectsText;
145161
form.examStartText.innerText = earliestExam.toLocaleTimeString("en-NL", { hour: '2-digit', minute: '2-digit' });
146162
form.examEndText.innerText = latestExam.toLocaleTimeString("en-NL", { hour: '2-digit', minute: '2-digit' });
163+
164+
// Enable or disable the exam start button based on the current time
165+
this._clearExamStartTimer();
166+
this._examStartTime = earliestExam;
167+
this._enableOrDisableSubmitButton();
168+
if (this._examStartTime.getTime() > Date.now()) {
169+
this._examStartButtonEnableInterval = setInterval(() => {
170+
const timeLeft = Math.floor((this._examStartTime.getTime() - Date.now()) / 1000);
171+
const minutes = Math.floor(timeLeft / 60);
172+
const seconds = timeLeft % 60;
173+
const formattedTime = `${(minutes > 0 ? `${minutes} minutes and ` : '')} ${seconds} seconds`;
174+
form.examStartTimer.innerText = `You may start your exam in ${formattedTime}.`;
175+
176+
if (this._examStartTime.getTime() <= Date.now()) {
177+
// Clear the timer and enable the button
178+
this._clearExamStartTimer();
179+
return;
180+
}
181+
}, 1000);
182+
}
147183
}
148184
}
149185

150186
// Returns true if the exam-start button is disabled, false otherwise
151187
protected _enableOrDisableSubmitButton(): boolean {
152188
const form = this._form as UIExamModeElements;
153-
form.examStartButton.disabled = false; // Always enable the button
154-
return false;
189+
const buttonDisabled = this._examStartTime.getTime() > Date.now(); // Disable the button if the exam start time is in the future
190+
form.examStartButton.disabled = buttonDisabled;
191+
if (!buttonDisabled) {
192+
form.examStartTimer.innerText = "Click the arrow below to start your exam.";
193+
const focusInput = this._getInputToFocusOn();
194+
if (focusInput) {
195+
focusInput.focus();
196+
}
197+
}
198+
return buttonDisabled;
155199
}
156200

157201
protected _wigglePasswordInput(clearInput: boolean = true): void {

static/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ <h4 id="active-user-session-login-name">username</h4>
5353
<h3>This computer is reserved for an exam between <span id="exam-mode-start">...</span> and <span id="exam-mode-end">...</span>.</h3>
5454
<p><span id="exam-mode-projects"></span></p>
5555
<br>
56-
<p>Click the arrow below to start your exam.</p>
56+
<p id="exam-mode-start-timer">Click the arrow below to start your exam.</p>
5757
<button type="submit" id="exam-mode-start-button" title="Start your exam">&#10149;</button>
5858
</form>
5959

0 commit comments

Comments
 (0)