From 4f56e08f9ba89f530d363bae78d9a4b2852cf2d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pau=20Ferrer=20Oca=C3=B1a?= Date: Thu, 21 Mar 2024 15:50:49 +0100 Subject: [PATCH] MOBILE-4543 course: Improvements on course options delegate --- src/addons/badges/services/handlers/user.ts | 2 +- .../blog/services/handlers/course-option.ts | 2 +- .../services/handlers/course-option.ts | 8 +- .../services/handlers/course-option.ts | 4 +- .../notes/services/handlers/course-option.ts | 6 +- .../course-summary/course-summary.page.ts | 2 +- .../services/course-options-delegate.ts | 143 ++++++++++-------- src/core/features/course/services/course.ts | 25 ++- .../features/grades/services/grades-helper.ts | 6 +- .../grades/services/handlers/course-option.ts | 2 +- .../handlers/course-participants-option.ts | 2 +- .../user/services/handlers/course-option.ts | 8 +- 12 files changed, 118 insertions(+), 92 deletions(-) diff --git a/src/addons/badges/services/handlers/user.ts b/src/addons/badges/services/handlers/user.ts index 54cf3e552..1f8201bdf 100644 --- a/src/addons/badges/services/handlers/user.ts +++ b/src/addons/badges/services/handlers/user.ts @@ -64,7 +64,7 @@ export class AddonBadgesUserHandlerService implements CoreUserProfileHandler { return false; } - if (navOptions && navOptions.badges !== undefined) { + if (navOptions?.badges !== undefined) { return navOptions.badges; } diff --git a/src/addons/blog/services/handlers/course-option.ts b/src/addons/blog/services/handlers/course-option.ts index 9a996d903..7ff4decfc 100644 --- a/src/addons/blog/services/handlers/course-option.ts +++ b/src/addons/blog/services/handlers/course-option.ts @@ -61,7 +61,7 @@ export class AddonBlogCourseOptionHandlerService implements CoreCourseOptionsHan ): Promise { const enabled = await CoreCourseHelper.hasABlockNamed(courseId, 'blog_menu'); - if (enabled && navOptions && navOptions.blogs !== undefined) { + if (enabled && navOptions?.blogs !== undefined) { return navOptions.blogs; } diff --git a/src/addons/competency/services/handlers/course-option.ts b/src/addons/competency/services/handlers/course-option.ts index ac611c4d8..f8148c170 100644 --- a/src/addons/competency/services/handlers/course-option.ts +++ b/src/addons/competency/services/handlers/course-option.ts @@ -13,7 +13,7 @@ // limitations under the License. import { Injectable } from '@angular/core'; -import { CoreCourseProvider } from '@features/course/services/course'; +import { CoreCourseAccessDataType } from '@features/course/services/course'; import { CoreCourseAccess, CoreCourseOptionsHandler, @@ -50,11 +50,11 @@ export class AddonCompetencyCourseOptionHandlerService implements CoreCourseOpti accessData: CoreCourseAccess, navOptions?: CoreCourseUserAdminOrNavOptionIndexed, ): Promise { - if (accessData && accessData.type === CoreCourseProvider.ACCESS_GUEST) { + if (accessData && accessData.type === CoreCourseAccessDataType.ACCESS_GUEST) { return false; // Not enabled for guest access. } - if (navOptions && navOptions.competencies !== undefined) { + if (navOptions?.competencies !== undefined) { return navOptions.competencies; } @@ -82,7 +82,7 @@ export class AddonCompetencyCourseOptionHandlerService implements CoreCourseOpti * @inheritdoc */ async invalidateEnabledForCourse(courseId: number, navOptions?: CoreCourseUserAdminOrNavOptionIndexed): Promise { - if (navOptions && navOptions.competencies !== undefined) { + if (navOptions?.competencies !== undefined) { // No need to invalidate anything. return; } diff --git a/src/addons/coursecompletion/services/handlers/course-option.ts b/src/addons/coursecompletion/services/handlers/course-option.ts index 8b1419a6b..928cb6691 100644 --- a/src/addons/coursecompletion/services/handlers/course-option.ts +++ b/src/addons/coursecompletion/services/handlers/course-option.ts @@ -13,7 +13,7 @@ // limitations under the License. import { Injectable } from '@angular/core'; -import { CoreCourseProvider } from '@features/course/services/course'; +import { CoreCourseAccessDataType } from '@features/course/services/course'; import { CoreCourseAccess, CoreCourseOptionsHandler, @@ -43,7 +43,7 @@ export class AddonCourseCompletionCourseOptionHandlerService implements CoreCour * @inheritdoc */ async isEnabledForCourse(courseId: number, accessData: CoreCourseAccess): Promise { - if (accessData && accessData.type === CoreCourseProvider.ACCESS_GUEST) { + if (accessData && accessData.type === CoreCourseAccessDataType.ACCESS_GUEST) { return false; // Not enabled for guest access. } diff --git a/src/addons/notes/services/handlers/course-option.ts b/src/addons/notes/services/handlers/course-option.ts index 736e9e6da..acb9153e6 100644 --- a/src/addons/notes/services/handlers/course-option.ts +++ b/src/addons/notes/services/handlers/course-option.ts @@ -13,7 +13,7 @@ // limitations under the License. import { Injectable } from '@angular/core'; -import { CoreCourseProvider } from '@features/course/services/course'; +import { CoreCourseAccessDataType } from '@features/course/services/course'; import { CoreCourseAccess, CoreCourseOptionsHandler, @@ -47,11 +47,11 @@ export class AddonNotesCourseOptionHandlerService implements CoreCourseOptionsHa accessData: CoreCourseAccess, navOptions?: CoreCourseUserAdminOrNavOptionIndexed, ): Promise { - if (accessData && accessData.type === CoreCourseProvider.ACCESS_GUEST) { + if (accessData && accessData.type === CoreCourseAccessDataType.ACCESS_GUEST) { return false; // Not enabled for guest access. } - if (navOptions && navOptions.notes !== undefined) { + if (navOptions?.notes !== undefined) { return navOptions.notes; } diff --git a/src/core/features/course/pages/course-summary/course-summary.page.ts b/src/core/features/course/pages/course-summary/course-summary.page.ts index c1943250d..0572a7f7d 100644 --- a/src/core/features/course/pages/course-summary/course-summary.page.ts +++ b/src/core/features/course/pages/course-summary/course-summary.page.ts @@ -254,7 +254,7 @@ export class CoreCourseSummaryPage implements OnInit, OnDestroy { * @returns Promise resolved when done. */ protected async loadMenuHandlers(refresh?: boolean): Promise { - if (!this.course) { + if (!this.course || !this.canAccessCourse) { return; } diff --git a/src/core/features/course/services/course-options-delegate.ts b/src/core/features/course/services/course-options-delegate.ts index 2d200c95c..b2f9def37 100644 --- a/src/core/features/course/services/course-options-delegate.ts +++ b/src/core/features/course/services/course-options-delegate.ts @@ -24,10 +24,9 @@ import { CoreCoursesProvider, CoreCourseUserAdminOrNavOptionIndexed, } from '@features/courses/services/courses'; -import { CoreCourseProvider } from './course'; +import { CoreCourseAccessDataType } from './course'; import { Params } from '@angular/router'; import { makeSingleton } from '@singletons'; -import { CoreEnrolledCourseDataWithExtraInfoAndOptions } from '@features/courses/services/courses-helper'; import { CorePromisedValue } from '@classes/promised-value'; /** @@ -313,20 +312,20 @@ export class CoreCourseOptionsDelegateService extends CoreDelegate { + ): Promise { // If the handlers aren't loaded, do not refresh. if (!this.loaded[courseId]) { refresh = false; } - if (refresh || !this.coursesHandlers[courseId] || this.coursesHandlers[courseId].access.type != accessData.type) { + if (refresh || !this.coursesHandlers[courseId] || this.coursesHandlers[courseId].access.type !== accessData.type) { if (!this.coursesHandlers[courseId]) { this.coursesHandlers[courseId] = { access: accessData, @@ -347,8 +346,6 @@ export class CoreCourseOptionsDelegateService extends CoreDelegate { - return this.getHandlersToDisplayInternal(false, course, refresh, isGuest, navOptions, admOptions) as + return this.getHandlersToDisplayInternal(false, course, refresh, isGuest) as Promise; } @@ -380,18 +373,14 @@ export class CoreCourseOptionsDelegateService extends CoreDelegate { - return this.getHandlersToDisplayInternal(true, course, refresh, isGuest, navOptions, admOptions) as + return this.getHandlersToDisplayInternal(true, course, refresh, isGuest) as Promise; } @@ -403,8 +392,6 @@ export class CoreCourseOptionsDelegateService extends CoreDelegate { const courseWithOptions: CoreCourseAnyCourseDataWithOptions = course; const accessData = { - type: isGuest ? CoreCourseProvider.ACCESS_GUEST : CoreCourseProvider.ACCESS_DEFAULT, + type: isGuest ? CoreCourseAccessDataType.ACCESS_GUEST : CoreCourseAccessDataType.ACCESS_DEFAULT, }; const handlersToDisplay: CoreCourseOptionsHandlerToDisplay[] | CoreCourseOptionsMenuHandlerToDisplay[] = []; - if (navOptions) { - courseWithOptions.navOptions = navOptions; - } - if (admOptions) { - courseWithOptions.admOptions = admOptions; - } - await this.loadCourseOptions(courseWithOptions, refresh); - // Call getHandlersForAccess to make sure the handlers have been loaded. - await this.getHandlersForAccess(course.id, refresh, accessData, courseWithOptions.navOptions, courseWithOptions.admOptions); + // Call updateHandlersForAccess to make sure the handlers have been loaded. + await this.updateHandlersForAccess( + course.id, + refresh, + accessData, + courseWithOptions.navOptions, + courseWithOptions.admOptions, + ); const promises: Promise[] = []; - let handlerList: CoreCourseOptionsMenuHandler[] | CoreCourseOptionsHandler[]; - if (menu) { - handlerList = this.coursesHandlers[course.id].enabledMenuHandlers; - } else { - handlerList = this.coursesHandlers[course.id].enabledHandlers; - } + const handlerList = menu + ? this.coursesHandlers[course.id].enabledMenuHandlers + : this.coursesHandlers[course.id].enabledHandlers; handlerList.forEach((handler: CoreCourseOptionsMenuHandler | CoreCourseOptionsHandler) => { const getFunction = menu @@ -461,8 +442,8 @@ export class CoreCourseOptionsDelegateService extends CoreDelegate { - this.logger.error('Error getting data for handler', handler.name, err); + }).catch((error) => { + this.logger.error(`Error getting data for handler ${handler.name}`, error); })); }); @@ -477,17 +458,44 @@ export class CoreCourseOptionsDelegateService extends CoreDelegate { + await this.updateHandlersForAccess(courseId, refresh, accessData, navOptions, admOptions); + + const handlers = this.coursesHandlers[courseId].enabledHandlers; + + return !!(handlers && handlers.length); + } + /** * Check if a course has any handler enabled for default access, using course object. * * @param course The course object. * @param refresh True if it should refresh the list. * @returns Promise resolved with boolean: true if it has handlers, false otherwise. + * @deprecated since 4.4. */ - async hasHandlersForCourse(course: CoreEnrolledCourseDataWithExtraInfoAndOptions, refresh = false): Promise { + async hasHandlersForCourse(course: CoreCourseAnyCourseDataWithOptions, refresh = false): Promise { // Load course options if missing. await this.loadCourseOptions(course, refresh); + // eslint-disable-next-line deprecation/deprecation return this.hasHandlersForDefault(course.id, refresh, course.navOptions, course.admOptions); } @@ -499,6 +507,7 @@ export class CoreCourseOptionsDelegateService extends CoreDelegate { - // Default access. - const accessData = { - type: CoreCourseProvider.ACCESS_DEFAULT, - }; - - const handlers = await this.getHandlersForAccess(courseId, refresh, accessData, navOptions, admOptions); - - return !!(handlers && handlers.length); + // eslint-disable-next-line deprecation/deprecation + return await this.hasHandlersForAccess( + courseId, + refresh, + { type: CoreCourseAccessDataType.ACCESS_DEFAULT }, + navOptions, + admOptions, + ); } /** @@ -524,6 +533,7 @@ export class CoreCourseOptionsDelegateService extends CoreDelegate { - // Guest access. - const accessData = { - type: CoreCourseProvider.ACCESS_GUEST, - }; - - const handlers = await this.getHandlersForAccess(courseId, refresh, accessData, navOptions, admOptions); - - return !!(handlers && handlers.length); + // eslint-disable-next-line deprecation/deprecation + return await this.hasHandlersForAccess( + courseId, + refresh, + { type: CoreCourseAccessDataType.ACCESS_GUEST }, + navOptions, + admOptions, + ); } /** @@ -547,7 +557,7 @@ export class CoreCourseOptionsDelegateService extends CoreDelegate { + protected async invalidateCourseHandlers(courseId: number): Promise { const promises: Promise[] = []; const courseData = this.coursesHandlers[courseId]; @@ -556,7 +566,7 @@ export class CoreCourseOptionsDelegateService extends CoreDelegate { - if (handler?.invalidateEnabledForCourse) { + if (handler.invalidateEnabledForCourse) { promises.push( handler.invalidateEnabledForCourse(courseId, courseData.navOptions, courseData.admOptions), ); @@ -579,7 +589,7 @@ export class CoreCourseOptionsDelegateService extends CoreDelegate { - if (course.navOptions === undefined || course.admOptions === undefined || refresh) { - - const options = await CoreCourses.getCoursesAdminAndNavOptions([course.id]); - course.navOptions = options.navOptions[course.id]; - course.admOptions = options.admOptions[course.id]; + if (!refresh && course.navOptions !== undefined && course.admOptions !== undefined) { + return; } + + const options = await CoreCourses.getCoursesAdminAndNavOptions([course.id]); + course.navOptions = options.navOptions[course.id]; + course.admOptions = options.admOptions[course.id]; } /** @@ -618,7 +629,7 @@ export class CoreCourseOptionsDelegateService extends CoreDelegate { - if (accessData && accessData.type == CoreCourseProvider.ACCESS_GUEST) { + if (accessData && accessData.type === CoreCourseAccessDataType.ACCESS_GUEST) { return false; // Not enabled for guests. } - if (navOptions && navOptions.grades !== undefined) { + if (navOptions?.grades !== undefined) { return navOptions.grades; } diff --git a/src/core/features/grades/services/handlers/course-option.ts b/src/core/features/grades/services/handlers/course-option.ts index b6f7f1448..46f68843a 100644 --- a/src/core/features/grades/services/handlers/course-option.ts +++ b/src/core/features/grades/services/handlers/course-option.ts @@ -38,7 +38,7 @@ export class CoreGradesCourseOptionHandlerService implements CoreCourseOptionsHa async invalidateEnabledForCourse(courseId: number, navOptions?: CoreCourseUserAdminOrNavOptionIndexed): Promise { await CoreGrades.invalidateCourseGradesPermissionsData(courseId); - if (navOptions && navOptions.grades !== undefined) { + if (navOptions?.grades !== undefined) { // No need to invalidate user courses. return; } diff --git a/src/core/features/grades/services/handlers/course-participants-option.ts b/src/core/features/grades/services/handlers/course-participants-option.ts index 5fc6bfa69..943611c0a 100644 --- a/src/core/features/grades/services/handlers/course-participants-option.ts +++ b/src/core/features/grades/services/handlers/course-participants-option.ts @@ -38,7 +38,7 @@ export class CoreGradesCourseParticipantsOptionHandlerService implements CoreCou async invalidateEnabledForCourse(courseId: number, navOptions?: CoreCourseUserAdminOrNavOptionIndexed): Promise { await CoreGrades.invalidateCourseGradesPermissionsData(courseId); - if (navOptions && navOptions.grades !== undefined) { + if (navOptions?.grades !== undefined) { // No need to invalidate user courses. return; } diff --git a/src/core/features/user/services/handlers/course-option.ts b/src/core/features/user/services/handlers/course-option.ts index 2d668ad66..322799f5f 100644 --- a/src/core/features/user/services/handlers/course-option.ts +++ b/src/core/features/user/services/handlers/course-option.ts @@ -13,7 +13,7 @@ // limitations under the License. import { Injectable } from '@angular/core'; -import { CoreCourseProvider } from '@features/course/services/course'; +import { CoreCourseAccessDataType } from '@features/course/services/course'; import { CoreCourseAccess, CoreCourseOptionsHandler, @@ -37,7 +37,7 @@ export class CoreUserCourseOptionHandlerService implements CoreCourseOptionsHand * @inheritdoc */ invalidateEnabledForCourse(courseId: number, navOptions?: CoreCourseUserAdminOrNavOptionIndexed): Promise { - if (navOptions && navOptions.participants !== undefined) { + if (navOptions?.participants !== undefined) { // No need to invalidate anything. return Promise.resolve(); } @@ -60,11 +60,11 @@ export class CoreUserCourseOptionHandlerService implements CoreCourseOptionsHand accessData: CoreCourseAccess, navOptions?: CoreCourseUserAdminOrNavOptionIndexed, ): boolean | Promise { - if (accessData && accessData.type == CoreCourseProvider.ACCESS_GUEST) { + if (accessData && accessData.type === CoreCourseAccessDataType.ACCESS_GUEST) { return false; // Not enabled for guests. } - if (navOptions && navOptions.participants !== undefined) { + if (navOptions?.participants !== undefined) { return navOptions.participants; }