MOBILE-3787 assign: Fix non-null checks

main
Pau Ferrer Ocaña 2022-01-07 13:34:19 +01:00
parent 7cc309275c
commit 9b93a0450c
2 changed files with 35 additions and 27 deletions

View File

@ -79,8 +79,8 @@ export class AddonModAssignIndexComponent extends CoreCourseModuleMainActivityCo
submissionStatusDraft = AddonModAssignProvider.SUBMISSION_STATUS_DRAFT; submissionStatusDraft = AddonModAssignProvider.SUBMISSION_STATUS_DRAFT;
needGrading = AddonModAssignProvider.NEED_GRADING; needGrading = AddonModAssignProvider.NEED_GRADING;
protected currentUserId?: number; // Current user ID. protected currentUserId!: number; // Current user ID.
protected currentSite?: CoreSite; // Current user ID. protected currentSite!: CoreSite; // Current site.
protected syncEventName = AddonModAssignSyncProvider.AUTO_SYNCED; protected syncEventName = AddonModAssignSyncProvider.AUTO_SYNCED;
// Observers. // Observers.
@ -93,6 +93,9 @@ export class AddonModAssignIndexComponent extends CoreCourseModuleMainActivityCo
@Optional() courseContentsPage?: CoreCourseContentsPage, @Optional() courseContentsPage?: CoreCourseContentsPage,
) { ) {
super('AddonModLessonIndexComponent', content, courseContentsPage); super('AddonModLessonIndexComponent', content, courseContentsPage);
this.currentSite = CoreSites.getRequiredCurrentSite();
this.currentUserId = this.currentSite.getUserId();
} }
/** /**
@ -101,9 +104,6 @@ export class AddonModAssignIndexComponent extends CoreCourseModuleMainActivityCo
async ngOnInit(): Promise<void> { async ngOnInit(): Promise<void> {
super.ngOnInit(); super.ngOnInit();
this.currentUserId = CoreSites.getCurrentSiteUserId();
this.currentSite = CoreSites.getCurrentSite();
// Listen to events. // Listen to events.
this.savedObserver = CoreEvents.on( this.savedObserver = CoreEvents.on(
AddonModAssignProvider.SUBMISSION_SAVED_EVENT, AddonModAssignProvider.SUBMISSION_SAVED_EVENT,
@ -139,8 +139,12 @@ export class AddonModAssignIndexComponent extends CoreCourseModuleMainActivityCo
await this.loadContent(false, true); await this.loadContent(false, true);
if (!this.assign) {
return;
}
try { try {
await AddonModAssign.logView(this.assign!.id, this.assign!.name); await AddonModAssign.logView(this.assign.id, this.assign.name);
CoreCourse.checkModuleCompletion(this.courseId, this.module.completiondata); CoreCourse.checkModuleCompletion(this.courseId, this.module.completiondata);
} catch { } catch {
// Ignore errors. Just don't check Module completion. // Ignore errors. Just don't check Module completion.
@ -148,10 +152,10 @@ export class AddonModAssignIndexComponent extends CoreCourseModuleMainActivityCo
if (this.canViewAllSubmissions) { if (this.canViewAllSubmissions) {
// User can see all submissions, log grading view. // User can see all submissions, log grading view.
CoreUtils.ignoreErrors(AddonModAssign.logGradingView(this.assign!.id, this.assign!.name)); CoreUtils.ignoreErrors(AddonModAssign.logGradingView(this.assign.id, this.assign.name));
} else if (this.canViewOwnSubmission) { } else if (this.canViewOwnSubmission) {
// User can only see their own submission, log view the user submission. // User can only see their own submission, log view the user submission.
CoreUtils.ignoreErrors(AddonModAssign.logSubmissionView(this.assign!.id, this.assign!.name)); CoreUtils.ignoreErrors(AddonModAssign.logSubmissionView(this.assign.id, this.assign.name));
} }
} }
@ -266,7 +270,11 @@ export class AddonModAssignIndexComponent extends CoreCourseModuleMainActivityCo
async setGroup(groupId = 0): Promise<void> { async setGroup(groupId = 0): Promise<void> {
this.group = groupId; this.group = groupId;
const submissionStatus = await AddonModAssign.getSubmissionStatus(this.assign!.id, { if (!this.assign) {
return;
}
const submissionStatus = await AddonModAssign.getSubmissionStatus(this.assign.id, {
groupId: this.group, groupId: this.group,
cmId: this.module.id, cmId: this.module.id,
}); });
@ -278,10 +286,10 @@ export class AddonModAssignIndexComponent extends CoreCourseModuleMainActivityCo
return; return;
} }
if (this.summary?.warnofungroupedusers === true) { if (this.summary.warnofungroupedusers === true) {
this.summary.warnofungroupedusers = 'ungroupedusers'; this.summary.warnofungroupedusers = 'ungroupedusers';
} else { } else {
switch (this.summary?.warnofungroupedusers) { switch (this.summary.warnofungroupedusers) {
case AddonModAssignProvider.WARN_GROUPS_REQUIRED: case AddonModAssignProvider.WARN_GROUPS_REQUIRED:
this.summary.warnofungroupedusers = 'ungroupedusers'; this.summary.warnofungroupedusers = 'ungroupedusers';
break; break;
@ -386,7 +394,10 @@ export class AddonModAssignIndexComponent extends CoreCourseModuleMainActivityCo
* @return True if refresh is needed, false otherwise. * @return True if refresh is needed, false otherwise.
*/ */
protected isRefreshSyncNeeded(syncEventData: AddonModAssignAutoSyncData): boolean { protected isRefreshSyncNeeded(syncEventData: AddonModAssignAutoSyncData): boolean {
if (this.assign && syncEventData.assignId == this.assign.id) { if (!this.assign || syncEventData.assignId != this.assign.id) {
return false;
}
if (syncEventData.warnings && syncEventData.warnings.length) { if (syncEventData.warnings && syncEventData.warnings.length) {
// Show warnings. // Show warnings.
CoreDomUtils.showErrorModal(syncEventData.warnings[0]); CoreDomUtils.showErrorModal(syncEventData.warnings[0]);
@ -395,16 +406,17 @@ export class AddonModAssignIndexComponent extends CoreCourseModuleMainActivityCo
return true; return true;
} }
return false;
}
/** /**
* Performs the sync of the activity. * Performs the sync of the activity.
* *
* @return Promise resolved when done. * @return Promise resolved when done.
*/ */
protected sync(): Promise<AddonModAssignSyncResult> { protected async sync(): Promise<AddonModAssignSyncResult | void> {
return AddonModAssignSync.syncAssign(this.assign!.id); if (!this.assign) {
return;
}
await AddonModAssignSync.syncAssign(this.assign.id);
} }
/** /**

View File

@ -612,11 +612,7 @@ export class AddonModAssignSubmissionComponent implements OnInit, OnDestroy, Can
// If we have data about the grader, get its profile. // If we have data about the grader, get its profile.
if (feedback.grade && feedback.grade.grader > 0) { if (feedback.grade && feedback.grade.grader > 0) {
try { this.grader = await CoreUtils.ignoreErrors(CoreUser.getProfile(feedback.grade.grader, this.courseId));
this.grader = await CoreUser.getProfile(feedback.grade.grader, this.courseId);
} catch {
// Ignore errors.
}
} else { } else {
delete this.grader; delete this.grader;
} }
@ -633,7 +629,7 @@ export class AddonModAssignSubmissionComponent implements OnInit, OnDestroy, Can
if (feedback.grade && feedback.grade.grade && !this.grade.grade) { if (feedback.grade && feedback.grade.grade && !this.grade.grade) {
const parsedGrade = parseFloat(feedback.grade.grade); const parsedGrade = parseFloat(feedback.grade.grade);
this.grade!.grade = parsedGrade >= 0 ? parsedGrade : undefined; this.grade.grade = parsedGrade >= 0 ? parsedGrade : undefined;
this.grade.gradebookGrade = CoreUtils.formatFloat(this.grade.grade); this.grade.gradebookGrade = CoreUtils.formatFloat(this.grade.grade);
this.originalGrades.grade = this.grade.grade; this.originalGrades.grade = this.grade.grade;
} }