MOBILE-2915 course: Fix title not displayed for guest courses

main
Dani Palou 2019-03-22 07:02:08 +01:00
parent 0d46a6a426
commit 0449c63c71
3 changed files with 73 additions and 4 deletions

View File

@ -169,7 +169,9 @@ export class CoreCourseSectionPage implements OnDestroy {
*/ */
protected loadData(refresh?: boolean, sync?: boolean): Promise<any> { protected loadData(refresh?: boolean, sync?: boolean): Promise<any> {
// First of all, get the course because the data might have changed. // First of all, get the course because the data might have changed.
return this.coursesProvider.getUserCourse(this.course.id).catch(() => { return this.courseHelper.getCourse(this.course.id).then((result) => {
return result.course;
}).catch(() => {
// Error getting the course, probably guest access. // Error getting the course, probably guest access.
}).then((course) => { }).then((course) => {
if (course) { if (course) {

View File

@ -767,6 +767,33 @@ export class CoreCourseHelperProvider {
}); });
} }
/**
* Get a course. It will first check the user courses, and fallback to another WS if not enrolled.
*
* @param {number} courseId Course ID.
* @param {string} [siteId] Site ID. If not defined, current site.
* @return {Promise<{enrolled: boolean, course: any}>} Promise resolved with the course.
*/
getCourse(courseId: number, siteId?: string): Promise<{enrolled: boolean, course: any}> {
siteId = siteId || this.sitesProvider.getCurrentSiteId();
// Try with enrolled courses first.
return this.coursesProvider.getUserCourse(courseId, false, siteId).then((course) => {
return { enrolled: true, course: course };
}).catch(() => {
// Not enrolled or an error happened. Try to use another WebService.
return this.coursesProvider.isGetCoursesByFieldAvailableInSite(siteId).then((available) => {
if (available) {
return this.coursesProvider.getCourseByField('id', courseId, siteId);
} else {
return this.coursesProvider.getCourse(courseId, siteId);
}
}).then((course) => {
return { enrolled: false, course: course };
});
});
}
/** /**
* Check if the course has a block with that name. * Check if the course has a block with that name.
* *

View File

@ -389,6 +389,30 @@ export class CoreCoursesProvider {
} }
} }
/**
* Get the first course returned by getCoursesByField.
*
* @param {string} [field] The field to search. Can be left empty for all courses or:
* id: course id.
* ids: comma separated course ids.
* shortname: course short name.
* idnumber: course id number.
* category: category id the course belongs to.
* @param {any} [value] The value to match.
* @param {string} [siteId] Site ID. If not defined, use current site.
* @return {Promise<any>} Promise resolved with the first course.
* @since 3.2
*/
getCourseByField(field?: string, value?: any, siteId?: string): Promise<any> {
return this.getCoursesByField(field, value, siteId).then((courses) => {
if (courses && courses.length > 0) {
return courses[0];
}
return Promise.reject(null);
});
}
/** /**
* Get courses. They can be filtered by field. * Get courses. They can be filtered by field.
* *
@ -482,13 +506,29 @@ export class CoreCoursesProvider {
} }
/** /**
* Check if get courses by field WS is available. * Check if get courses by field WS is available in a certain site.
* *
* @param {CoreSite} [site] Site to check.
* @return {boolean} Whether get courses by field is available. * @return {boolean} Whether get courses by field is available.
* @since 3.2 * @since 3.2
*/ */
isGetCoursesByFieldAvailable(): boolean { isGetCoursesByFieldAvailable(site?: CoreSite): boolean {
return this.sitesProvider.wsAvailableInCurrentSite('core_course_get_courses_by_field'); site = site || this.sitesProvider.getCurrentSite();
return site.wsAvailable('core_course_get_courses_by_field');
}
/**
* Check if get courses by field WS is available in a certain site, by site ID.
*
* @param {string} [siteId] Site ID. If not defined, current site.
* @return {Promise<boolean>} Promise resolved with boolean: whether get courses by field is available.
* @since 3.2
*/
isGetCoursesByFieldAvailableInSite(siteId?: string): Promise<boolean> {
return this.sitesProvider.getSite(siteId).then((site) => {
return this.isGetCoursesByFieldAvailable(site);
});
} }
/** /**