From 61df45a76d0e7c051dfe28dcbb9e89f51a10cb68 Mon Sep 17 00:00:00 2001 From: Rayson Yeap <88478542+Respirayson@users.noreply.github.com> Date: Mon, 15 Jul 2024 21:03:21 +0800 Subject: [PATCH] [#12655] Instructor Edit Session Page: If make session visible is later than submission opening time, automatically default to opening time (#13143) * Update functionality to ensure session visibility time defaults to submission opening time * Add unit tests * Fix linting --- .../session-edit-form.component.html | 2 +- .../session-edit-form.component.spec.ts | 36 ++++++++++ .../session-edit-form.component.ts | 71 +++++++++++++++++++ 3 files changed, 108 insertions(+), 1 deletion(-) diff --git a/src/web/app/components/session-edit-form/session-edit-form.component.html b/src/web/app/components/session-edit-form/session-edit-form.component.html index 27aabc5873d..cda6bc80da9 100644 --- a/src/web/app/components/session-edit-form/session-edit-form.component.html +++ b/src/web/app/components/session-edit-form/session-edit-form.component.html @@ -144,7 +144,7 @@
Or
[date]="model.submissionStartDate">
- { let dateTimeService: DateTimeService; const submissionStartDateField = 'submissionStartDate'; + const submissionStartTimeField = 'submissionStartTime'; beforeEach(waitForAsync(() => { TestBed.configureTestingModule({ @@ -94,6 +95,41 @@ describe('SessionEditFormComponent', () => { expect(configureSubmissionOpeningTimeSpy).not.toHaveBeenCalled(); }); + it('should trigger the change of the model when the submission opening time ' + + 'changes to before the visibility time', () => { + const date: DateFormat = { day: 12, month: 7, year: 2024 }; + const time: TimeFormat = { hour: 4, minute: 0 }; + const visibilityTime: TimeFormat = { hour: 14, minute: 0 }; + const triggerModelChangeSpy = jest.spyOn(component, 'triggerModelChange'); + const configureSessionVisibleDateTimeSpy = jest.spyOn(component, 'configureSessionVisibleDateTime'); + component.model.customSessionVisibleDate = date; + component.model.submissionStartDate = date; + component.model.customSessionVisibleTime = visibilityTime; + component.triggerSubmissionOpeningTimeModelChange(submissionStartTimeField, time); + expect(triggerModelChangeSpy).toHaveBeenCalledWith(submissionStartTimeField, time); + expect(configureSessionVisibleDateTimeSpy).toHaveBeenCalledWith(date, time); + }); + + it('should adjust the session visibility date if submission opening date is earlier', () => { + const date: DateFormat = { day: 12, month: 7, year: 2024 }; + const time: TimeFormat = { hour: 14, minute: 0 }; + component.model.customSessionVisibleDate = { day: 13, month: 7, year: 2024 }; + component.model.customSessionVisibleTime = time; + component.configureSessionVisibleDateTime(date, time); + expect(component.model.customSessionVisibleDate).toEqual(date); + expect(component.model.customSessionVisibleTime).toEqual(time); + }); + + it('should not adjust the session visibility date and time if submission opening date and time are later', () => { + const date: DateFormat = component.minDateForSubmissionStart; + const time: TimeFormat = component.minTimeForSubmissionStart; + component.model.customSessionVisibleDate = dateTimeService.getDateInstance(moment().subtract(1, 'days')); + component.model.customSessionVisibleTime = dateTimeService.getTimeInstance(moment().subtract(1, 'hours')); + component.configureSessionVisibleDateTime(date, time); + expect(component.model.customSessionVisibleDate).not.toEqual(date); + expect(component.model.customSessionVisibleTime).not.toEqual(time); + }); + it('should emit a modelChange event with the updated field when triggerModelChange is called', () => { const field = 'courseId'; const data = 'testId'; diff --git a/src/web/app/components/session-edit-form/session-edit-form.component.ts b/src/web/app/components/session-edit-form/session-edit-form.component.ts index 700ed3e1a48..b6fc2791afb 100644 --- a/src/web/app/components/session-edit-form/session-edit-form.component.ts +++ b/src/web/app/components/session-edit-form/session-edit-form.component.ts @@ -128,12 +128,51 @@ export class SessionEditFormComponent { * Triggers the change of the model for the form. */ triggerModelChange(field: string, data: any): void { + if (field === 'submissionStartDate' || field === 'submissionStartTime') { + this.adjustSessionVisibilityTime(data, field); + } this.modelChange.emit({ ...this.model, [field]: data, }); } + /** + * Adjusts session visibility time to ensure it does not occur after submission opening time. + */ + adjustSessionVisibilityTime(value: any, field: string): void { + const submissionDateTime = this.combineDateAndTime( + field === 'submissionStartDate' ? value : this.model.submissionStartDate, + field === 'submissionStartTime' ? value : this.model.submissionStartTime, + ); + + const visibilityDateTime = this.combineDateAndTime( + this.model.customSessionVisibleDate, + this.model.customSessionVisibleTime, + ); + + if (submissionDateTime.isBefore(visibilityDateTime)) { + if (field === 'submissionStartDate') { + this.model.customSessionVisibleDate = value; + } else { + this.model.customSessionVisibleTime = value; + } + } + } + + /** + * Combines date and time into a single moment instance. + */ + combineDateAndTime(date: DateFormat, time: TimeFormat): moment.Moment { + return moment.tz({ + year: date.year, + month: date.month - 1, + day: date.day, + hour: time.hour, + minute: time.minute, + }, this.model.timeZone); + } + /** * Triggers the change of the model when the submission opening date changes. */ @@ -148,6 +187,7 @@ export class SessionEditFormComponent { this.model.submissionStartTime = minTime; } + this.adjustSessionVisibilityTime(date, field); this.triggerModelChange(field, date); } @@ -164,6 +204,37 @@ export class SessionEditFormComponent { } } + /** + * Triggers the change of the model when the submission opening time changes. + */ + triggerSubmissionOpeningTimeModelChange(field: string, time: TimeFormat): void { + const date: DateFormat = this.model.submissionStartDate; + const sessionDate: DateFormat = this.model.customSessionVisibleDate; + const sessionTime: TimeFormat = this.model.customSessionVisibleTime; + + if (DateTimeService.compareDateFormat(date, sessionDate) === 0 + && DateTimeService.compareTimeFormat(time, sessionTime) === -1) { + this.configureSessionVisibleDateTime(date, time); + } + + this.triggerModelChange(field, time); + } + + /** + * Configures the session visible date and time to ensure it is not after submission opening time. + */ + configureSessionVisibleDateTime(date: DateFormat, time: TimeFormat): void { + const sessionDate: DateFormat = this.model.customSessionVisibleDate; + const sessionTime: TimeFormat = this.model.customSessionVisibleTime; + + if (DateTimeService.compareDateFormat(date, sessionDate) === -1) { + this.model.customSessionVisibleDate = date; + } else if (DateTimeService.compareDateFormat(date, sessionDate) === 0 + && DateTimeService.compareTimeFormat(time, sessionTime) === -1) { + this.model.customSessionVisibleTime = time; + } + } + /** * Handles course Id change event. *