Skip to content

Commit

Permalink
[#12655] Instructor Edit Session Page: If make session visible is lat…
Browse files Browse the repository at this point in the history
…er 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
  • Loading branch information
Respirayson authored Jul 15, 2024
1 parent 3f5f68d commit 61df45a
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ <h5>Or</h5>
[date]="model.submissionStartDate"></tm-datepicker>
</div>
<div class="col-md-5">
<tm-timepicker id="submission-start-time" [isDisabled]="!model.isEditable" (timeChange)="triggerModelChange('submissionStartTime', $event)"
<tm-timepicker id="submission-start-time" [isDisabled]="!model.isEditable" (timeChange)="triggerSubmissionOpeningTimeModelChange('submissionStartTime', $event)"
[minDate]="minDateForSubmissionStart" [maxDate]="maxDateForSubmissionStart"
[date]="model.submissionStartDate"
[minTime]="minTimeForSubmissionStart" [maxTime]="maxTimeForSubmissionStart"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ describe('SessionEditFormComponent', () => {
let dateTimeService: DateTimeService;

const submissionStartDateField = 'submissionStartDate';
const submissionStartTimeField = 'submissionStartTime';

beforeEach(waitForAsync(() => {
TestBed.configureTestingModule({
Expand Down Expand Up @@ -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';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand All @@ -148,6 +187,7 @@ export class SessionEditFormComponent {
this.model.submissionStartTime = minTime;
}

this.adjustSessionVisibilityTime(date, field);
this.triggerModelChange(field, date);
}

Expand All @@ -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.
*
Expand Down

0 comments on commit 61df45a

Please sign in to comment.