2021-03-03 13:55:43 +01:00
|
|
|
// (C) Copyright 2015 Moodle Pty Ltd.
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2021-11-18 14:42:12 +01:00
|
|
|
import { CoreConstants } from '@/core/constants';
|
2021-06-09 11:38:05 +02:00
|
|
|
import { Component, OnDestroy, OnInit, Optional } from '@angular/core';
|
2021-03-03 13:55:43 +01:00
|
|
|
import { CoreError } from '@classes/errors/error';
|
2021-08-26 09:58:07 +02:00
|
|
|
import { CoreCourseModuleMainResourceComponent } from '@features/course/classes/main-resource-component';
|
2021-03-03 13:55:43 +01:00
|
|
|
import { CoreCourseContentsPage } from '@features/course/pages/contents/contents';
|
2021-09-03 11:51:26 +02:00
|
|
|
import { CoreCourse } from '@features/course/services/course';
|
2021-03-03 13:55:43 +01:00
|
|
|
import { CoreCourseModulePrefetchDelegate } from '@features/course/services/module-prefetch-delegate';
|
2021-05-25 11:01:23 +02:00
|
|
|
import { CoreApp } from '@services/app';
|
2021-06-09 11:38:05 +02:00
|
|
|
import { CoreFileHelper } from '@services/file-helper';
|
2021-03-03 13:55:43 +01:00
|
|
|
import { CoreSites } from '@services/sites';
|
2021-11-18 14:42:12 +01:00
|
|
|
import { CoreDomUtils } from '@services/utils/dom';
|
2021-06-09 11:38:05 +02:00
|
|
|
import { CoreMimetypeUtils } from '@services/utils/mimetype';
|
2021-03-03 13:55:43 +01:00
|
|
|
import { CoreTextUtils } from '@services/utils/text';
|
2021-05-27 13:18:47 +02:00
|
|
|
import { CoreUtils, OpenFileAction } from '@services/utils/utils';
|
2021-06-09 11:38:05 +02:00
|
|
|
import { Network, NgZone, Translate } from '@singletons';
|
|
|
|
import { Subscription } from 'rxjs';
|
2021-03-03 13:55:43 +01:00
|
|
|
import {
|
|
|
|
AddonModResource,
|
|
|
|
AddonModResourceCustomData,
|
|
|
|
AddonModResourceProvider,
|
|
|
|
} from '../../services/resource';
|
|
|
|
import { AddonModResourceHelper } from '../../services/resource-helper';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Component that displays a resource.
|
|
|
|
*/
|
|
|
|
@Component({
|
|
|
|
selector: 'addon-mod-resource-index',
|
|
|
|
templateUrl: 'addon-mod-resource-index.html',
|
2021-11-18 14:42:12 +01:00
|
|
|
styleUrls: ['index.scss'],
|
2021-03-03 13:55:43 +01:00
|
|
|
})
|
2021-06-09 11:38:05 +02:00
|
|
|
export class AddonModResourceIndexComponent extends CoreCourseModuleMainResourceComponent implements OnInit, OnDestroy {
|
2021-03-03 13:55:43 +01:00
|
|
|
|
|
|
|
component = AddonModResourceProvider.COMPONENT;
|
|
|
|
|
|
|
|
mode = '';
|
|
|
|
src = '';
|
|
|
|
contentText = '';
|
|
|
|
displayDescription = true;
|
|
|
|
warning = '';
|
2021-05-25 11:01:23 +02:00
|
|
|
isIOS = false;
|
2021-05-27 13:18:47 +02:00
|
|
|
openFileAction = OpenFileAction;
|
2021-06-09 11:38:05 +02:00
|
|
|
isOnline = false;
|
|
|
|
isStreamedFile = false;
|
|
|
|
shouldOpenInBrowser = false;
|
|
|
|
|
2021-11-18 14:42:12 +01:00
|
|
|
// Variables for 'external' mode.
|
|
|
|
type = '';
|
|
|
|
readableSize = '';
|
|
|
|
timecreated = -1;
|
|
|
|
timemodified = -1;
|
|
|
|
isExternalFile = false;
|
|
|
|
outdatedStatus = CoreConstants.OUTDATED;
|
|
|
|
|
2021-06-09 11:38:05 +02:00
|
|
|
protected onlineObserver?: Subscription;
|
2021-03-03 13:55:43 +01:00
|
|
|
|
|
|
|
constructor(@Optional() courseContentsPage?: CoreCourseContentsPage) {
|
|
|
|
super('AddonModResourceIndexComponent', courseContentsPage);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-06-09 11:38:05 +02:00
|
|
|
* @inheritdoc
|
2021-03-03 13:55:43 +01:00
|
|
|
*/
|
|
|
|
async ngOnInit(): Promise<void> {
|
|
|
|
super.ngOnInit();
|
|
|
|
|
2021-05-25 11:01:23 +02:00
|
|
|
this.isIOS = CoreApp.isIOS();
|
2021-06-09 11:38:05 +02:00
|
|
|
this.isOnline = CoreApp.isOnline();
|
|
|
|
|
2021-11-18 14:42:12 +01:00
|
|
|
// Refresh online status when changes.
|
|
|
|
this.onlineObserver = Network.onChange().subscribe(() => {
|
|
|
|
// Execute the callback in the Angular zone, so change detection doesn't stop working.
|
|
|
|
NgZone.run(() => {
|
|
|
|
this.isOnline = CoreApp.isOnline();
|
2021-06-09 11:38:05 +02:00
|
|
|
});
|
2021-11-18 14:42:12 +01:00
|
|
|
});
|
2021-03-03 13:55:43 +01:00
|
|
|
|
|
|
|
await this.loadContent();
|
|
|
|
try {
|
2021-12-16 22:52:36 +01:00
|
|
|
await AddonModResource.logView(this.module.instance, this.module.name);
|
2021-03-12 10:09:38 +01:00
|
|
|
CoreCourse.checkModuleCompletion(this.courseId, this.module.completiondata);
|
2021-03-03 13:55:43 +01:00
|
|
|
} catch {
|
|
|
|
// Ignore errors.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-06-09 11:38:05 +02:00
|
|
|
* @inheritdoc
|
2021-03-03 13:55:43 +01:00
|
|
|
*/
|
|
|
|
protected async invalidateContent(): Promise<void> {
|
2021-03-12 10:09:38 +01:00
|
|
|
return AddonModResource.invalidateContent(this.module.id, this.courseId);
|
2021-03-03 13:55:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-06-09 11:38:05 +02:00
|
|
|
* @inheritdoc
|
2021-03-03 13:55:43 +01:00
|
|
|
*/
|
|
|
|
protected async fetchContent(refresh?: boolean): Promise<void> {
|
|
|
|
// Load module contents if needed. Passing refresh is needed to force reloading contents.
|
2021-12-17 00:27:04 +01:00
|
|
|
const contents = await CoreCourse.getModuleContents(this.module, undefined, undefined, false, refresh);
|
2021-03-03 13:55:43 +01:00
|
|
|
|
2021-09-07 12:43:09 +02:00
|
|
|
if (!contents.length) {
|
2021-03-03 13:55:43 +01:00
|
|
|
throw new CoreError(Translate.instant('core.filenotfound'));
|
|
|
|
}
|
|
|
|
|
2021-07-29 10:43:19 +02:00
|
|
|
let hasCalledDownloadResource = false;
|
2021-03-03 13:55:43 +01:00
|
|
|
|
|
|
|
// Get the resource instance to get the latest name/description and to know if it's embedded.
|
2021-09-03 11:51:26 +02:00
|
|
|
const resource = await AddonModResource.getResourceData(this.courseId, this.module.id);
|
|
|
|
this.description = resource.intro || '';
|
|
|
|
const options: AddonModResourceCustomData =
|
|
|
|
resource.displayoptions ? CoreTextUtils.unserialize(resource.displayoptions) : {};
|
2021-03-03 13:55:43 +01:00
|
|
|
|
|
|
|
try {
|
2021-12-16 10:46:40 +01:00
|
|
|
this.displayDescription = options.printintro === undefined || !!options.printintro;
|
2021-09-03 11:51:26 +02:00
|
|
|
this.dataRetrieved.emit(resource);
|
2021-03-03 13:55:43 +01:00
|
|
|
|
2021-03-12 10:09:38 +01:00
|
|
|
if (AddonModResourceHelper.isDisplayedInIframe(this.module)) {
|
2021-07-29 10:43:19 +02:00
|
|
|
hasCalledDownloadResource = true;
|
|
|
|
|
2021-03-03 13:55:43 +01:00
|
|
|
const downloadResult = await this.downloadResourceIfNeeded(refresh, true);
|
2021-03-12 10:09:38 +01:00
|
|
|
const src = await AddonModResourceHelper.getIframeSrc(this.module);
|
2021-03-03 13:55:43 +01:00
|
|
|
this.mode = 'iframe';
|
|
|
|
|
|
|
|
if (this.src && src.toString() == this.src.toString()) {
|
|
|
|
// Re-loading same page.
|
|
|
|
// Set it to empty and then re-set the src in the next digest so it detects it has changed.
|
|
|
|
this.src = '';
|
|
|
|
setTimeout(() => {
|
|
|
|
this.src = src;
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
this.src = src;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.warning = downloadResult.failed
|
|
|
|
? this.getErrorDownloadingSomeFilesMessage(downloadResult.error!)
|
|
|
|
: '';
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-03-12 10:09:38 +01:00
|
|
|
if (resource && 'display' in resource && AddonModResourceHelper.isDisplayedEmbedded(this.module, resource.display)) {
|
2021-03-03 13:55:43 +01:00
|
|
|
this.mode = 'embedded';
|
|
|
|
this.warning = '';
|
|
|
|
|
2021-12-17 00:27:04 +01:00
|
|
|
this.contentText = await AddonModResourceHelper.getEmbeddedHtml(this.module);
|
2021-03-03 13:55:43 +01:00
|
|
|
this.mode = this.contentText.length > 0 ? 'embedded' : 'external';
|
|
|
|
} else {
|
|
|
|
this.mode = 'external';
|
|
|
|
this.warning = '';
|
2021-11-18 14:42:12 +01:00
|
|
|
let mimetype: string;
|
2021-06-09 11:38:05 +02:00
|
|
|
|
|
|
|
if (this.isIOS) {
|
2021-09-07 12:43:09 +02:00
|
|
|
this.shouldOpenInBrowser = CoreFileHelper.shouldOpenInBrowser(contents[0]);
|
2021-06-09 11:38:05 +02:00
|
|
|
}
|
|
|
|
|
2021-11-18 14:42:12 +01:00
|
|
|
if ('contentsinfo' in this.module && this.module.contentsinfo) {
|
|
|
|
mimetype = this.module.contentsinfo.mimetypes[0];
|
|
|
|
this.readableSize = CoreTextUtils.bytesToSize(this.module.contentsinfo.filessize, 1);
|
|
|
|
this.timemodified = this.module.contentsinfo.lastmodified * 1000;
|
|
|
|
} else {
|
|
|
|
mimetype = await CoreUtils.getMimeTypeFromUrl(CoreFileHelper.getFileUrl(contents[0]));
|
|
|
|
this.readableSize = CoreTextUtils.bytesToSize(contents[0].filesize, 1);
|
|
|
|
this.timemodified = contents[0].timemodified * 1000;
|
|
|
|
}
|
2021-06-09 11:38:05 +02:00
|
|
|
|
2021-11-18 14:42:12 +01:00
|
|
|
this.timecreated = contents[0].timecreated * 1000;
|
|
|
|
this.isExternalFile = !!contents[0].isexternalfile;
|
|
|
|
this.type = CoreMimetypeUtils.getMimetypeDescription(mimetype);
|
2021-06-09 11:38:05 +02:00
|
|
|
this.isStreamedFile = CoreMimetypeUtils.isStreamedMimetype(mimetype);
|
2021-03-03 13:55:43 +01:00
|
|
|
}
|
|
|
|
} finally {
|
2021-07-29 10:43:19 +02:00
|
|
|
// Pass false in some cases because downloadResourceIfNeeded already invalidates and refresh data if refresh=true.
|
|
|
|
this.fillContextMenu(hasCalledDownloadResource ? false : refresh);
|
2021-03-03 13:55:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Opens a file.
|
|
|
|
*
|
2021-05-27 13:18:47 +02:00
|
|
|
* @param iOSOpenFileAction Action to do in iOS.
|
2021-03-03 13:55:43 +01:00
|
|
|
* @return Promise resolved when done.
|
|
|
|
*/
|
2021-05-27 13:18:47 +02:00
|
|
|
async open(iOSOpenFileAction?: OpenFileAction): Promise<void> {
|
2021-03-12 10:09:38 +01:00
|
|
|
let downloadable = await CoreCourseModulePrefetchDelegate.isModuleDownloadable(this.module, this.courseId);
|
2021-03-03 13:55:43 +01:00
|
|
|
|
|
|
|
if (downloadable) {
|
|
|
|
// Check if the main file is downloadle.
|
|
|
|
// This isn't done in "isDownloadable" to prevent extra WS calls in the course page.
|
2021-03-12 10:09:38 +01:00
|
|
|
downloadable = await AddonModResourceHelper.isMainFileDownloadable(this.module);
|
2021-03-03 13:55:43 +01:00
|
|
|
|
|
|
|
if (downloadable) {
|
2021-11-18 14:42:12 +01:00
|
|
|
if (this.prefetchStatus === CoreConstants.OUTDATED && !this.isOnline) {
|
|
|
|
// Warn the user that the file isn't updated.
|
|
|
|
const alert = await CoreDomUtils.showAlert(
|
|
|
|
undefined,
|
2022-01-31 09:41:41 +01:00
|
|
|
Translate.instant('addon.mod_resource.resourcestatusoutdatedconfirm'),
|
2021-11-18 14:42:12 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
await alert.onWillDismiss();
|
|
|
|
}
|
|
|
|
|
2021-05-27 13:18:47 +02:00
|
|
|
return AddonModResourceHelper.openModuleFile(this.module, this.courseId, { iOSOpenFileAction });
|
2021-03-03 13:55:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// The resource cannot be downloaded, open the activity in browser.
|
2021-10-07 08:58:05 +02:00
|
|
|
await CoreSites.getCurrentSite()?.openInBrowserWithAutoLoginIfSameSite(this.module.url || '');
|
2021-03-03 13:55:43 +01:00
|
|
|
}
|
|
|
|
|
2021-06-09 11:38:05 +02:00
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
ngOnDestroy(): void {
|
|
|
|
super.ngOnDestroy();
|
|
|
|
this.onlineObserver?.unsubscribe();
|
|
|
|
}
|
|
|
|
|
2021-03-03 13:55:43 +01:00
|
|
|
}
|