Merge pull request #2970 from dpalou/MOBILE-3667

MOBILE-3667 myovervew: Fix last access sort after view course
main
Pau Ferrer Ocaña 2021-10-05 13:44:47 +02:00 committed by GitHub
commit be8a7b35b7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 52 additions and 18 deletions

View File

@ -16,8 +16,8 @@ import { Component, OnInit, Input, OnDestroy, ViewChild, OnChanges, SimpleChange
import { IonSearchbar } from '@ionic/angular'; import { IonSearchbar } from '@ionic/angular';
import { CoreEventObserver, CoreEvents } from '@singletons/events'; import { CoreEventObserver, CoreEvents } from '@singletons/events';
import { CoreTimeUtils } from '@services/utils/time'; import { CoreTimeUtils } from '@services/utils/time';
import { CoreSites } from '@services/sites'; import { CoreSites, CoreSitesReadingStrategy } from '@services/sites';
import { CoreCoursesProvider, CoreCourses } from '@features/courses/services/courses'; import { CoreCoursesProvider, CoreCourses, CoreCoursesMyCoursesUpdatedEventData } from '@features/courses/services/courses';
import { CoreCoursesHelper, CoreEnrolledCourseDataWithOptions } from '@features/courses/services/courses-helper'; import { CoreCoursesHelper, CoreEnrolledCourseDataWithOptions } from '@features/courses/services/courses-helper';
import { CoreCourseHelper, CorePrefetchStatusInfo } from '@features/course/services/course-helper'; import { CoreCourseHelper, CorePrefetchStatusInfo } from '@features/course/services/course-helper';
import { CoreCourseOptionsDelegate } from '@features/course/services/course-options-delegate'; import { CoreCourseOptionsDelegate } from '@features/course/services/course-options-delegate';
@ -162,7 +162,7 @@ export class AddonBlockMyOverviewComponent extends CoreBlockBaseComponent implem
CoreCoursesProvider.EVENT_MY_COURSES_UPDATED, CoreCoursesProvider.EVENT_MY_COURSES_UPDATED,
(data) => { (data) => {
if (data.action == CoreCoursesProvider.ACTION_ENROL || data.action == CoreCoursesProvider.ACTION_STATE_CHANGED) { if (this.shouldRefreshOnUpdatedEvent(data)) {
this.refreshCourseList(); this.refreshCourseList();
} }
}, },
@ -224,16 +224,16 @@ export class AddonBlockMyOverviewComponent extends CoreBlockBaseComponent implem
} }
/** /**
* Fetch the courses for my overview. * @inheritdoc
*
* @return Promise resolved when done.
*/ */
protected async fetchContent(): Promise<void> { protected async fetchContent(refresh?: boolean): Promise<void> {
const config = this.block.configsRecord; const config = this.block.configsRecord;
const showCategories = config?.displaycategories?.value == '1'; const showCategories = config?.displaycategories?.value == '1';
const courses = await CoreCoursesHelper.getUserCoursesWithOptions(this.sort, undefined, undefined, showCategories); const courses = await CoreCoursesHelper.getUserCoursesWithOptions(this.sort, undefined, undefined, showCategories, {
readingStrategy: refresh ? CoreSitesReadingStrategy.PREFER_NETWORK : undefined,
});
// Check to show sort by short name only if the text is visible. // Check to show sort by short name only if the text is visible.
if (courses.length > 0) { if (courses.length > 0) {
@ -335,6 +335,41 @@ export class AddonBlockMyOverviewComponent extends CoreBlockBaseComponent implem
return showCondition ? (disabledCondition ? 'disabled' : 'show') : 'hidden'; return showCondition ? (disabledCondition ? 'disabled' : 'show') : 'hidden';
} }
/**
* Whether list should be refreshed based on a EVENT_MY_COURSES_UPDATED event.
*
* @param data Event data.
* @return Whether to refresh.
*/
protected shouldRefreshOnUpdatedEvent(data: CoreCoursesMyCoursesUpdatedEventData): boolean {
if (data.action == CoreCoursesProvider.ACTION_ENROL) {
// Always update if user enrolled in a course.
return true;
}
if (data.action == CoreCoursesProvider.ACTION_STATE_CHANGED) {
// Update list when course state changes (favourite, hidden).
return true;
}
if (data.action == CoreCoursesProvider.ACTION_VIEW && data.courseId != CoreSites.getCurrentSiteHomeId()) {
// User viewed a course. If it isn't the most recent accessed course, update the list.
let recentAccessedCourse: CoreEnrolledCourseDataWithOptions | undefined;
if (this.sort == 'lastaccess') {
recentAccessedCourse = this.courses.allincludinghidden[0];
} else {
recentAccessedCourse = Array.from(this.courses.allincludinghidden)
.sort((a, b) => (b.lastaccess || 0) - (a.lastaccess || 0))[0];
}
if (recentAccessedCourse && data.courseId != recentAccessedCourse.id) {
return true;
}
}
return false;
}
/** /**
* The filter has changed. * The filter has changed.
* *
@ -403,12 +438,6 @@ export class AddonBlockMyOverviewComponent extends CoreBlockBaseComponent implem
protected async refreshCourseList(): Promise<void> { protected async refreshCourseList(): Promise<void> {
CoreEvents.trigger(CoreCoursesProvider.EVENT_MY_COURSES_REFRESHED); CoreEvents.trigger(CoreCoursesProvider.EVENT_MY_COURSES_REFRESHED);
try {
await CoreCourses.invalidateUserCourses();
} catch (error) {
// Ignore errors.
}
await this.loadContent(true); await this.loadContent(true);
} }

View File

@ -14,7 +14,7 @@
import { Injectable } from '@angular/core'; import { Injectable } from '@angular/core';
import { CoreUtils } from '@services/utils/utils'; import { CoreUtils } from '@services/utils/utils';
import { CoreSites } from '@services/sites'; import { CoreSites, CoreSitesCommonWSOptions } from '@services/sites';
import { import {
CoreCourseAnyCourseDataWithOptions, CoreCourseAnyCourseDataWithOptions,
CoreCourses, CoreCourses,
@ -216,9 +216,14 @@ export class CoreCoursesHelperProvider {
slice: number = 0, slice: number = 0,
filter?: string, filter?: string,
loadCategoryNames: boolean = false, loadCategoryNames: boolean = false,
options: CoreSitesCommonWSOptions = {},
): Promise<CoreEnrolledCourseDataWithOptions[]> { ): Promise<CoreEnrolledCourseDataWithOptions[]> {
let courses: CoreEnrolledCourseDataWithOptions[] = await CoreCourses.getUserCourses(); let courses: CoreEnrolledCourseDataWithOptions[] = await CoreCourses.getUserCourses(
false,
options.siteId,
options.readingStrategy,
);
if (courses.length <= 0) { if (courses.length <= 0) {
return []; return [];
} }
@ -227,7 +232,7 @@ export class CoreCoursesHelperProvider {
const courseIds = courses.map((course) => course.id); const courseIds = courses.map((course) => course.id);
// Load course options of the course. // Load course options of the course.
promises.push(CoreCourses.getCoursesAdminAndNavOptions(courseIds).then((options) => { promises.push(CoreCourses.getCoursesAdminAndNavOptions(courseIds, options.siteId).then((options) => {
courses.forEach((course) => { courses.forEach((course) => {
course.navOptions = options.navOptions[course.id]; course.navOptions = options.navOptions[course.id];
course.admOptions = options.admOptions[course.id]; course.admOptions = options.admOptions[course.id];
@ -290,7 +295,7 @@ export class CoreCoursesHelperProvider {
} }
try { try {
const completion = await AddonCourseCompletion.getCompletion(course.id); const completion = await AddonCourseCompletion.getCompletion(course.id, undefined, undefined, options.siteId);
course.completed = completion?.completed; course.completed = completion?.completed;
} catch { } catch {