MOBILE-2337 choice: PR fixes

main
Albert Gasset 2018-04-20 13:00:25 +02:00
parent 241f1f2180
commit 3131f50306
6 changed files with 71 additions and 45 deletions

View File

@ -62,8 +62,13 @@ export class AddonModChoiceIndexComponent extends CoreCourseModuleMainActivityCo
this.userId = this.sitesProvider.getCurrentSiteUserId(); this.userId = this.sitesProvider.getCurrentSiteUserId();
this.loadContent(false, true).then(() => { this.loadContent(false, true).then(() => {
if (!this.choice) {
return;
}
this.choiceProvider.logView(this.choice.id).then(() => { this.choiceProvider.logView(this.choice.id).then(() => {
this.courseProvider.checkModuleCompletion(this.courseId, this.module.completionstatus); this.courseProvider.checkModuleCompletion(this.courseId, this.module.completionstatus);
}).catch((error) => {
// Ignore errors.
}); });
}); });
} }
@ -74,11 +79,16 @@ export class AddonModChoiceIndexComponent extends CoreCourseModuleMainActivityCo
* @return {Promise<any>} Resolved when done. * @return {Promise<any>} Resolved when done.
*/ */
protected invalidateContent(): Promise<any> { protected invalidateContent(): Promise<any> {
return Promise.all([ const promises = [];
this.choiceProvider.invalidateChoiceData(this.courseId),
this.choiceProvider.invalidateOptions(this.choice.id), promises.push(this.choiceProvider.invalidateChoiceData(this.courseId));
this.choiceProvider.invalidateResults(this.choice.id),
]); 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. // Cannot see results yet.
this.canSeeResults = false; this.canSeeResults = false;
return Promise.resolve(null); return Promise.resolve();
} }
return this.choiceProvider.getResults(this.choice.id).then((results) => { return this.choiceProvider.getResults(this.choice.id).then((results) => {

View File

@ -87,13 +87,13 @@ export class AddonModChoiceProvider {
return this.deleteResponsesOnline(choiceId, responses, siteId).then(() => { return this.deleteResponsesOnline(choiceId, responses, siteId).then(() => {
return true; return true;
}).catch((error) => { }).catch((error) => {
if (error && error.wserror) { if (this.utils.isWebServiceError(error)) {
// The WebService has thrown an error, this means that responses cannot be deleted. // The WebService has thrown an error, this means that responses cannot be submitted.
return Promise.reject(error.error); return Promise.reject(error);
} else { }
// Couldn't connect to server, store in offline. // Couldn't connect to server, store in offline.
return storeOffline(); return storeOffline();
}
}); });
}); });
} }
@ -185,12 +185,7 @@ export class AddonModChoiceProvider {
return site.read('mod_choice_get_choices_by_courses', params, preSets).then((response) => { return site.read('mod_choice_get_choices_by_courses', params, preSets).then((response) => {
if (response && response.choices) { if (response && response.choices) {
let currentChoice; const currentChoice = response.choices.find((choice) => choice[key] == value);
response.choices.forEach((choice) => {
if (!currentChoice && choice[key] == value) {
currentChoice = choice;
}
});
if (currentChoice) { if (currentChoice) {
return currentChoice; return currentChoice;
} }
@ -351,7 +346,6 @@ export class AddonModChoiceProvider {
* @return {Promise<any>} Promise resolved when the WS call is successful. * @return {Promise<any>} Promise resolved when the WS call is successful.
*/ */
logView(id: string): Promise<any> { logView(id: string): Promise<any> {
if (id) {
const params = { const params = {
choiceid: id choiceid: id
}; };
@ -359,9 +353,6 @@ export class AddonModChoiceProvider {
return this.sitesProvider.getCurrentSite().write('mod_choice_view_choice', params); return this.sitesProvider.getCurrentSite().write('mod_choice_view_choice', params);
} }
return Promise.reject(null);
}
/** /**
* Send a response to a choice to Moodle. * Send a response to a choice to Moodle.
* *

View File

@ -50,6 +50,7 @@ export class AddonModChoiceModuleHandler implements CoreCourseModuleHandler {
icon: this.courseProvider.getModuleIconSrc('choice'), icon: this.courseProvider.getModuleIconSrc('choice'),
title: module.name, title: module.name,
class: 'addon-mod_choice-handler', class: 'addon-mod_choice-handler',
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('AddonModChoiceIndexPage', {module: module, courseId: courseId}, options); navCtrl.push('AddonModChoiceIndexPage', {module: module, courseId: courseId}, options);
} }

View File

@ -16,6 +16,7 @@ import { Injectable, Injector } from '@angular/core';
import { CoreCourseModulePrefetchHandlerBase } from '@core/course/classes/module-prefetch-handler'; import { CoreCourseModulePrefetchHandlerBase } from '@core/course/classes/module-prefetch-handler';
import { CoreUserProvider } from '@core/user/providers/user'; import { CoreUserProvider } from '@core/user/providers/user';
import { AddonModChoiceProvider } from './choice'; import { AddonModChoiceProvider } from './choice';
import { AddonModChoiceSyncProvider } from './sync';
/** /**
* Handler to prefetch choices. * Handler to prefetch choices.
@ -27,27 +28,48 @@ export class AddonModChoicePrefetchHandler extends CoreCourseModulePrefetchHandl
component = AddonModChoiceProvider.COMPONENT; component = AddonModChoiceProvider.COMPONENT;
updatesNames = /^configuration$|^.*files$|^answers$/; 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); super(injector);
} }
/** /**
* Download or prefetch the content. * Download the module.
* *
* @param {any} module The module object returned by WS. * @param {any} module The module object returned by WS.
* @param {number} courseId Course ID. * @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. @see downloadOrPrefetch.
* @param {string} [dirPath] Path of the directory where to store all the content files. This is to keep the files * @return {Promise<any>} Promise resolved when all content is downloaded.
* relative paths and make the package work in an iframe. Undefined to download the files
* in the filepool root choice.
* @return {Promise<any>} Promise resolved when all content is downloaded. Data returned is not reliable.
*/ */
downloadOrPrefetch(module: any, courseId: number, prefetch?: boolean, dirPath?: string): Promise<any> { download(module: any, courseId: number, dirPath?: string): Promise<any> {
const siteId = this.sitesProvider.getCurrentSiteId(); // Same implementation for download or prefetch.
const promises = []; 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<any>} Promise resolved when done.
*/
prefetch(module: any, courseId?: number, single?: boolean, dirPath?: string): Promise<any> {
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<any>} Promise resolved when done.
*/
protected prefetchChoice(module: any, courseId: number, single: boolean, siteId: string): Promise<any> {
return this.choiceProvider.getChoice(courseId, module.id, siteId).then((choice) => {
const promises = []; const promises = [];
// Get the options and results. // Get the options and results.
@ -71,10 +93,12 @@ export class AddonModChoicePrefetchHandler extends CoreCourseModulePrefetchHandl
return Promise.all(subPromises); 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);
});
} }
/** /**

View File

@ -164,7 +164,7 @@ export class AddonModChoiceSyncProvider extends CoreSyncBaseProvider {
} }
// Couldn't connect to server, reject. // Couldn't connect to server, reject.
return Promise.reject(error && error.error); return Promise.reject(error);
}); });
}).then(() => { }).then(() => {
if (courseId) { if (courseId) {