MOBILE-3469 course: Get course data when entering from search

main
Pau Ferrer Ocaña 2020-09-28 14:40:40 +02:00
parent 0eccc7a1ae
commit 0badecfc8a
5 changed files with 62 additions and 42 deletions

View File

@ -28,7 +28,7 @@ import { CoreCourseOfflineProvider } from './course-offline';
import { CoreSitePluginsProvider } from '@core/siteplugins/providers/siteplugins'; import { CoreSitePluginsProvider } from '@core/siteplugins/providers/siteplugins';
import { CoreCourseFormatDelegate } from './format-delegate'; import { CoreCourseFormatDelegate } from './format-delegate';
import { CorePushNotificationsProvider } from '@core/pushnotifications/providers/pushnotifications'; 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'; import { makeSingleton } from '@singletons/core.singletons';
/** /**
@ -978,49 +978,69 @@ export class CoreCourseProvider {
* @param params Other params to pass to the course page. * @param params Other params to pass to the course page.
* @return Promise resolved when done. * @return Promise resolved when done.
*/ */
openCourse(navCtrl: NavController, course: any, params?: any): Promise<any> { async openCourse(navCtrl: NavController, course: any, params?: any): Promise<void> {
const loading = this.domUtils.showModalLoading(); const loading = this.domUtils.showModalLoading();
// Wait for site plugins to be fetched. // Wait for site plugins to be fetched.
return this.sitePluginsProvider.waitFetchPlugins().then(() => { await this.sitePluginsProvider.waitFetchPlugins();
if (this.sitePluginsProvider.sitePluginPromiseExists('format_' + course.format)) {
// This course uses a custom format plugin, wait for the format plugin to finish loading.
return this.sitePluginsProvider.sitePluginLoaded('format_' + course.format).then(() => { if (typeof course.format == 'undefined') {
// The format loaded successfully, but the handlers wont be registered until all site plugins have loaded. const coursesProvider = CoreCourses.instance;
if (this.sitePluginsProvider.sitePluginsFinishedLoading) { try {
return this.courseFormatDelegate.openCourse(navCtrl, course, params); 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 { } else {
// Wait for plugins to be loaded. course = await CoreCourses.instance.getCourse(course.id);
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(() => { } catch (error) {
// The site plugin failed to load. The user needs to restart the app to try loading it again. // Ignore errors.
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(); 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);
} else { loading.dismiss();
// No custom format plugin. We don't need to wait for anything.
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); 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();
});
}
} }
/** /**

View File

@ -172,7 +172,7 @@ export class CoreCourseFormatDefaultHandler implements CoreCourseFormatHandler {
* @param params Params to pass to the course page. * @param params Params to pass to the course page.
* @return Promise resolved when done. * @return Promise resolved when done.
*/ */
openCourse(navCtrl: NavController, course: any, params?: any): Promise<any> { openCourse(navCtrl: NavController, course: any, params?: any): Promise<void> {
params = params || {}; params = params || {};
Object.assign(params, { course: course }); Object.assign(params, { course: course });

View File

@ -102,7 +102,7 @@ export interface CoreCourseFormatHandler extends CoreDelegateHandler {
* @param params Params to pass to the course page. * @param params Params to pass to the course page.
* @return Promise resolved when done. * @return Promise resolved when done.
*/ */
openCourse?(navCtrl: NavController, course: any, params?: any): Promise<any>; openCourse?(navCtrl: NavController, course: any, params?: any): Promise<void>;
/** /**
* Return the Component to use to display the course format instead of using the default one. * 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 navCtrl The NavController instance to use.
* @param course The course to open. It should contain a "format" attribute. * @param course The course to open. It should contain a "format" attribute.
* @param params Params to pass to the course page. * @param params Params to pass to the course page.
* @return Promise resolved when done. * @return Promise resolved when done.
*/ */
openCourse(navCtrl: NavController, course: any, params?: any): Promise<any> { openCourse(navCtrl: NavController, course: any, params?: any): Promise<void> {
return this.executeFunctionOnEnabled(course.format, 'openCourse', [navCtrl, course, params]); return this.executeFunctionOnEnabled(course.format, 'openCourse', [navCtrl, course, params]);
} }

View File

@ -1601,7 +1601,7 @@ export class CoreCourseHelperProvider {
* @param siteId Site ID. If not defined, current site. * @param siteId Site ID. If not defined, current site.
* @return Promise resolved when done. * @return Promise resolved when done.
*/ */
openCourse(navCtrl: NavController, course: any, params?: any, siteId?: string): Promise<any> { openCourse(navCtrl: NavController, course: any, params?: any, siteId?: string): Promise<void> {
if (!siteId || siteId == this.sitesProvider.getCurrentSiteId()) { if (!siteId || siteId == this.sitesProvider.getCurrentSiteId()) {
// Current site, we can open the course. // Current site, we can open the course.
return this.courseProvider.openCourse(navCtrl, course, params); return this.courseProvider.openCourse(navCtrl, course, params);

View File

@ -259,7 +259,7 @@ export class CoreCoursesCoursePreviewPage implements OnDestroy {
return; return;
} }
this.courseFormatDelegate.openCourse(this.navCtrl, this.course); this.courseHelper.openCourse(this.navCtrl, this.course);
} }
/** /**