Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mobile 4263 #3574

Merged
merged 2 commits into from
Mar 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
42 changes: 23 additions & 19 deletions src/addons/mod/quiz/components/index/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -333,15 +333,16 @@ export class AddonModQuizIndexComponent extends CoreCourseModuleMainActivityComp
return;
}

const bestGrade = this.bestGrade.grade;
const formattedGradebookGrade = AddonModQuiz.formatGrade(this.gradebookData.grade, quiz.decimalpoints);
const formattedBestGrade = AddonModQuiz.formatGrade(this.bestGrade.grade, quiz.decimalpoints);
const formattedBestGrade = AddonModQuiz.formatGrade(bestGrade, quiz.decimalpoints);
let gradeToShow = formattedGradebookGrade; // By default we show the grade in the gradebook.

this.showResults = true;
this.gradeOverridden = formattedGradebookGrade != formattedBestGrade;
this.gradebookFeedback = this.gradebookData.feedback;

if (this.bestGrade.grade! > this.gradebookData.grade && this.gradebookData.grade == quiz.grade) {
if (bestGrade && bestGrade > this.gradebookData.grade && this.gradebookData.grade == quiz.grade) {
// The best grade is higher than the max grade for the quiz.
// We'll do like Moodle web and show the best grade instead of the gradebook grade.
this.gradeOverridden = false;
Expand Down Expand Up @@ -556,8 +557,16 @@ export class AddonModQuizIndexComponent extends CoreCourseModuleMainActivityComp
*
* @returns Promise resolved when done.
*/
protected sync(): Promise<AddonModQuizSyncResult> {
return AddonModQuizSync.syncQuiz(this.candidateQuiz!, true);
protected async sync(): Promise<AddonModQuizSyncResult> {
if (!this.candidateQuiz) {
return {
warnings: [],
attemptFinished: false,
updated: false,
};
}

return AddonModQuizSync.syncQuiz(this.candidateQuiz, true);
}

/**
Expand All @@ -579,36 +588,31 @@ export class AddonModQuizIndexComponent extends CoreCourseModuleMainActivityComp
}

const lastFinished = AddonModQuiz.getLastFinishedAttemptFromList(attempts);
const promises: Promise<unknown>[] = [];
let openReview = false;

if (this.autoReview && lastFinished && lastFinished.id >= this.autoReview.attemptId) {
// User just finished an attempt in offline and it seems it's been synced, since it's finished in online.
// Go to the review of this attempt if the user hasn't left this view.
if (!this.isDestroyed && this.isCurrentView) {
promises.push(this.goToAutoReview());
openReview = true;
}
this.autoReview = undefined;
}

// Get combined review options.
promises.push(AddonModQuiz.getCombinedReviewOptions(quiz.id, { cmId: this.module.id }).then((options) => {
this.options = options;

return;
}));

// Get best grade.
promises.push(this.getQuizGrade());

await Promise.all(promises);
const [options] = await Promise.all([
AddonModQuiz.getCombinedReviewOptions(quiz.id, { cmId: this.module.id }),
this.getQuizGrade(),
openReview ? this.goToAutoReview() : undefined,
]);

this.options = options;
const grade = this.gradebookData?.grade !== undefined ? this.gradebookData.grade : this.bestGrade?.grade;
const quizGrade = AddonModQuiz.formatGrade(grade, quiz.decimalpoints);

// Calculate data to construct the header of the attempts table.
AddonModQuizHelper.setQuizCalculatedData(quiz, this.options!);
AddonModQuizHelper.setQuizCalculatedData(quiz, this.options);

this.overallStats = !!lastFinished && this.options!.alloptions.marks >= AddonModQuizProvider.QUESTION_OPTIONS_MARK_AND_MAX;
this.overallStats = !!lastFinished && this.options.alloptions.marks >= AddonModQuizProvider.QUESTION_OPTIONS_MARK_AND_MAX;

// Calculate data to show for each attempt.
const formattedAttempts = await Promise.all(attempts.map((attempt, index) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,6 @@ export type AddonModQuizNavigationQuestion = CoreQuestionQuestionParsed & {
};

export type AddonModQuizNavigationModalReturn = {
page?: number;
page: number;
slot?: number;
};
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,13 @@ export class AddonModQuizPreflightModalComponent implements OnInit {
}

try {
const quiz = this.quiz;

await Promise.all(this.rules.map(async (rule) => {
// Check if preflight is required for rule and, if so, get the component to render it.
const required = await AddonModQuizAccessRuleDelegate.isPreflightCheckRequiredForRule(
rule,
this.quiz!,
quiz,
this.attempt,
this.prefetch,
this.siteId,
Expand Down
28 changes: 17 additions & 11 deletions src/addons/mod/quiz/pages/attempt/attempt.page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,20 +94,20 @@ export class AddonModQuizAttemptPage implements OnInit {
// Load attempt data.
const [options, accessInfo, attempt] = await Promise.all([
AddonModQuiz.getCombinedReviewOptions(this.quiz.id, { cmId: this.quiz.coursemodule }),
this.fetchAccessInfo(),
this.fetchAttempt(),
this.fetchAccessInfo(this.quiz),
this.fetchAttempt(this.quiz.id),
]);

// Set calculated data.
this.showReviewColumn = accessInfo.canreviewmyattempts;
AddonModQuizHelper.setQuizCalculatedData(this.quiz, options);

this.attempt = await AddonModQuizHelper.setAttemptCalculatedData(this.quiz!, attempt, false, undefined, true);
this.attempt = await AddonModQuizHelper.setAttemptCalculatedData(this.quiz, attempt, false, undefined, true);

// Check if the feedback should be displayed.
const grade = Number(this.attempt!.rescaledGrade);
const grade = Number(this.attempt.rescaledGrade);

if (this.quiz.showFeedbackColumn && AddonModQuiz.isAttemptFinished(this.attempt!.state) &&
if (this.quiz.showFeedbackColumn && AddonModQuiz.isAttemptFinished(this.attempt.state) &&
options.someoptions.overallfeedback && !isNaN(grade)) {

// Feedback should be displayed, get the feedback for the grade.
Expand All @@ -127,11 +127,12 @@ export class AddonModQuizAttemptPage implements OnInit {
/**
* Get the attempt.
*
* @param quizId Quiz ID.
* @returns Promise resolved when done.
*/
protected async fetchAttempt(): Promise<AddonModQuizAttemptWSData> {
protected async fetchAttempt(quizId: number): Promise<AddonModQuizAttemptWSData> {
// Get all the attempts and search the one we want.
const attempts = await AddonModQuiz.getUserAttempts(this.quiz!.id, { cmId: this.cmId });
const attempts = await AddonModQuiz.getUserAttempts(quizId, { cmId: this.cmId });

const attempt = attempts.find(attempt => attempt.id == this.attemptId);

Expand All @@ -148,10 +149,11 @@ export class AddonModQuizAttemptPage implements OnInit {
/**
* Get the access info.
*
* @param quiz Quiz instance.
* @returns Promise resolved when done.
*/
protected async fetchAccessInfo(): Promise<AddonModQuizGetQuizAccessInformationWSResponse> {
const accessInfo = await AddonModQuiz.getQuizAccessInformation(this.quiz!.id, { cmId: this.cmId });
protected async fetchAccessInfo(quiz: AddonModQuizQuizData): Promise<AddonModQuizGetQuizAccessInformationWSResponse> {
const accessInfo = await AddonModQuiz.getQuizAccessInformation(quiz.id, { cmId: this.cmId });

if (!accessInfo.canreviewmyattempts) {
return accessInfo;
Expand All @@ -161,7 +163,7 @@ export class AddonModQuizAttemptPage implements OnInit {
await CoreUtils.ignoreErrors(AddonModQuiz.invalidateAttemptReviewForPage(this.attemptId, -1));

try {
await AddonModQuiz.getAttemptReview(this.attemptId, { page: -1, cmId: this.quiz!.coursemodule });
await AddonModQuiz.getAttemptReview(this.attemptId, { page: -1, cmId: quiz.coursemodule });
} catch {
// Error getting the review, assume the user cannot review the attempt.
accessInfo.canreviewmyattempts = false;
Expand Down Expand Up @@ -202,7 +204,11 @@ export class AddonModQuizAttemptPage implements OnInit {
* @returns Promise resolved when done.
*/
async reviewAttempt(): Promise<void> {
CoreNavigator.navigate(`../../review/${this.attempt!.id}`);
if (!this.attempt) {
return;
}

CoreNavigator.navigate(`../../review/${this.attempt.id}`);
}

}