From 8afeac3a947bb1f888a30aaf39c8679c5c639e5e Mon Sep 17 00:00:00 2001 From: Dani Palou Date: Tue, 27 Feb 2024 16:14:43 +0100 Subject: [PATCH] MOBILE-4526 filter: Convert block and user contexts to parent --- src/addons/blog/pages/entries/entries.html | 13 +++---- .../pre-rendered-block/pre-rendered-block.ts | 6 +-- .../features/filter/services/filter-helper.ts | 39 +++---------------- src/core/features/filter/services/filter.ts | 24 ++++++++++++ src/core/features/user/pages/about/about.html | 3 +- 5 files changed, 40 insertions(+), 45 deletions(-) diff --git a/src/addons/blog/pages/entries/entries.html b/src/addons/blog/pages/entries/entries.html index ee8584a0834..67120df9f1f 100644 --- a/src/addons/blog/pages/entries/entries.html +++ b/src/addons/blog/pages/entries/entries.html @@ -30,7 +30,7 @@

{{ title | translate }}

+ [contextInstanceId]="contextInstanceId" [courseId]="entry.courseid" />

{{ 'addon.blog.' + entry.publishTranslated! | translate}} @@ -47,8 +47,8 @@

- + @@ -57,10 +57,9 @@

- - + + {{ 'addon.blog.linktooriginalentry' | translate }} diff --git a/src/core/features/block/components/pre-rendered-block/pre-rendered-block.ts b/src/core/features/block/components/pre-rendered-block/pre-rendered-block.ts index fda6ef0a7f2..bb7931eacd1 100644 --- a/src/core/features/block/components/pre-rendered-block/pre-rendered-block.ts +++ b/src/core/features/block/components/pre-rendered-block/pre-rendered-block.ts @@ -36,13 +36,11 @@ export class CoreBlockPreRenderedComponent extends CoreBlockBaseComponent implem * @inheritdoc */ async ngOnInit(): Promise { - await super.ngOnInit(); - this.courseId = this.contextLevel == 'course' ? this.instanceId : undefined; - this.fetchContentDefaultError = 'Error getting ' + this.block.contents?.title + ' data.'; - this.id = `block-${this.block.instanceid}`; + + await super.ngOnInit(); } } diff --git a/src/core/features/filter/services/filter-helper.ts b/src/core/features/filter/services/filter-helper.ts index 5fe6845affb..249cf87f0ee 100644 --- a/src/core/features/filter/services/filter-helper.ts +++ b/src/core/features/filter/services/filter-helper.ts @@ -69,32 +69,6 @@ export class CoreFilterHelperProvider { }); } - /** - * Get the contexts of all blocks in a course. - * - * @param courseId Course ID. - * @param siteId Site ID. If not defined, current site. - * @returns Promise resolved with the contexts. - */ - async getBlocksContexts(courseId: number, siteId?: string): Promise { - // Use stale while revalidate, but always use the first value. If data is updated it will be stored in DB. - const blocks = await firstValueFrom(CoreCourse.getCourseBlocksObservable(courseId, { - readingStrategy: CoreSitesReadingStrategy.STALE_WHILE_REVALIDATE, - siteId, - })); - - const contexts: CoreFiltersGetAvailableInContextWSParamContext[] = []; - - blocks.forEach((block) => { - contexts.push({ - contextlevel: 'block', - instanceid: block.instanceid, - }); - }); - - return contexts; - } - /** * Get some filters from memory cache. If not in cache, get them and store them in cache. * @@ -199,10 +173,14 @@ export class CoreFilterHelperProvider { async getFilters( contextLevel: string, instanceId: number, - options?: CoreFilterFormatTextOptions, + options: CoreFilterFormatTextOptions = {}, siteId?: string, ): Promise { - options = options || {}; + // Check the right context to use. + const newContext = CoreFilter.convertContext(contextLevel, instanceId, { courseId: options.courseId }); + contextLevel = newContext.contextLevel; + instanceId = newContext.instanceId; + options.contextLevel = contextLevel; options.instanceId = instanceId; options.filter = false; @@ -246,11 +224,6 @@ export class CoreFilterHelperProvider { // If enrolled, get all enrolled courses filters with a single call to decrease number of WS calls. const getFilters = () => this.getCourseContexts(instanceId, siteId); - return await this.getCacheableFilters(contextLevel, instanceId, getFilters, options, site); - } else if (contextLevel == 'block' && courseId && CoreBlockHelper.canGetCourseBlocks(site)) { - // Get all the course blocks filters with a single call to decrease number of WS calls. - const getFilters = () => this.getBlocksContexts(courseId, siteId); - return await this.getCacheableFilters(contextLevel, instanceId, getFilters, options, site); } diff --git a/src/core/features/filter/services/filter.ts b/src/core/features/filter/services/filter.ts index b2eb8c3baf4..9f75d7fad52 100644 --- a/src/core/features/filter/services/filter.ts +++ b/src/core/features/filter/services/filter.ts @@ -24,6 +24,7 @@ import { makeSingleton } from '@singletons'; import { CoreEvents, CoreEventSiteData } from '@singletons/events'; import { CoreLogger } from '@singletons/logger'; import { CoreSiteWSPreSets } from '@classes/sites/authenticated-site'; +import { ContextLevel } from '@/core/constants'; /** * Service to provide filter functionalities. @@ -162,6 +163,29 @@ export class CoreFilterProvider { return classified; } + /** + * Given a context level and instance ID, return the proper context to use. + * + * @param contextLevel The context level. + * @param instanceId Instance ID related to the context. + * @param options Options. + * @returns Context to use. + */ + convertContext( + contextLevel: string, + instanceId: number, + options: {courseId?: number} = {}, + ): {contextLevel: string; instanceId: number} { + if (contextLevel === ContextLevel.BLOCK || contextLevel === ContextLevel.USER) { + // Blocks and users cannot have specific filters, use the parent context instead. + return options.courseId ? + { contextLevel: ContextLevel.COURSE, instanceId: options.courseId } : + { contextLevel: ContextLevel.SYSTEM, instanceId: 0 }; + } + + return { contextLevel, instanceId }; + } + /** * Given some HTML code, this function returns the text as safe HTML. * diff --git a/src/core/features/user/pages/about/about.html b/src/core/features/user/pages/about/about.html index e7b7c66388c..6617eae5dca 100644 --- a/src/core/features/user/pages/about/about.html +++ b/src/core/features/user/pages/about/about.html @@ -29,7 +29,8 @@

{{ user.fullname }}

- +