From 0badecfc8a2dc8c72c76ec0b4253b2f0641af0f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pau=20Ferrer=20Oca=C3=B1a?= Date: Mon, 28 Sep 2020 14:40:40 +0200 Subject: [PATCH] MOBILE-3469 course: Get course data when entering from search --- src/core/course/providers/course.ts | 92 +++++++++++-------- src/core/course/providers/default-format.ts | 2 +- src/core/course/providers/format-delegate.ts | 6 +- src/core/course/providers/helper.ts | 2 +- .../pages/course-preview/course-preview.ts | 2 +- 5 files changed, 62 insertions(+), 42 deletions(-) diff --git a/src/core/course/providers/course.ts b/src/core/course/providers/course.ts index e4de0e08e..c40e5c7ba 100644 --- a/src/core/course/providers/course.ts +++ b/src/core/course/providers/course.ts @@ -28,7 +28,7 @@ import { CoreCourseOfflineProvider } from './course-offline'; import { CoreSitePluginsProvider } from '@core/siteplugins/providers/siteplugins'; import { CoreCourseFormatDelegate } from './format-delegate'; import { CorePushNotificationsProvider } from '@core/pushnotifications/providers/pushnotifications'; -import { CoreCoursesProvider } from '@core/courses/providers/courses'; +import { CoreCoursesProvider, CoreCourses } from '@core/courses/providers/courses'; import { makeSingleton } from '@singletons/core.singletons'; /** @@ -978,49 +978,69 @@ export class CoreCourseProvider { * @param params Other params to pass to the course page. * @return Promise resolved when done. */ - openCourse(navCtrl: NavController, course: any, params?: any): Promise { + async openCourse(navCtrl: NavController, course: any, params?: any): Promise { const loading = this.domUtils.showModalLoading(); // Wait for site plugins to be fetched. - return this.sitePluginsProvider.waitFetchPlugins().then(() => { - if (this.sitePluginsProvider.sitePluginPromiseExists('format_' + course.format)) { - // This course uses a custom format plugin, wait for the format plugin to finish loading. + await this.sitePluginsProvider.waitFetchPlugins(); - return this.sitePluginsProvider.sitePluginLoaded('format_' + course.format).then(() => { - // The format loaded successfully, but the handlers wont be registered until all site plugins have loaded. - if (this.sitePluginsProvider.sitePluginsFinishedLoading) { - return this.courseFormatDelegate.openCourse(navCtrl, course, params); + if (typeof course.format == 'undefined') { + const coursesProvider = CoreCourses.instance; + try { + course = await coursesProvider.getUserCourse(course.id, true); + } catch (error) { + // Not enrolled or an error happened. Try to use another WebService. + const available = coursesProvider.isGetCoursesByFieldAvailableInSite(); + try { + if (available) { + course = await CoreCourses.instance.getCourseByField('id', course.id); } else { - // Wait for plugins to be loaded. - const deferred = this.utils.promiseDefer(), - observer = this.eventsProvider.on(CoreEventsProvider.SITE_PLUGINS_LOADED, () => { - observer && observer.off(); - - this.courseFormatDelegate.openCourse(navCtrl, course, params).then((response) => { - deferred.resolve(response); - }).catch((error) => { - deferred.reject(error); - }); - }); - - return deferred.promise; + course = await CoreCourses.instance.getCourse(course.id); } - }).catch(() => { - // The site plugin failed to load. The user needs to restart the app to try loading it again. - const message = this.translate.instant('core.courses.errorloadplugins'); - const reload = this.translate.instant('core.courses.reload'); - const ignore = this.translate.instant('core.courses.ignore'); - this.domUtils.showConfirm(message, '', reload, ignore).then(() => { - window.location.reload(); - }); - }); - } else { - // No custom format plugin. We don't need to wait for anything. + } catch (error) { + // Ignore errors. + } + } + } + + if (!this.sitePluginsProvider.sitePluginPromiseExists('format_' + course.format)) { + // No custom format plugin. We don't need to wait for anything. + await this.courseFormatDelegate.openCourse(navCtrl, course, params); + loading.dismiss(); + + return; + } + + // This course uses a custom format plugin, wait for the format plugin to finish loading. + try { + await this.sitePluginsProvider.sitePluginLoaded('format_' + course.format); + // The format loaded successfully, but the handlers wont be registered until all site plugins have loaded. + if (this.sitePluginsProvider.sitePluginsFinishedLoading) { return this.courseFormatDelegate.openCourse(navCtrl, course, params); } - }).finally(() => { - loading.dismiss(); - }); + + // Wait for plugins to be loaded. + const deferred = this.utils.promiseDefer(), + observer = this.eventsProvider.on(CoreEventsProvider.SITE_PLUGINS_LOADED, () => { + observer && observer.off(); + + this.courseFormatDelegate.openCourse(navCtrl, course, params).then((response) => { + deferred.resolve(response); + }).catch((error) => { + deferred.reject(error); + }); + }); + + return deferred.promise; + } catch (error) { + // The site plugin failed to load. The user needs to restart the app to try loading it again. + const message = this.translate.instant('core.courses.errorloadplugins'); + const reload = this.translate.instant('core.courses.reload'); + const ignore = this.translate.instant('core.courses.ignore'); + this.domUtils.showConfirm(message, '', reload, ignore).then(() => { + window.location.reload(); + }); + } } /** diff --git a/src/core/course/providers/default-format.ts b/src/core/course/providers/default-format.ts index bbf673151..7b5390a9c 100644 --- a/src/core/course/providers/default-format.ts +++ b/src/core/course/providers/default-format.ts @@ -172,7 +172,7 @@ export class CoreCourseFormatDefaultHandler implements CoreCourseFormatHandler { * @param params Params to pass to the course page. * @return Promise resolved when done. */ - openCourse(navCtrl: NavController, course: any, params?: any): Promise { + openCourse(navCtrl: NavController, course: any, params?: any): Promise { params = params || {}; Object.assign(params, { course: course }); diff --git a/src/core/course/providers/format-delegate.ts b/src/core/course/providers/format-delegate.ts index ceee5af13..aafbdb462 100644 --- a/src/core/course/providers/format-delegate.ts +++ b/src/core/course/providers/format-delegate.ts @@ -102,7 +102,7 @@ export interface CoreCourseFormatHandler extends CoreDelegateHandler { * @param params Params to pass to the course page. * @return Promise resolved when done. */ - openCourse?(navCtrl: NavController, course: any, params?: any): Promise; + openCourse?(navCtrl: NavController, course: any, params?: any): Promise; /** * Return the Component to use to display the course format instead of using the default one. @@ -351,14 +351,14 @@ export class CoreCourseFormatDelegate extends CoreDelegate { } /** - * Open a course. + * Open a course. Should not be called directly. Call CoreCourseHelper.openCourse instead. * * @param navCtrl The NavController instance to use. * @param course The course to open. It should contain a "format" attribute. * @param params Params to pass to the course page. * @return Promise resolved when done. */ - openCourse(navCtrl: NavController, course: any, params?: any): Promise { + openCourse(navCtrl: NavController, course: any, params?: any): Promise { return this.executeFunctionOnEnabled(course.format, 'openCourse', [navCtrl, course, params]); } diff --git a/src/core/course/providers/helper.ts b/src/core/course/providers/helper.ts index bc1d52fef..87fcd202e 100644 --- a/src/core/course/providers/helper.ts +++ b/src/core/course/providers/helper.ts @@ -1601,7 +1601,7 @@ export class CoreCourseHelperProvider { * @param siteId Site ID. If not defined, current site. * @return Promise resolved when done. */ - openCourse(navCtrl: NavController, course: any, params?: any, siteId?: string): Promise { + openCourse(navCtrl: NavController, course: any, params?: any, siteId?: string): Promise { if (!siteId || siteId == this.sitesProvider.getCurrentSiteId()) { // Current site, we can open the course. return this.courseProvider.openCourse(navCtrl, course, params); diff --git a/src/core/courses/pages/course-preview/course-preview.ts b/src/core/courses/pages/course-preview/course-preview.ts index a6ff05ca4..664960ed4 100644 --- a/src/core/courses/pages/course-preview/course-preview.ts +++ b/src/core/courses/pages/course-preview/course-preview.ts @@ -259,7 +259,7 @@ export class CoreCoursesCoursePreviewPage implements OnDestroy { return; } - this.courseFormatDelegate.openCourse(this.navCtrl, this.course); + this.courseHelper.openCourse(this.navCtrl, this.course); } /**