2018-03-02 16:59:39 +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-22 16:14:51 +02:00
|
|
|
import { TranslateService } from '@ngx-translate/core';
|
2018-03-02 16:59:39 +01:00
|
|
|
import { AddonModResourceProvider } from './resource';
|
2018-03-05 15:19:39 +01:00
|
|
|
import { AddonModResourceHelperProvider } from './helper';
|
2018-03-02 16:59:39 +01:00
|
|
|
import { AddonModResourceIndexComponent } from '../components/index/index';
|
|
|
|
import { CoreCourseModuleHandler, CoreCourseModuleHandlerData } from '@core/course/providers/module-delegate';
|
|
|
|
import { CoreCourseProvider } from '@core/course/providers/course';
|
|
|
|
import { CoreMimetypeUtilsProvider } from '@providers/utils/mimetype';
|
2018-08-22 16:14:51 +02:00
|
|
|
import { CoreTextUtilsProvider } from '@providers/utils/text';
|
2018-04-09 13:14:55 +02:00
|
|
|
import { CoreCourseModulePrefetchDelegate } from '@core/course/providers/module-prefetch-delegate';
|
|
|
|
import { CoreConstants } from '@core/constants';
|
2018-08-22 16:14:51 +02:00
|
|
|
import * as moment from 'moment';
|
2018-03-02 16:59:39 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Handler to support resource modules.
|
|
|
|
*/
|
|
|
|
@Injectable()
|
|
|
|
export class AddonModResourceModuleHandler implements CoreCourseModuleHandler {
|
2018-03-23 13:24:31 +01:00
|
|
|
name = 'AddonModResource';
|
|
|
|
modName = 'resource';
|
2018-03-02 16:59:39 +01:00
|
|
|
|
2018-04-09 13:14:55 +02:00
|
|
|
protected statusObserver;
|
|
|
|
|
2018-03-02 16:59:39 +01:00
|
|
|
constructor(protected resourceProvider: AddonModResourceProvider, private courseProvider: CoreCourseProvider,
|
2018-04-09 13:14:55 +02:00
|
|
|
protected mimetypeUtils: CoreMimetypeUtilsProvider, private resourceHelper: AddonModResourceHelperProvider,
|
2018-08-22 16:14:51 +02:00
|
|
|
protected prefetchDelegate: CoreCourseModulePrefetchDelegate, protected textUtils: CoreTextUtilsProvider,
|
|
|
|
protected translate: TranslateService) {
|
2018-04-09 13:14:55 +02:00
|
|
|
}
|
2018-03-02 16:59:39 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if the handler is enabled on a site level.
|
|
|
|
*
|
|
|
|
* @return {boolean|Promise<boolean>} Whether or not the handler is enabled on a site level.
|
|
|
|
*/
|
|
|
|
isEnabled(): boolean | Promise<boolean> {
|
|
|
|
return this.resourceProvider.isPluginEnabled();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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-04-09 13:14:55 +02:00
|
|
|
const updateStatus = (status: string): void => {
|
|
|
|
handlerData.buttons[0].hidden = status !== CoreConstants.DOWNLOADED ||
|
|
|
|
this.resourceHelper.isDisplayedInIframe(module);
|
|
|
|
};
|
|
|
|
|
2018-08-22 16:14:51 +02:00
|
|
|
const handlerData: CoreCourseModuleHandlerData = {
|
2018-08-24 11:05:39 +02:00
|
|
|
icon: this.courseProvider.getModuleIconSrc(this.modName),
|
2018-03-02 16:59:39 +01:00
|
|
|
title: module.name,
|
|
|
|
class: 'addon-mod_resource-handler',
|
|
|
|
showDownloadButton: true,
|
|
|
|
action(event: Event, navCtrl: NavController, module: any, courseId: number, options: NavOptions): void {
|
|
|
|
navCtrl.push('AddonModResourceIndexPage', {module: module, courseId: courseId}, options);
|
2018-03-05 15:19:39 +01:00
|
|
|
},
|
2018-04-09 13:14:55 +02:00
|
|
|
updateStatus: updateStatus.bind(this),
|
2018-03-05 15:19:39 +01:00
|
|
|
buttons: [ {
|
2018-04-09 13:14:55 +02:00
|
|
|
hidden: true,
|
2018-03-05 15:19:39 +01:00
|
|
|
icon: 'document',
|
|
|
|
label: 'addon.mod_resource.openthefile',
|
|
|
|
action: (event: Event, navCtrl: NavController, module: any, courseId: number): void => {
|
|
|
|
this.hideOpenButton(module, courseId).then((hide) => {
|
|
|
|
if (!hide) {
|
|
|
|
this.resourceHelper.openModuleFile(module, courseId);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
} ]
|
2018-03-02 16:59:39 +01:00
|
|
|
};
|
2018-03-05 15:19:39 +01:00
|
|
|
|
2018-08-24 11:05:39 +02:00
|
|
|
this.getResourceData(module, courseId, handlerData).then((data) => {
|
2018-08-22 16:14:51 +02:00
|
|
|
handlerData.icon = data.icon;
|
|
|
|
handlerData.extraBadge = data.extra;
|
|
|
|
handlerData.extraBadgeColor = 'light';
|
2018-03-05 15:19:39 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
return handlerData;
|
2018-03-02 16:59:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-03-05 15:19:39 +01:00
|
|
|
* Returns if contents are loaded to show open button.
|
2018-03-02 16:59:39 +01:00
|
|
|
*
|
|
|
|
* @param {any} module The module object.
|
|
|
|
* @param {number} courseId The course ID.
|
2018-03-05 15:19:39 +01:00
|
|
|
* @return {Promise<boolean>} Resolved when done.
|
2018-03-02 16:59:39 +01:00
|
|
|
*/
|
2018-03-05 15:19:39 +01:00
|
|
|
protected hideOpenButton(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-04-09 13:14:55 +02:00
|
|
|
return this.prefetchDelegate.getModuleStatus(module, courseId).then((status) => {
|
|
|
|
return status !== CoreConstants.DOWNLOADED || this.resourceHelper.isDisplayedInIframe(module);
|
|
|
|
});
|
2018-03-05 15:19:39 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-08-22 16:14:51 +02:00
|
|
|
* Returns the activity icon and data.
|
2018-03-05 15:19:39 +01:00
|
|
|
*
|
|
|
|
* @param {any} module The module object.
|
|
|
|
* @param {number} courseId The course ID.
|
2018-08-22 16:14:51 +02:00
|
|
|
* @return {Promise<any>} Resource data.
|
2018-03-05 15:19:39 +01:00
|
|
|
*/
|
2018-08-24 11:05:39 +02:00
|
|
|
protected getResourceData(module: any, courseId: number, handlerData: CoreCourseModuleHandlerData): Promise<any> {
|
|
|
|
const promises = [];
|
|
|
|
let resourceInfo;
|
2018-03-02 16:59:39 +01:00
|
|
|
|
2018-08-24 11:05:39 +02:00
|
|
|
// Check if the button needs to be shown or not. This also loads the module contents.
|
|
|
|
promises.push(this.hideOpenButton(module, courseId).then((hideOpenButton) => {
|
|
|
|
handlerData.buttons[0].hidden = hideOpenButton;
|
|
|
|
}));
|
|
|
|
|
2018-09-28 08:22:15 +02:00
|
|
|
if (this.resourceProvider.isGetResourceWSAvailable()) {
|
|
|
|
// Get the resource data.
|
|
|
|
promises.push(this.resourceProvider.getResourceData(courseId, module.id).then((info) => {
|
|
|
|
resourceInfo = info;
|
|
|
|
}));
|
|
|
|
}
|
2018-08-24 11:05:39 +02:00
|
|
|
|
|
|
|
return Promise.all(promises).then(() => {
|
2018-09-28 08:22:15 +02:00
|
|
|
const files = module.contents && module.contents.length ? module.contents : resourceInfo && resourceInfo.contentfiles,
|
2018-08-24 11:05:39 +02:00
|
|
|
resourceData = {
|
|
|
|
icon: '',
|
|
|
|
extra: ''
|
|
|
|
},
|
2018-09-28 08:22:15 +02:00
|
|
|
options = (resourceInfo && this.textUtils.unserialize(resourceInfo.displayoptions)) || {},
|
2018-08-24 11:05:39 +02:00
|
|
|
extra = [];
|
|
|
|
|
|
|
|
if (files && files.length) {
|
|
|
|
const file = files[0];
|
|
|
|
resourceData.icon = this.mimetypeUtils.getFileIcon(file.filename);
|
|
|
|
|
|
|
|
if (options.showsize) {
|
|
|
|
const size = files.reduce((result, file) => {
|
|
|
|
return result + file.filesize;
|
|
|
|
}, 0);
|
|
|
|
extra.push(this.textUtils.bytesToSize(size, 1));
|
2018-08-22 16:14:51 +02:00
|
|
|
}
|
2018-08-24 11:05:39 +02:00
|
|
|
if (options.showtype) {
|
|
|
|
extra.push(this.mimetypeUtils.getMimetypeDescription(file));
|
2018-08-22 16:14:51 +02:00
|
|
|
}
|
2018-09-07 15:41:48 +02:00
|
|
|
|
|
|
|
if (options.showdate) {
|
|
|
|
/* Modified date may be up to several minutes later than uploaded date just because
|
|
|
|
teacher did not submit the form promptly. Give teacher up to 5 minutes to do it. */
|
|
|
|
if (file.timemodified > file.timecreated + CoreConstants.SECONDS_MINUTE * 5) {
|
|
|
|
extra.push(this.translate.instant('addon.mod_resource.modifieddate',
|
|
|
|
{$a: moment(file.timemodified * 1000).format('LLL')}));
|
|
|
|
} else {
|
|
|
|
extra.push(this.translate.instant('addon.mod_resource.uploadeddate',
|
|
|
|
{$a: moment(file.timecreated * 1000).format('LLL')}));
|
|
|
|
}
|
|
|
|
}
|
2018-08-24 11:05:39 +02:00
|
|
|
}
|
2018-08-22 16:14:51 +02:00
|
|
|
|
2018-08-24 11:05:39 +02:00
|
|
|
if (resourceData.icon == '') {
|
|
|
|
resourceData.icon = this.courseProvider.getModuleIconSrc(this.modName);
|
|
|
|
}
|
2018-08-22 16:14:51 +02:00
|
|
|
|
2018-08-24 11:05:39 +02:00
|
|
|
resourceData.extra += extra.join(' ');
|
|
|
|
|
|
|
|
return resourceData;
|
2018-03-02 16:59:39 +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 AddonModResourceIndexComponent;
|
|
|
|
}
|
|
|
|
}
|