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-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';
|
|
|
|
import {
|
|
|
|
CoreCourseModuleMainResourceComponent,
|
|
|
|
} from '@features/course/classes/main-resource-component';
|
|
|
|
import { CoreCourseContentsPage } from '@features/course/pages/contents/contents';
|
|
|
|
import { CoreCourse, CoreCourseWSModule } from '@features/course/services/course';
|
|
|
|
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-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,
|
|
|
|
AddonModResourceResource,
|
|
|
|
} 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-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;
|
|
|
|
|
|
|
|
canGetResource = false;
|
|
|
|
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;
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
this.canGetResource = AddonModResource.isGetResourceWSAvailable();
|
2021-05-25 11:01:23 +02:00
|
|
|
this.isIOS = CoreApp.isIOS();
|
2021-06-09 11:38:05 +02:00
|
|
|
this.isOnline = CoreApp.isOnline();
|
|
|
|
|
|
|
|
if (this.isIOS) {
|
|
|
|
// 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-03-03 13:55:43 +01:00
|
|
|
|
|
|
|
await this.loadContent();
|
|
|
|
try {
|
2021-03-12 10:09:38 +01:00
|
|
|
await AddonModResource.logView(this.module.instance!, this.module.name);
|
|
|
|
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-03-12 10:09:38 +01:00
|
|
|
await CoreCourse.loadModuleContents(this.module, this.courseId, undefined, false, refresh);
|
2021-03-03 13:55:43 +01:00
|
|
|
|
2021-03-12 10:09:38 +01:00
|
|
|
if (!this.module.contents || !this.module.contents.length) {
|
2021-03-03 13:55:43 +01:00
|
|
|
throw new CoreError(Translate.instant('core.filenotfound'));
|
|
|
|
}
|
|
|
|
|
|
|
|
let resource: AddonModResourceResource | CoreCourseWSModule | undefined;
|
|
|
|
let options: AddonModResourceCustomData = {};
|
|
|
|
|
|
|
|
// Get the resource instance to get the latest name/description and to know if it's embedded.
|
|
|
|
if (this.canGetResource) {
|
2021-03-12 10:09:38 +01:00
|
|
|
resource = await CoreUtils.ignoreErrors(AddonModResource.getResourceData(this.courseId, this.module.id));
|
2021-03-03 13:55:43 +01:00
|
|
|
this.description = resource?.intro || '';
|
|
|
|
options = resource?.displayoptions ? CoreTextUtils.unserialize(resource.displayoptions) : {};
|
|
|
|
} else {
|
2021-03-12 10:09:38 +01:00
|
|
|
resource = await CoreUtils.ignoreErrors(CoreCourse.getModule(this.module.id, this.courseId));
|
2021-03-03 13:55:43 +01:00
|
|
|
this.description = resource?.description || '';
|
|
|
|
options = resource?.customdata ? CoreTextUtils.unserialize(CoreTextUtils.parseJSON(resource.customdata)) : {};
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
if (resource) {
|
|
|
|
this.displayDescription = typeof options.printintro == 'undefined' || !!options.printintro;
|
|
|
|
this.dataRetrieved.emit(resource);
|
|
|
|
}
|
|
|
|
|
2021-03-12 10:09:38 +01:00
|
|
|
if (AddonModResourceHelper.isDisplayedInIframe(this.module)) {
|
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-03-12 10:09:38 +01:00
|
|
|
this.contentText = await AddonModResourceHelper.getEmbeddedHtml(this.module, this.courseId);
|
2021-03-03 13:55:43 +01:00
|
|
|
this.mode = this.contentText.length > 0 ? 'embedded' : 'external';
|
|
|
|
} else {
|
|
|
|
this.mode = 'external';
|
|
|
|
this.warning = '';
|
2021-06-09 11:38:05 +02:00
|
|
|
|
|
|
|
if (this.isIOS) {
|
|
|
|
this.shouldOpenInBrowser = CoreFileHelper.shouldOpenInBrowser(this.module.contents[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
const mimetype = await CoreUtils.getMimeTypeFromUrl(CoreFileHelper.getFileUrl(this.module.contents[0]));
|
|
|
|
|
|
|
|
this.isStreamedFile = CoreMimetypeUtils.isStreamedMimetype(mimetype);
|
2021-03-03 13:55:43 +01:00
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
this.fillContextMenu(refresh);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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-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-03-12 10:09:38 +01: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
|
|
|
}
|