2018-03-05 17:42:54 +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.
|
|
|
|
|
2018-03-22 15:17:12 +01:00
|
|
|
import { Component, Input, Injector } from '@angular/core';
|
2018-03-05 17:42:54 +01:00
|
|
|
import { CoreAppProvider } from '@providers/app';
|
|
|
|
import { CoreCourseProvider } from '@core/course/providers/course';
|
2018-03-15 16:14:54 +01:00
|
|
|
import { CoreCourseModuleMainResourceComponent } from '@core/course/classes/main-resource-component';
|
2018-03-05 17:42:54 +01:00
|
|
|
import { AddonModFolderProvider } from '../../providers/folder';
|
|
|
|
import { AddonModFolderHelperProvider } from '../../providers/helper';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Component that displays a folder.
|
|
|
|
* @todo Adding a new file in a folder updates the revision of all the files, so they're all shown as outdated.
|
2018-03-05 18:01:25 +01:00
|
|
|
* To ignore revision in folders we'll have to modify CoreCourseModulePrefetchDelegate, core-file and CoreFilepoolProvider.
|
2018-03-05 17:42:54 +01:00
|
|
|
*/
|
|
|
|
@Component({
|
|
|
|
selector: 'addon-mod-folder-index',
|
2018-06-12 08:41:17 +02:00
|
|
|
templateUrl: 'addon-mod-folder-index.html',
|
2018-03-05 17:42:54 +01:00
|
|
|
})
|
2018-03-15 16:14:54 +01:00
|
|
|
export class AddonModFolderIndexComponent extends CoreCourseModuleMainResourceComponent {
|
2018-03-05 17:42:54 +01:00
|
|
|
@Input() path: string; // For subfolders. Use the path instead of a boolean so Angular detects them as different states.
|
|
|
|
|
|
|
|
component = AddonModFolderProvider.COMPONENT;
|
|
|
|
canGetFolder: boolean;
|
|
|
|
contents: any;
|
|
|
|
|
2018-03-22 15:17:12 +01:00
|
|
|
constructor(injector: Injector, private folderProvider: AddonModFolderProvider, private courseProvider: CoreCourseProvider,
|
|
|
|
private appProvider: CoreAppProvider, private folderHelper: AddonModFolderHelperProvider) {
|
|
|
|
super(injector);
|
2018-03-05 17:42:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Component being initialized.
|
|
|
|
*/
|
|
|
|
ngOnInit(): void {
|
2018-03-15 16:14:54 +01:00
|
|
|
super.ngOnInit();
|
2018-03-05 17:42:54 +01:00
|
|
|
|
|
|
|
this.canGetFolder = this.folderProvider.isGetFolderWSAvailable();
|
|
|
|
|
|
|
|
if (this.path) {
|
|
|
|
// Subfolder. Use module param.
|
|
|
|
this.showModuleData(this.module);
|
|
|
|
this.loaded = true;
|
|
|
|
this.refreshIcon = 'refresh';
|
|
|
|
} else {
|
2018-03-15 16:14:54 +01:00
|
|
|
this.loadContent().then(() => {
|
2018-03-05 17:42:54 +01:00
|
|
|
this.folderProvider.logView(this.module.instance).then(() => {
|
|
|
|
this.courseProvider.checkModuleCompletion(this.courseId, this.module.completionstatus);
|
|
|
|
});
|
|
|
|
}).finally(() => {
|
|
|
|
this.loaded = true;
|
|
|
|
this.refreshIcon = 'refresh';
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-03-15 16:14:54 +01:00
|
|
|
* Perform the invalidate content function.
|
2018-03-05 17:42:54 +01:00
|
|
|
*
|
2018-03-15 16:14:54 +01:00
|
|
|
* @return {Promise<any>} Resolved when done.
|
2018-03-05 17:42:54 +01:00
|
|
|
*/
|
2018-03-15 16:14:54 +01:00
|
|
|
protected invalidateContent(): Promise<any> {
|
|
|
|
return this.folderProvider.invalidateContent(this.module.id, this.courseId);
|
2018-03-05 17:42:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convenience function to set scope data using module.
|
|
|
|
* @param {any} module Module to show.
|
|
|
|
*/
|
|
|
|
protected showModuleData(module: any): void {
|
|
|
|
this.description = module.intro || module.description;
|
|
|
|
|
2018-03-15 16:14:54 +01:00
|
|
|
this.dataRetrieved.emit(module);
|
2018-03-05 17:42:54 +01:00
|
|
|
|
|
|
|
if (this.path) {
|
|
|
|
// Subfolder.
|
|
|
|
this.contents = module.contents;
|
|
|
|
} else {
|
|
|
|
this.contents = this.folderHelper.formatContents(module.contents);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Download folder contents.
|
|
|
|
*
|
|
|
|
* @param {boolean} [refresh] Whether we're refreshing data.
|
|
|
|
* @return {Promise<any>} Promise resolved when done.
|
|
|
|
*/
|
|
|
|
protected fetchContent(refresh?: boolean): Promise<any> {
|
|
|
|
let promise;
|
|
|
|
|
|
|
|
if (this.canGetFolder) {
|
|
|
|
promise = this.folderProvider.getFolder(this.courseId, this.module.id).then((folder) => {
|
|
|
|
return this.courseProvider.loadModuleContents(this.module, this.courseId).then(() => {
|
|
|
|
folder.contents = this.module.contents;
|
|
|
|
|
|
|
|
return folder;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
promise = this.courseProvider.getModule(this.module.id, this.courseId).then((folder) => {
|
|
|
|
if (!folder.contents.length && this.module.contents.length && !this.appProvider.isOnline()) {
|
|
|
|
// The contents might be empty due to a cached data. Use the old ones.
|
|
|
|
folder.contents = this.module.contents;
|
|
|
|
}
|
|
|
|
this.module = folder;
|
|
|
|
|
|
|
|
return folder;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return promise.then((folder) => {
|
|
|
|
if (folder) {
|
|
|
|
this.description = folder.intro || folder.description;
|
2018-03-15 16:14:54 +01:00
|
|
|
this.dataRetrieved.emit(folder);
|
2018-03-05 17:42:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
this.showModuleData(folder);
|
|
|
|
|
|
|
|
// All data obtained, now fill the context menu.
|
2018-03-15 16:14:54 +01:00
|
|
|
this.fillContextMenu(refresh);
|
2018-03-05 17:42:54 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|