diff --git a/src/addons/mod/quiz/components/index/index.ts b/src/addons/mod/quiz/components/index/index.ts index 21df59c34..2271165a0 100644 --- a/src/addons/mod/quiz/components/index/index.ts +++ b/src/addons/mod/quiz/components/index/index.ts @@ -196,15 +196,9 @@ export class AddonModQuizIndexComponent extends CoreCourseModuleMainActivityComp 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; - } + await CoreUtils.ignoreErrors(this.syncActivity(showErrors)); } } else { - this.autoReview = undefined; this.showStatusSpinner = false; } @@ -400,7 +394,7 @@ export class AddonModQuizIndexComponent extends CoreCourseModuleMainActivityComp * * @returns Promise resolved when done. */ - protected async goToAutoReview(): Promise { + protected async goToAutoReview(attempts: AddonModQuizAttemptWSData[]): Promise { if (!this.autoReview) { return; } @@ -409,20 +403,19 @@ export class AddonModQuizIndexComponent extends CoreCourseModuleMainActivityComp this.checkCompletion(); // Verify that user can see the review. - const attemptId = this.autoReview.attemptId; + const attempt = attempts.find(attempt => attempt.id === this.autoReview?.attemptId); this.autoReview = undefined; - if (this.quizAccessInfo?.canreviewmyattempts) { - try { - await AddonModQuiz.getAttemptReview(attemptId, { page: -1, cmId: this.module.id }); - - await CoreNavigator.navigateToSitePath( - `${AddonModQuizModuleHandlerService.PAGE_NAME}/${this.courseId}/${this.module.id}/review/${attemptId}`, - ); - } catch { - // Ignore errors. - } + if (!this.quiz || !this.quizAccessInfo || !attempt) { + return; } + + const canReview = await AddonModQuizHelper.canReviewAttempt(this.quiz, this.quizAccessInfo, attempt); + if (!canReview) { + return; + } + + await this.reviewAttempt(attempt.id); } /** @@ -451,22 +444,15 @@ export class AddonModQuizIndexComponent extends CoreCourseModuleMainActivityComp } this.hasPlayed = false; - let promise = Promise.resolve(); - - // Update data when we come back from the player since the attempt status could have changed. - // Check if we need to go to review an attempt automatically. - if (this.autoReview && this.autoReview.synced) { - promise = this.goToAutoReview(); - } // Refresh data. this.showLoading = true; this.content?.scrollToTop(); - await promise; await CoreUtils.ignoreErrors(this.refreshContent(true)); this.showLoading = false; + this.autoReview = undefined; } /** @@ -605,7 +591,7 @@ export class AddonModQuizIndexComponent extends CoreCourseModuleMainActivityComp const [options] = await Promise.all([ AddonModQuiz.getCombinedReviewOptions(quiz.id, { cmId: this.module.id }), this.getQuizGrade(), - openReview ? this.goToAutoReview() : undefined, + openReview ? this.goToAutoReview(attempts) : undefined, ]); this.options = options; diff --git a/src/addons/mod/quiz/services/handlers/prefetch.ts b/src/addons/mod/quiz/services/handlers/prefetch.ts index 595175af4..b6c17276b 100644 --- a/src/addons/mod/quiz/services/handlers/prefetch.ts +++ b/src/addons/mod/quiz/services/handlers/prefetch.ts @@ -380,7 +380,7 @@ export class AddonModQuizPrefetchHandlerService extends CoreCourseActivityPrefet // We have quiz data, now we'll get specific data for each attempt. await Promise.all(attempts.map(async (attempt) => { - await this.prefetchAttempt(quiz, attempt, preflightData, siteId); + await this.prefetchAttempt(quiz, quizAccessInfo, attempt, preflightData, siteId); })); if (!canStart) { @@ -400,6 +400,7 @@ export class AddonModQuizPrefetchHandlerService extends CoreCourseActivityPrefet * Prefetch all WS data for an attempt. * * @param quiz Quiz. + * @param accessInfo Quiz access info. * @param attempt Attempt. * @param preflightData Preflight required data (like password). * @param siteId Site ID. If not defined, current site. @@ -407,11 +408,11 @@ export class AddonModQuizPrefetchHandlerService extends CoreCourseActivityPrefet */ async prefetchAttempt( quiz: AddonModQuizQuizWSData, + accessInfo: AddonModQuizGetQuizAccessInformationWSResponse, attempt: AddonModQuizAttemptWSData, preflightData: Record, siteId?: string, ): Promise { - const pages = AddonModQuiz.getPagesFromLayout(attempt.layout); const isSequential = AddonModQuiz.isNavigationSequential(quiz); let promises: Promise[] = []; @@ -429,16 +430,7 @@ export class AddonModQuizPrefetchHandlerService extends CoreCourseActivityPrefet promises.push(AddonModQuiz.getFeedbackForGrade(quiz.id, attemptGradeNumber, modOptions)); } - // Get the review for each page. - pages.forEach((page) => { - promises.push(CoreUtils.ignoreErrors(AddonModQuiz.getAttemptReview(attempt.id, { - page, - ...modOptions, // Include all options. - }))); - }); - - // Get the review for all questions in same page. - promises.push(this.prefetchAttemptReviewFiles(quiz, attempt, modOptions, siteId)); + promises.push(this.prefetchAttemptReview(quiz, accessInfo, attempt, modOptions)); } else { // Attempt not finished, get data needed to continue the attempt. @@ -447,6 +439,8 @@ export class AddonModQuizPrefetchHandlerService extends CoreCourseActivityPrefet if (attempt.state === AddonModQuizAttemptStates.IN_PROGRESS) { // Get data for each page. + const pages = AddonModQuiz.getPagesFromLayout(attempt.layout); + promises = promises.concat(pages.map(async (page) => { if (isSequential && typeof attempt.currentpage === 'number' && page < attempt.currentpage) { // Sequential quiz, cannot get pages before the current one. @@ -473,20 +467,57 @@ export class AddonModQuizPrefetchHandlerService extends CoreCourseActivityPrefet await Promise.all(promises); } + /** + * Prefetch attempt review data. + * + * @param quiz Quiz. + * @param accessInfo Quiz access info. + * @param attempt Attempt. + * @param modOptions Other options. + * @param siteId Site ID. + * @returns Promise resolved when done. + */ + protected async prefetchAttemptReview( + quiz: AddonModQuizQuizWSData, + accessInfo: AddonModQuizGetQuizAccessInformationWSResponse, + attempt: AddonModQuizAttemptWSData, + modOptions: CoreCourseCommonModWSOptions, + ): Promise { + // Check if attempt can be reviewed. + const canReview = await AddonModQuizHelper.canReviewAttempt(quiz, accessInfo, attempt); + if (!canReview) { + return; + } + + const pages = AddonModQuiz.getPagesFromLayout(attempt.layout); + const promises: Promise[] = []; + + // Get the review for each page. + pages.forEach((page) => { + promises.push(CoreUtils.ignoreErrors(AddonModQuiz.getAttemptReview(attempt.id, { + page, + ...modOptions, // Include all options. + }))); + }); + + // Get the review for all questions in same page. + promises.push(this.prefetchAttemptReviewFiles(quiz, attempt, modOptions)); + + await Promise.all(promises); + } + /** * Prefetch attempt review and its files. * * @param quiz Quiz. * @param attempt Attempt. * @param modOptions Other options. - * @param siteId Site ID. * @returns Promise resolved when done. */ protected async prefetchAttemptReviewFiles( quiz: AddonModQuizQuizWSData, attempt: AddonModQuizAttemptWSData, modOptions: CoreCourseCommonModWSOptions, - siteId?: string, ): Promise { // Get the review for all questions in same page. const data = await CoreUtils.ignoreErrors(AddonModQuiz.getAttemptReview(attempt.id, { @@ -503,7 +534,7 @@ export class AddonModQuizPrefetchHandlerService extends CoreCourseActivityPrefet question, this.component, quiz.coursemodule, - siteId, + modOptions.siteId, attempt.uniqueid, ); })); @@ -569,7 +600,7 @@ export class AddonModQuizPrefetchHandlerService extends CoreCourseActivityPrefet preflightData = await this.getPreflightData(quiz, quizAccessInfo, lastAttempt, askPreflight, 'core.download', siteId); // Get data for last attempt. - await this.prefetchAttempt(quiz, lastAttempt, preflightData, siteId); + await this.prefetchAttempt(quiz, quizAccessInfo, lastAttempt, preflightData, siteId); } // Prefetch finished, set the right status. diff --git a/src/addons/mod/quiz/services/quiz.ts b/src/addons/mod/quiz/services/quiz.ts index 66419643b..e360df44c 100644 --- a/src/addons/mod/quiz/services/quiz.ts +++ b/src/addons/mod/quiz/services/quiz.ts @@ -513,7 +513,6 @@ export class AddonModQuizProvider { }; const preSets = { cacheKey: this.getAttemptReviewCacheKey(attemptId, page), - cacheErrors: ['noreview'], component: ADDON_MOD_QUIZ_COMPONENT, componentId: options.cmId, ...CoreSites.getReadingStrategyPreSets(options.readingStrategy), // Include reading strategy preSets.