MOBILE-3227 filter: Get all course blocks filters at once

main
Dani Palou 2019-12-04 11:50:53 +01:00
parent 466da5c7af
commit dca93cb6e4
4 changed files with 40 additions and 6 deletions

View File

@ -1,11 +1,11 @@
<ion-item-divider text-wrap *ngIf="title">
<h2><core-format-text [text]="title | translate" contextLevel="block" [contextInstanceId]="block.instanceid"></core-format-text></h2>
<h2><core-format-text [text]="title | translate" contextLevel="block" [contextInstanceId]="block.instanceid" [courseId]="courseId"></core-format-text></h2>
</ion-item-divider>
<core-loading [hideUntil]="loaded" class="core-loading-center">
<ion-item *ngIf="block.contents.content" text-wrap class="core-block-content">
<core-format-text [text]="block.contents.content" contextLevel="block" [contextInstanceId]="block.instanceid"></core-format-text>
<core-format-text [text]="block.contents.content" contextLevel="block" [contextInstanceId]="block.instanceid" [courseId]="courseId"></core-format-text>
</ion-item>
<ion-item *ngIf="block.contents.footer" text-wrap class="core-block-footer">
<core-format-text [text]="block.contents.footer" contextLevel="block" [contextInstanceId]="block.instanceid"></core-format-text>
<core-format-text [text]="block.contents.footer" contextLevel="block" [contextInstanceId]="block.instanceid" [courseId]="courseId"></core-format-text>
</ion-item>
</core-loading>

View File

@ -24,6 +24,8 @@ import { CoreBlockBaseComponent } from '../../classes/base-block-component';
})
export class CoreBlockPreRenderedComponent extends CoreBlockBaseComponent implements OnInit {
courseId: number;
constructor(injector: Injector) {
super(injector, 'CoreBlockPreRenderedComponent');
}
@ -34,6 +36,8 @@ export class CoreBlockPreRenderedComponent extends CoreBlockBaseComponent imple
ngOnInit(): void {
super.ngOnInit();
this.courseId = this.contextLevel == 'course' ? this.instanceId : undefined;
this.fetchContentDefaultError = 'Error getting ' + this.block.contents.title + ' data.';
}

View File

@ -113,12 +113,14 @@ export class CoreCourseProvider {
/**
* Check if the get course blocks WS is available in current site.
*
* @param site Site to check. If not defined, current site.
* @return Whether it's available.
* @since 3.7
*/
canGetCourseBlocks(): boolean {
return this.sitesProvider.getCurrentSite().isVersionGreaterEqualThan('3.7') &&
this.sitesProvider.wsAvailableInCurrentSite('core_block_get_course_blocks');
canGetCourseBlocks(site?: CoreSite): boolean {
site = site || this.sitesProvider.getCurrentSite();
return site.isVersionGreaterEqualThan('3.7') && site.wsAvailable('core_block_get_course_blocks');
}
/**

View File

@ -64,6 +64,29 @@ 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.
* @return Promise resolved with the contexts.
*/
getBlocksContexts(courseId: number, siteId?: string): Promise<{contextlevel: string, instanceid: number}[]> {
return this.courseProvider.getCourseBlocks(courseId, siteId).then((blocks) => {
const contexts: {contextlevel: string, instanceid: number}[] = [];
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.
*
@ -200,6 +223,11 @@ export class CoreFilterHelperProvider {
// If enrolled, get all enrolled courses filters with a single call to decrease number of WS calls.
const getFilters = this.getCourseContexts.bind(this, instanceId, siteId);
return this.getCacheableFilters(contextLevel, instanceId, getFilters, options, site);
} else if (contextLevel == 'block' && options.courseId && this.courseProvider.canGetCourseBlocks(site)) {
// Get all the course blocks filters with a single call to decrease number of WS calls.
const getFilters = this.getBlocksContexts.bind(this, options.courseId, siteId);
return this.getCacheableFilters(contextLevel, instanceId, getFilters, options, site);
}