@@ -28,7 +28,7 @@ import { CoreCourseOfflineProvider } from './course-offline';
2828import { CoreSitePluginsProvider } from '@core/siteplugins/providers/siteplugins' ;
2929import { CoreCourseFormatDelegate } from './format-delegate' ;
3030import { CorePushNotificationsProvider } from '@core/pushnotifications/providers/pushnotifications' ;
31- import { CoreCoursesProvider } from '@core/courses/providers/courses' ;
31+ import { CoreCoursesProvider , CoreCourses } from '@core/courses/providers/courses' ;
3232import { makeSingleton } from '@singletons/core.singletons' ;
3333
3434/**
@@ -978,49 +978,69 @@ export class CoreCourseProvider {
978978 * @param params Other params to pass to the course page.
979979 * @return Promise resolved when done.
980980 */
981- openCourse ( navCtrl : NavController , course : any , params ?: any ) : Promise < any > {
981+ async openCourse ( navCtrl : NavController , course : any , params ?: any ) : Promise < void > {
982982 const loading = this . domUtils . showModalLoading ( ) ;
983983
984984 // Wait for site plugins to be fetched.
985- return this . sitePluginsProvider . waitFetchPlugins ( ) . then ( ( ) => {
986- if ( this . sitePluginsProvider . sitePluginPromiseExists ( 'format_' + course . format ) ) {
987- // This course uses a custom format plugin, wait for the format plugin to finish loading.
988-
989- return this . sitePluginsProvider . sitePluginLoaded ( 'format_' + course . format ) . then ( ( ) => {
990- // The format loaded successfully, but the handlers wont be registered until all site plugins have loaded.
991- if ( this . sitePluginsProvider . sitePluginsFinishedLoading ) {
992- return this . courseFormatDelegate . openCourse ( navCtrl , course , params ) ;
985+ await this . sitePluginsProvider . waitFetchPlugins ( ) ;
986+
987+ if ( typeof course . format == 'undefined' ) {
988+ const coursesProvider = CoreCourses . instance ;
989+ try {
990+ course = await coursesProvider . getUserCourse ( course . id , true ) ;
991+ } catch ( error ) {
992+ // Not enrolled or an error happened. Try to use another WebService.
993+ const available = coursesProvider . isGetCoursesByFieldAvailableInSite ( ) ;
994+ try {
995+ if ( available ) {
996+ course = await CoreCourses . instance . getCourseByField ( 'id' , course . id ) ;
993997 } else {
994- // Wait for plugins to be loaded.
995- const deferred = this . utils . promiseDefer ( ) ,
996- observer = this . eventsProvider . on ( CoreEventsProvider . SITE_PLUGINS_LOADED , ( ) => {
997- observer && observer . off ( ) ;
998-
999- this . courseFormatDelegate . openCourse ( navCtrl , course , params ) . then ( ( response ) => {
1000- deferred . resolve ( response ) ;
1001- } ) . catch ( ( error ) => {
1002- deferred . reject ( error ) ;
1003- } ) ;
1004- } ) ;
1005-
1006- return deferred . promise ;
998+ course = await CoreCourses . instance . getCourse ( course . id ) ;
1007999 }
1008- } ) . catch ( ( ) => {
1009- // The site plugin failed to load. The user needs to restart the app to try loading it again.
1010- const message = this . translate . instant ( 'core.courses.errorloadplugins' ) ;
1011- const reload = this . translate . instant ( 'core.courses.reload' ) ;
1012- const ignore = this . translate . instant ( 'core.courses.ignore' ) ;
1013- this . domUtils . showConfirm ( message , '' , reload , ignore ) . then ( ( ) => {
1014- window . location . reload ( ) ;
1015- } ) ;
1016- } ) ;
1017- } else {
1018- // No custom format plugin. We don't need to wait for anything.
1019- return this . courseFormatDelegate . openCourse ( navCtrl , course , params ) ;
1000+ } catch ( error ) {
1001+ // Ignore errors.
1002+ }
10201003 }
1021- } ) . finally ( ( ) => {
1004+ }
1005+
1006+ if ( ! this . sitePluginsProvider . sitePluginPromiseExists ( 'format_' + course . format ) ) {
1007+ // No custom format plugin. We don't need to wait for anything.
1008+ await this . courseFormatDelegate . openCourse ( navCtrl , course , params ) ;
10221009 loading . dismiss ( ) ;
1023- } ) ;
1010+
1011+ return ;
1012+ }
1013+
1014+ // This course uses a custom format plugin, wait for the format plugin to finish loading.
1015+ try {
1016+ await this . sitePluginsProvider . sitePluginLoaded ( 'format_' + course . format ) ;
1017+ // The format loaded successfully, but the handlers wont be registered until all site plugins have loaded.
1018+ if ( this . sitePluginsProvider . sitePluginsFinishedLoading ) {
1019+ return this . courseFormatDelegate . openCourse ( navCtrl , course , params ) ;
1020+ }
1021+
1022+ // Wait for plugins to be loaded.
1023+ const deferred = this . utils . promiseDefer ( ) ,
1024+ observer = this . eventsProvider . on ( CoreEventsProvider . SITE_PLUGINS_LOADED , ( ) => {
1025+ observer && observer . off ( ) ;
1026+
1027+ this . courseFormatDelegate . openCourse ( navCtrl , course , params ) . then ( ( response ) => {
1028+ deferred . resolve ( response ) ;
1029+ } ) . catch ( ( error ) => {
1030+ deferred . reject ( error ) ;
1031+ } ) ;
1032+ } ) ;
1033+
1034+ return deferred . promise ;
1035+ } catch ( error ) {
1036+ // The site plugin failed to load. The user needs to restart the app to try loading it again.
1037+ const message = this . translate . instant ( 'core.courses.errorloadplugins' ) ;
1038+ const reload = this . translate . instant ( 'core.courses.reload' ) ;
1039+ const ignore = this . translate . instant ( 'core.courses.ignore' ) ;
1040+ this . domUtils . showConfirm ( message , '' , reload , ignore ) . then ( ( ) => {
1041+ window . location . reload ( ) ;
1042+ } ) ;
1043+ }
10241044 }
10251045
10261046 /**
0 commit comments