Skip to content

Commit 0badecf

Browse files
committed
MOBILE-3469 course: Get course data when entering from search
1 parent 0eccc7a commit 0badecf

File tree

5 files changed

+63
-43
lines changed

5 files changed

+63
-43
lines changed

src/core/course/providers/course.ts

Lines changed: 57 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import { CoreCourseOfflineProvider } from './course-offline';
2828
import { CoreSitePluginsProvider } from '@core/siteplugins/providers/siteplugins';
2929
import { CoreCourseFormatDelegate } from './format-delegate';
3030
import { CorePushNotificationsProvider } from '@core/pushnotifications/providers/pushnotifications';
31-
import { CoreCoursesProvider } from '@core/courses/providers/courses';
31+
import { CoreCoursesProvider, CoreCourses } from '@core/courses/providers/courses';
3232
import { 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
/**

src/core/course/providers/default-format.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ export class CoreCourseFormatDefaultHandler implements CoreCourseFormatHandler {
172172
* @param params Params to pass to the course page.
173173
* @return Promise resolved when done.
174174
*/
175-
openCourse(navCtrl: NavController, course: any, params?: any): Promise<any> {
175+
openCourse(navCtrl: NavController, course: any, params?: any): Promise<void> {
176176
params = params || {};
177177
Object.assign(params, { course: course });
178178

src/core/course/providers/format-delegate.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ export interface CoreCourseFormatHandler extends CoreDelegateHandler {
102102
* @param params Params to pass to the course page.
103103
* @return Promise resolved when done.
104104
*/
105-
openCourse?(navCtrl: NavController, course: any, params?: any): Promise<any>;
105+
openCourse?(navCtrl: NavController, course: any, params?: any): Promise<void>;
106106

107107
/**
108108
* 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 {
351351
}
352352

353353
/**
354-
* Open a course.
354+
* Open a course. Should not be called directly. Call CoreCourseHelper.openCourse instead.
355355
*
356356
* @param navCtrl The NavController instance to use.
357357
* @param course The course to open. It should contain a "format" attribute.
358358
* @param params Params to pass to the course page.
359359
* @return Promise resolved when done.
360360
*/
361-
openCourse(navCtrl: NavController, course: any, params?: any): Promise<any> {
361+
openCourse(navCtrl: NavController, course: any, params?: any): Promise<void> {
362362
return this.executeFunctionOnEnabled(course.format, 'openCourse', [navCtrl, course, params]);
363363
}
364364

src/core/course/providers/helper.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1601,7 +1601,7 @@ export class CoreCourseHelperProvider {
16011601
* @param siteId Site ID. If not defined, current site.
16021602
* @return Promise resolved when done.
16031603
*/
1604-
openCourse(navCtrl: NavController, course: any, params?: any, siteId?: string): Promise<any> {
1604+
openCourse(navCtrl: NavController, course: any, params?: any, siteId?: string): Promise<void> {
16051605
if (!siteId || siteId == this.sitesProvider.getCurrentSiteId()) {
16061606
// Current site, we can open the course.
16071607
return this.courseProvider.openCourse(navCtrl, course, params);

src/core/courses/pages/course-preview/course-preview.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ export class CoreCoursesCoursePreviewPage implements OnDestroy {
259259
return;
260260
}
261261

262-
this.courseFormatDelegate.openCourse(this.navCtrl, this.course);
262+
this.courseHelper.openCourse(this.navCtrl, this.course);
263263
}
264264

265265
/**

0 commit comments

Comments
 (0)