Skip to content

Commit

Permalink
MOBILE-4526 filter: Convert block and user contexts to parent
Browse files Browse the repository at this point in the history
  • Loading branch information
dpalou committed Mar 13, 2024
1 parent 44b9f49 commit 8afeac3
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 45 deletions.
13 changes: 6 additions & 7 deletions src/addons/blog/pages/entries/entries.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ <h1>{{ title | translate }}</h1>
<div class="flex-row ion-justify-content-between ion-align-items-center">
<h2>
<core-format-text [text]="entry.subject" [contextLevel]="contextLevel"
[contextInstanceId]="contextInstanceId" />
[contextInstanceId]="contextInstanceId" [courseId]="entry.courseid" />
</h2>
<ion-note class="ion-text-end">
{{ 'addon.blog.' + entry.publishTranslated! | translate}}
Expand All @@ -47,8 +47,8 @@ <h2>
<ion-card-content>
<ion-item class="ion-text-wrap">
<ion-label>
<core-format-text [text]="entry.summary" [component]="this.component" [componentId]="entry.id"
[contextLevel]="contextLevel" [contextInstanceId]="contextInstanceId" />
<core-format-text [text]="entry.summary" [component]="component" [componentId]="entry.id"
[contextLevel]="contextLevel" [contextInstanceId]="contextInstanceId" [courseId]="entry.courseid" />
</ion-label>
</ion-item>
<ion-item class="ion-text-wrap" *ngIf="tagsEnabled && entry.tags && entry.tags!.length > 0">
Expand All @@ -57,10 +57,9 @@ <h2>
<core-tag-list [tags]="entry.tags" />
</ion-label>
</ion-item>
<core-comments *ngIf="commentsEnabled" [component]="this.component" [itemId]="entry.id" area="format_blog"
[instanceId]="entry.userid" contextLevel="user" [showItem]="true" />
<core-file *ngFor="let file of entry.attachmentfiles" [file]="file" [component]="this.component"
[componentId]="entry.id" />
<core-comments *ngIf="commentsEnabled" [component]="component" [itemId]="entry.id" area="format_blog"
[instanceId]="entry.userid" contextLevel="user" [showItem]="true" [courseId]="entry.courseid" />
<core-file *ngFor="let file of entry.attachmentfiles" [file]="file" [component]="component" [componentId]="entry.id" />
<ion-item *ngIf="entry.uniquehash" [href]="entry.uniquehash" core-link [detail]="true">
<ion-label>{{ 'addon.blog.linktooriginalentry' | translate }}</ion-label>
</ion-item>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,11 @@ export class CoreBlockPreRenderedComponent extends CoreBlockBaseComponent implem
* @inheritdoc
*/
async ngOnInit(): Promise<void> {
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();
}

}
39 changes: 6 additions & 33 deletions src/core/features/filter/services/filter-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<CoreFiltersGetAvailableInContextWSParamContext[]> {
// 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.
*
Expand Down Expand Up @@ -199,10 +173,14 @@ export class CoreFilterHelperProvider {
async getFilters(
contextLevel: string,
instanceId: number,
options?: CoreFilterFormatTextOptions,
options: CoreFilterFormatTextOptions = {},
siteId?: string,
): Promise<CoreFilterFilter[]> {
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;
Expand Down Expand Up @@ -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);
}

Expand Down
24 changes: 24 additions & 0 deletions src/core/features/filter/services/filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
*
Expand Down
3 changes: 2 additions & 1 deletion src/core/features/user/pages/about/about.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ <h2>{{ user.fullname }}</h2>
<ion-item class="ion-text-wrap" *ngIf="user.description" lines="full">
<ion-label>
<p>
<core-format-text [text]="user.description" contextLevel="user" [contextInstanceId]="user.id" />
<core-format-text [text]="user.description" contextLevel="user" [contextInstanceId]="user.id"
[courseId]="courseId" />
</p>
</ion-label>
</ion-item>
Expand Down

0 comments on commit 8afeac3

Please sign in to comment.