Skip to content

Commit c1411c3

Browse files
committed
MOBILE-3523 essay: Improve essay files prefetch
1 parent 6631488 commit c1411c3

File tree

4 files changed

+42
-12
lines changed

4 files changed

+42
-12
lines changed

src/addon/qtype/ddmarker/providers/handler.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { CoreQuestionProvider } from '@core/question/providers/question';
1818
import { CoreQuestionHandler } from '@core/question/providers/delegate';
1919
import { CoreQuestionHelperProvider } from '@core/question/providers/helper';
2020
import { AddonQtypeDdMarkerComponent } from '../component/ddmarker';
21+
import { CoreWSExternalFile } from '@providers/ws';
2122

2223
/**
2324
* Handler to support drag-and-drop markers question type.
@@ -119,9 +120,9 @@ export class AddonQtypeDdMarkerHandler implements CoreQuestionHandler {
119120
*
120121
* @param question Question.
121122
* @param usageId Usage ID.
122-
* @return List of URLs.
123+
* @return List of files or URLs.
123124
*/
124-
getAdditionalDownloadableFiles(question: any, usageId: number): string[] {
125+
getAdditionalDownloadableFiles(question: any, usageId: number): (string | CoreWSExternalFile)[] {
125126
this.questionHelper.extractQuestionScripts(question, usageId);
126127

127128
if (question.amdArgs && typeof question.amdArgs[1] == 'string') {

src/addon/qtype/essay/providers/handler.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import { CoreQuestionHandler } from '@core/question/providers/delegate';
2424
import { CoreQuestionHelperProvider } from '@core/question/providers/helper';
2525
import { CoreQuestion } from '@core/question/providers/question';
2626
import { AddonQtypeEssayComponent } from '../component/essay';
27+
import { CoreWSExternalFile } from '@providers/ws';
2728

2829
/**
2930
* Handler to support essay question type.
@@ -67,6 +68,23 @@ export class AddonQtypeEssayHandler implements CoreQuestionHandler {
6768
return this.questionHelper.deleteStoredQuestionFiles(question, component, componentId, siteId);
6869
}
6970

71+
/**
72+
* Get the list of files that needs to be downloaded in addition to the files embedded in the HTML.
73+
*
74+
* @param question Question.
75+
* @param usageId Usage ID.
76+
* @return List of files or URLs.
77+
*/
78+
getAdditionalDownloadableFiles(question: any, usageId: number): (string | CoreWSExternalFile)[] {
79+
if (!question.responsefileareas) {
80+
return [];
81+
}
82+
83+
return question.responsefileareas.reduce((urlsList, area) => {
84+
return urlsList.concat(area.files || []);
85+
}, []);
86+
}
87+
7088
/**
7189
* Check whether the question allows text and/or attachments.
7290
*

src/core/question/providers/delegate.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { CoreEventsProvider } from '@providers/events';
1818
import { CoreSitesProvider } from '@providers/sites';
1919
import { CoreDelegate, CoreDelegateHandler } from '@classes/delegate';
2020
import { CoreQuestionDefaultHandler } from './default-question-handler';
21+
import { CoreWSExternalFile } from '@providers/ws';
2122

2223
/**
2324
* Interface that all question type handlers must implement.
@@ -119,9 +120,9 @@ export interface CoreQuestionHandler extends CoreDelegateHandler {
119120
*
120121
* @param question Question.
121122
* @param usageId Usage ID.
122-
* @return List of URLs.
123+
* @return List of files or URLs.
123124
*/
124-
getAdditionalDownloadableFiles?(question: any, usageId: number): string[];
125+
getAdditionalDownloadableFiles?(question: any, usageId: number): (string | CoreWSExternalFile)[];
125126

126127
/**
127128
* Clear temporary data after the data has been saved.
@@ -324,9 +325,9 @@ export class CoreQuestionDelegate extends CoreDelegate {
324325
*
325326
* @param question Question.
326327
* @param usageId Usage ID.
327-
* @return List of URLs.
328+
* @return List of files or URLs.
328329
*/
329-
getAdditionalDownloadableFiles(question: any, usageId: number): string[] {
330+
getAdditionalDownloadableFiles(question: any, usageId: number): (string | CoreWSExternalFile)[] {
330331
const type = this.getTypeName(question);
331332

332333
return this.executeFunctionOnEnabled(type, 'getAdditionalDownloadableFiles', [question, usageId]) || [];

src/core/question/providers/helper.ts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -590,29 +590,39 @@ export class CoreQuestionHelperProvider {
590590
*/
591591
prefetchQuestionFiles(question: any, component?: string, componentId?: string | number, siteId?: string, usageId?: number)
592592
: Promise<any> {
593-
const urls = this.filepoolProvider.extractDownloadableFilesFromHtml(question.html);
594593

595594
if (!component) {
596595
component = CoreQuestionProvider.COMPONENT;
597596
componentId = question.number;
598597
}
599598

600-
urls.push(...this.questionDelegate.getAdditionalDownloadableFiles(question, usageId));
599+
const files = this.questionDelegate.getAdditionalDownloadableFiles(question, usageId) || [];
600+
601+
files.push(...this.filepoolProvider.extractDownloadableFilesFromHtml(question.html));
601602

602603
return this.sitesProvider.getSite(siteId).then((site) => {
603604
const promises = [];
605+
const treated = {};
606+
607+
files.forEach((file) => {
608+
const fileUrl = typeof file == 'string' ? file : file.fileurl;
609+
const timemodified = (typeof file != 'string' && file.timemodified) || 0;
610+
611+
if (treated[fileUrl]) {
612+
return;
613+
}
614+
treated[fileUrl] = true;
604615

605-
urls.forEach((url) => {
606-
if (!site.canDownloadFiles() && this.urlUtils.isPluginFileUrl(url)) {
616+
if (!site.canDownloadFiles() && this.urlUtils.isPluginFileUrl(fileUrl)) {
607617
return;
608618
}
609619

610-
if (url.indexOf('theme/image.php') > -1 && url.indexOf('flagged') > -1) {
620+
if (fileUrl.indexOf('theme/image.php') > -1 && fileUrl.indexOf('flagged') > -1) {
611621
// Ignore flag images.
612622
return;
613623
}
614624

615-
promises.push(this.filepoolProvider.addToQueueByUrl(siteId, url, component, componentId));
625+
promises.push(this.filepoolProvider.addToQueueByUrl(siteId, fileUrl, component, componentId, timemodified));
616626
});
617627

618628
return Promise.all(promises);

0 commit comments

Comments
 (0)