MOBILE-2431 courses: Fix course options not available
parent
11013c69ab
commit
10da720a4a
|
@ -395,7 +395,9 @@ export class CoreCourseOptionsDelegate extends CoreDelegate {
|
|||
* @return {Promise<void>} Promise resolved when done.
|
||||
*/
|
||||
protected loadCourseOptions(course: any, refresh?: boolean): Promise<void> {
|
||||
if (typeof course.navOptions == 'undefined' || typeof course.admOptions == 'undefined' || refresh) {
|
||||
if (this.coursesProvider.canGetAdminAndNavOptions() &&
|
||||
(typeof course.navOptions == 'undefined' || typeof course.admOptions == 'undefined' || refresh)) {
|
||||
|
||||
return this.coursesProvider.getCoursesAdminAndNavOptions([course.id]).then((options) => {
|
||||
course.navOptions = options.navOptions[course.id];
|
||||
course.admOptions = options.admOptions[course.id];
|
||||
|
|
|
@ -111,12 +111,14 @@ export class CoreCoursesMyCoursesPage implements OnDestroy {
|
|||
}));
|
||||
}
|
||||
|
||||
promises.push(this.coursesProvider.getCoursesAdminAndNavOptions(courseIds).then((options) => {
|
||||
courses.forEach((course) => {
|
||||
course.navOptions = options.navOptions[course.id];
|
||||
course.admOptions = options.admOptions[course.id];
|
||||
});
|
||||
}));
|
||||
if (this.coursesProvider.canGetAdminAndNavOptions()) {
|
||||
promises.push(this.coursesProvider.getCoursesAdminAndNavOptions(courseIds).then((options) => {
|
||||
courses.forEach((course) => {
|
||||
course.navOptions = options.navOptions[course.id];
|
||||
course.admOptions = options.admOptions[course.id];
|
||||
});
|
||||
}));
|
||||
}
|
||||
|
||||
return Promise.all(promises).then(() => {
|
||||
this.courses = courses;
|
||||
|
|
|
@ -223,13 +223,15 @@ export class CoreCoursesMyOverviewPage implements OnDestroy {
|
|||
return course.id;
|
||||
});
|
||||
|
||||
// Load course options of the course.
|
||||
promises.push(this.coursesProvider.getCoursesAdminAndNavOptions(courseIds).then((options) => {
|
||||
courses.forEach((course) => {
|
||||
course.navOptions = options.navOptions[course.id];
|
||||
course.admOptions = options.admOptions[course.id];
|
||||
});
|
||||
}));
|
||||
if (this.coursesProvider.canGetAdminAndNavOptions()) {
|
||||
// Load course options of the course.
|
||||
promises.push(this.coursesProvider.getCoursesAdminAndNavOptions(courseIds).then((options) => {
|
||||
courses.forEach((course) => {
|
||||
course.navOptions = options.navOptions[course.id];
|
||||
course.admOptions = options.admOptions[course.id];
|
||||
});
|
||||
}));
|
||||
}
|
||||
|
||||
this.courseIds = courseIds.join(',');
|
||||
|
||||
|
|
|
@ -33,6 +33,16 @@ export class CoreCoursesProvider {
|
|||
this.logger = logger.getInstance('CoreCoursesProvider');
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether current site supports getting course options.
|
||||
*
|
||||
* @return {boolean} Whether current site supports getting course options.
|
||||
*/
|
||||
canGetAdminAndNavOptions(): boolean {
|
||||
return this.sitesProvider.wsAvailableInCurrentSite('core_course_get_user_navigation_options') &&
|
||||
this.sitesProvider.wsAvailableInCurrentSite('core_course_get_user_administration_options');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get categories. They can be filtered by id.
|
||||
*
|
||||
|
|
|
@ -213,51 +213,62 @@ export class CoreUserDelegate extends CoreDelegate {
|
|||
* @return {Subject<CoreUserProfileHandlerToDisplay[]>} Resolved with the handlers.
|
||||
*/
|
||||
getProfileHandlersFor(user: any, courseId: number): Subject<CoreUserProfileHandlerToDisplay[]> {
|
||||
const promises = [];
|
||||
let promise,
|
||||
navOptions,
|
||||
admOptions;
|
||||
|
||||
if (this.coursesProvider.canGetAdminAndNavOptions()) {
|
||||
// Get course options.
|
||||
promise = this.coursesProvider.getUserCourses(true).then((courses) => {
|
||||
const courseIds = courses.map((course) => {
|
||||
return course.id;
|
||||
});
|
||||
|
||||
return this.coursesProvider.getCoursesAdminAndNavOptions(courseIds).then((options) => {
|
||||
// For backwards compatibility we don't modify the courseId.
|
||||
const courseIdForOptions = courseId || this.sitesProvider.getCurrentSiteHomeId();
|
||||
|
||||
navOptions = options.navOptions[courseIdForOptions];
|
||||
admOptions = options.admOptions[courseIdForOptions];
|
||||
});
|
||||
});
|
||||
} else {
|
||||
promise = Promise.resolve();
|
||||
}
|
||||
|
||||
this.userHandlers = [];
|
||||
|
||||
// Retrieve course options forcing cache.
|
||||
this.coursesProvider.getUserCourses(true).then((courses) => {
|
||||
const courseIds = courses.map((course) => {
|
||||
return course.id;
|
||||
});
|
||||
promise.then(() => {
|
||||
const promises = [];
|
||||
|
||||
return this.coursesProvider.getCoursesAdminAndNavOptions(courseIds).then((options) => {
|
||||
// For backwards compatibility we don't modify the courseId.
|
||||
const courseIdForOptions = courseId || this.sitesProvider.getCurrentSiteHomeId(),
|
||||
navOptions = options.navOptions[courseIdForOptions],
|
||||
admOptions = options.admOptions[courseIdForOptions];
|
||||
|
||||
for (const name in this.enabledHandlers) {
|
||||
// Checks if the handler is enabled for the user.
|
||||
const handler = <CoreUserProfileHandler> this.handlers[name],
|
||||
isEnabledForUser = handler.isEnabledForUser(user, courseId, navOptions, admOptions),
|
||||
promise = Promise.resolve(isEnabledForUser).then((enabled) => {
|
||||
if (enabled) {
|
||||
this.userHandlers.push({
|
||||
name: name,
|
||||
data: handler.getDisplayData(user, courseId),
|
||||
priority: handler.priority,
|
||||
type: handler.type || CoreUserDelegate.TYPE_NEW_PAGE
|
||||
});
|
||||
} else {
|
||||
return Promise.reject(null);
|
||||
}
|
||||
}).catch(() => {
|
||||
// Nothing to do here, it is not enabled for this user.
|
||||
});
|
||||
promises.push(promise);
|
||||
}
|
||||
|
||||
return Promise.all(promises).then(() => {
|
||||
// Sort them by priority.
|
||||
this.userHandlers.sort((a, b) => {
|
||||
return b.priority - a.priority;
|
||||
for (const name in this.enabledHandlers) {
|
||||
// Checks if the handler is enabled for the user.
|
||||
const handler = <CoreUserProfileHandler> this.handlers[name],
|
||||
isEnabledForUser = handler.isEnabledForUser(user, courseId, navOptions, admOptions),
|
||||
promise = Promise.resolve(isEnabledForUser).then((enabled) => {
|
||||
if (enabled) {
|
||||
this.userHandlers.push({
|
||||
name: name,
|
||||
data: handler.getDisplayData(user, courseId),
|
||||
priority: handler.priority,
|
||||
type: handler.type || CoreUserDelegate.TYPE_NEW_PAGE
|
||||
});
|
||||
} else {
|
||||
return Promise.reject(null);
|
||||
}
|
||||
}).catch(() => {
|
||||
// Nothing to do here, it is not enabled for this user.
|
||||
});
|
||||
this.loaded = true;
|
||||
this.observableHandlers.next(this.userHandlers);
|
||||
promises.push(promise);
|
||||
}
|
||||
|
||||
return Promise.all(promises).then(() => {
|
||||
// Sort them by priority.
|
||||
this.userHandlers.sort((a, b) => {
|
||||
return b.priority - a.priority;
|
||||
});
|
||||
this.loaded = true;
|
||||
this.observableHandlers.next(this.userHandlers);
|
||||
});
|
||||
}).catch(() => {
|
||||
// Never fails.
|
||||
|
|
Loading…
Reference in New Issue