Skip to content

Commit 0f5416e

Browse files
committed
MOBILE-3881 timeline: Only show in progress courses
1 parent f9b1951 commit 0f5416e

File tree

3 files changed

+76
-48
lines changed

3 files changed

+76
-48
lines changed

src/addons/block/myoverview/components/myoverview/myoverview.ts

Lines changed: 9 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import { CoreDomUtils } from '@services/utils/dom';
2727
import { CoreTextUtils } from '@services/utils/text';
2828
import { AddonCourseCompletion } from '@addons/coursecompletion/services/coursecompletion';
2929
import { IonSearchbar } from '@ionic/angular';
30-
import moment from 'moment';
3130
import { CoreNavigator } from '@services/navigator';
3231

3332
const FILTER_PRIORITY: AddonBlockMyOverviewTimeFilters[] =
@@ -478,13 +477,19 @@ export class AddonBlockMyOverviewComponent extends CoreBlockBaseComponent implem
478477
break;
479478
case 'inprogress':
480479
this.filteredCourses = this.filteredCourses.filter((course) =>
481-
!course.hidden && !this.isPastCourse(course) && !this.isFutureCourse(course));
480+
!course.hidden &&
481+
!CoreCoursesHelper.isPastCourse(course, this.gradePeriodAfter) &&
482+
!CoreCoursesHelper.isFutureCourse(course, this.gradePeriodAfter, this.gradePeriodBefore));
482483
break;
483484
case 'future':
484-
this.filteredCourses = this.filteredCourses.filter((course) => !course.hidden && this.isFutureCourse(course));
485+
this.filteredCourses = this.filteredCourses.filter((course) =>
486+
!course.hidden &&
487+
CoreCoursesHelper.isFutureCourse(course, this.gradePeriodAfter, this.gradePeriodBefore));
485488
break;
486489
case 'past':
487-
this.filteredCourses = this.filteredCourses.filter((course) => !course.hidden && this.isPastCourse(course));
490+
this.filteredCourses = this.filteredCourses.filter((course) =>
491+
!course.hidden &&
492+
CoreCoursesHelper.isPastCourse(course, this.gradePeriodAfter));
488493
break;
489494
case 'favourite':
490495
this.filteredCourses = this.filteredCourses.filter((course) => !course.hidden && course.isfavourite);
@@ -515,44 +520,6 @@ export class AddonBlockMyOverviewComponent extends CoreBlockBaseComponent implem
515520
this.initPrefetchCoursesIcons();
516521
}
517522

518-
/**
519-
* Calculates if course date is past.
520-
*
521-
* @param course Course Object.
522-
* @return Wether the course is past.
523-
*/
524-
protected isPastCourse(course: CoreEnrolledCourseDataWithOptions): boolean {
525-
if (course.completed) {
526-
return true;
527-
}
528-
529-
if (!course.enddate) {
530-
return false;
531-
}
532-
533-
// Calculate the end date to use for display classification purposes, incorporating the grace period, if any.
534-
const endDate = moment(course.enddate * 1000).add(this.gradePeriodAfter, 'days').valueOf();
535-
536-
return endDate < this.today;
537-
}
538-
539-
/**
540-
* Calculates if course date is future.
541-
*
542-
* @param course Course Object.
543-
* @return Wether the course is future.
544-
*/
545-
protected isFutureCourse(course: CoreEnrolledCourseDataWithOptions): boolean {
546-
if (this.isPastCourse(course) || !course.startdate) {
547-
return false;
548-
}
549-
550-
// Calculate the start date to use for display classification purposes, incorporating the grace period, if any.
551-
const startDate = moment(course.startdate * 1000).subtract(this.gradePeriodBefore, 'days').valueOf();
552-
553-
return startDate > this.today;
554-
}
555-
556523
/**
557524
* Sort courses
558525
*

src/addons/block/timeline/components/timeline/timeline.ts

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,10 @@ export class AddonBlockTimelineComponent extends CoreBlockBaseComponent implemen
6363
searchEnabled = false;
6464
searchText = '';
6565

66-
protected courseIds: number[] = [];
66+
protected courseIdsToInvalidate: number[] = [];
6767
protected fetchContentDefaultError = 'Error getting timeline data.';
68+
protected gradePeriodAfter = 0;
69+
protected gradePeriodBefore = 0;
6870

6971
constructor() {
7072
super('AddonBlockTimelineComponent');
@@ -106,8 +108,8 @@ export class AddonBlockTimelineComponent extends CoreBlockBaseComponent implemen
106108
promises.push(AddonBlockTimeline.invalidateActionEventsByCourses());
107109
promises.push(CoreCourses.invalidateUserCourses());
108110
promises.push(CoreCourseOptionsDelegate.clearAndInvalidateCoursesOptions());
109-
if (this.courseIds.length > 0) {
110-
promises.push(CoreCourses.invalidateCoursesByField('ids', this.courseIds.join(',')));
111+
if (this.courseIdsToInvalidate.length > 0) {
112+
promises.push(CoreCourses.invalidateCoursesByField('ids', this.courseIdsToInvalidate.join(',')));
111113
}
112114

113115
return CoreUtils.allPromises(promises);
@@ -172,13 +174,26 @@ export class AddonBlockTimelineComponent extends CoreBlockBaseComponent implemen
172174
* @return Promise resolved when done.
173175
*/
174176
protected async fetchMyOverviewTimelineByCourses(): Promise<void> {
177+
try {
178+
this.gradePeriodAfter = parseInt(await this.currentSite.getConfig('coursegraceperiodafter'), 10);
179+
this.gradePeriodBefore = parseInt(await this.currentSite.getConfig('coursegraceperiodbefore'), 10);
180+
} catch {
181+
this.gradePeriodAfter = 0;
182+
this.gradePeriodBefore = 0;
183+
}
184+
175185
// Do not filter courses by date because they can contain activities due.
176186
this.timelineCourses.courses = await CoreCoursesHelper.getUserCoursesWithOptions();
187+
this.courseIdsToInvalidate = this.timelineCourses.courses.map((course) => course.id);
177188

178-
if (this.timelineCourses.courses.length > 0) {
179-
this.courseIds = this.timelineCourses.courses.map((course) => course.id);
189+
// Filter only in progress courses.
190+
this.timelineCourses.courses = this.timelineCourses.courses.filter((course) =>
191+
!course.hidden &&
192+
!CoreCoursesHelper.isPastCourse(course, this.gradePeriodAfter) &&
193+
!CoreCoursesHelper.isFutureCourse(course, this.gradePeriodAfter, this.gradePeriodBefore));
180194

181-
const courseEvents = await AddonBlockTimeline.getActionEventsByCourses(this.courseIds, this.searchText);
195+
if (this.timelineCourses.courses.length > 0) {
196+
const courseEvents = await AddonBlockTimeline.getActionEventsByCourses(this.courseIdsToInvalidate, this.searchText);
182197

183198
this.timelineCourses.courses = this.timelineCourses.courses.filter((course) => {
184199
if (courseEvents[course.id].events.length == 0) {

src/core/features/courses/services/courses-helper.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import {
2525
import { makeSingleton, Translate } from '@singletons';
2626
import { CoreWSExternalFile } from '@services/ws';
2727
import { AddonCourseCompletion } from '@addons/coursecompletion/services/coursecompletion';
28+
import moment from 'moment';
2829

2930
/**
3031
* Helper to gather some common courses functions.
@@ -293,6 +294,51 @@ export class CoreCoursesHelperProvider {
293294
}));
294295
}
295296

297+
/**
298+
* Calculates if course date is past.
299+
*
300+
* @param course Course Object.
301+
* @param gradePeriodAfter Classify past courses as in progress for these many days after the course end date.
302+
* @return Wether the course is past.
303+
*/
304+
isPastCourse(course: CoreEnrolledCourseDataWithOptions, gradePeriodAfter = 0): boolean {
305+
if (course.completed) {
306+
return true;
307+
}
308+
309+
if (!course.enddate) {
310+
return false;
311+
}
312+
313+
// Calculate the end date to use for display classification purposes, incorporating the grace period, if any.
314+
const endDate = moment(course.enddate * 1000).add(gradePeriodAfter, 'days').valueOf();
315+
316+
return endDate < Date.now();
317+
}
318+
319+
/**
320+
* Calculates if course date is future.
321+
*
322+
* @param course Course Object.
323+
* @param gradePeriodAfter Classify past courses as in progress for these many days after the course end date.
324+
* @param gradePeriodBefore Classify future courses as in progress for these many days prior to the course start date.
325+
* @return Wether the course is future.
326+
*/
327+
isFutureCourse(
328+
course: CoreEnrolledCourseDataWithOptions,
329+
gradePeriodAfter = 0,
330+
gradePeriodBefore = 0,
331+
): boolean {
332+
if (this.isPastCourse(course, gradePeriodAfter) || !course.startdate) {
333+
return false;
334+
}
335+
336+
// Calculate the start date to use for display classification purposes, incorporating the grace period, if any.
337+
const startDate = moment(course.startdate * 1000).subtract(gradePeriodBefore, 'days').valueOf();
338+
339+
return startDate > Date.now();
340+
}
341+
296342
}
297343

298344
export const CoreCoursesHelper = makeSingleton(CoreCoursesHelperProvider);

0 commit comments

Comments
 (0)