From 53ce905a9c8cc8abcb0f5b8e5724269afbc14381 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pau=20Ferrer=20Oca=C3=B1a?= Date: Fri, 3 Dec 2021 15:53:50 +0100 Subject: [PATCH] MOBILE-3900 myoverview: Honour course graces periods --- .../components/myoverview/myoverview.ts | 31 +++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/src/addons/block/myoverview/components/myoverview/myoverview.ts b/src/addons/block/myoverview/components/myoverview/myoverview.ts index d99e22201..fa7e2f7fa 100644 --- a/src/addons/block/myoverview/components/myoverview/myoverview.ts +++ b/src/addons/block/myoverview/components/myoverview/myoverview.ts @@ -29,6 +29,7 @@ import { CoreTextUtils } from '@services/utils/text'; import { AddonCourseCompletion } from '@/addons/coursecompletion/services/coursecompletion'; import { AddonBlockMyOverviewFilterOptionsComponent } from '../filteroptions/filteroptions'; import { IonSearchbar } from '@ionic/angular'; +import moment from 'moment'; const FILTER_PRIORITY: AddonBlockMyOverviewTimeFilters[] = ['all', 'inprogress', 'future', 'past']; @@ -98,6 +99,8 @@ export class AddonBlockMyOverviewComponent extends CoreBlockBaseComponent implem protected coursesObserver?: CoreEventObserver; protected updateSiteObserver?: CoreEventObserver; protected fetchContentDefaultError = 'Error getting my overview data.'; + protected gradePeriodAfter = 0; + protected gradePeriodBefore = 0; constructor() { super('AddonBlockMyOverviewComponent'); @@ -261,6 +264,9 @@ export class AddonBlockMyOverviewComponent extends CoreBlockBaseComponent implem this.hasCourses = this.allCourses.length > 0; + this.gradePeriodAfter = parseInt(await this.currentSite.getConfig('coursegraceperiodafter', refresh), 10) || 0; + this.gradePeriodBefore = parseInt(await this.currentSite.getConfig('coursegraceperiodbefore', refresh), 10) || 0; + this.loadSort(); this.loadLayouts(config?.layouts?.value.split(',')); this.loadFilters(config); @@ -507,14 +513,15 @@ export class AddonBlockMyOverviewComponent extends CoreBlockBaseComponent implem // Time filter, favourite and hidden. const today = CoreTimeUtils.timestamp(); + this.filteredCourses = this.filteredCourses.filter((course) => { let include = timeFilter == 'all'; if (!include) { - if ((course.enddate && course.enddate < today) || course.completed) { + if ((course.enddate && this.courseClassifyEndDate(course.enddate) < today) || course.completed) { // Courses that have already ended. include = timeFilter == 'past'; - } else if (course.startdate && course.startdate > today) { + } else if (course.startdate && this.courseClassifyStartDate(course.startdate) > today) { // Courses that have not started yet. include = timeFilter == 'future'; } else { @@ -554,6 +561,26 @@ export class AddonBlockMyOverviewComponent extends CoreBlockBaseComponent implem this.initPrefetchCoursesIcons(); } + /** + * This function calculates the end date to use for display classification purposes, incorporating the grace period, if any. + * + * @param endDate Course end date. + * @return The new enddate. + */ + protected courseClassifyEndDate(endDate: number): number { + return moment(endDate).add(this.gradePeriodAfter, 'days').valueOf(); + } + + /** + * This function calculates the start date to use for display classification purposes, incorporating the grace period, if any. + * + * @param startDate Course start date. + * @return The new startdate. + */ + protected courseClassifyStartDate(startDate: number): number { + return moment(startDate).subtract(this.gradePeriodBefore, 'days').valueOf(); + } + /** * Sort courses *