MOBILE-3523 essay: Improve essay files prefetch

main
Dani Palou 2020-11-25 09:14:32 +01:00
parent 6631488ec3
commit c1411c3ed4
4 changed files with 42 additions and 12 deletions

View File

@ -18,6 +18,7 @@ import { CoreQuestionProvider } from '@core/question/providers/question';
import { CoreQuestionHandler } from '@core/question/providers/delegate';
import { CoreQuestionHelperProvider } from '@core/question/providers/helper';
import { AddonQtypeDdMarkerComponent } from '../component/ddmarker';
import { CoreWSExternalFile } from '@providers/ws';
/**
* Handler to support drag-and-drop markers question type.
@ -119,9 +120,9 @@ export class AddonQtypeDdMarkerHandler implements CoreQuestionHandler {
*
* @param question Question.
* @param usageId Usage ID.
* @return List of URLs.
* @return List of files or URLs.
*/
getAdditionalDownloadableFiles(question: any, usageId: number): string[] {
getAdditionalDownloadableFiles(question: any, usageId: number): (string | CoreWSExternalFile)[] {
this.questionHelper.extractQuestionScripts(question, usageId);
if (question.amdArgs && typeof question.amdArgs[1] == 'string') {

View File

@ -24,6 +24,7 @@ import { CoreQuestionHandler } from '@core/question/providers/delegate';
import { CoreQuestionHelperProvider } from '@core/question/providers/helper';
import { CoreQuestion } from '@core/question/providers/question';
import { AddonQtypeEssayComponent } from '../component/essay';
import { CoreWSExternalFile } from '@providers/ws';
/**
* Handler to support essay question type.
@ -67,6 +68,23 @@ export class AddonQtypeEssayHandler implements CoreQuestionHandler {
return this.questionHelper.deleteStoredQuestionFiles(question, component, componentId, siteId);
}
/**
* Get the list of files that needs to be downloaded in addition to the files embedded in the HTML.
*
* @param question Question.
* @param usageId Usage ID.
* @return List of files or URLs.
*/
getAdditionalDownloadableFiles(question: any, usageId: number): (string | CoreWSExternalFile)[] {
if (!question.responsefileareas) {
return [];
}
return question.responsefileareas.reduce((urlsList, area) => {
return urlsList.concat(area.files || []);
}, []);
}
/**
* Check whether the question allows text and/or attachments.
*

View File

@ -18,6 +18,7 @@ import { CoreEventsProvider } from '@providers/events';
import { CoreSitesProvider } from '@providers/sites';
import { CoreDelegate, CoreDelegateHandler } from '@classes/delegate';
import { CoreQuestionDefaultHandler } from './default-question-handler';
import { CoreWSExternalFile } from '@providers/ws';
/**
* Interface that all question type handlers must implement.
@ -119,9 +120,9 @@ export interface CoreQuestionHandler extends CoreDelegateHandler {
*
* @param question Question.
* @param usageId Usage ID.
* @return List of URLs.
* @return List of files or URLs.
*/
getAdditionalDownloadableFiles?(question: any, usageId: number): string[];
getAdditionalDownloadableFiles?(question: any, usageId: number): (string | CoreWSExternalFile)[];
/**
* Clear temporary data after the data has been saved.
@ -324,9 +325,9 @@ export class CoreQuestionDelegate extends CoreDelegate {
*
* @param question Question.
* @param usageId Usage ID.
* @return List of URLs.
* @return List of files or URLs.
*/
getAdditionalDownloadableFiles(question: any, usageId: number): string[] {
getAdditionalDownloadableFiles(question: any, usageId: number): (string | CoreWSExternalFile)[] {
const type = this.getTypeName(question);
return this.executeFunctionOnEnabled(type, 'getAdditionalDownloadableFiles', [question, usageId]) || [];

View File

@ -590,29 +590,39 @@ export class CoreQuestionHelperProvider {
*/
prefetchQuestionFiles(question: any, component?: string, componentId?: string | number, siteId?: string, usageId?: number)
: Promise<any> {
const urls = this.filepoolProvider.extractDownloadableFilesFromHtml(question.html);
if (!component) {
component = CoreQuestionProvider.COMPONENT;
componentId = question.number;
}
urls.push(...this.questionDelegate.getAdditionalDownloadableFiles(question, usageId));
const files = this.questionDelegate.getAdditionalDownloadableFiles(question, usageId) || [];
files.push(...this.filepoolProvider.extractDownloadableFilesFromHtml(question.html));
return this.sitesProvider.getSite(siteId).then((site) => {
const promises = [];
const treated = {};
urls.forEach((url) => {
if (!site.canDownloadFiles() && this.urlUtils.isPluginFileUrl(url)) {
files.forEach((file) => {
const fileUrl = typeof file == 'string' ? file : file.fileurl;
const timemodified = (typeof file != 'string' && file.timemodified) || 0;
if (treated[fileUrl]) {
return;
}
treated[fileUrl] = true;
if (!site.canDownloadFiles() && this.urlUtils.isPluginFileUrl(fileUrl)) {
return;
}
if (url.indexOf('theme/image.php') > -1 && url.indexOf('flagged') > -1) {
if (fileUrl.indexOf('theme/image.php') > -1 && fileUrl.indexOf('flagged') > -1) {
// Ignore flag images.
return;
}
promises.push(this.filepoolProvider.addToQueueByUrl(siteId, url, component, componentId));
promises.push(this.filepoolProvider.addToQueueByUrl(siteId, fileUrl, component, componentId, timemodified));
});
return Promise.all(promises);