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.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<any>} Resolved when done.
*/
protected invalidateContent(): Promise<any> {
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) => {

View File

@ -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 {
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,7 +346,6 @@ export class AddonModChoiceProvider {
* @return {Promise<any>} Promise resolved when the WS call is successful.
*/
logView(id: string): Promise<any> {
if (id) {
const params = {
choiceid: id
};
@ -359,9 +353,6 @@ export class AddonModChoiceProvider {
return this.sitesProvider.getCurrentSite().write('mod_choice_view_choice', params);
}
return Promise.reject(null);
}
/**
* Send a response to a choice to Moodle.
*

View File

@ -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);
}

View File

@ -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<any>} Promise resolved when all content is downloaded. Data returned is not reliable.
* @param {string} [dirPath] Path of the directory where to store all the content files. @see downloadOrPrefetch.
* @return {Promise<any>} Promise resolved when all content is downloaded.
*/
downloadOrPrefetch(module: any, courseId: number, prefetch?: boolean, dirPath?: string): Promise<any> {
const siteId = this.sitesProvider.getCurrentSiteId();
const promises = [];
download(module: any, courseId: number, dirPath?: string): Promise<any> {
// 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<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 = [];
// 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);
});
}
/**

View File

@ -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) {