commit
0d4b990332
|
@ -1,11 +1,11 @@
|
||||||
<ion-item-divider text-wrap *ngIf="title">
|
<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>
|
</ion-item-divider>
|
||||||
<core-loading [hideUntil]="loaded" class="core-loading-center">
|
<core-loading [hideUntil]="loaded" class="core-loading-center">
|
||||||
<ion-item *ngIf="block.contents.content" text-wrap class="core-block-content">
|
<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>
|
||||||
<ion-item *ngIf="block.contents.footer" text-wrap class="core-block-footer">
|
<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>
|
</ion-item>
|
||||||
</core-loading>
|
</core-loading>
|
||||||
|
|
|
@ -24,6 +24,8 @@ import { CoreBlockBaseComponent } from '../../classes/base-block-component';
|
||||||
})
|
})
|
||||||
export class CoreBlockPreRenderedComponent extends CoreBlockBaseComponent implements OnInit {
|
export class CoreBlockPreRenderedComponent extends CoreBlockBaseComponent implements OnInit {
|
||||||
|
|
||||||
|
courseId: number;
|
||||||
|
|
||||||
constructor(injector: Injector) {
|
constructor(injector: Injector) {
|
||||||
super(injector, 'CoreBlockPreRenderedComponent');
|
super(injector, 'CoreBlockPreRenderedComponent');
|
||||||
}
|
}
|
||||||
|
@ -34,6 +36,8 @@ export class CoreBlockPreRenderedComponent extends CoreBlockBaseComponent imple
|
||||||
ngOnInit(): void {
|
ngOnInit(): void {
|
||||||
super.ngOnInit();
|
super.ngOnInit();
|
||||||
|
|
||||||
|
this.courseId = this.contextLevel == 'course' ? this.instanceId : undefined;
|
||||||
|
|
||||||
this.fetchContentDefaultError = 'Error getting ' + this.block.contents.title + ' data.';
|
this.fetchContentDefaultError = 'Error getting ' + this.block.contents.title + ' data.';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -113,12 +113,14 @@ export class CoreCourseProvider {
|
||||||
/**
|
/**
|
||||||
* Check if the get course blocks WS is available in current site.
|
* 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.
|
* @return Whether it's available.
|
||||||
* @since 3.7
|
* @since 3.7
|
||||||
*/
|
*/
|
||||||
canGetCourseBlocks(): boolean {
|
canGetCourseBlocks(site?: CoreSite): boolean {
|
||||||
return this.sitesProvider.getCurrentSite().isVersionGreaterEqualThan('3.7') &&
|
site = site || this.sitesProvider.getCurrentSite();
|
||||||
this.sitesProvider.wsAvailableInCurrentSite('core_block_get_course_blocks');
|
|
||||||
|
return site.isVersionGreaterEqualThan('3.7') && site.wsAvailable('core_block_get_course_blocks');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -99,42 +99,7 @@ export class CoreCoursesProvider {
|
||||||
|
|
||||||
if (courseIds.length == 1) {
|
if (courseIds.length == 1) {
|
||||||
// Only 1 course, check if it belongs to the user courses. If so, use all user courses.
|
// Only 1 course, check if it belongs to the user courses. If so, use all user courses.
|
||||||
return this.getUserCourses(true, siteId).then((courses) => {
|
return this.getCourseIdsIfEnrolled(courseIds[0], siteId);
|
||||||
const courseId = courseIds[0];
|
|
||||||
let useAllCourses = false;
|
|
||||||
|
|
||||||
if (courseId == siteHomeId) {
|
|
||||||
// It's site home, use all courses.
|
|
||||||
useAllCourses = true;
|
|
||||||
} else {
|
|
||||||
for (let i = 0; i < courses.length; i++) {
|
|
||||||
if (courses[i].id == courseId) {
|
|
||||||
useAllCourses = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (useAllCourses) {
|
|
||||||
// User is enrolled, retrieve all the courses.
|
|
||||||
courseIds = courses.map((course) => {
|
|
||||||
return course.id;
|
|
||||||
});
|
|
||||||
|
|
||||||
// Always add the site home ID.
|
|
||||||
courseIds.push(siteHomeId);
|
|
||||||
|
|
||||||
// Sort the course IDs.
|
|
||||||
courseIds.sort((a, b) => {
|
|
||||||
return b - a;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
return courseIds;
|
|
||||||
}).catch(() => {
|
|
||||||
// Ignore errors.
|
|
||||||
return courseIds;
|
|
||||||
});
|
|
||||||
} else {
|
} else {
|
||||||
if (courseIds.length > 1 && courseIds.indexOf(siteHomeId) == -1) {
|
if (courseIds.length > 1 && courseIds.indexOf(siteHomeId) == -1) {
|
||||||
courseIds.push(siteHomeId);
|
courseIds.push(siteHomeId);
|
||||||
|
@ -150,6 +115,56 @@ export class CoreCoursesProvider {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Given a course ID, if user is enrolled in the course it will return the IDs of all enrolled courses and site home.
|
||||||
|
* Return only the course ID otherwise.
|
||||||
|
*
|
||||||
|
* @param courseIds Course IDs.
|
||||||
|
* @param siteId Site Id. If not defined, use current site.
|
||||||
|
* @return Promise resolved with the list of course IDs.
|
||||||
|
*/
|
||||||
|
getCourseIdsIfEnrolled(courseId: number, siteId?: string): Promise<number[]> {
|
||||||
|
return this.sitesProvider.getSite(siteId).then((site) => {
|
||||||
|
const siteHomeId = site.getSiteHomeId();
|
||||||
|
|
||||||
|
// Check if user is enrolled in the course.
|
||||||
|
return this.getUserCourses(true, siteId).then((courses) => {
|
||||||
|
let useAllCourses = false;
|
||||||
|
|
||||||
|
if (courseId == siteHomeId) {
|
||||||
|
// It's site home, use all courses.
|
||||||
|
useAllCourses = true;
|
||||||
|
} else {
|
||||||
|
useAllCourses = !!courses.find((course) => {
|
||||||
|
return course.id == courseId;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if (useAllCourses) {
|
||||||
|
// User is enrolled, return all the courses.
|
||||||
|
const courseIds = courses.map((course) => {
|
||||||
|
return course.id;
|
||||||
|
});
|
||||||
|
|
||||||
|
// Always add the site home ID.
|
||||||
|
courseIds.push(siteHomeId);
|
||||||
|
|
||||||
|
// Sort the course IDs.
|
||||||
|
courseIds.sort((a, b) => {
|
||||||
|
return b - a;
|
||||||
|
});
|
||||||
|
|
||||||
|
return courseIds;
|
||||||
|
}
|
||||||
|
|
||||||
|
return [courseId];
|
||||||
|
}).catch(() => {
|
||||||
|
// Ignore errors.
|
||||||
|
return [courseId];
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check if download a whole course is disabled in a certain site.
|
* Check if download a whole course is disabled in a certain site.
|
||||||
*
|
*
|
||||||
|
|
|
@ -20,6 +20,7 @@ import { CoreSitesProvider } from '@providers/sites';
|
||||||
import { CoreFilterDelegate } from './delegate';
|
import { CoreFilterDelegate } from './delegate';
|
||||||
import { CoreFilterProvider, CoreFilterFilter, CoreFilterFormatTextOptions, CoreFilterClassifiedFilters } from './filter';
|
import { CoreFilterProvider, CoreFilterFilter, CoreFilterFormatTextOptions, CoreFilterClassifiedFilters } from './filter';
|
||||||
import { CoreCourseProvider } from '@core/course/providers/course';
|
import { CoreCourseProvider } from '@core/course/providers/course';
|
||||||
|
import { CoreCoursesProvider } from '@core/courses/providers/courses';
|
||||||
import { CoreSite } from '@classes/site';
|
import { CoreSite } from '@classes/site';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -45,11 +46,12 @@ export class CoreFilterHelperProvider {
|
||||||
|
|
||||||
constructor(logger: CoreLoggerProvider,
|
constructor(logger: CoreLoggerProvider,
|
||||||
eventsProvider: CoreEventsProvider,
|
eventsProvider: CoreEventsProvider,
|
||||||
private appProvider: CoreAppProvider,
|
protected appProvider: CoreAppProvider,
|
||||||
private sitesProvider: CoreSitesProvider,
|
protected sitesProvider: CoreSitesProvider,
|
||||||
private filterDelegate: CoreFilterDelegate,
|
protected filterDelegate: CoreFilterDelegate,
|
||||||
private courseProvider: CoreCourseProvider,
|
protected courseProvider: CoreCourseProvider,
|
||||||
private filterProvider: CoreFilterProvider) {
|
protected filterProvider: CoreFilterProvider,
|
||||||
|
protected coursesProvider: CoreCoursesProvider) {
|
||||||
|
|
||||||
this.logger = logger.getInstance('CoreFilterHelperProvider');
|
this.logger = logger.getInstance('CoreFilterHelperProvider');
|
||||||
|
|
||||||
|
@ -62,6 +64,82 @@ 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.
|
||||||
|
*
|
||||||
|
* @param contextLevel The context level.
|
||||||
|
* @param instanceId Instance ID related to the context.
|
||||||
|
* @param options Options for format text.
|
||||||
|
* @param siteId Site ID. If not defined, current site.
|
||||||
|
* @return Promise resolved with the filters.
|
||||||
|
*/
|
||||||
|
protected getCacheableFilters(contextLevel: string, instanceId: number, getFilters: () => Promise<{contextlevel: string,
|
||||||
|
instanceid: number}[]>, options?: CoreFilterFormatTextOptions, site?: CoreSite): Promise<CoreFilterFilter[]> {
|
||||||
|
|
||||||
|
// Check the memory cache first.
|
||||||
|
const result = this.getFromMemoryCache(options.courseId, contextLevel, instanceId, site);
|
||||||
|
if (result) {
|
||||||
|
return Promise.resolve(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
const siteId = site.getId();
|
||||||
|
|
||||||
|
return getFilters().then((contexts) => {
|
||||||
|
|
||||||
|
return this.filterProvider.getAvailableInContexts(contexts, siteId).then((filters) => {
|
||||||
|
this.storeInMemoryCache(options.courseId, filters, siteId);
|
||||||
|
|
||||||
|
return filters[contextLevel][instanceId] || [];
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* If user is enrolled in the course, return contexts of all enrolled courses to decrease number of WS requests.
|
||||||
|
*
|
||||||
|
* @param courseId Course ID.
|
||||||
|
* @param siteId Site ID. If not defined, current site.
|
||||||
|
* @return Promise resolved with the contexts.
|
||||||
|
*/
|
||||||
|
getCourseContexts(courseId: number, siteId?: string): Promise<{contextlevel: string, instanceid: number}[]> {
|
||||||
|
|
||||||
|
return this.coursesProvider.getCourseIdsIfEnrolled(courseId, siteId).then((courseIds) => {
|
||||||
|
const contexts: {contextlevel: string, instanceid: number}[] = [];
|
||||||
|
|
||||||
|
courseIds.forEach((courseId) => {
|
||||||
|
contexts.push({
|
||||||
|
contextlevel: 'course',
|
||||||
|
instanceid: courseId
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
return contexts;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the contexts of all course modules in a course.
|
* Get the contexts of all course modules in a course.
|
||||||
*
|
*
|
||||||
|
@ -137,21 +215,20 @@ export class CoreFilterHelperProvider {
|
||||||
|
|
||||||
if (contextLevel == 'module' && options.courseId) {
|
if (contextLevel == 'module' && options.courseId) {
|
||||||
// Get all the modules filters with a single call to decrease the number of WS calls.
|
// Get all the modules filters with a single call to decrease the number of WS calls.
|
||||||
// Check the memory cache first to speed up the process.
|
const getFilters = this.getCourseModulesContexts.bind(this, options.courseId, siteId);
|
||||||
|
|
||||||
const result = this.getFromMemoryCache(options.courseId, contextLevel, instanceId, site);
|
return this.getCacheableFilters(contextLevel, instanceId, getFilters, options, site);
|
||||||
if (result) {
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
return this.getCourseModulesContexts(options.courseId, siteId).then((contexts) => {
|
} else if (contextLevel == 'course') {
|
||||||
|
// 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.filterProvider.getAvailableInContexts(contexts, siteId).then((filters) => {
|
return this.getCacheableFilters(contextLevel, instanceId, getFilters, options, site);
|
||||||
this.storeInMemoryCache(options.courseId, filters, siteId);
|
} 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 filters[contextLevel][instanceId] || [];
|
return this.getCacheableFilters(contextLevel, instanceId, getFilters, options, site);
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return this.filterProvider.getAvailableInContext(contextLevel, instanceId, siteId);
|
return this.filterProvider.getAvailableInContext(contextLevel, instanceId, siteId);
|
||||||
|
|
Loading…
Reference in New Issue