MOBILE-2340 folder: Folder activity implementation
parent
bf16815e68
commit
14ccd74b3a
|
@ -0,0 +1,45 @@
|
|||
// (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 { NgModule } from '@angular/core';
|
||||
import { CommonModule } from '@angular/common';
|
||||
import { IonicModule } from 'ionic-angular';
|
||||
import { TranslateModule } from '@ngx-translate/core';
|
||||
import { CoreComponentsModule } from '@components/components.module';
|
||||
import { CoreDirectivesModule } from '@directives';
|
||||
import { CoreCourseComponentsModule } from '@core/course/components/components.module';
|
||||
import { AddonModFolderIndexComponent } from './index/index';
|
||||
|
||||
@NgModule({
|
||||
declarations: [
|
||||
AddonModFolderIndexComponent
|
||||
],
|
||||
imports: [
|
||||
CommonModule,
|
||||
IonicModule,
|
||||
TranslateModule.forChild(),
|
||||
CoreComponentsModule,
|
||||
CoreDirectivesModule,
|
||||
CoreCourseComponentsModule
|
||||
],
|
||||
providers: [
|
||||
],
|
||||
exports: [
|
||||
AddonModFolderIndexComponent
|
||||
],
|
||||
entryComponents: [
|
||||
AddonModFolderIndexComponent
|
||||
]
|
||||
})
|
||||
export class AddonModFolderComponentsModule {}
|
|
@ -0,0 +1,29 @@
|
|||
<!-- Buttons to add to the header. -->
|
||||
<core-navbar-buttons end>
|
||||
<core-context-menu>
|
||||
<core-context-menu-item *ngIf="externalUrl" [priority]="900" [content]="'core.openinbrowser' | translate" [href]="externalUrl" [iconAction]="'open'"></core-context-menu-item>
|
||||
<core-context-menu-item *ngIf="description" [priority]="800" [content]="'core.moduleintro' | translate" (action)="expandDescription()" [iconAction]="'arrow-forward'"></core-context-menu-item>
|
||||
<core-context-menu-item [priority]="700" [content]="'core.refresh' | translate" (action)="doRefresh(null, $event)" [iconAction]="refreshIcon" [closeOnClick]="false"></core-context-menu-item>
|
||||
<core-context-menu-item *ngIf="prefetchStatusIcon" [priority]="600" [content]="prefetchText" (action)="prefetch()" [iconAction]="prefetchStatusIcon" [closeOnClick]="false"></core-context-menu-item>
|
||||
<core-context-menu-item *ngIf="size" [priority]="500" [content]="size" [iconDescription]="'cube'" (action)="removeFiles()" [iconAction]="'trash'"></core-context-menu-item>
|
||||
</core-context-menu>
|
||||
</core-navbar-buttons>
|
||||
|
||||
<!-- Content. -->
|
||||
<core-loading [hideUntil]="loaded" class="core-loading-center">
|
||||
|
||||
<core-course-module-description [description]="description" [component]="component" [componentId]="componentId"></core-course-module-description>
|
||||
|
||||
<ion-list *ngIf="contents && contents.length > 0">
|
||||
<ng-container *ngFor="let file of contents">
|
||||
<a *ngIf="file.type === 'folder'" ion-item class="item-media" [navPush]="'AddonModFolderIndexPage'" [navParams]="{path: file.filepath, courseId: courseId, module: file}">
|
||||
<img [src]="file.fileicon" alt="" role="presentation" item-start>
|
||||
<p>{{file.name}}</p>
|
||||
</a>
|
||||
<core-file *ngIf="file.type !== 'folder'" [file]="file" [component]="component" [componentId]="file.contextid"></core-file>
|
||||
</ng-container>
|
||||
</ion-list>
|
||||
|
||||
<core-empty-box *ngIf="!contents || contents.length == 0" icon="folder" [message]=" 'addon.mod_folder.emptyfilelist' | translate"></core-empty-box>
|
||||
|
||||
</core-loading>
|
|
@ -0,0 +1,212 @@
|
|||
// (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 { Component, OnInit, OnDestroy, Input, Output, EventEmitter, Optional } from '@angular/core';
|
||||
import { NavParams, NavController, Content } from 'ionic-angular';
|
||||
import { TranslateService } from '@ngx-translate/core';
|
||||
import { CoreAppProvider } from '@providers/app';
|
||||
import { CoreDomUtilsProvider } from '@providers/utils/dom';
|
||||
import { CoreTextUtilsProvider } from '@providers/utils/text';
|
||||
import { CoreCourseProvider } from '@core/course/providers/course';
|
||||
import { CoreCourseHelperProvider } from '@core/course/providers/helper';
|
||||
import { CoreCourseModuleMainComponent } from '@core/course/providers/module-delegate';
|
||||
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.
|
||||
* To ignore revision in folders we'll have to modify $mmCoursePrefetchDelegate, core-file and $mmFilepool.
|
||||
*/
|
||||
@Component({
|
||||
selector: 'addon-mod-folder-index',
|
||||
templateUrl: 'index.html',
|
||||
})
|
||||
export class AddonModFolderIndexComponent implements OnInit, OnDestroy, CoreCourseModuleMainComponent {
|
||||
@Input() module: any; // The module of the folder.
|
||||
@Input() courseId: number; // Course ID the folder belongs to.
|
||||
@Input() path: string; // For subfolders. Use the path instead of a boolean so Angular detects them as different states.
|
||||
@Output() folderRetrieved?: EventEmitter<any>;
|
||||
|
||||
loaded: boolean;
|
||||
canReload: boolean;
|
||||
component = AddonModFolderProvider.COMPONENT;
|
||||
componentId: number;
|
||||
canGetFolder: boolean;
|
||||
contents: any;
|
||||
|
||||
// Data for context menu.
|
||||
externalUrl: string;
|
||||
description: string;
|
||||
refreshIcon: string;
|
||||
prefetchStatusIcon: string;
|
||||
prefetchText: string;
|
||||
size: string;
|
||||
|
||||
protected isDestroyed;
|
||||
protected statusObserver;
|
||||
|
||||
constructor(private folderProvider: AddonModFolderProvider, private courseProvider: CoreCourseProvider,
|
||||
private domUtils: CoreDomUtilsProvider, private appProvider: CoreAppProvider, private textUtils: CoreTextUtilsProvider,
|
||||
private courseHelper: CoreCourseHelperProvider, private translate: TranslateService,
|
||||
@Optional() private content: Content, private folderHelper: AddonModFolderHelperProvider) {
|
||||
this.folderRetrieved = new EventEmitter();
|
||||
}
|
||||
|
||||
/**
|
||||
* Component being initialized.
|
||||
*/
|
||||
ngOnInit(): void {
|
||||
this.description = this.module.description;
|
||||
this.componentId = this.module.id;
|
||||
this.externalUrl = this.module.url;
|
||||
this.loaded = false;
|
||||
this.canReload = false;
|
||||
this.refreshIcon = 'spinner';
|
||||
|
||||
this.canGetFolder = this.folderProvider.isGetFolderWSAvailable();
|
||||
|
||||
if (this.path) {
|
||||
// Subfolder. Use module param.
|
||||
this.showModuleData(this.module);
|
||||
this.loaded = true;
|
||||
this.canReload = false;
|
||||
this.refreshIcon = 'refresh';
|
||||
} else {
|
||||
this.fetchContent().then(() => {
|
||||
this.folderProvider.logView(this.module.instance).then(() => {
|
||||
this.courseProvider.checkModuleCompletion(this.courseId, this.module.completionstatus);
|
||||
});
|
||||
}).finally(() => {
|
||||
this.loaded = true;
|
||||
this.canReload = true;
|
||||
this.refreshIcon = 'refresh';
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Refresh the data.
|
||||
*
|
||||
* @param {any} [refresher] Refresher.
|
||||
* @param {Function} [done] Function to call when done.
|
||||
* @return {Promise<any>} Promise resolved when done.
|
||||
*/
|
||||
doRefresh(refresher?: any, done?: () => void): Promise<any> {
|
||||
if (this.canReload) {
|
||||
this.refreshIcon = 'spinner';
|
||||
|
||||
return this.folderProvider.invalidateContent(this.module.id, this.courseId).catch(() => {
|
||||
// Ignore errors.
|
||||
}).then(() => {
|
||||
return this.fetchContent(true);
|
||||
}).finally(() => {
|
||||
this.refreshIcon = 'refresh';
|
||||
refresher && refresher.complete();
|
||||
done && done();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Expand the description.
|
||||
*/
|
||||
expandDescription(): void {
|
||||
this.textUtils.expandText(this.translate.instant('core.description'), this.description, this.component, this.module.id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Prefetch the module.
|
||||
*/
|
||||
prefetch(): void {
|
||||
this.courseHelper.contextMenuPrefetch(this, this.module, this.courseId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Confirm and remove downloaded files.
|
||||
*/
|
||||
removeFiles(): void {
|
||||
this.courseHelper.confirmAndRemoveFiles(this.module, this.courseId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
|
||||
this.folderRetrieved.emit(module);
|
||||
|
||||
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;
|
||||
this.folderRetrieved.emit(folder);
|
||||
}
|
||||
|
||||
this.showModuleData(folder);
|
||||
|
||||
// All data obtained, now fill the context menu.
|
||||
this.courseHelper.fillContextMenu(this, this.module, this.courseId, refresh, this.component);
|
||||
}).catch((error) => {
|
||||
// Error getting data, fail.
|
||||
this.domUtils.showErrorModalDefault(error, 'core.course.errorgetmodule', true);
|
||||
}).finally(() => {
|
||||
this.loaded = true;
|
||||
this.refreshIcon = 'refresh';
|
||||
});
|
||||
}
|
||||
|
||||
ngOnDestroy(): void {
|
||||
this.isDestroyed = true;
|
||||
this.statusObserver && this.statusObserver.off();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,38 @@
|
|||
// (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 { NgModule } from '@angular/core';
|
||||
import { AddonModFolderProvider } from './providers/folder';
|
||||
import { AddonModFolderHelperProvider } from './providers/helper';
|
||||
import { AddonModFolderModuleHandler } from './providers/module-handler';
|
||||
import { CoreCourseModuleDelegate } from '@core/course/providers/module-delegate';
|
||||
import { AddonModFolderComponentsModule } from './components/components.module';
|
||||
|
||||
@NgModule({
|
||||
declarations: [
|
||||
],
|
||||
imports: [
|
||||
AddonModFolderComponentsModule
|
||||
],
|
||||
providers: [
|
||||
AddonModFolderProvider,
|
||||
AddonModFolderHelperProvider,
|
||||
AddonModFolderModuleHandler
|
||||
]
|
||||
})
|
||||
export class AddonModFolderModule {
|
||||
constructor(moduleDelegate: CoreCourseModuleDelegate, moduleHandler: AddonModFolderModuleHandler) {
|
||||
moduleDelegate.registerHandler(moduleHandler);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,4 @@
|
|||
{
|
||||
"emptyfilelist": "There are no files to show.",
|
||||
"errorwhilegettingfolder": "Error while getting folder data."
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
<ion-header>
|
||||
<ion-navbar>
|
||||
<ion-title><core-format-text [text]="title"></core-format-text></ion-title>
|
||||
|
||||
<ion-buttons end>
|
||||
<!-- The buttons defined by the component will be added in here. -->
|
||||
</ion-buttons>
|
||||
</ion-navbar>
|
||||
</ion-header>
|
||||
<ion-content>
|
||||
<ion-refresher [enabled]="folderComponent.loaded" (ionRefresh)="folderComponent.doRefresh($event)">
|
||||
<ion-refresher-content pullingText="{{ 'core.pulltorefresh' | translate }}"></ion-refresher-content>
|
||||
</ion-refresher>
|
||||
|
||||
<addon-mod-folder-index [module]="module" [courseId]="courseId" [path]="path" (folderRetrieved)="updateData($event)"></addon-mod-folder-index>
|
||||
</ion-content>
|
|
@ -0,0 +1,33 @@
|
|||
// (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 { NgModule } from '@angular/core';
|
||||
import { IonicPageModule } from 'ionic-angular';
|
||||
import { TranslateModule } from '@ngx-translate/core';
|
||||
import { CoreDirectivesModule } from '@directives';
|
||||
import { AddonModFolderComponentsModule } from '../../components/components.module';
|
||||
import { AddonModFolderIndexPage } from './index';
|
||||
|
||||
@NgModule({
|
||||
declarations: [
|
||||
AddonModFolderIndexPage,
|
||||
],
|
||||
imports: [
|
||||
CoreDirectivesModule,
|
||||
AddonModFolderComponentsModule,
|
||||
IonicPageModule.forChild(AddonModFolderIndexPage),
|
||||
TranslateModule.forChild()
|
||||
],
|
||||
})
|
||||
export class AddonModFolderIndexPageModule {}
|
|
@ -0,0 +1,50 @@
|
|||
// (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 { Component, ViewChild } from '@angular/core';
|
||||
import { IonicPage, NavParams } from 'ionic-angular';
|
||||
import { AddonModFolderIndexComponent } from '../../components/index/index';
|
||||
|
||||
/**
|
||||
* Page that displays a folder.
|
||||
*/
|
||||
@IonicPage({ segment: 'addon-mod-folder-index' })
|
||||
@Component({
|
||||
selector: 'page-addon-mod-folder-index',
|
||||
templateUrl: 'index.html',
|
||||
})
|
||||
export class AddonModFolderIndexPage {
|
||||
@ViewChild(AddonModFolderIndexComponent) folderComponent: AddonModFolderIndexComponent;
|
||||
|
||||
title: string;
|
||||
module: any;
|
||||
courseId: number;
|
||||
path: string;
|
||||
|
||||
constructor(navParams: NavParams) {
|
||||
this.module = navParams.get('module') || {};
|
||||
this.courseId = navParams.get('courseId');
|
||||
this.path = navParams.get('path');
|
||||
this.title = this.module.name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update some data based on the folder instance.
|
||||
*
|
||||
* @param {any} folder Folder instance.
|
||||
*/
|
||||
updateData(folder: any): void {
|
||||
this.title = folder.name || this.title;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,147 @@
|
|||
// (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 { CoreLoggerProvider } from '@providers/logger';
|
||||
import { CoreSitesProvider } from '@providers/sites';
|
||||
import { CoreUtilsProvider } from '@providers/utils/utils';
|
||||
import { CoreCourseProvider } from '@core/course/providers/course';
|
||||
|
||||
/**
|
||||
* Service that provides some features for folder.
|
||||
*/
|
||||
@Injectable()
|
||||
export class AddonModFolderProvider {
|
||||
static COMPONENT = 'mmaModFolder';
|
||||
|
||||
protected ROOT_CACHE_KEY = 'mmaModFolder:';
|
||||
protected logger;
|
||||
|
||||
constructor(logger: CoreLoggerProvider, private sitesProvider: CoreSitesProvider, private courseProvider: CoreCourseProvider,
|
||||
private utils: CoreUtilsProvider) {
|
||||
this.logger = logger.getInstance('mmaModFolderProvider');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a folder by course module ID.
|
||||
*
|
||||
* @param {number} courseId Course ID.
|
||||
* @param {number} cmId Course module ID.
|
||||
* @param {string} [siteId] Site ID. If not defined, current site.
|
||||
* @return {Promise<any>} Promise resolved when the book is retrieved.
|
||||
*/
|
||||
getFolder(courseId: number, cmId: number, siteId?: string): Promise<any> {
|
||||
return this.getFolderByKey(courseId, 'coursemodule', cmId, siteId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a folder.
|
||||
*
|
||||
* @param {number} courseId Course ID.
|
||||
* @param {string} key Name of the property to check.
|
||||
* @param {any} value Value to search.
|
||||
* @param {string} [siteId] Site ID. If not defined, current site.
|
||||
* @return {Promise<any>} Promise resolved when the book is retrieved.
|
||||
*/
|
||||
protected getFolderByKey(courseId: number, key: string, value: any, siteId?: string): Promise<any> {
|
||||
return this.sitesProvider.getSite(siteId).then((site) => {
|
||||
const params = {
|
||||
courseids: [courseId]
|
||||
},
|
||||
preSets = {
|
||||
cacheKey: this.getFolderCacheKey(courseId)
|
||||
};
|
||||
|
||||
return site.read('mod_folder_get_folders_by_courses', params, preSets).then((response) => {
|
||||
if (response && response.folders) {
|
||||
const currentFolder = response.folders.find((folder) => {
|
||||
return folder[key] == value;
|
||||
});
|
||||
if (currentFolder) {
|
||||
return currentFolder;
|
||||
}
|
||||
}
|
||||
|
||||
return Promise.reject(null);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Get cache key for folder data WS calls.
|
||||
*
|
||||
* @param {number} courseId Course ID.
|
||||
* @return {string} Cache key.
|
||||
*/
|
||||
protected getFolderCacheKey(courseId: number): string {
|
||||
return this.ROOT_CACHE_KEY + 'folder:' + courseId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Invalidate the prefetched content.
|
||||
*
|
||||
* @param {number} moduleId The module ID.
|
||||
* @param {number} courseId Course ID of the module.
|
||||
* @param {string} [siteId] Site ID. If not defined, current site.
|
||||
* @return {Promise<any>}
|
||||
*/
|
||||
invalidateContent(moduleId: number, courseId: number, siteId?: string): Promise<any> {
|
||||
const promises = [];
|
||||
|
||||
promises.push(this.invalidateFolderData(courseId, siteId));
|
||||
promises.push(this.courseProvider.invalidateModule(moduleId, siteId));
|
||||
|
||||
return this.utils.allPromises(promises);
|
||||
}
|
||||
|
||||
/**
|
||||
* Invalidates folder data.
|
||||
*
|
||||
* @param {number} courseId Course ID.
|
||||
* @param {string} [siteId] Site ID. If not defined, current site.
|
||||
* @return {Promise<any>} Promise resolved when the data is invalidated.
|
||||
*/
|
||||
invalidateFolderData(courseId: number, siteId?: string): Promise<any> {
|
||||
return this.sitesProvider.getSite(siteId).then((site) => {
|
||||
return site.invalidateWsCacheForKey(this.getFolderCacheKey(courseId));
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether or not getFolder WS available or not.
|
||||
*
|
||||
* @return {boolean} If WS is avalaible.
|
||||
*/
|
||||
isGetFolderWSAvailable(): boolean {
|
||||
return this.sitesProvider.wsAvailableInCurrentSite('mod_folder_get_folders_by_courses');
|
||||
}
|
||||
|
||||
/**
|
||||
* Report a folder as being viewed.
|
||||
*
|
||||
* @param {number} id Module ID.
|
||||
* @return {Promise<any>} Promise resolved when the WS call is successful.
|
||||
*/
|
||||
logView(id: number): Promise<any> {
|
||||
if (id) {
|
||||
const params = {
|
||||
folderid: id
|
||||
};
|
||||
|
||||
return this.sitesProvider.getCurrentSite().write('mod_folder_view_folder', params);
|
||||
}
|
||||
|
||||
return Promise.reject(null);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,90 @@
|
|||
// (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 { CoreLoggerProvider } from '@providers/logger';
|
||||
import { CoreCourseProvider } from '@core/course/providers/course';
|
||||
|
||||
/**
|
||||
* Service that provides some features for folder.
|
||||
*/
|
||||
@Injectable()
|
||||
export class AddonModFolderHelperProvider {
|
||||
|
||||
constructor(logger: CoreLoggerProvider, private courseProvider: CoreCourseProvider) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Format folder contents, creating directory structure.
|
||||
* Folders found in filepaths are added to the array. Each folder has the properties: name, fileicon,
|
||||
* type (folder), filepath and contents (array with files and subfolders).
|
||||
*
|
||||
* @param {any[]} contents Folder contents.
|
||||
* @return {any[]} Formatted contents.
|
||||
*/
|
||||
formatContents(contents: any[]): any[] {
|
||||
const files = [],
|
||||
folders = [],
|
||||
folderIcon = this.courseProvider.getModuleIconSrc('folder');
|
||||
|
||||
contents.forEach((entry) => {
|
||||
if (entry.filepath !== '/') {
|
||||
// It's a file in a subfolder. Lets treat the path to add the subfolders to the array.
|
||||
let directories,
|
||||
currentList = folders, // Start at root level.
|
||||
path = entry.filepath,
|
||||
subpath = '';
|
||||
|
||||
// Remove first and last slash if needed.
|
||||
if (path.substr(0, 1) === '/') {
|
||||
path = path.substr(1);
|
||||
}
|
||||
if (path.substr(path.length - 1) === '/') {
|
||||
path = path.slice(0, -1);
|
||||
}
|
||||
|
||||
directories = path.split('/');
|
||||
|
||||
directories.forEach((directory) => {
|
||||
subpath = subpath + '/' + directory;
|
||||
// Search if the directory is already stored in folders array.
|
||||
const foundList = currentList.find((list) => {
|
||||
return list.name === directory;
|
||||
});
|
||||
|
||||
if (foundList) {
|
||||
currentList = foundList.contents;
|
||||
} else {
|
||||
// Directory not found. Add it to the array.
|
||||
const newFolder = {
|
||||
name: directory,
|
||||
fileicon: folderIcon,
|
||||
contents: [],
|
||||
filepath: subpath,
|
||||
type: 'folder'
|
||||
};
|
||||
currentList.push(newFolder);
|
||||
currentList = newFolder.contents;
|
||||
}
|
||||
});
|
||||
|
||||
currentList.push(entry);
|
||||
} else {
|
||||
files.push(entry);
|
||||
}
|
||||
});
|
||||
|
||||
return folders.concat(files);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,70 @@
|
|||
// (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';
|
||||
import { AddonModFolderIndexComponent } from '../components/index/index';
|
||||
import { CoreCourseModuleHandler, CoreCourseModuleHandlerData } from '@core/course/providers/module-delegate';
|
||||
import { CoreCourseProvider } from '@core/course/providers/course';
|
||||
|
||||
/**
|
||||
* Handler to support folder modules.
|
||||
*/
|
||||
@Injectable()
|
||||
export class AddonModFolderModuleHandler implements CoreCourseModuleHandler {
|
||||
name = 'folder';
|
||||
|
||||
constructor(private courseProvider: CoreCourseProvider) { }
|
||||
|
||||
/**
|
||||
* 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 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 {
|
||||
return {
|
||||
icon: this.courseProvider.getModuleIconSrc('folder'),
|
||||
title: module.name,
|
||||
class: 'addon-mod_folder-handler',
|
||||
showDownloadButton: true,
|
||||
action(event: Event, navCtrl: NavController, module: any, courseId: number, options: NavOptions): void {
|
||||
navCtrl.push('AddonModFolderIndexPage', {module: module, courseId: courseId}, options);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 AddonModFolderIndexComponent;
|
||||
}
|
||||
}
|
|
@ -76,6 +76,7 @@ import { AddonFilesModule } from '@addon/files/files.module';
|
|||
import { AddonModBookModule } from '@addon/mod/book/book.module';
|
||||
import { AddonModLabelModule } from '@addon/mod/label/label.module';
|
||||
import { AddonModResourceModule } from '@addon/mod/resource/resource.module';
|
||||
import { AddonModFolderModule } from '@addon/mod/folder/folder.module';
|
||||
import { AddonMessagesModule } from '@addon/messages/messages.module';
|
||||
import { AddonPushNotificationsModule } from '@addon/pushnotifications/pushnotifications.module';
|
||||
|
||||
|
@ -155,6 +156,7 @@ export const CORE_PROVIDERS: any[] = [
|
|||
AddonModBookModule,
|
||||
AddonModLabelModule,
|
||||
AddonModResourceModule,
|
||||
AddonModFolderModule,
|
||||
AddonMessagesModule,
|
||||
AddonPushNotificationsModule
|
||||
],
|
||||
|
|
Loading…
Reference in New Issue