From 3131f503069820fe68108de366031e09450293d6 Mon Sep 17 00:00:00 2001 From: Albert Gasset Date: Fri, 20 Apr 2018 13:00:25 +0200 Subject: [PATCH] MOBILE-2337 choice: PR fixes --- .../mod/choice/components/index/index.html | 2 +- .../mod/choice/components/index/index.ts | 22 +++++-- src/addon/mod/choice/providers/choice.ts | 31 ++++------ .../mod/choice/providers/module-handler.ts | 1 + .../mod/choice/providers/prefetch-handler.ts | 58 +++++++++++++------ src/addon/mod/choice/providers/sync.ts | 2 +- 6 files changed, 71 insertions(+), 45 deletions(-) diff --git a/src/addon/mod/choice/components/index/index.html b/src/addon/mod/choice/components/index/index.html index d0d85de75..870b14089 100644 --- a/src/addon/mod/choice/components/index/index.html +++ b/src/addon/mod/choice/components/index/index.html @@ -23,7 +23,7 @@ -

{{ 'addon.mod_choice.yourselection' | translate }}

+

{{ 'addon.mod_choice.yourselection' | translate }}

{{ 'addon.mod_choice.expired' | translate:{$a: choice.closeTimeReadable} }}

diff --git a/src/addon/mod/choice/components/index/index.ts b/src/addon/mod/choice/components/index/index.ts index 28c178bea..3c52e925f 100644 --- a/src/addon/mod/choice/components/index/index.ts +++ b/src/addon/mod/choice/components/index/index.ts @@ -62,8 +62,13 @@ export class AddonModChoiceIndexComponent extends CoreCourseModuleMainActivityCo this.userId = this.sitesProvider.getCurrentSiteUserId(); this.loadContent(false, true).then(() => { + if (!this.choice) { + return; + } this.choiceProvider.logView(this.choice.id).then(() => { this.courseProvider.checkModuleCompletion(this.courseId, this.module.completionstatus); + }).catch((error) => { + // Ignore errors. }); }); } @@ -74,11 +79,16 @@ export class AddonModChoiceIndexComponent extends CoreCourseModuleMainActivityCo * @return {Promise} Resolved when done. */ protected invalidateContent(): Promise { - return Promise.all([ - this.choiceProvider.invalidateChoiceData(this.courseId), - this.choiceProvider.invalidateOptions(this.choice.id), - this.choiceProvider.invalidateResults(this.choice.id), - ]); + const promises = []; + + promises.push(this.choiceProvider.invalidateChoiceData(this.courseId)); + + if (this.choice) { + promises.push(this.choiceProvider.invalidateOptions(this.choice.id)); + promises.push(this.choiceProvider.invalidateResults(this.choice.id)); + } + + return Promise.all(promises); } /** @@ -242,7 +252,7 @@ export class AddonModChoiceIndexComponent extends CoreCourseModuleMainActivityCo // Cannot see results yet. this.canSeeResults = false; - return Promise.resolve(null); + return Promise.resolve(); } return this.choiceProvider.getResults(this.choice.id).then((results) => { diff --git a/src/addon/mod/choice/providers/choice.ts b/src/addon/mod/choice/providers/choice.ts index 8ce53366b..a26b6be29 100644 --- a/src/addon/mod/choice/providers/choice.ts +++ b/src/addon/mod/choice/providers/choice.ts @@ -87,13 +87,13 @@ export class AddonModChoiceProvider { return this.deleteResponsesOnline(choiceId, responses, siteId).then(() => { return true; }).catch((error) => { - if (error && error.wserror) { - // The WebService has thrown an error, this means that responses cannot be deleted. - return Promise.reject(error.error); - } else { - // Couldn't connect to server, store in offline. - return storeOffline(); + if (this.utils.isWebServiceError(error)) { + // The WebService has thrown an error, this means that responses cannot be submitted. + return Promise.reject(error); } + + // Couldn't connect to server, store in offline. + return storeOffline(); }); }); } @@ -185,12 +185,7 @@ export class AddonModChoiceProvider { return site.read('mod_choice_get_choices_by_courses', params, preSets).then((response) => { if (response && response.choices) { - let currentChoice; - response.choices.forEach((choice) => { - if (!currentChoice && choice[key] == value) { - currentChoice = choice; - } - }); + const currentChoice = response.choices.find((choice) => choice[key] == value); if (currentChoice) { return currentChoice; } @@ -351,15 +346,11 @@ export class AddonModChoiceProvider { * @return {Promise} Promise resolved when the WS call is successful. */ logView(id: string): Promise { - if (id) { - const params = { - choiceid: id - }; + const params = { + choiceid: id + }; - return this.sitesProvider.getCurrentSite().write('mod_choice_view_choice', params); - } - - return Promise.reject(null); + return this.sitesProvider.getCurrentSite().write('mod_choice_view_choice', params); } /** diff --git a/src/addon/mod/choice/providers/module-handler.ts b/src/addon/mod/choice/providers/module-handler.ts index 2e2032790..402f5555f 100644 --- a/src/addon/mod/choice/providers/module-handler.ts +++ b/src/addon/mod/choice/providers/module-handler.ts @@ -50,6 +50,7 @@ export class AddonModChoiceModuleHandler implements CoreCourseModuleHandler { icon: this.courseProvider.getModuleIconSrc('choice'), title: module.name, class: 'addon-mod_choice-handler', + showDownloadButton: true, action(event: Event, navCtrl: NavController, module: any, courseId: number, options: NavOptions): void { navCtrl.push('AddonModChoiceIndexPage', {module: module, courseId: courseId}, options); } diff --git a/src/addon/mod/choice/providers/prefetch-handler.ts b/src/addon/mod/choice/providers/prefetch-handler.ts index d30849fa4..8aee0ec61 100644 --- a/src/addon/mod/choice/providers/prefetch-handler.ts +++ b/src/addon/mod/choice/providers/prefetch-handler.ts @@ -16,6 +16,7 @@ import { Injectable, Injector } from '@angular/core'; import { CoreCourseModulePrefetchHandlerBase } from '@core/course/classes/module-prefetch-handler'; import { CoreUserProvider } from '@core/user/providers/user'; import { AddonModChoiceProvider } from './choice'; +import { AddonModChoiceSyncProvider } from './sync'; /** * Handler to prefetch choices. @@ -27,27 +28,48 @@ export class AddonModChoicePrefetchHandler extends CoreCourseModulePrefetchHandl component = AddonModChoiceProvider.COMPONENT; updatesNames = /^configuration$|^.*files$|^answers$/; - constructor(injector: Injector, protected choiceProvider: AddonModChoiceProvider, private userProvider: CoreUserProvider) { + constructor(protected injector: Injector, protected choiceProvider: AddonModChoiceProvider, + protected syncProvider: AddonModChoiceSyncProvider, protected userProvider: CoreUserProvider) { super(injector); } /** - * Download or prefetch the content. + * Download the module. * - * @param {any} module The module object returned by WS. - * @param {number} courseId Course ID. - * @param {boolean} [prefetch] True to prefetch, false to download right away. - * @param {string} [dirPath] Path of the directory where to store all the content files. This is to keep the files - * relative paths and make the package work in an iframe. Undefined to download the files - * in the filepool root choice. - * @return {Promise} Promise resolved when all content is downloaded. Data returned is not reliable. + * @param {any} module The module object returned by WS. + * @param {number} courseId Course ID. + * @param {string} [dirPath] Path of the directory where to store all the content files. @see downloadOrPrefetch. + * @return {Promise} Promise resolved when all content is downloaded. */ - downloadOrPrefetch(module: any, courseId: number, prefetch?: boolean, dirPath?: string): Promise { - const siteId = this.sitesProvider.getCurrentSiteId(); - const promises = []; + download(module: any, courseId: number, dirPath?: string): Promise { + // Same implementation for download or prefetch. + return this.prefetch(module, courseId, false, dirPath); + } - promises.push(super.downloadOrPrefetch(module, courseId, prefetch)); - promises.push(this.choiceProvider.getChoice(courseId, module.id, siteId).then((choice) => { + /** + * Prefetch a module. + * + * @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. + * @param {string} [dirPath] Path of the directory where to store all the content files. @see downloadOrPrefetch. + * @return {Promise} Promise resolved when done. + */ + prefetch(module: any, courseId?: number, single?: boolean, dirPath?: string): Promise { + return this.prefetchPackage(module, courseId, single, this.prefetchChoice.bind(this)); + } + + /** + * Prefetch a choice. + * + * @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. + * @param {String} siteId Site ID. + * @return {Promise} Promise resolved when done. + */ + protected prefetchChoice(module: any, courseId: number, single: boolean, siteId: string): Promise { + return this.choiceProvider.getChoice(courseId, module.id, siteId).then((choice) => { const promises = []; // Get the options and results. @@ -71,10 +93,12 @@ export class AddonModChoicePrefetchHandler extends CoreCourseModulePrefetchHandl return Promise.all(subPromises); })); - return Promise.all(promises); - })); + // Get the intro files. + const introFiles = this.getIntroFilesFromInstance(module, choice); + promises.push(this.filepoolProvider.addFilesToQueue(siteId, introFiles, AddonModChoiceProvider.COMPONENT, module.id)); - return Promise.all(promises); + return Promise.all(promises); + }); } /** diff --git a/src/addon/mod/choice/providers/sync.ts b/src/addon/mod/choice/providers/sync.ts index 3b736ccef..fb86705b3 100644 --- a/src/addon/mod/choice/providers/sync.ts +++ b/src/addon/mod/choice/providers/sync.ts @@ -164,7 +164,7 @@ export class AddonModChoiceSyncProvider extends CoreSyncBaseProvider { } // Couldn't connect to server, reject. - return Promise.reject(error && error.error); + return Promise.reject(error); }); }).then(() => { if (courseId) {