From aa6824e4954ad3b9397947381fe671ade5c5d092 Mon Sep 17 00:00:00 2001 From: Dani Palou Date: Thu, 23 Sep 2021 12:01:00 +0200 Subject: [PATCH] MOBILE-3877 quiz: Fix review not opened after finish offline quiz --- src/addons/mod/quiz/components/index/index.ts | 16 +++++++++------- src/addons/mod/quiz/services/quiz.ts | 14 ++++++++++++-- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/src/addons/mod/quiz/components/index/index.ts b/src/addons/mod/quiz/components/index/index.ts index d8e45071e..078c97054 100644 --- a/src/addons/mod/quiz/components/index/index.ts +++ b/src/addons/mod/quiz/components/index/index.ts @@ -208,13 +208,15 @@ export class AddonModQuizIndexComponent extends CoreCourseModuleMainActivityComp await AddonModQuizSync.setSyncWarnings(quiz.id, []); } - if (AddonModQuiz.isQuizOffline(quiz) && sync) { - // Try to sync the quiz. - try { - await this.syncActivity(showErrors); - } catch { - // Ignore errors, keep getting data even if sync fails. - this.autoReview = undefined; + if (AddonModQuiz.isQuizOffline(quiz)) { + if (sync) { + // Try to sync the quiz. + try { + await this.syncActivity(showErrors); + } catch { + // Ignore errors, keep getting data even if sync fails. + this.autoReview = undefined; + } } } else { this.autoReview = undefined; diff --git a/src/addons/mod/quiz/services/quiz.ts b/src/addons/mod/quiz/services/quiz.ts index f64bda703..c3cc895df 100644 --- a/src/addons/mod/quiz/services/quiz.ts +++ b/src/addons/mod/quiz/services/quiz.ts @@ -664,11 +664,21 @@ export class AddonModQuizProvider { /** * Given a list of attempts, returns the last finished attempt. * - * @param attempts Attempts. + * @param attempts Attempts sorted. First attempt should be the first on the list. * @return Last finished attempt. */ getLastFinishedAttemptFromList(attempts?: AddonModQuizAttemptWSData[]): AddonModQuizAttemptWSData | undefined { - return attempts?.find(attempt => this.isAttemptFinished(attempt.state)); + if (!attempts) { + return; + } + + for (let i = attempts.length - 1; i >= 0; i--) { + const attempt = attempts[i]; + + if (this.isAttemptFinished(attempt.state)) { + return attempt; + } + } } /**