MOBILE-4263 quiz: Avoid 'activerulenames' of undefined error

main
Dani Palou 2023-03-02 10:53:33 +01:00
parent 7cd9f6172c
commit 4792e47737
1 changed files with 74 additions and 80 deletions

View File

@ -332,62 +332,58 @@ export class AddonModQuizPlayerPage implements OnInit, OnDestroy, CanLeave {
* @returns Promise resolved when done. * @returns Promise resolved when done.
*/ */
protected async fetchData(): Promise<void> { protected async fetchData(): Promise<void> {
try { this.quiz = await AddonModQuiz.getQuiz(this.courseId, this.cmId);
this.quiz = await AddonModQuiz.getQuiz(this.courseId, this.cmId);
// Block the quiz so it cannot be synced. // Block the quiz so it cannot be synced.
CoreSync.blockOperation(AddonModQuizProvider.COMPONENT, this.quiz.id); CoreSync.blockOperation(AddonModQuizProvider.COMPONENT, this.quiz.id);
// Wait for any ongoing sync to finish. We won't sync a quiz while it's being played. // Wait for any ongoing sync to finish. We won't sync a quiz while it's being played.
await AddonModQuizSync.waitForSync(this.quiz.id); await AddonModQuizSync.waitForSync(this.quiz.id);
this.isSequential = AddonModQuiz.isNavigationSequential(this.quiz); this.isSequential = AddonModQuiz.isNavigationSequential(this.quiz);
if (AddonModQuiz.isQuizOffline(this.quiz)) { if (AddonModQuiz.isQuizOffline(this.quiz)) {
// Quiz supports offline. // Quiz supports offline.
this.offline = true; this.offline = true;
} else { } else {
// Quiz doesn't support offline right now, but maybe it did and then the setting was changed. // Quiz doesn't support offline right now, but maybe it did and then the setting was changed.
// If we have an unfinished offline attempt then we'll use offline mode. // If we have an unfinished offline attempt then we'll use offline mode.
this.offline = await AddonModQuiz.isLastAttemptOfflineUnfinished(this.quiz); this.offline = await AddonModQuiz.isLastAttemptOfflineUnfinished(this.quiz);
}
if (this.quiz!.timelimit && this.quiz!.timelimit > 0) {
this.readableTimeLimit = CoreTime.formatTime(this.quiz.timelimit);
}
// Get access information for the quiz.
this.quizAccessInfo = await AddonModQuiz.getQuizAccessInformation(this.quiz.id, {
cmId: this.quiz.coursemodule,
readingStrategy: this.offline ? CoreSitesReadingStrategy.PREFER_CACHE : CoreSitesReadingStrategy.ONLY_NETWORK,
});
// Get user attempts to determine last attempt.
const attempts = await AddonModQuiz.getUserAttempts(this.quiz.id, {
cmId: this.quiz.coursemodule,
readingStrategy: this.offline ? CoreSitesReadingStrategy.PREFER_CACHE : CoreSitesReadingStrategy.ONLY_NETWORK,
});
if (!attempts.length) {
// There are no attempts, start a new one.
this.newAttempt = true;
return;
}
// Get the last attempt. If it's finished, start a new one.
this.lastAttempt = await AddonModQuizHelper.setAttemptCalculatedData(
this.quiz,
attempts[attempts.length - 1],
false,
undefined,
true,
);
this.newAttempt = AddonModQuiz.isAttemptFinished(this.lastAttempt.state);
} catch (error) {
CoreDomUtils.showErrorModalDefault(error, 'addon.mod_quiz.errorgetquiz', true);
} }
if (this.quiz!.timelimit && this.quiz!.timelimit > 0) {
this.readableTimeLimit = CoreTime.formatTime(this.quiz.timelimit);
}
// Get access information for the quiz.
this.quizAccessInfo = await AddonModQuiz.getQuizAccessInformation(this.quiz.id, {
cmId: this.quiz.coursemodule,
readingStrategy: this.offline ? CoreSitesReadingStrategy.PREFER_CACHE : CoreSitesReadingStrategy.ONLY_NETWORK,
});
// Get user attempts to determine last attempt.
const attempts = await AddonModQuiz.getUserAttempts(this.quiz.id, {
cmId: this.quiz.coursemodule,
readingStrategy: this.offline ? CoreSitesReadingStrategy.PREFER_CACHE : CoreSitesReadingStrategy.ONLY_NETWORK,
});
if (!attempts.length) {
// There are no attempts, start a new one.
this.newAttempt = true;
return;
}
// Get the last attempt. If it's finished, start a new one.
this.lastAttempt = await AddonModQuizHelper.setAttemptCalculatedData(
this.quiz,
attempts[attempts.length - 1],
false,
undefined,
true,
);
this.newAttempt = AddonModQuiz.isAttemptFinished(this.lastAttempt.state);
} }
/** /**
@ -722,6 +718,8 @@ export class AddonModQuizPlayerPage implements OnInit, OnDestroy, CanLeave {
// Quiz data has been loaded, try to start or continue. // Quiz data has been loaded, try to start or continue.
await this.startOrContinueAttempt(); await this.startOrContinueAttempt();
} catch (error) {
CoreDomUtils.showErrorModalDefault(error, 'addon.mod_quiz.errorgetquiz', true);
} finally { } finally {
this.loaded = true; this.loaded = true;
} }
@ -733,41 +731,37 @@ export class AddonModQuizPlayerPage implements OnInit, OnDestroy, CanLeave {
* @returns Promise resolved when done. * @returns Promise resolved when done.
*/ */
protected async startOrContinueAttempt(): Promise<void> { protected async startOrContinueAttempt(): Promise<void> {
try { let attempt = this.newAttempt ? undefined : this.lastAttempt;
let attempt = this.newAttempt ? undefined : this.lastAttempt;
// Get the preflight data and start attempt if needed. // Get the preflight data and start attempt if needed.
attempt = await AddonModQuizHelper.getAndCheckPreflightData( attempt = await AddonModQuizHelper.getAndCheckPreflightData(
this.quiz!, this.quiz!,
this.quizAccessInfo!, this.quizAccessInfo!,
this.preflightData, this.preflightData,
attempt, attempt,
this.offline, this.offline,
false, false,
'addon.mod_quiz.startattempt', 'addon.mod_quiz.startattempt',
); );
// Re-fetch attempt access information with the right attempt (might have changed because a new attempt was created). // Re-fetch attempt access information with the right attempt (might have changed because a new attempt was created).
this.attemptAccessInfo = await AddonModQuiz.getAttemptAccessInformation(this.quiz!.id, attempt.id, { this.attemptAccessInfo = await AddonModQuiz.getAttemptAccessInformation(this.quiz!.id, attempt.id, {
cmId: this.quiz!.coursemodule, cmId: this.quiz!.coursemodule,
readingStrategy: this.offline ? CoreSitesReadingStrategy.PREFER_CACHE : CoreSitesReadingStrategy.ONLY_NETWORK, readingStrategy: this.offline ? CoreSitesReadingStrategy.PREFER_CACHE : CoreSitesReadingStrategy.ONLY_NETWORK,
}); });
this.attempt = attempt; this.attempt = attempt;
await this.loadNavigation(); await this.loadNavigation();
if (this.attempt.state != AddonModQuizProvider.ATTEMPT_OVERDUE && !this.attempt.finishedOffline) { if (this.attempt.state != AddonModQuizProvider.ATTEMPT_OVERDUE && !this.attempt.finishedOffline) {
// Attempt not overdue and not finished in offline, load page. // Attempt not overdue and not finished in offline, load page.
await this.loadPage(this.attempt.currentpage!); await this.loadPage(this.attempt.currentpage!);
this.initTimer(); this.initTimer();
} else { } else {
// Attempt is overdue or finished in offline, we can only load the summary. // Attempt is overdue or finished in offline, we can only load the summary.
await this.loadSummary(); await this.loadSummary();
}
} catch (error) {
CoreDomUtils.showErrorModalDefault(error, 'addon.mod_quiz.errorgetquestions', true);
} }
} }