2018-03-06 15:28:52 +01:00
|
|
|
// (C) Copyright 2015 Martin Dougiamas
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
import { Injectable } from '@angular/core';
|
|
|
|
import { NavController, NavOptions } from 'ionic-angular';
|
2018-08-28 11:46:53 +02:00
|
|
|
import { CoreDomUtilsProvider } from '@providers/utils/dom';
|
2018-03-06 15:28:52 +01:00
|
|
|
import { AddonModUrlIndexComponent } from '../components/index/index';
|
|
|
|
import { CoreCourseModuleHandler, CoreCourseModuleHandlerData } from '@core/course/providers/module-delegate';
|
|
|
|
import { CoreCourseProvider } from '@core/course/providers/course';
|
2018-10-04 16:42:14 +02:00
|
|
|
import { CoreContentLinksHelperProvider } from '@core/contentlinks/providers/helper';
|
2018-03-06 15:28:52 +01:00
|
|
|
import { AddonModUrlProvider } from './url';
|
|
|
|
import { AddonModUrlHelperProvider } from './helper';
|
2018-08-28 11:46:53 +02:00
|
|
|
import { CoreConstants } from '@core/constants';
|
2018-03-06 15:28:52 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Handler to support url modules.
|
|
|
|
*/
|
|
|
|
@Injectable()
|
|
|
|
export class AddonModUrlModuleHandler implements CoreCourseModuleHandler {
|
2018-03-23 13:24:31 +01:00
|
|
|
name = 'AddonModUrl';
|
|
|
|
modName = 'url';
|
2018-03-06 15:28:52 +01:00
|
|
|
|
2018-10-18 11:04:04 +02:00
|
|
|
supportedFeatures = {
|
|
|
|
[CoreConstants.FEATURE_MOD_ARCHETYPE]: CoreConstants.MOD_ARCHETYPE_RESOURCE,
|
|
|
|
[CoreConstants.FEATURE_GROUPS]: false,
|
|
|
|
[CoreConstants.FEATURE_GROUPINGS]: false,
|
|
|
|
[CoreConstants.FEATURE_MOD_INTRO]: true,
|
|
|
|
[CoreConstants.FEATURE_COMPLETION_TRACKS_VIEWS]: true,
|
|
|
|
[CoreConstants.FEATURE_GRADE_HAS_GRADE]: false,
|
|
|
|
[CoreConstants.FEATURE_GRADE_OUTCOMES]: false,
|
|
|
|
[CoreConstants.FEATURE_BACKUP_MOODLE2]: true,
|
|
|
|
[CoreConstants.FEATURE_SHOW_DESCRIPTION]: true
|
|
|
|
};
|
|
|
|
|
2018-03-06 15:28:52 +01:00
|
|
|
constructor(private courseProvider: CoreCourseProvider, private urlProvider: AddonModUrlProvider,
|
2018-10-04 16:42:14 +02:00
|
|
|
private urlHelper: AddonModUrlHelperProvider, private domUtils: CoreDomUtilsProvider,
|
|
|
|
private contentLinksHelper: CoreContentLinksHelperProvider) { }
|
2018-03-06 15:28:52 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if the handler is enabled on a site level.
|
|
|
|
*
|
|
|
|
* @return {boolean} Whether or not the handler is enabled on a site level.
|
|
|
|
*/
|
|
|
|
isEnabled(): boolean {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the data required to display the module in the course contents view.
|
|
|
|
*
|
|
|
|
* @param {any} module The module object.
|
|
|
|
* @param {number} courseId The course ID.
|
|
|
|
* @param {number} sectionId The section ID.
|
|
|
|
* @return {CoreCourseModuleHandlerData} Data to render the module.
|
|
|
|
*/
|
|
|
|
getData(module: any, courseId: number, sectionId: number): CoreCourseModuleHandlerData {
|
2018-08-28 11:46:53 +02:00
|
|
|
// tslint:disable: no-this-assignment
|
|
|
|
const handler = this;
|
2018-03-06 15:28:52 +01:00
|
|
|
const handlerData = {
|
2018-08-24 11:05:39 +02:00
|
|
|
icon: this.courseProvider.getModuleIconSrc(this.modName),
|
2018-03-06 15:28:52 +01:00
|
|
|
title: module.name,
|
|
|
|
class: 'addon-mod_url-handler',
|
|
|
|
showDownloadButton: false,
|
|
|
|
action(event: Event, navCtrl: NavController, module: any, courseId: number, options: NavOptions): void {
|
2018-10-04 16:42:14 +02:00
|
|
|
const modal = handler.domUtils.showModalLoading();
|
2018-08-28 11:46:53 +02:00
|
|
|
|
2018-10-04 16:42:14 +02:00
|
|
|
// First of all, check if the URL can be handled by the app. If so, always open it directly.
|
|
|
|
handler.contentLinksHelper.canHandleLink(module.contents[0].fileurl, courseId).then((canHandle) => {
|
|
|
|
if (canHandle) {
|
|
|
|
// URL handled by the app, open it directly.
|
|
|
|
return true;
|
|
|
|
}
|
2018-08-28 11:46:53 +02:00
|
|
|
|
2018-10-04 16:42:14 +02:00
|
|
|
// Not handled by the app, check the display type.
|
|
|
|
if (handler.urlProvider.isGetUrlWSAvailable()) {
|
|
|
|
return handler.urlProvider.getUrl(courseId, module.id).catch(() => {
|
|
|
|
// Ignore errors.
|
|
|
|
}).then((url) => {
|
|
|
|
const displayType = handler.urlProvider.getFinalDisplayType(url);
|
2018-08-28 11:46:53 +02:00
|
|
|
|
2018-10-04 16:42:14 +02:00
|
|
|
return displayType == CoreConstants.RESOURCELIB_DISPLAY_OPEN ||
|
|
|
|
displayType == CoreConstants.RESOURCELIB_DISPLAY_POPUP;
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
2018-08-28 11:46:53 +02:00
|
|
|
|
2018-10-04 16:42:14 +02:00
|
|
|
}).then((shouldOpen) => {
|
2018-08-28 11:46:53 +02:00
|
|
|
if (shouldOpen) {
|
|
|
|
handler.openUrl(module, courseId);
|
|
|
|
} else {
|
|
|
|
navCtrl.push('AddonModUrlIndexPage', {module: module, courseId: courseId}, options);
|
|
|
|
}
|
2018-10-04 16:42:14 +02:00
|
|
|
}).finally(() => {
|
|
|
|
modal.dismiss();
|
2018-08-28 11:46:53 +02:00
|
|
|
});
|
2018-03-06 15:28:52 +01:00
|
|
|
},
|
|
|
|
buttons: [ {
|
2018-08-28 11:46:53 +02:00
|
|
|
hidden: true, // Hide it until we calculate if it should be displayed or not.
|
2018-03-06 15:28:52 +01:00
|
|
|
icon: 'link',
|
|
|
|
label: 'core.openinbrowser',
|
|
|
|
action: (event: Event, navCtrl: NavController, module: any, courseId: number): void => {
|
2018-08-28 11:46:53 +02:00
|
|
|
handler.openUrl(module, courseId);
|
2018-03-06 15:28:52 +01:00
|
|
|
}
|
|
|
|
} ]
|
|
|
|
};
|
|
|
|
|
|
|
|
this.hideLinkButton(module, courseId).then((hideButton) => {
|
|
|
|
handlerData.buttons[0].hidden = hideButton;
|
2018-08-28 11:46:53 +02:00
|
|
|
|
|
|
|
if (module.contents && module.contents[0]) {
|
|
|
|
// Calculate the icon to use.
|
|
|
|
handlerData.icon = this.urlProvider.guessIcon(module.contents[0].fileurl) ||
|
|
|
|
this.courseProvider.getModuleIconSrc(this.modName);
|
|
|
|
}
|
2018-03-06 15:28:52 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
return handlerData;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns if contents are loaded to show link button.
|
|
|
|
*
|
|
|
|
* @param {any} module The module object.
|
|
|
|
* @param {number} courseId The course ID.
|
|
|
|
* @return {Promise<boolean>} Resolved when done.
|
|
|
|
*/
|
|
|
|
protected hideLinkButton(module: any, courseId: number): Promise<boolean> {
|
2018-08-24 11:05:39 +02:00
|
|
|
return this.courseProvider.loadModuleContents(module, courseId, undefined, false, false, undefined, this.modName)
|
|
|
|
.then(() => {
|
2018-10-04 16:42:14 +02:00
|
|
|
return !(module.contents && module.contents[0] && module.contents[0].fileurl);
|
2018-03-06 15:28:52 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the component to render the module. This is needed to support singleactivity course format.
|
|
|
|
* The component returned must implement CoreCourseModuleMainComponent.
|
|
|
|
*
|
|
|
|
* @param {any} course The course object.
|
|
|
|
* @param {any} module The module object.
|
|
|
|
* @return {any} The component to use, undefined if not found.
|
|
|
|
*/
|
|
|
|
getMainComponent(course: any, module: any): any {
|
|
|
|
return AddonModUrlIndexComponent;
|
|
|
|
}
|
2018-08-28 11:46:53 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Open the URL.
|
|
|
|
*
|
|
|
|
* @param {any} module The module object.
|
|
|
|
* @param {number} courseId The course ID.
|
|
|
|
*/
|
|
|
|
protected openUrl(module: any, courseId: number): void {
|
|
|
|
this.urlProvider.logView(module.instance).then(() => {
|
|
|
|
this.courseProvider.checkModuleCompletion(courseId, module.completionstatus);
|
|
|
|
});
|
|
|
|
this.urlHelper.open(module.contents[0].fileurl);
|
|
|
|
}
|
2018-03-06 15:28:52 +01:00
|
|
|
}
|