MOBILE-2349 resource: Add link and plugin file handlers

main
Pau Ferrer Ocaña 2018-03-05 15:19:39 +01:00
parent e0cda27cac
commit 3b8b44c23a
12 changed files with 167 additions and 252 deletions

View File

@ -381,19 +381,11 @@ export class AddonModBookProvider {
* @return {Promise<any>} Promise resolved when the WS call is successful. * @return {Promise<any>} Promise resolved when the WS call is successful.
*/ */
logView(id: number, chapterId: string): Promise<any> { logView(id: number, chapterId: string): Promise<any> {
if (id) {
const params = { const params = {
bookid: id, bookid: id,
chapterid: chapterId chapterid: chapterId
}; };
return this.sitesProvider.getCurrentSite().write('mod_book_view_book', params).then((response) => { return this.sitesProvider.getCurrentSite().write('mod_book_view_book', params);
if (!response.status) {
return Promise.reject(null);
}
});
}
return Promise.reject(null);
} }
} }

View File

@ -17,7 +17,7 @@ import { CommonModule } from '@angular/common';
import { IonicModule } from 'ionic-angular'; import { IonicModule } from 'ionic-angular';
import { TranslateModule } from '@ngx-translate/core'; import { TranslateModule } from '@ngx-translate/core';
import { CoreComponentsModule } from '@components/components.module'; import { CoreComponentsModule } from '@components/components.module';
import { CoreDirectivesModule } from '@directives'; import { CoreDirectivesModule } from '@directives/directives.module';
import { CoreCourseComponentsModule } from '@core/course/components/components.module'; import { CoreCourseComponentsModule } from '@core/course/components/components.module';
import { AddonModResourceIndexComponent } from './index/index'; import { AddonModResourceIndexComponent } from './index/index';

View File

@ -12,8 +12,7 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
import { Component, OnInit, OnDestroy, Input, Output, EventEmitter, Optional } from '@angular/core'; import { Component, OnInit, OnDestroy, Input, Output, EventEmitter } from '@angular/core';
import { NavParams, NavController, Content } from 'ionic-angular';
import { TranslateService } from '@ngx-translate/core'; import { TranslateService } from '@ngx-translate/core';
import { CoreAppProvider } from '@providers/app'; import { CoreAppProvider } from '@providers/app';
import { CoreDomUtilsProvider } from '@providers/utils/dom'; import { CoreDomUtilsProvider } from '@providers/utils/dom';
@ -60,8 +59,7 @@ export class AddonModResourceIndexComponent implements OnInit, OnDestroy, CoreCo
constructor(private resourceProvider: AddonModResourceProvider, private courseProvider: CoreCourseProvider, constructor(private resourceProvider: AddonModResourceProvider, private courseProvider: CoreCourseProvider,
private domUtils: CoreDomUtilsProvider, private appProvider: CoreAppProvider, private textUtils: CoreTextUtilsProvider, private domUtils: CoreDomUtilsProvider, private appProvider: CoreAppProvider, private textUtils: CoreTextUtilsProvider,
private courseHelper: CoreCourseHelperProvider, private translate: TranslateService, private courseHelper: CoreCourseHelperProvider, private translate: TranslateService,
@Optional() private content: Content, private prefetchHandler: AddonModResourcePrefetchHandler, private prefetchHandler: AddonModResourcePrefetchHandler, private resourceHelper: AddonModResourceHelperProvider) {
private resourceHelper: AddonModResourceHelperProvider) {
this.resourceRetrieved = new EventEmitter(); this.resourceRetrieved = new EventEmitter();
} }
@ -166,7 +164,6 @@ export class AddonModResourceIndexComponent implements OnInit, OnDestroy, CoreCo
downloadFailed = true; downloadFailed = true;
}).then(() => { }).then(() => {
return this.resourceHelper.getIframeSrc(this.module).then((src) => { return this.resourceHelper.getIframeSrc(this.module).then((src) => {
console.error(src);
this.mode = 'iframe'; this.mode = 'iframe';
if (this.src && src.toString() == this.src.toString()) { if (this.src && src.toString() == this.src.toString()) {

View File

@ -15,7 +15,7 @@
import { NgModule } from '@angular/core'; import { NgModule } from '@angular/core';
import { IonicPageModule } from 'ionic-angular'; import { IonicPageModule } from 'ionic-angular';
import { TranslateModule } from '@ngx-translate/core'; import { TranslateModule } from '@ngx-translate/core';
import { CoreDirectivesModule } from '@directives'; import { CoreDirectivesModule } from '@directives/directives.module';
import { AddonModResourceComponentsModule } from '../../components/components.module'; import { AddonModResourceComponentsModule } from '../../components/components.module';
import { AddonModResourceIndexPage } from './index'; import { AddonModResourceIndexPage } from './index';

View File

@ -15,6 +15,7 @@
import { Injectable } from '@angular/core'; import { Injectable } from '@angular/core';
import { CoreDomUtilsProvider } from '@providers/utils/dom'; import { CoreDomUtilsProvider } from '@providers/utils/dom';
import { CoreCourseProvider } from '@core/course/providers/course'; import { CoreCourseProvider } from '@core/course/providers/course';
import { CoreCourseHelperProvider } from '@core/course/providers/helper';
import { AddonModResourceProvider } from './resource'; import { AddonModResourceProvider } from './resource';
import { TranslateService } from '@ngx-translate/core'; import { TranslateService } from '@ngx-translate/core';
import { CoreSitesProvider } from '@providers/sites'; import { CoreSitesProvider } from '@providers/sites';
@ -37,19 +38,9 @@ export class AddonModResourceHelperProvider {
protected DISPLAY_AUTO = 0; protected DISPLAY_AUTO = 0;
// Display using object tag. // Display using object tag.
protected DISPLAY_EMBED = 1; protected DISPLAY_EMBED = 1;
// Display inside frame.
protected DISPLAY_FRAME = 2;
// Display normal link in new window.
protected DISPLAY_NEW = 3;
// Force download of file instead of display.
protected DISPLAY_DOWNLOAD = 4;
// Open directly.
protected DISPLAY_OPEN = 5;
// Open in "emulated" pop-up without navigation.
protected DISPLAY_POPUP = 6;
constructor(private courseProvider: CoreCourseProvider, private domUtils: CoreDomUtilsProvider, constructor(private courseProvider: CoreCourseProvider, private domUtils: CoreDomUtilsProvider,
private resourceProvider: AddonModResourceProvider, private resourceProvider: AddonModResourceProvider, private courseHelper: CoreCourseHelperProvider,
private textUtils: CoreTextUtilsProvider, private mimetypeUtils: CoreMimetypeUtilsProvider, private textUtils: CoreTextUtilsProvider, private mimetypeUtils: CoreMimetypeUtilsProvider,
private fileProvider: CoreFileProvider, private appProvider: CoreAppProvider, private fileProvider: CoreFileProvider, private appProvider: CoreAppProvider,
private filepoolProvider: CoreFilepoolProvider, private utils: CoreUtilsProvider, private filepoolProvider: CoreFilepoolProvider, private utils: CoreUtilsProvider,
@ -61,17 +52,12 @@ export class AddonModResourceHelperProvider {
* *
* @param {any} module The module object. * @param {any} module The module object.
* @return {Promise<any>} Promise resolved with the iframe src. * @return {Promise<any>} Promise resolved with the iframe src.
* @since 3.3
*/ */
getEmbeddedHtml(module: any): Promise<any> { getEmbeddedHtml(module: any): Promise<any> {
if (!module.contents || !module.contents.length) { return this.courseHelper.downloadModuleWithMainFileIfNeeded(module, module.course, AddonModResourceProvider.COMPONENT,
return Promise.reject(null); module.id, module.contents).then((result) => {
} const file = module.contents[0],
ext = this.mimetypeUtils.getFileExtension(file.filename),
const file = module.contents[0];
return this.treatResourceMainFile(file, module.id).then((result) => {
const ext = this.mimetypeUtils.getFileExtension(file.filename),
type = this.mimetypeUtils.getExtensionType(ext), type = this.mimetypeUtils.getExtensionType(ext),
mimeType = this.mimetypeUtils.getMimeType(ext); mimeType = this.mimetypeUtils.getMimeType(ext);
@ -85,7 +71,7 @@ export class AddonModResourceHelperProvider {
'</' + type + '>'; '</' + type + '>';
} }
// Shouldn't reach here, the user should have called $mmFS#canBeEmbedded. // Shouldn't reach here, the user should have called CoreMimetypeUtilsProvider#canBeEmbedded.
return ''; return '';
}); });
} }
@ -94,9 +80,9 @@ export class AddonModResourceHelperProvider {
* Download all the files needed and returns the src of the iframe. * Download all the files needed and returns the src of the iframe.
* *
* @param {any} module The module object. * @param {any} module The module object.
* @return {Promise<any>} Promise resolved with the iframe src. * @return {Promise<string>} Promise resolved with the iframe src.
*/ */
getIframeSrc(module: any): Promise<any> { getIframeSrc(module: any): Promise<string> {
if (!module.contents.length) { if (!module.contents.length) {
return Promise.reject(null); return Promise.reject(null);
} }
@ -127,8 +113,7 @@ export class AddonModResourceHelperProvider {
* *
* @param {any} module The module object. * @param {any} module The module object.
* @param {number} [display] The display mode (if available). * @param {number} [display] The display mode (if available).
* @return {boolean} Whether the resource should be displayed in an iframe. * @return {boolean} Whether the resource should be displayed embeded.
* @since 3.3
*/ */
isDisplayedEmbedded(module: any, display: number): boolean { isDisplayedEmbedded(module: any, display: number): boolean {
if (!module.contents.length || !this.fileProvider.isAvailable()) { if (!module.contents.length || !this.fileProvider.isAvailable()) {
@ -167,7 +152,9 @@ export class AddonModResourceHelperProvider {
openModuleFile(module: any, courseId: number): Promise<any> { openModuleFile(module: any, courseId: number): Promise<any> {
const modal = this.domUtils.showModalLoading(); const modal = this.domUtils.showModalLoading();
return this.openFile(module.contents, module.id).then(() => { // Download and open the file from the resource contents.
return this.courseHelper.downloadModuleAndOpenFile(module, courseId, AddonModResourceProvider.COMPONENT, module.id,
module.contents).then(() => {
this.resourceProvider.logView(module.instance).then(() => { this.resourceProvider.logView(module.instance).then(() => {
this.courseProvider.checkModuleCompletion(courseId, module.completionstatus); this.courseProvider.checkModuleCompletion(courseId, module.completionstatus);
}); });
@ -177,180 +164,4 @@ export class AddonModResourceHelperProvider {
modal.dismiss(); modal.dismiss();
}); });
} }
/**
* Download and open the file from the resource.
*
* @param {any} contents Array of content objects.
* @param {number} moduleId The module ID.
* @return {Promise<any>}
*/
protected openFile(contents: any, moduleId: number): Promise<any> {
if (!contents || !contents.length) {
return Promise.reject(null);
}
const siteId = this.sitesProvider.getCurrentSiteId(),
file = contents[0],
files = [file],
component = AddonModResourceProvider.COMPONENT;
if (this.shouldOpenInBrowser(contents[0])) {
if (this.appProvider.isOnline()) {
// Open in browser.
let fixedUrl = this.sitesProvider.getCurrentSite().fixPluginfileURL(file.fileurl).replace('&offline=1', '');
fixedUrl = fixedUrl.replace(/forcedownload=\d+&/, ''); // Remove forcedownload when followed by another param.
fixedUrl = fixedUrl.replace(/[\?|\&]forcedownload=\d+/, ''); // Remove forcedownload when not followed by any param.
this.utils.openInBrowser(fixedUrl);
if (this.fileProvider.isAvailable()) {
// Download the file if needed (file outdated or not downloaded).
// Download will be in background, don't return the promise.
this.filepoolProvider.downloadPackage(siteId, files, component, moduleId);
}
return Promise.resolve();
}
// Not online, get the offline file. It will fail if not found.
return this.filepoolProvider.getInternalUrlByUrl(siteId, file.fileurl).then((path) => {
return this.utils.openFile(path);
}).catch(() => {
return Promise.reject(this.translate.instant('core.networkerrormsg'));
});
}
return this.treatResourceMainFile(file, moduleId).then((result) => {
if (result.path.indexOf('http') === 0) {
return this.utils.openOnlineFile(result.path).catch((error) => {
// Error opening the file, some apps don't allow opening online files.
if (!this.fileProvider.isAvailable()) {
return Promise.reject(error);
}
let subPromise;
if (result.status === CoreConstants.DOWNLOADING) {
subPromise = Promise.reject(this.translate.instant('core.erroropenfiledownloading'));
} else if (result.status === CoreConstants.NOT_DOWNLOADED) {
subPromise = this.filepoolProvider.downloadPackage(siteId, files, AddonModResourceProvider.COMPONENT,
moduleId).then(() => {
return this.filepoolProvider.getInternalUrlByUrl(siteId, file.fileurl);
});
} else {
// File is outdated or stale and can't be opened in online, return the local URL.
subPromise = this.filepoolProvider.getInternalUrlByUrl(siteId, file.fileurl);
}
return subPromise.then((path) => {
return this.utils.openFile(path);
});
});
}
return this.utils.openFile(result.path);
});
}
/**
* Treat the main file of a resource, downloading it if needed and returning the URL to use and the status of the resource.
*
* @param {any} file Resource's main file.
* @param {number} moduleId The module ID.
* @return {Promise<any>} Promise resolved with an object containing:
* * path: The URL to use; can be an online URL or an offline path.
* * status: The status of the resource.
*/
protected treatResourceMainFile(file: any, moduleId: number): Promise<any> {
const files = [file],
url = file.fileurl,
fixedUrl = this.sitesProvider.getCurrentSite().fixPluginfileURL(url),
result = {
status: '',
path: fixedUrl
};
if (!this.fileProvider.isAvailable()) {
// We use the live URL.
return Promise.resolve(result);
}
const siteId = this.sitesProvider.getCurrentSiteId(),
component = AddonModResourceProvider.COMPONENT;
// The file system is available.
return this.filepoolProvider.getPackageStatus(siteId, component, moduleId).then((status) => {
result.status = status;
const isWifi = !this.appProvider.isNetworkAccessLimited(),
isOnline = this.appProvider.isOnline();
if (status === CoreConstants.DOWNLOADED) {
// Get the local file URL.
return this.filepoolProvider.getInternalUrlByUrl(siteId, url);
}
if (status === CoreConstants.DOWNLOADING && !this.appProvider.isDesktop()) {
// Return the online URL.
return fixedUrl;
}
if (!isOnline && status === CoreConstants.NOT_DOWNLOADED) {
// Not downloaded and we're offline, reject.
return Promise.reject(null);
}
return this.filepoolProvider.shouldDownloadBeforeOpen(fixedUrl, file.filesize).then(() => {
// Download and then return the local URL.
return this.filepoolProvider.downloadPackage(siteId, files, component, moduleId).then(() => {
return this.filepoolProvider.getInternalUrlByUrl(siteId, url);
});
}).catch(() => {
// Start the download if in wifi, but return the URL right away so the file is opened.
if (isWifi && isOnline) {
this.filepoolProvider.downloadPackage(siteId, files, component, moduleId);
}
if (status === CoreConstants.NOT_DOWNLOADED || isOnline) {
// Not downloaded or outdated and online, return the online URL.
return fixedUrl;
}
const timeMod = this.filepoolProvider.getTimemodifiedFromFileList(files);
// Outdated but offline, so we return the local URL.
return this.filepoolProvider.getUrlByUrl(siteId, url, component, moduleId, timeMod, false, false, file);
});
}).then((path) => {
result.path = path;
return result;
});
}
/**
* Whether the resource has to be opened in browser.
*
* @param {any} file Module's main file.
* @return {boolean} Whether the resource should be opened in browser.
* @since 3.3
*/
shouldOpenInBrowser(file: any): boolean {
if (!file || !file.isexternalfile || !file.mimetype) {
return false;
}
const mimetype = file.mimetype;
if (mimetype.indexOf('application/vnd.google-apps.') != -1) {
// Google Docs file, always open in browser.
return true;
}
if (file.repositorytype == 'onedrive') {
// In OneDrive, open in browser the office docs
return mimetype.indexOf('application/vnd.openxmlformats-officedocument') != -1 ||
mimetype == 'text/plain' || mimetype == 'document/unknown';
}
return false;
}
} }

View File

@ -0,0 +1,29 @@
// (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 { CoreContentLinksModuleIndexHandler } from '@core/contentlinks/classes/module-index-handler';
import { CoreCourseHelperProvider } from '@core/course/providers/helper';
/**
* Handler to treat links to resource.
*/
@Injectable()
export class AddonModResourceLinkHandler extends CoreContentLinksModuleIndexHandler {
name = 'AddonModResourceLinkHandler';
constructor(courseHelper: CoreCourseHelperProvider) {
super(courseHelper, AddonModResourceLinkHandler.name, 'resource');
}
}

View File

@ -15,6 +15,7 @@
import { Injectable } from '@angular/core'; import { Injectable } from '@angular/core';
import { NavController, NavOptions } from 'ionic-angular'; import { NavController, NavOptions } from 'ionic-angular';
import { AddonModResourceProvider } from './resource'; import { AddonModResourceProvider } from './resource';
import { AddonModResourceHelperProvider } from './helper';
import { AddonModResourceIndexComponent } from '../components/index/index'; import { AddonModResourceIndexComponent } from '../components/index/index';
import { CoreCourseModuleHandler, CoreCourseModuleHandlerData } from '@core/course/providers/module-delegate'; import { CoreCourseModuleHandler, CoreCourseModuleHandlerData } from '@core/course/providers/module-delegate';
import { CoreCourseProvider } from '@core/course/providers/course'; import { CoreCourseProvider } from '@core/course/providers/course';
@ -28,7 +29,7 @@ export class AddonModResourceModuleHandler implements CoreCourseModuleHandler {
name = 'resource'; name = 'resource';
constructor(protected resourceProvider: AddonModResourceProvider, private courseProvider: CoreCourseProvider, constructor(protected resourceProvider: AddonModResourceProvider, private courseProvider: CoreCourseProvider,
protected mimetypeUtils: CoreMimetypeUtilsProvider) { } protected mimetypeUtils: CoreMimetypeUtilsProvider, private resourceHelper: AddonModResourceHelperProvider) { }
/** /**
* Check if the handler is enabled on a site level. * Check if the handler is enabled on a site level.
@ -48,17 +49,50 @@ export class AddonModResourceModuleHandler implements CoreCourseModuleHandler {
* @return {CoreCourseModuleHandlerData} Data to render the module. * @return {CoreCourseModuleHandlerData} Data to render the module.
*/ */
getData(module: any, courseId: number, sectionId: number): CoreCourseModuleHandlerData { getData(module: any, courseId: number, sectionId: number): CoreCourseModuleHandlerData {
this.getIcon(module, courseId); const handlerData = {
return {
icon: this.courseProvider.getModuleIconSrc('resource'), icon: this.courseProvider.getModuleIconSrc('resource'),
title: module.name, title: module.name,
class: 'addon-mod_resource-handler', class: 'addon-mod_resource-handler',
showDownloadButton: true, showDownloadButton: true,
action(event: Event, navCtrl: NavController, module: any, courseId: number, options: NavOptions): void { action(event: Event, navCtrl: NavController, module: any, courseId: number, options: NavOptions): void {
navCtrl.push('AddonModResourceIndexPage', {module: module, courseId: courseId}, options); navCtrl.push('AddonModResourceIndexPage', {module: module, courseId: courseId}, options);
},
buttons: [ {
hidden: !this.resourceHelper.isDisplayedInIframe(module),
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);
} }
});
}
} ]
}; };
this.getIcon(module, courseId).then((icon) => {
handlerData.icon = icon;
});
this.hideOpenButton(module, courseId).then((hideOpenButton) => {
handlerData.buttons[0].hidden = hideOpenButton;
});
return handlerData;
}
/**
* Returns if contents are loaded to show open button.
*
* @param {any} module The module object.
* @param {number} courseId The course ID.
* @return {Promise<boolean>} Resolved when done.
*/
protected hideOpenButton(module: any, courseId: number): Promise<boolean> {
return this.courseProvider.loadModuleContents(module, courseId).then(() => {
return this.resourceHelper.isDisplayedInIframe(module);
});
} }
/** /**
@ -66,10 +100,10 @@ export class AddonModResourceModuleHandler implements CoreCourseModuleHandler {
* *
* @param {any} module The module object. * @param {any} module The module object.
* @param {number} courseId The course ID. * @param {number} courseId The course ID.
* @return {string} Icon URL. * @return {Promise<string>} Icon URL.
*/ */
protected getIcon(module: any, courseId: number): string { protected getIcon(module: any, courseId: number): Promise<string> {
this.courseProvider.loadModuleContents(module, courseId).then(() => { return this.courseProvider.loadModuleContents(module, courseId).then(() => {
if (module.contents.length) { if (module.contents.length) {
const filename = module.contents[0].filename, const filename = module.contents[0].filename,
extension = this.mimetypeUtils.getFileExtension(filename); extension = this.mimetypeUtils.getFileExtension(filename);
@ -80,8 +114,6 @@ export class AddonModResourceModuleHandler implements CoreCourseModuleHandler {
return this.courseProvider.getModuleIconSrc('resource'); return this.courseProvider.getModuleIconSrc('resource');
}); });
return this.courseProvider.getModuleIconSrc('resource');
} }
/** /**

View File

@ -0,0 +1,49 @@
// (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 { CorePluginFileHandler } from '@providers/plugin-file-delegate';
/**
* Handler to treat links to resource.
*/
@Injectable()
export class AddonModResourcePluginFileHandler implements CorePluginFileHandler {
name = 'AddonModResourcePluginFileHandler';
/**
* Return the RegExp to match the revision on pluginfile URLs.
*
* @param {string[]} args Arguments of the pluginfile URL defining component and filearea at least.
* @return {RegExp} RegExp to match the revision on pluginfile URLs.
*/
getComponentRevisionRegExp(args: string[]): RegExp {
// Check filearea.
if (args[2] == 'content') {
// Component + Filearea + Revision
return new RegExp('/mod_resource/content/([0-9]+)/');
}
}
/**
* Should return the string to remove the revision on pluginfile url.
*
* @param {string[]} args Arguments of the pluginfile URL defining component and filearea at least.
* @return {string} String to remove the revision on pluginfile url.
*/
getComponentRevisionReplace(args: string[]): string {
// Component + Filearea + Revision
return '/mod_resource/content/0/';
}
}

View File

@ -41,7 +41,7 @@ export class AddonModResourceProvider {
* @return {string} Cache key. * @return {string} Cache key.
*/ */
protected getResourceCacheKey(courseId: number): string { protected getResourceCacheKey(courseId: number): string {
return this.ROOT_CACHE_KEY + ':resource:' + courseId; return this.ROOT_CACHE_KEY + 'resource:' + courseId;
} }
/** /**
@ -64,11 +64,8 @@ export class AddonModResourceProvider {
return site.read('mod_resource_get_resources_by_courses', params, preSets).then((response) => { return site.read('mod_resource_get_resources_by_courses', params, preSets).then((response) => {
if (response && response.resources) { if (response && response.resources) {
let currentResource; const currentResource = response.resources.find((resource) => {
response.resources.forEach((resource) => { return resource[key] == value;
if (!currentResource && resource[key] == value) {
currentResource = resource;
}
}); });
if (currentResource) { if (currentResource) {
return currentResource; return currentResource;
@ -154,14 +151,10 @@ export class AddonModResourceProvider {
* @return {Promise<any>} Promise resolved when the WS call is successful. * @return {Promise<any>} Promise resolved when the WS call is successful.
*/ */
logView(id: number): Promise<any> { logView(id: number): Promise<any> {
if (id) {
const params = { const params = {
resourceid: id resourceid: id
}; };
return this.sitesProvider.getCurrentSite().write('mod_resource_view_resource', params); return this.sitesProvider.getCurrentSite().write('mod_resource_view_resource', params);
} }
return Promise.reject(null);
}
} }

View File

@ -17,9 +17,13 @@ import { AddonModResourceComponentsModule } from './components/components.module
import { AddonModResourceModuleHandler } from './providers/module-handler'; import { AddonModResourceModuleHandler } from './providers/module-handler';
import { AddonModResourceProvider } from './providers/resource'; import { AddonModResourceProvider } from './providers/resource';
import { AddonModResourcePrefetchHandler } from './providers/prefetch-handler'; import { AddonModResourcePrefetchHandler } from './providers/prefetch-handler';
import { AddonModResourceLinkHandler } from './providers/link-handler';
import { AddonModResourcePluginFileHandler } from './providers/pluginfile-handler';
import { AddonModResourceHelperProvider } from './providers/helper'; import { AddonModResourceHelperProvider } from './providers/helper';
import { CoreContentLinksDelegate } from '@core/contentlinks/providers/delegate';
import { CoreCourseModuleDelegate } from '@core/course/providers/module-delegate'; import { CoreCourseModuleDelegate } from '@core/course/providers/module-delegate';
import { CoreCourseModulePrefetchDelegate } from '@core/course/providers/module-prefetch-delegate'; import { CoreCourseModulePrefetchDelegate } from '@core/course/providers/module-prefetch-delegate';
import { CorePluginFileDelegate } from '@providers/plugin-file-delegate';
@NgModule({ @NgModule({
declarations: [ declarations: [
@ -31,13 +35,19 @@ import { CoreCourseModulePrefetchDelegate } from '@core/course/providers/module-
AddonModResourceProvider, AddonModResourceProvider,
AddonModResourceModuleHandler, AddonModResourceModuleHandler,
AddonModResourceHelperProvider, AddonModResourceHelperProvider,
AddonModResourcePrefetchHandler AddonModResourcePrefetchHandler,
AddonModResourceLinkHandler,
AddonModResourcePluginFileHandler
] ]
}) })
export class AddonModResourceModule { export class AddonModResourceModule {
constructor(moduleDelegate: CoreCourseModuleDelegate, moduleHandler: AddonModResourceModuleHandler, constructor(moduleDelegate: CoreCourseModuleDelegate, moduleHandler: AddonModResourceModuleHandler,
prefetchDelegate: CoreCourseModulePrefetchDelegate, prefetchHandler: AddonModResourcePrefetchHandler) { prefetchDelegate: CoreCourseModulePrefetchDelegate, prefetchHandler: AddonModResourcePrefetchHandler,
contentLinksDelegate: CoreContentLinksDelegate, linkHandler: AddonModResourceLinkHandler,
pluginfileDelegate: CorePluginFileDelegate, pluginfileHandler: AddonModResourcePluginFileHandler) {
moduleDelegate.registerHandler(moduleHandler); moduleDelegate.registerHandler(moduleHandler);
prefetchDelegate.registerHandler(prefetchHandler); prefetchDelegate.registerHandler(prefetchHandler);
contentLinksDelegate.registerHandler(linkHandler);
pluginfileDelegate.registerHandler(pluginfileHandler);
} }
} }

View File

@ -102,6 +102,9 @@ export class CoreCourseModuleComponent implements OnInit, OnDestroy {
*/ */
buttonClicked(event: Event, button: CoreCourseModuleHandlerButton): void { buttonClicked(event: Event, button: CoreCourseModuleHandlerButton): void {
if (button && button.action) { if (button && button.action) {
event.preventDefault();
event.stopPropagation();
button.action(event, this.navCtrl, this.module, this.courseId); button.action(event, this.navCtrl, this.module, this.courseId);
} }
} }

View File

@ -585,7 +585,7 @@ export class CoreCourseHelperProvider {
* @param {string} [siteId] The site ID. If not defined, current site. * @param {string} [siteId] The site ID. If not defined, current site.
* @return {Promise<{fixedUrl: string, path: string, status: string}>} Promise resolved when done. * @return {Promise<{fixedUrl: string, path: string, status: string}>} Promise resolved when done.
*/ */
protected downloadModuleWithMainFileIfNeeded(module: any, courseId: number, component?: string, componentId?: string | number, downloadModuleWithMainFileIfNeeded(module: any, courseId: number, component?: string, componentId?: string | number,
files?: any[], siteId?: string): Promise<{fixedUrl: string, path: string, status: string}> { files?: any[], siteId?: string): Promise<{fixedUrl: string, path: string, status: string}> {
siteId = siteId || this.sitesProvider.getCurrentSiteId(); siteId = siteId || this.sitesProvider.getCurrentSiteId();
@ -598,7 +598,6 @@ export class CoreCourseHelperProvider {
const mainFile = files[0], const mainFile = files[0],
fileUrl = this.fileHelper.getFileUrl(mainFile), fileUrl = this.fileHelper.getFileUrl(mainFile),
timemodified = this.fileHelper.getFileTimemodified(mainFile), timemodified = this.fileHelper.getFileTimemodified(mainFile),
prefetchHandler = this.prefetchDelegate.getPrefetchHandlerFor(module),
result = { result = {
fixedUrl: undefined, fixedUrl: undefined,
path: undefined, path: undefined,