Skip to content

Commit 55d3af3

Browse files
committed
MOBILE-3447 lti: Support disable InAppBrowser
1 parent ecb4071 commit 55d3af3

File tree

5 files changed

+157
-37
lines changed

5 files changed

+157
-37
lines changed

src/addon/mod/lti/components/index/index.ts

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { Component, Optional, Injector } from '@angular/core';
1616
import { Content } from 'ionic-angular';
1717
import { CoreCourseModuleMainActivityComponent } from '@core/course/classes/main-activity-component';
1818
import { AddonModLtiProvider, AddonModLtiLti } from '../../providers/lti';
19+
import { AddonModLtiHelper } from '../../providers/helper';
1920

2021
/**
2122
* Component that displays an LTI entry page.
@@ -92,18 +93,6 @@ export class AddonModLtiIndexComponent extends CoreCourseModuleMainActivityCompo
9293
* Launch the LTI.
9394
*/
9495
launch(): void {
95-
this.ltiProvider.getLtiLaunchData(this.lti.id).then((launchData) => {
96-
// "View" LTI.
97-
this.ltiProvider.logView(this.lti.id, this.lti.name).then(() => {
98-
this.checkCompletion();
99-
}).catch((error) => {
100-
// Ignore errors.
101-
});
102-
103-
// Launch LTI.
104-
return this.ltiProvider.launch(launchData.endpoint, launchData.parameters);
105-
}).catch((message) => {
106-
this.domUtils.showErrorModalDefault(message, 'core.error', true);
107-
});
96+
AddonModLtiHelper.instance.getDataAndLaunch(this.courseId, this.module, this.lti);
10897
}
10998
}

src/addon/mod/lti/lti.module.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { NgModule } from '@angular/core';
1616
import { AddonModLtiComponentsModule } from './components/components.module';
1717
import { AddonModLtiModuleHandler } from './providers/module-handler';
1818
import { AddonModLtiProvider } from './providers/lti';
19+
import { AddonModLtiHelperProvider } from './providers/helper';
1920
import { AddonModLtiLinkHandler } from './providers/link-handler';
2021
import { AddonModLtiListLinkHandler } from './providers/list-link-handler';
2122
import { AddonModLtiPrefetchHandler } from './providers/prefetch-handler';
@@ -25,7 +26,8 @@ import { CoreCourseModulePrefetchDelegate } from '@core/course/providers/module-
2526

2627
// List of providers (without handlers).
2728
export const ADDON_MOD_LTI_PROVIDERS: any[] = [
28-
AddonModLtiProvider
29+
AddonModLtiProvider,
30+
AddonModLtiHelperProvider,
2931
];
3032

3133
@NgModule({
@@ -36,10 +38,11 @@ export const ADDON_MOD_LTI_PROVIDERS: any[] = [
3638
],
3739
providers: [
3840
AddonModLtiProvider,
41+
AddonModLtiHelperProvider,
3942
AddonModLtiModuleHandler,
4043
AddonModLtiLinkHandler,
4144
AddonModLtiListLinkHandler,
42-
AddonModLtiPrefetchHandler
45+
AddonModLtiPrefetchHandler,
4346
]
4447
})
4548
export class AddonModLtiModule {
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
// (C) Copyright 2015 Moodle Pty Ltd.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
import { Injectable } from '@angular/core';
16+
import { Platform } from 'ionic-angular';
17+
import { CoreEvents, CoreEventsProvider } from '@providers/events';
18+
import { CoreSites } from '@providers/sites';
19+
import { CoreDomUtils } from '@providers/utils/dom';
20+
import { CoreCourse } from '@core/course/providers/course';
21+
import { AddonModLti, AddonModLtiLti } from './lti';
22+
23+
import { makeSingleton } from '@singletons/core.singletons';
24+
25+
/**
26+
* Service that provides some helper functions for LTI.
27+
*/
28+
@Injectable()
29+
export class AddonModLtiHelperProvider {
30+
31+
protected pendingCheckCompletion: {[moduleId: string]: {courseId: number, module: any}} = {};
32+
33+
constructor(platform: Platform) {
34+
35+
platform.resume.subscribe(() => {
36+
// User went back to the app, check pending completions.
37+
for (const moduleId in this.pendingCheckCompletion) {
38+
const data = this.pendingCheckCompletion[moduleId];
39+
40+
CoreCourse.instance.checkModuleCompletion(data.courseId, data.module.completiondata);
41+
}
42+
});
43+
44+
// Clear pending completion on logout.
45+
CoreEvents.instance.on(CoreEventsProvider.LOGOUT, () => {
46+
this.pendingCheckCompletion = {};
47+
});
48+
}
49+
50+
/**
51+
* Get needed data and launch the LTI.
52+
*
53+
* @param courseId Course ID.
54+
* @param module Module.
55+
* @param lti LTI instance. If not provided it will be obtained.
56+
* @param siteId Site ID. If not defined, current site.
57+
* @return Promise resolved when done.
58+
*/
59+
async getDataAndLaunch(courseId: number, module: any, lti?: AddonModLtiLti, siteId?: string): Promise<void> {
60+
siteId = siteId || CoreSites.instance.getCurrentSiteId();
61+
62+
const modal = CoreDomUtils.instance.showModalLoading();
63+
64+
try {
65+
const openInBrowser = await AddonModLti.instance.isOpenInAppBrowserDisabled(siteId);
66+
67+
if (openInBrowser) {
68+
const site = await CoreSites.instance.getSite(siteId);
69+
70+
// The view event is triggered by the browser, mark the module as pending to check completion.
71+
this.pendingCheckCompletion[module.id] = {
72+
courseId,
73+
module,
74+
};
75+
76+
await site.openInBrowserWithAutoLogin(module.url);
77+
} else {
78+
// Open in app.
79+
if (!lti) {
80+
lti = await AddonModLti.instance.getLti(courseId, module.id);
81+
}
82+
83+
const launchData = await AddonModLti.instance.getLtiLaunchData(lti.id);
84+
85+
// "View" LTI without blocking the UI.
86+
this.logViewAndCheckCompletion(courseId, module, lti.id, lti.name, siteId);
87+
88+
// Launch LTI.
89+
return AddonModLti.instance.launch(launchData.endpoint, launchData.parameters);
90+
}
91+
} catch (error) {
92+
CoreDomUtils.instance.showErrorModalDefault(error, 'addon.mod_lti.errorgetlti', true);
93+
} finally {
94+
modal.dismiss();
95+
}
96+
}
97+
98+
/**
99+
* Report the LTI as being viewed and check completion.
100+
*
101+
* @param courseId Course ID.
102+
* @param module Module.
103+
* @param ltiId LTI id.
104+
* @param name Name of the lti.
105+
* @param siteId Site ID. If not defined, current site.
106+
* @return Promise resolved when done.
107+
*/
108+
async logViewAndCheckCompletion(courseId: number, module: any, ltiId: number, name?: string, siteId?: string): Promise<void> {
109+
try {
110+
await AddonModLti.instance.logView(ltiId, name);
111+
112+
CoreCourse.instance.checkModuleCompletion(courseId, module.completiondata);
113+
} catch (error) {
114+
// Ignore errors.
115+
}
116+
}
117+
}
118+
119+
export class AddonModLtiHelper extends makeSingleton(AddonModLtiHelperProvider) {}

src/addon/mod/lti/providers/lti.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ import { CoreCourseLogHelperProvider } from '@core/course/providers/log-helper';
2424
import { CoreSite } from '@classes/site';
2525
import { CoreWSExternalWarning, CoreWSExternalFile } from '@providers/ws';
2626

27+
import { makeSingleton } from '@singletons/core.singletons';
28+
2729
/**
2830
* Service that provides some features for LTI.
2931
*/
@@ -193,6 +195,30 @@ export class AddonModLtiProvider {
193195
return this.sitesProvider.getCurrentSite().invalidateWsCacheForKey(this.getLtiLaunchDataCacheKey(id));
194196
}
195197

198+
/**
199+
* Check if open in InAppBrowser is disabled.
200+
*
201+
* @param siteId Site ID. If not defined, current site.
202+
* @return Promise resolved with boolean: whether it's disabled.
203+
*/
204+
async isOpenInAppBrowserDisabled(siteId?: string): Promise<boolean> {
205+
const site = await this.sitesProvider.getSite(siteId);
206+
207+
return this.isOpenInAppBrowserDisabledInSite(site);
208+
}
209+
210+
/**
211+
* Check if open in InAppBrowser is disabled.
212+
*
213+
* @param site Site. If not defined, current site.
214+
* @return Whether it's disabled.
215+
*/
216+
isOpenInAppBrowserDisabledInSite(site?: CoreSite): boolean {
217+
site = site || this.sitesProvider.getCurrentSite();
218+
219+
return site.isFeatureDisabled('CoreCourseModuleDelegate_AddonModLti:openInAppBrowser');
220+
}
221+
196222
/**
197223
* Launch LTI.
198224
*
@@ -233,6 +259,8 @@ export class AddonModLtiProvider {
233259
}
234260
}
235261

262+
export class AddonModLti extends makeSingleton(AddonModLtiProvider) {}
263+
236264
/**
237265
* LTI returned by mod_lti_get_ltis_by_courses.
238266
*/

src/addon/mod/lti/providers/module-handler.ts

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ import { DomSanitizer } from '@angular/platform-browser';
1818
import { CoreCourseModuleHandler, CoreCourseModuleHandlerData } from '@core/course/providers/module-delegate';
1919
import { CoreAppProvider } from '@providers/app';
2020
import { CoreCourseProvider } from '@core/course/providers/course';
21-
import { CoreDomUtilsProvider } from '@providers/utils/dom';
2221
import { CoreFilepoolProvider } from '@providers/filepool';
2322
import { CoreSitesProvider } from '@providers/sites';
2423
import { AddonModLtiIndexComponent } from '../components/index/index';
2524
import { AddonModLtiProvider } from './lti';
25+
import { AddonModLtiHelper } from './helper';
2626
import { CoreConstants } from '@core/constants';
2727

2828
/**
@@ -46,7 +46,6 @@ export class AddonModLtiModuleHandler implements CoreCourseModuleHandler {
4646

4747
constructor(private appProvider: CoreAppProvider,
4848
private courseProvider: CoreCourseProvider,
49-
private domUtils: CoreDomUtilsProvider,
5049
private filepoolProvider: CoreFilepoolProvider,
5150
private sitesProvider: CoreSitesProvider,
5251
private ltiProvider: AddonModLtiProvider,
@@ -85,26 +84,8 @@ export class AddonModLtiModuleHandler implements CoreCourseModuleHandler {
8584
icon: 'link',
8685
label: 'addon.mod_lti.launchactivity',
8786
action: (event: Event, navCtrl: NavController, module: any, courseId: number): void => {
88-
const modal = this.domUtils.showModalLoading();
89-
90-
// Get LTI and launch data.
91-
this.ltiProvider.getLti(courseId, module.id).then((ltiData) => {
92-
return this.ltiProvider.getLtiLaunchData(ltiData.id).then((launchData) => {
93-
// "View" LTI.
94-
this.ltiProvider.logView(ltiData.id, ltiData.name).then(() => {
95-
this.courseProvider.checkModuleCompletion(courseId, module.completiondata);
96-
}).catch(() => {
97-
// Ignore errors.
98-
});
99-
100-
// Launch LTI.
101-
return this.ltiProvider.launch(launchData.endpoint, launchData.parameters);
102-
});
103-
}).catch((message) => {
104-
this.domUtils.showErrorModalDefault(message, 'addon.mod_lti.errorgetlti', true);
105-
}).finally(() => {
106-
modal.dismiss();
107-
});
87+
// Launch the LTI.
88+
AddonModLtiHelper.instance.getDataAndLaunch(courseId, module);
10889
}
10990
}]
11091
};

0 commit comments

Comments
 (0)