diff --git a/src/core/block/components/pre-rendered-block/core-block-pre-rendered.html b/src/core/block/components/pre-rendered-block/core-block-pre-rendered.html
index 4b3f46315..ab4dcb92e 100644
--- a/src/core/block/components/pre-rendered-block/core-block-pre-rendered.html
+++ b/src/core/block/components/pre-rendered-block/core-block-pre-rendered.html
@@ -1,11 +1,11 @@
-
+
-
+
diff --git a/src/core/block/components/pre-rendered-block/pre-rendered-block.ts b/src/core/block/components/pre-rendered-block/pre-rendered-block.ts
index a88659e36..0f9cd1087 100644
--- a/src/core/block/components/pre-rendered-block/pre-rendered-block.ts
+++ b/src/core/block/components/pre-rendered-block/pre-rendered-block.ts
@@ -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.';
}
diff --git a/src/core/course/providers/course.ts b/src/core/course/providers/course.ts
index 832eeda4c..c6f99929b 100644
--- a/src/core/course/providers/course.ts
+++ b/src/core/course/providers/course.ts
@@ -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');
}
/**
diff --git a/src/core/courses/providers/courses.ts b/src/core/courses/providers/courses.ts
index b9d135c54..226dca101 100644
--- a/src/core/courses/providers/courses.ts
+++ b/src/core/courses/providers/courses.ts
@@ -99,42 +99,7 @@ export class CoreCoursesProvider {
if (courseIds.length == 1) {
// Only 1 course, check if it belongs to the user courses. If so, use all user courses.
- return this.getUserCourses(true, siteId).then((courses) => {
- 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;
- });
+ return this.getCourseIdsIfEnrolled(courseIds[0], siteId);
} else {
if (courseIds.length > 1 && courseIds.indexOf(siteHomeId) == -1) {
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 {
+ 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.
*
diff --git a/src/core/filter/providers/helper.ts b/src/core/filter/providers/helper.ts
index ee4767346..82c89e8ac 100644
--- a/src/core/filter/providers/helper.ts
+++ b/src/core/filter/providers/helper.ts
@@ -20,6 +20,7 @@ import { CoreSitesProvider } from '@providers/sites';
import { CoreFilterDelegate } from './delegate';
import { CoreFilterProvider, CoreFilterFilter, CoreFilterFormatTextOptions, CoreFilterClassifiedFilters } from './filter';
import { CoreCourseProvider } from '@core/course/providers/course';
+import { CoreCoursesProvider } from '@core/courses/providers/courses';
import { CoreSite } from '@classes/site';
/**
@@ -45,11 +46,12 @@ export class CoreFilterHelperProvider {
constructor(logger: CoreLoggerProvider,
eventsProvider: CoreEventsProvider,
- private appProvider: CoreAppProvider,
- private sitesProvider: CoreSitesProvider,
- private filterDelegate: CoreFilterDelegate,
- private courseProvider: CoreCourseProvider,
- private filterProvider: CoreFilterProvider) {
+ protected appProvider: CoreAppProvider,
+ protected sitesProvider: CoreSitesProvider,
+ protected filterDelegate: CoreFilterDelegate,
+ protected courseProvider: CoreCourseProvider,
+ protected filterProvider: CoreFilterProvider,
+ protected coursesProvider: CoreCoursesProvider) {
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 {
+
+ // 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.
*
@@ -137,21 +215,20 @@ export class CoreFilterHelperProvider {
if (contextLevel == 'module' && options.courseId) {
// 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);
- if (result) {
- return result;
- }
+ return this.getCacheableFilters(contextLevel, instanceId, getFilters, options, site);
- 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) => {
- this.storeInMemoryCache(options.courseId, filters, 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 filters[contextLevel][instanceId] || [];
- });
- });
+ return this.getCacheableFilters(contextLevel, instanceId, getFilters, options, site);
}
return this.filterProvider.getAvailableInContext(contextLevel, instanceId, siteId);