|
| 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) {} |
0 commit comments