MOBILE-2272 quiz: Fix error when calculating new state in offline

main
Dani Palou 2020-09-30 16:33:23 +02:00
parent d2f4c85af2
commit f144a29fee
2 changed files with 113 additions and 104 deletions

View File

@ -83,14 +83,19 @@ export class AddonQbehaviourDeferredFeedbackHandler implements CoreQuestionBehav
* @param isSameFn Function to override the default isSameResponse check. * @param isSameFn Function to override the default isSameResponse check.
* @return Promise resolved with state. * @return Promise resolved with state.
*/ */
determineNewStateDeferred(component: string, attemptId: number, question: any, componentId: string | number, siteId?: string, async determineNewStateDeferred(component: string, attemptId: number, question: any, componentId: string | number,
isCompleteFn?: isCompleteResponseFunction, isSameFn?: isSameResponseFunction): Promise<CoreQuestionState> { siteId?: string, isCompleteFn?: isCompleteResponseFunction, isSameFn?: isSameResponseFunction)
: Promise<CoreQuestionState> {
// Check if we have local data for the question. // Check if we have local data for the question.
return this.questionProvider.getQuestion(component, attemptId, question.slot, siteId).catch(() => { let dbQuestion;
try {
dbQuestion = await this.questionProvider.getQuestion(component, attemptId, question.slot, siteId);
} catch (error) {
// No entry found, use the original data. // No entry found, use the original data.
return question; dbQuestion = question;
}).then((dbQuestion) => { }
const state = this.questionProvider.getState(dbQuestion.state); const state = this.questionProvider.getState(dbQuestion.state);
if (state.finished || !state.active) { if (state.finished || !state.active) {
@ -98,12 +103,12 @@ export class AddonQbehaviourDeferredFeedbackHandler implements CoreQuestionBehav
return state; return state;
} }
// We need to check if the answers have changed. Retrieve current stored answers.
return this.questionProvider.getQuestionAnswers(component, attemptId, question.slot, false, siteId)
.then((prevAnswers) => {
const newBasicAnswers = this.questionProvider.getBasicAnswers(question.answers); const newBasicAnswers = this.questionProvider.getBasicAnswers(question.answers);
if (dbQuestion.state) {
// Question already has a state stored. Check if answer has changed.
let prevAnswers = await this.questionProvider.getQuestionAnswers(component, attemptId, question.slot, false, siteId);
prevAnswers = this.questionProvider.convertAnswersArrayToObject(prevAnswers, true); prevAnswers = this.questionProvider.convertAnswersArrayToObject(prevAnswers, true);
const prevBasicAnswers = this.questionProvider.getBasicAnswers(prevAnswers); const prevBasicAnswers = this.questionProvider.getBasicAnswers(prevAnswers);
@ -118,10 +123,12 @@ export class AddonQbehaviourDeferredFeedbackHandler implements CoreQuestionBehav
return state; return state;
} }
} }
}
// Answers have changed. Now check if the response is complete and calculate the new state. // Answers have changed. Now check if the response is complete and calculate the new state.
let complete: number, let complete: number;
newState: string; let newState: string;
if (isCompleteFn) { if (isCompleteFn) {
// Pass all the answers since some behaviours might need the extra data. // Pass all the answers since some behaviours might need the extra data.
complete = isCompleteFn(question, question.answers, component, componentId); complete = isCompleteFn(question, question.answers, component, componentId);
@ -146,8 +153,6 @@ export class AddonQbehaviourDeferredFeedbackHandler implements CoreQuestionBehav
} }
return this.questionProvider.getState(newState); return this.questionProvider.getState(newState);
});
});
} }
/** /**

View File

@ -83,15 +83,19 @@ export class AddonQbehaviourManualGradedHandler implements CoreQuestionBehaviour
* @param isSameFn Function to override the default isSameResponse check. * @param isSameFn Function to override the default isSameResponse check.
* @return Promise resolved with state. * @return Promise resolved with state.
*/ */
determineNewStateManualGraded(component: string, attemptId: number, question: any, componentId: string | number, async determineNewStateManualGraded(component: string, attemptId: number, question: any, componentId: string | number,
siteId?: string, isCompleteFn?: isCompleteResponseFunction, isSameFn?: isSameResponseFunction) siteId?: string, isCompleteFn?: isCompleteResponseFunction, isSameFn?: isSameResponseFunction)
: Promise<CoreQuestionState> { : Promise<CoreQuestionState> {
// Check if we have local data for the question. // Check if we have local data for the question.
return this.questionProvider.getQuestion(component, attemptId, question.slot, siteId).catch(() => { let dbQuestion;
try {
dbQuestion = await this.questionProvider.getQuestion(component, attemptId, question.slot, siteId);
} catch (error) {
// No entry found, use the original data. // No entry found, use the original data.
return question; dbQuestion = question;
}).then((dbQuestion) => { }
const state = this.questionProvider.getState(dbQuestion.state); const state = this.questionProvider.getState(dbQuestion.state);
if (state.finished || !state.active) { if (state.finished || !state.active) {
@ -99,12 +103,12 @@ export class AddonQbehaviourManualGradedHandler implements CoreQuestionBehaviour
return state; return state;
} }
// We need to check if the answers have changed. Retrieve current stored answers.
return this.questionProvider.getQuestionAnswers(component, attemptId, question.slot, false, siteId)
.then((prevAnswers) => {
const newBasicAnswers = this.questionProvider.getBasicAnswers(question.answers); const newBasicAnswers = this.questionProvider.getBasicAnswers(question.answers);
if (dbQuestion.state) {
// Question already has a state stored. Check if answer has changed.
let prevAnswers = await this.questionProvider.getQuestionAnswers(component, attemptId, question.slot, false, siteId);
prevAnswers = this.questionProvider.convertAnswersArrayToObject(prevAnswers, true); prevAnswers = this.questionProvider.convertAnswersArrayToObject(prevAnswers, true);
const prevBasicAnswers = this.questionProvider.getBasicAnswers(prevAnswers); const prevBasicAnswers = this.questionProvider.getBasicAnswers(prevAnswers);
@ -119,10 +123,12 @@ export class AddonQbehaviourManualGradedHandler implements CoreQuestionBehaviour
return state; return state;
} }
} }
}
// Check if the response is complete and calculate the new state.
let complete: number;
let newState: string;
// Answers have changed. Now check if the response is complete and calculate the new state.
let complete: number,
newState: string;
if (isCompleteFn) { if (isCompleteFn) {
// Pass all the answers since some behaviours might need the extra data. // Pass all the answers since some behaviours might need the extra data.
complete = isCompleteFn(question, question.answers, component, componentId); complete = isCompleteFn(question, question.answers, component, componentId);
@ -140,8 +146,6 @@ export class AddonQbehaviourManualGradedHandler implements CoreQuestionBehaviour
} }
return this.questionProvider.getState(newState); return this.questionProvider.getState(newState);
});
});
} }
/** /**