From 012c08ded9e6473e2f94e685c02c9a9784310275 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pau=20Ferrer=20Oca=C3=B1a?= Date: Fri, 30 Nov 2018 16:05:39 +0100 Subject: [PATCH] MOBILE-1764 prefetch: Improve prefetch size calculation --- .../mod/forum/providers/prefetch-handler.ts | 5 +- .../glossary/providers/prefetch-handler.ts | 9 ++- .../mod/quiz/providers/prefetch-handler.ts | 67 +++++++++++++++++-- 3 files changed, 72 insertions(+), 9 deletions(-) diff --git a/src/addon/mod/forum/providers/prefetch-handler.ts b/src/addon/mod/forum/providers/prefetch-handler.ts index e8b8fda15..f13b34540 100644 --- a/src/addon/mod/forum/providers/prefetch-handler.ts +++ b/src/addon/mod/forum/providers/prefetch-handler.ts @@ -80,12 +80,15 @@ export class AddonModForumPrefetchHandler extends CoreCourseActivityPrefetchHand */ protected getPostsFiles(posts: any[]): any[] { let files = []; + const getInlineFiles = this.sitesProvider.getCurrentSite().isVersionGreaterEqualThan('3.2'); posts.forEach((post) => { if (post.attachments && post.attachments.length) { files = files.concat(post.attachments); } - if (post.message) { + if (getInlineFiles && post.messageinlinefiles && post.messageinlinefiles.length) { + files = files.concat(post.messageinlinefiles); + } else if (post.message && !getInlineFiles) { files = files.concat(this.domUtils.extractDownloadableFilesFromHtmlAsFakeFileObjects(post.message)); } }); diff --git a/src/addon/mod/glossary/providers/prefetch-handler.ts b/src/addon/mod/glossary/providers/prefetch-handler.ts index 24ac9d4c9..cb4e8ac9d 100644 --- a/src/addon/mod/glossary/providers/prefetch-handler.ts +++ b/src/addon/mod/glossary/providers/prefetch-handler.ts @@ -77,10 +77,17 @@ export class AddonModGlossaryPrefetchHandler extends CoreCourseActivityPrefetchH */ protected getFilesFromGlossaryAndEntries(module: any, glossary: any, entries: any[]): any[] { let files = this.getIntroFilesFromInstance(module, glossary); + const getInlineFiles = this.sitesProvider.getCurrentSite().isVersionGreaterEqualThan('3.2'); + // Get entries files. entries.forEach((entry) => { - files = files.concat(this.domUtils.extractDownloadableFilesFromHtmlAsFakeFileObjects(entry.definition)); files = files.concat(entry.attachments); + + if (getInlineFiles && entry.definitioninlinefiles && entry.definitioninlinefiles.length) { + files = files.concat(entry.definitioninlinefiles); + } else if (entry.definition && !getInlineFiles) { + files = files.concat(this.domUtils.extractDownloadableFilesFromHtmlAsFakeFileObjects(entry.definition)); + } }); return files; diff --git a/src/addon/mod/quiz/providers/prefetch-handler.ts b/src/addon/mod/quiz/providers/prefetch-handler.ts index 69ec84506..794cd9c5b 100644 --- a/src/addon/mod/quiz/providers/prefetch-handler.ts +++ b/src/addon/mod/quiz/providers/prefetch-handler.ts @@ -51,18 +51,62 @@ export class AddonModQuizPrefetchHandler extends CoreCourseActivityPrefetchHandl } /** - * Get the download size of a module. + * Get list of files. If not defined, we'll assume they're in module.contents. * * @param {any} module Module. * @param {Number} courseId Course ID the module belongs to. * @param {boolean} [single] True if we're downloading a single module, false if we're downloading a whole section. - * @return {Promise<{size: number, total: boolean}>} Promise resolved with the size and a boolean indicating if it was able - * to calculate the total size. + * @return {Promise} Promise resolved with the list of files. */ - getDownloadSize(module: any, courseId: any, single?: boolean): Promise<{ size: number, total: boolean }> { - return Promise.resolve({ - size: -1, - total: false + getFiles(module: any, courseId: number, single?: boolean): Promise { + return this.quizProvider.getQuiz(courseId, module.id).then((quiz) => { + const files = this.getIntroFilesFromInstance(module, quiz); + + return this.quizProvider.getUserAttempts(quiz.id, 'all', true, false, true).then((attempts) => { + return this.getAttemptsFeedbackFiles(quiz, attempts).then((attemptFiles) => { + return files.concat(attemptFiles); + }); + }); + }).catch(() => { + // Quiz not found, return empty list. + return []; + }); + } + + /** + * Get the list of downloadable files on feedback attemptss. + * + * @param {any} quiz Quiz. + * @param {any[]} attempts Quiz user attempts. + * @return {Promise} List of Files. + */ + protected getAttemptsFeedbackFiles(quiz: any, attempts: any[]): Promise { + // We have quiz data, now we'll get specific data for each attempt. + const promises = [], + getInlineFiles = this.sitesProvider.getCurrentSite().isVersionGreaterEqualThan('3.2'); + let files = []; + + attempts.forEach((attempt) => { + if (this.quizProvider.isAttemptFinished(attempt.state)) { + // Attempt is finished, get feedback and review data. + + const attemptGrade = this.quizProvider.rescaleGrade(attempt.sumgrades, quiz, false); + if (typeof attemptGrade != 'undefined') { + promises.push(this.quizProvider.getFeedbackForGrade(quiz.id, Number(attemptGrade), true) + .then((feedback) => { + if (getInlineFiles && feedback.feedbackinlinefiles && feedback.feedbackinlinefiles.length) { + files = files.concat(feedback.feedbackinlinefiles); + } else if (feedback.feedbacktext && !getInlineFiles) { + files = files.concat( + this.domUtils.extractDownloadableFilesFromHtmlAsFakeFileObjects(feedback.feedbacktext)); + } + })); + } + } + }); + + return Promise.all(promises).then(() => { + return files; }); } @@ -213,6 +257,10 @@ export class AddonModQuizPrefetchHandler extends CoreCourseActivityPrefetchHandl promises.push(this.quizProvider.getQuizRequiredQtypes(quiz.id, true, siteId)); promises.push(this.quizProvider.getUserAttempts(quiz.id, 'all', true, false, true, siteId).then((atts) => { attempts = atts; + + return this.getAttemptsFeedbackFiles(quiz, attempts).then((attemptFiles) => { + return this.filepoolProvider.addFilesToQueue(siteId, attemptFiles, AddonModQuizProvider.COMPONENT, module.id); + }); })); promises.push(this.quizProvider.getAttemptAccessInformation(quiz.id, 0, false, true, siteId).then((info) => { attemptAccessInfo = info; @@ -246,6 +294,11 @@ export class AddonModQuizPrefetchHandler extends CoreCourseActivityPrefetchHandl // Re-fetch user attempts since we created a new one. promises.push(this.quizProvider.getUserAttempts(quiz.id, 'all', true, false, true, siteId).then((atts) => { attempts = atts; + + return this.getAttemptsFeedbackFiles(quiz, attempts).then((attemptFiles) => { + return this.filepoolProvider.addFilesToQueue(siteId, attemptFiles, AddonModQuizProvider.COMPONENT, + module.id); + }); })); // Update the download time to prevent detecting the new attempt as an update.