From 34f9c3ab5757db575b1e999eb74b57dd7560f2bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pau=20Ferrer=20Oca=C3=B1a?= Date: Thu, 9 Sep 2021 16:38:38 +0200 Subject: [PATCH] MOBILE-3833 navigator: Add required route params functions --- src/addons/calendar/pages/event/event.page.ts | 11 +- .../competency/pages/competency/competency.ts | 18 ++- .../competencysummary/competencysummary.ts | 14 ++- .../coursecompetencies.page.ts | 12 +- src/addons/competency/pages/plan/plan.ts | 10 +- .../coursecompletion/pages/report/report.ts | 12 +- src/addons/mod/assign/pages/edit/edit.ts | 14 ++- .../submission-list/submission-list.page.ts | 16 ++- .../submission-review/submission-review.ts | 16 ++- src/addons/mod/chat/pages/chat/chat.ts | 10 +- .../session-messages/session-messages.ts | 20 +++- .../mod/chat/pages/sessions/sessions.ts | 16 ++- .../components/field-plugin/field-plugin.ts | 2 +- src/addons/mod/data/pages/edit/edit.ts | 16 ++- src/addons/mod/data/pages/entry/entry.ts | 19 ++- .../mod/feedback/pages/attempt/attempt.ts | 14 ++- src/addons/mod/feedback/pages/form/form.ts | 20 +++- .../pages/nonrespondents/nonrespondents.ts | 14 ++- .../feedback/pages/respondents/respondents.ts | 14 ++- .../forum/pages/discussion/discussion.page.ts | 110 +++++++++++------- .../new-discussion/new-discussion.page.ts | 27 ++++- src/addons/mod/glossary/pages/edit/edit.ts | 35 ++++-- src/addons/mod/glossary/pages/entry/entry.ts | 16 ++- .../pages/attempt-results/attempt-results.ts | 14 ++- .../pages/user-attempts/user-attempts.ts | 15 ++- .../pages/users-attempts/users-attempts.ts | 12 +- .../mod/lesson/pages/player/player.page.ts | 20 +++- .../pages/user-retake/user-retake.page.ts | 16 ++- .../mod/quiz/pages/attempt/attempt.page.ts | 14 ++- .../mod/quiz/pages/player/player.page.ts | 14 ++- .../mod/quiz/pages/review/review.page.ts | 18 ++- src/addons/mod/scorm/pages/player/player.ts | 24 ++-- .../workshop/pages/assessment/assessment.ts | 16 ++- .../pages/edit-submission/edit-submission.ts | 16 ++- .../workshop/pages/submission/submission.ts | 23 ++-- src/addons/notes/pages/list/list.page.ts | 18 ++- src/addons/privatefiles/pages/index/index.ts | 32 +++-- .../pages/course-storage/course-storage.ts | 10 +- src/core/components/split-view/split-view.ts | 21 ++-- .../comments/pages/viewer/viewer.page.ts | 25 ++-- .../course/classes/main-activity-page.ts | 15 ++- .../courses/pages/categories/categories.ts | 2 +- .../grades/pages/course/course.page.ts | 18 ++- .../features/grades/pages/grade/grade.page.ts | 20 +++- .../pages/participants/participants.page.ts | 14 ++- src/core/services/navigator.ts | 68 ++++++++++- 46 files changed, 673 insertions(+), 228 deletions(-) diff --git a/src/addons/calendar/pages/event/event.page.ts b/src/addons/calendar/pages/event/event.page.ts index e92b62b8e..c449856b4 100644 --- a/src/addons/calendar/pages/event/event.page.ts +++ b/src/addons/calendar/pages/event/event.page.ts @@ -147,7 +147,16 @@ export class AddonCalendarEventPage implements OnInit, OnDestroy { * View loaded. */ ngOnInit(): void { - this.eventId = CoreNavigator.getRouteNumberParam('id')!; + try { + this.eventId = CoreNavigator.getRequiredRouteNumberParam('id'); + } catch (error) { + CoreDomUtils.showErrorModal(error); + + CoreNavigator.back(); + + return; + } + this.syncIcon = CoreConstants.ICON_LOADING; this.fetchEvent(); diff --git a/src/addons/competency/pages/competency/competency.ts b/src/addons/competency/pages/competency/competency.ts index e4ebe432e..f9bd310cc 100644 --- a/src/addons/competency/pages/competency/competency.ts +++ b/src/addons/competency/pages/competency/competency.ts @@ -60,11 +60,19 @@ export class AddonCompetencyCompetencyPage implements OnInit { * @inheritdoc */ async ngOnInit(): Promise { - this.competencyId = CoreNavigator.getRouteNumberParam('competencyId')!; - this.planId = CoreNavigator.getRouteNumberParam('planId'); - if (!this.planId) { - this.courseId = CoreNavigator.getRouteNumberParam('courseId')!; - this.userId = CoreNavigator.getRouteNumberParam('userId'); + try { + this.competencyId = CoreNavigator.getRequiredRouteNumberParam('competencyId'); + this.planId = CoreNavigator.getRouteNumberParam('planId'); + if (!this.planId) { + this.courseId = CoreNavigator.getRequiredRouteNumberParam('courseId'); + this.userId = CoreNavigator.getRouteNumberParam('userId'); + } + } catch (error) { + CoreDomUtils.showErrorModal(error); + + CoreNavigator.back(); + + return; } try { diff --git a/src/addons/competency/pages/competencysummary/competencysummary.ts b/src/addons/competency/pages/competencysummary/competencysummary.ts index 451d85553..2d150eb4e 100644 --- a/src/addons/competency/pages/competencysummary/competencysummary.ts +++ b/src/addons/competency/pages/competencysummary/competencysummary.ts @@ -40,9 +40,17 @@ export class AddonCompetencyCompetencySummaryPage implements OnInit { * @inheritdoc */ async ngOnInit(): Promise { - this.competencyId = CoreNavigator.getRouteNumberParam('competencyId')!; - this.contextLevel = CoreNavigator.getRouteParam('contextLevel'); - this.contextInstanceId = CoreNavigator.getRouteNumberParam('contextInstanceId'); + try { + this.competencyId = CoreNavigator.getRequiredRouteNumberParam('competencyId'); + this.contextLevel = CoreNavigator.getRouteParam('contextLevel'); + this.contextInstanceId = CoreNavigator.getRouteNumberParam('contextInstanceId'); + } catch (error) { + CoreDomUtils.showErrorModal(error); + + CoreNavigator.back(); + + return; + } try { await this.fetchCompetency(); diff --git a/src/addons/competency/pages/coursecompetencies/coursecompetencies.page.ts b/src/addons/competency/pages/coursecompetencies/coursecompetencies.page.ts index 58c91bd56..cbe9a49eb 100644 --- a/src/addons/competency/pages/coursecompetencies/coursecompetencies.page.ts +++ b/src/addons/competency/pages/coursecompetencies/coursecompetencies.page.ts @@ -42,8 +42,16 @@ export class AddonCompetencyCourseCompetenciesPage implements OnInit { * View loaded. */ ngOnInit(): void { - this.courseId = CoreNavigator.getRouteNumberParam('courseId')!; - this.userId = CoreNavigator.getRouteNumberParam('userId')!; + try { + this.courseId = CoreNavigator.getRequiredRouteNumberParam('courseId'); + this.userId = CoreNavigator.getRequiredRouteNumberParam('userId'); + } catch (error) { + CoreDomUtils.showErrorModal(error); + + CoreNavigator.back(); + + return; + } this.fetchCourseCompetencies().finally(() => { this.competenciesLoaded = true; diff --git a/src/addons/competency/pages/plan/plan.ts b/src/addons/competency/pages/plan/plan.ts index 71fedb68a..bf86a1a46 100644 --- a/src/addons/competency/pages/plan/plan.ts +++ b/src/addons/competency/pages/plan/plan.ts @@ -39,7 +39,15 @@ export class AddonCompetencyPlanPage implements OnInit { * @inheritdoc */ ngOnInit(): void { - this.planId = CoreNavigator.getRouteNumberParam('planId')!; + try { + this.planId = CoreNavigator.getRequiredRouteNumberParam('planId'); + } catch (error) { + CoreDomUtils.showErrorModal(error); + + CoreNavigator.back(); + + return; + } this.fetchLearningPlan().finally(() => { this.loaded = true; diff --git a/src/addons/coursecompletion/pages/report/report.ts b/src/addons/coursecompletion/pages/report/report.ts index 5cda867eb..027d0480e 100644 --- a/src/addons/coursecompletion/pages/report/report.ts +++ b/src/addons/coursecompletion/pages/report/report.ts @@ -44,11 +44,15 @@ export class AddonCourseCompletionReportPage implements OnInit { * @inheritdoc */ ngOnInit(): void { - this.courseId = CoreNavigator.getRouteNumberParam('courseId')!; - this.userId = CoreNavigator.getRouteNumberParam('userId') || CoreSites.getCurrentSiteUserId(); + try { + this.courseId = CoreNavigator.getRequiredRouteNumberParam('courseId'); + this.userId = CoreNavigator.getRouteNumberParam('userId') || CoreSites.getCurrentSiteUserId(); + } catch (error) { + CoreDomUtils.showErrorModal(error); - if (!this.userId) { - this.userId = CoreSites.getCurrentSiteUserId(); + CoreNavigator.back(); + + return; } this.fetchCompletion().finally(() => { diff --git a/src/addons/mod/assign/pages/edit/edit.ts b/src/addons/mod/assign/pages/edit/edit.ts index 98884b13e..01ddb7499 100644 --- a/src/addons/mod/assign/pages/edit/edit.ts +++ b/src/addons/mod/assign/pages/edit/edit.ts @@ -78,9 +78,17 @@ export class AddonModAssignEditPage implements OnInit, OnDestroy, CanLeave { * Component being initialized. */ ngOnInit(): void { - this.moduleId = CoreNavigator.getRouteNumberParam('cmId')!; - this.courseId = CoreNavigator.getRouteNumberParam('courseId')!; - this.isBlind = !!CoreNavigator.getRouteNumberParam('blindId'); + try { + this.moduleId = CoreNavigator.getRequiredRouteNumberParam('cmId'); + this.courseId = CoreNavigator.getRequiredRouteNumberParam('courseId'); + this.isBlind = !!CoreNavigator.getRouteNumberParam('blindId'); + } catch (error) { + CoreDomUtils.showErrorModal(error); + + CoreNavigator.back(); + + return; + } this.fetchAssignment().finally(() => { this.loaded = true; diff --git a/src/addons/mod/assign/pages/submission-list/submission-list.page.ts b/src/addons/mod/assign/pages/submission-list/submission-list.page.ts index 86443ad08..90f02a382 100644 --- a/src/addons/mod/assign/pages/submission-list/submission-list.page.ts +++ b/src/addons/mod/assign/pages/submission-list/submission-list.page.ts @@ -120,10 +120,18 @@ export class AddonModAssignSubmissionListPage implements AfterViewInit, OnDestro * Component being initialized. */ ngAfterViewInit(): void { - this.moduleId = CoreNavigator.getRouteNumberParam('cmId')!; - this.courseId = CoreNavigator.getRouteNumberParam('courseId')!; - this.groupId = CoreNavigator.getRouteNumberParam('groupId') || 0; - this.selectedStatus = CoreNavigator.getRouteParam('status'); + try { + this.moduleId = CoreNavigator.getRequiredRouteNumberParam('cmId'); + this.courseId = CoreNavigator.getRequiredRouteNumberParam('courseId'); + this.groupId = CoreNavigator.getRouteNumberParam('groupId') || 0; + this.selectedStatus = CoreNavigator.getRouteParam('status'); + } catch (error) { + CoreDomUtils.showErrorModal(error); + + CoreNavigator.back(); + + return; + } if (this.selectedStatus) { if (this.selectedStatus == AddonModAssignProvider.NEED_GRADING) { diff --git a/src/addons/mod/assign/pages/submission-review/submission-review.ts b/src/addons/mod/assign/pages/submission-review/submission-review.ts index 831dea8b8..803074c9f 100644 --- a/src/addons/mod/assign/pages/submission-review/submission-review.ts +++ b/src/addons/mod/assign/pages/submission-review/submission-review.ts @@ -55,10 +55,18 @@ export class AddonModAssignSubmissionReviewPage implements OnInit, CanLeave { */ ngOnInit(): void { this.route.queryParams.subscribe((params) => { - this.moduleId = CoreNavigator.getRouteNumberParam('cmId')!; - this.courseId = CoreNavigator.getRouteNumberParam('courseId')!; - this.submitId = CoreNavigator.getRouteNumberParam('submitId') || 0; - this.blindId = CoreNavigator.getRouteNumberParam('blindId', { params }); + try { + this.moduleId = CoreNavigator.getRequiredRouteNumberParam('cmId'); + this.courseId = CoreNavigator.getRequiredRouteNumberParam('courseId'); + this.submitId = CoreNavigator.getRouteNumberParam('submitId') || 0; + this.blindId = CoreNavigator.getRouteNumberParam('blindId', { params }); + } catch (error) { + CoreDomUtils.showErrorModal(error); + + CoreNavigator.back(); + + return; + } this.fetchSubmission().finally(() => { this.loaded = true; diff --git a/src/addons/mod/chat/pages/chat/chat.ts b/src/addons/mod/chat/pages/chat/chat.ts index 76ffdeb6b..809d5106b 100644 --- a/src/addons/mod/chat/pages/chat/chat.ts +++ b/src/addons/mod/chat/pages/chat/chat.ts @@ -78,12 +78,12 @@ export class AddonModChatChatPage implements OnInit, OnDestroy, CanLeave { * @inheritdoc */ async ngOnInit(): Promise { - this.courseId = CoreNavigator.getRouteNumberParam('courseId')!; - this.cmId = CoreNavigator.getRouteNumberParam('cmId')!; - this.chatId = CoreNavigator.getRouteNumberParam('chatId')!; - this.title = CoreNavigator.getRouteParam('title') || ''; - try { + this.courseId = CoreNavigator.getRequiredRouteNumberParam('courseId'); + this.cmId = CoreNavigator.getRequiredRouteNumberParam('cmId'); + this.chatId = CoreNavigator.getRequiredRouteNumberParam('chatId'); + this.title = CoreNavigator.getRouteParam('title') || ''; + await this.loginUser(); await this.fetchMessages(); diff --git a/src/addons/mod/chat/pages/session-messages/session-messages.ts b/src/addons/mod/chat/pages/session-messages/session-messages.ts index 00b7e8a71..02d185a30 100644 --- a/src/addons/mod/chat/pages/session-messages/session-messages.ts +++ b/src/addons/mod/chat/pages/session-messages/session-messages.ts @@ -47,12 +47,20 @@ export class AddonModChatSessionMessagesPage implements OnInit { * @inheritdoc */ ngOnInit(): void { - this.courseId = CoreNavigator.getRouteNumberParam('courseId')!; - this.cmId = CoreNavigator.getRouteNumberParam('cmId')!; - this.sessionStart = CoreNavigator.getRouteNumberParam('sessionStart')!; - this.sessionEnd = CoreNavigator.getRouteNumberParam('sessionEnd')!; - this.chatId = CoreNavigator.getRouteNumberParam('chatId')!; - this.groupId = CoreNavigator.getRouteNumberParam('groupId') || 0; + try { + this.courseId = CoreNavigator.getRequiredRouteNumberParam('courseId'); + this.cmId = CoreNavigator.getRequiredRouteNumberParam('cmId'); + this.sessionStart = CoreNavigator.getRequiredRouteNumberParam('sessionStart'); + this.sessionEnd = CoreNavigator.getRequiredRouteNumberParam('sessionEnd'); + this.chatId = CoreNavigator.getRequiredRouteNumberParam('chatId'); + this.groupId = CoreNavigator.getRouteNumberParam('groupId') || 0; + } catch (error) { + CoreDomUtils.showErrorModal(error); + + CoreNavigator.back(); + + return; + } this.currentUserId = CoreSites.getCurrentSiteUserId(); diff --git a/src/addons/mod/chat/pages/sessions/sessions.ts b/src/addons/mod/chat/pages/sessions/sessions.ts index c579fcb35..c262d4d74 100644 --- a/src/addons/mod/chat/pages/sessions/sessions.ts +++ b/src/addons/mod/chat/pages/sessions/sessions.ts @@ -53,10 +53,18 @@ export class AddonModChatSessionsPage implements AfterViewInit, OnDestroy { * @inheritdoc */ async ngAfterViewInit(): Promise { - this.courseId = CoreNavigator.getRouteNumberParam('courseId')!; - this.cmId = CoreNavigator.getRouteNumberParam('cmId')!; - this.chatId = CoreNavigator.getRouteNumberParam('chatId')!; - this.sessions.setChatId(this.chatId); + try { + this.courseId = CoreNavigator.getRequiredRouteNumberParam('courseId'); + this.cmId = CoreNavigator.getRequiredRouteNumberParam('cmId'); + this.chatId = CoreNavigator.getRequiredRouteNumberParam('chatId'); + this.sessions.setChatId(this.chatId); + } catch (error) { + CoreDomUtils.showErrorModal(error); + + CoreNavigator.back(); + + return; + } await this.fetchSessions(); diff --git a/src/addons/mod/data/components/field-plugin/field-plugin.ts b/src/addons/mod/data/components/field-plugin/field-plugin.ts index 28d20115f..74ed1e710 100644 --- a/src/addons/mod/data/components/field-plugin/field-plugin.ts +++ b/src/addons/mod/data/components/field-plugin/field-plugin.ts @@ -53,7 +53,7 @@ export class AddonModDataFieldPluginComponent implements OnInit, OnChanges { return; } - try{ + try { // Check if the plugin has defined its own component to render itself. this.fieldComponent = await AddonModDataFieldsDelegate.getComponentForField(this.field); diff --git a/src/addons/mod/data/pages/edit/edit.ts b/src/addons/mod/data/pages/edit/edit.ts index 2b1786d41..5af31f6ed 100644 --- a/src/addons/mod/data/pages/edit/edit.ts +++ b/src/addons/mod/data/pages/edit/edit.ts @@ -96,10 +96,18 @@ export class AddonModDataEditPage implements OnInit { * @inheritdoc */ ngOnInit(): void { - this.module = CoreNavigator.getRouteParam('module')!; - this.entryId = CoreNavigator.getRouteNumberParam('entryId') || undefined; - this.courseId = CoreNavigator.getRouteNumberParam('courseId')!; - this.selectedGroup = CoreNavigator.getRouteNumberParam('group') || 0; + try { + this.module = CoreNavigator.getRequiredRouteParam('module'); + this.entryId = CoreNavigator.getRouteNumberParam('entryId') || undefined; + this.courseId = CoreNavigator.getRequiredRouteNumberParam('courseId'); + this.selectedGroup = CoreNavigator.getRouteNumberParam('group') || 0; + } catch (error) { + CoreDomUtils.showErrorModal(error); + + CoreNavigator.back(); + + return; + } // If entryId is lower than 0 or null, it is a new entry or an offline entry. this.isEditing = typeof this.entryId != 'undefined' && this.entryId > 0; diff --git a/src/addons/mod/data/pages/entry/entry.ts b/src/addons/mod/data/pages/entry/entry.ts index c9c813307..95e37b326 100644 --- a/src/addons/mod/data/pages/entry/entry.ts +++ b/src/addons/mod/data/pages/entry/entry.ts @@ -132,11 +132,20 @@ export class AddonModDataEntryPage implements OnInit, OnDestroy { * @inheritdoc */ async ngOnInit(): Promise { - this.module = CoreNavigator.getRouteParam('module')!; - this.entryId = CoreNavigator.getRouteNumberParam('entryId') || undefined; - this.courseId = CoreNavigator.getRouteNumberParam('courseId')!; - this.selectedGroup = CoreNavigator.getRouteNumberParam('group') || 0; - this.offset = CoreNavigator.getRouteNumberParam('offset'); + try { + this.module = CoreNavigator.getRequiredRouteParam('module'); + this.entryId = CoreNavigator.getRouteNumberParam('entryId') || undefined; + this.courseId = CoreNavigator.getRequiredRouteNumberParam('courseId'); + this.selectedGroup = CoreNavigator.getRouteNumberParam('group') || 0; + this.offset = CoreNavigator.getRouteNumberParam('offset'); + } catch (error) { + CoreDomUtils.showErrorModal(error); + + CoreNavigator.back(); + + return; + } + this.title = this.module.name; this.commentsEnabled = !CoreComments.areCommentsDisabledInSite(); diff --git a/src/addons/mod/feedback/pages/attempt/attempt.ts b/src/addons/mod/feedback/pages/attempt/attempt.ts index 52e9736fe..194f22f06 100644 --- a/src/addons/mod/feedback/pages/attempt/attempt.ts +++ b/src/addons/mod/feedback/pages/attempt/attempt.ts @@ -49,9 +49,17 @@ export class AddonModFeedbackAttemptPage implements OnInit { * @inheritdoc */ ngOnInit(): void { - this.cmId = CoreNavigator.getRouteNumberParam('cmId')!; - this.courseId = CoreNavigator.getRouteNumberParam('courseId')!; - this.attemptId = CoreNavigator.getRouteNumberParam('attemptId')!; + try { + this.cmId = CoreNavigator.getRequiredRouteNumberParam('cmId'); + this.courseId = CoreNavigator.getRequiredRouteNumberParam('courseId'); + this.attemptId = CoreNavigator.getRequiredRouteNumberParam('attemptId'); + } catch (error) { + CoreDomUtils.showErrorModal(error); + + CoreNavigator.back(); + + return; + } this.fetchData(); } diff --git a/src/addons/mod/feedback/pages/form/form.ts b/src/addons/mod/feedback/pages/form/form.ts index d88967c29..1a11ebb21 100644 --- a/src/addons/mod/feedback/pages/form/form.ts +++ b/src/addons/mod/feedback/pages/form/form.ts @@ -92,12 +92,20 @@ export class AddonModFeedbackFormPage implements OnInit, OnDestroy, CanLeave { * @inheritdoc */ async ngOnInit(): Promise { - this.cmId = CoreNavigator.getRouteNumberParam('cmId')!; - this.courseId = CoreNavigator.getRouteNumberParam('courseId')!; - this.currentPage = CoreNavigator.getRouteNumberParam('page'); - this.title = CoreNavigator.getRouteParam('title'); - this.preview = !!CoreNavigator.getRouteBooleanParam('preview'); - this.fromIndex = !!CoreNavigator.getRouteBooleanParam('fromIndex'); + try { + this.cmId = CoreNavigator.getRequiredRouteNumberParam('cmId'); + this.courseId = CoreNavigator.getRequiredRouteNumberParam('courseId'); + this.currentPage = CoreNavigator.getRouteNumberParam('page'); + this.title = CoreNavigator.getRouteParam('title'); + this.preview = !!CoreNavigator.getRouteBooleanParam('preview'); + this.fromIndex = !!CoreNavigator.getRouteBooleanParam('fromIndex'); + } catch (error) { + CoreDomUtils.showErrorModal(error); + + CoreNavigator.back(); + + return; + } await this.fetchData(); diff --git a/src/addons/mod/feedback/pages/nonrespondents/nonrespondents.ts b/src/addons/mod/feedback/pages/nonrespondents/nonrespondents.ts index d95111480..12d312bc0 100644 --- a/src/addons/mod/feedback/pages/nonrespondents/nonrespondents.ts +++ b/src/addons/mod/feedback/pages/nonrespondents/nonrespondents.ts @@ -47,9 +47,17 @@ export class AddonModFeedbackNonRespondentsPage implements OnInit { * @inheritdoc */ ngOnInit(): void { - this.cmId = CoreNavigator.getRouteNumberParam('cmId')!; - this.courseId = CoreNavigator.getRouteNumberParam('courseId')!; - this.selectedGroup = CoreNavigator.getRouteNumberParam('group') || 0; + try { + this.cmId = CoreNavigator.getRequiredRouteNumberParam('cmId'); + this.courseId = CoreNavigator.getRequiredRouteNumberParam('courseId'); + this.selectedGroup = CoreNavigator.getRouteNumberParam('group') || 0; + } catch (error) { + CoreDomUtils.showErrorModal(error); + + CoreNavigator.back(); + + return; + } this.fetchData(); } diff --git a/src/addons/mod/feedback/pages/respondents/respondents.ts b/src/addons/mod/feedback/pages/respondents/respondents.ts index 8539b2cb2..3ef2266a7 100644 --- a/src/addons/mod/feedback/pages/respondents/respondents.ts +++ b/src/addons/mod/feedback/pages/respondents/respondents.ts @@ -63,9 +63,17 @@ export class AddonModFeedbackRespondentsPage implements AfterViewInit { * @inheritdoc */ async ngAfterViewInit(): Promise { - this.cmId = CoreNavigator.getRouteNumberParam('cmId')!; - this.courseId = CoreNavigator.getRouteNumberParam('courseId')!; - this.selectedGroup = CoreNavigator.getRouteNumberParam('group') || 0; + try { + this.cmId = CoreNavigator.getRequiredRouteNumberParam('cmId'); + this.courseId = CoreNavigator.getRequiredRouteNumberParam('courseId'); + this.selectedGroup = CoreNavigator.getRouteNumberParam('group') || 0; + } catch (error) { + CoreDomUtils.showErrorModal(error); + + CoreNavigator.back(); + + return; + } await this.fetchData(); diff --git a/src/addons/mod/forum/pages/discussion/discussion.page.ts b/src/addons/mod/forum/pages/discussion/discussion.page.ts index 34522f5b6..3894c38c4 100644 --- a/src/addons/mod/forum/pages/discussion/discussion.page.ts +++ b/src/addons/mod/forum/pages/discussion/discussion.page.ts @@ -65,7 +65,7 @@ export class AddonModForumDiscussionPage implements OnInit, AfterViewInit, OnDes discussionId!: number; forum: Partial = {}; accessInfo: AddonModForumAccessInformation = {}; - discussion!: AddonModForumDiscussion; + discussion?: AddonModForumDiscussion; startingPost?: Post; posts!: Post[]; discussionLoaded = false; @@ -100,8 +100,8 @@ export class AddonModForumDiscussionPage implements OnInit, AfterViewInit, OnDes leavingPage = false; protected forumId!: number; - protected postId!: number; - protected parent!: number; + protected postId?: number; + protected parent?: number; protected onlineObserver?: Subscription; protected syncObserver?: CoreEventObserver; protected syncManualObserver?: CoreEventObserver; @@ -122,16 +122,25 @@ export class AddonModForumDiscussionPage implements OnInit, AfterViewInit, OnDes } ngOnInit(): void { - this.courseId = CoreNavigator.getRouteNumberParam('courseId')!; - this.cmId = CoreNavigator.getRouteNumberParam('cmId')!; - this.forumId = CoreNavigator.getRouteNumberParam('forumId')!; - this.discussion = CoreNavigator.getRouteParam('discussion')!; - this.discussionId = this.discussion - ? this.discussion.discussion - : CoreNavigator.getRouteNumberParam('discussionId')!; - this.trackPosts = CoreNavigator.getRouteBooleanParam('trackPosts')!; - this.postId = CoreNavigator.getRouteNumberParam('postId')!; - this.parent = CoreNavigator.getRouteNumberParam('parent')!; + try { + this.courseId = CoreNavigator.getRequiredRouteNumberParam('courseId'); + this.cmId = CoreNavigator.getRequiredRouteNumberParam('cmId'); + this.forumId = CoreNavigator.getRequiredRouteNumberParam('forumId'); + this.discussion = CoreNavigator.getRouteParam('discussion'); + this.discussionId = this.discussion + ? this.discussion.discussion + : CoreNavigator.getRequiredRouteNumberParam('discussionId'); + this.trackPosts = CoreNavigator.getRouteBooleanParam('trackPosts') || false; + this.postId = CoreNavigator.getRouteNumberParam('postId'); + this.parent = CoreNavigator.getRouteNumberParam('parent'); + + } catch (error) { + CoreDomUtils.showErrorModal(error); + + this.goBack(); + + return; + } this.isOnline = CoreApp.isOnline(); this.onlineObserver = Network.onChange().subscribe(() => { @@ -148,11 +157,9 @@ export class AddonModForumDiscussionPage implements OnInit, AfterViewInit, OnDes * View loaded. */ async ngAfterViewInit(): Promise { - if (this.parent) { - this.sort = 'nested'; // Force nested order. - } else { - this.sort = await this.getUserSort(); - } + this.sort = this.parent + ? 'nested' // Force nested order. + : await this.getUserSort(); await this.fetchPosts(true, false, true); @@ -183,6 +190,16 @@ export class AddonModForumDiscussionPage implements OnInit, AfterViewInit, OnDes await this.loadDiscussion(this.forumId, this.cmId, this.discussionId); } + if (!this.discussion) { + CoreDomUtils.showErrorModal('Cannot get the discussion'); + + this.goBack(); + + return; + } + + const discussion = this.discussion; + // Refresh data if this discussion is synchronized automatically. this.syncObserver = CoreEvents.on(AddonModForumSyncProvider.AUTO_SYNCED, data => { if (data.forumId == this.forumId && this.discussionId == data.discussionId @@ -204,7 +221,7 @@ export class AddonModForumDiscussionPage implements OnInit, AfterViewInit, OnDes }, CoreSites.getCurrentSiteId()); // Invalidate discussion list if it was not read. - if (this.discussion.numunread > 0) { + if (discussion.numunread > 0) { AddonModForum.invalidateDiscussionsList(this.forumId); } @@ -227,22 +244,18 @@ export class AddonModForumDiscussionPage implements OnInit, AfterViewInit, OnDes if ((this.forumId && this.forumId === data.forumId) || data.cmId === this.cmId) { AddonModForum.invalidateDiscussionsList(this.forumId).finally(() => { if (typeof data.locked != 'undefined') { - this.discussion.locked = data.locked; + discussion.locked = data.locked; } if (typeof data.pinned != 'undefined') { - this.discussion.pinned = data.pinned; + discussion.pinned = data.pinned; } if (typeof data.starred != 'undefined') { - this.discussion.starred = data.starred; + discussion.starred = data.starred; } if (typeof data.deleted != 'undefined' && data.deleted) { if (!data.post?.parentid) { - if (this.splitView?.outletActivated) { - CoreNavigator.navigate('../'); - } else { - CoreNavigator.back(); - } + this.goBack(); } else { this.discussionLoaded = false; this.refreshPosts(); @@ -272,6 +285,17 @@ export class AddonModForumDiscussionPage implements OnInit, AfterViewInit, OnDes return true; } + /** + * Helper function to go back. + */ + protected goBack(): void { + if (this.splitView?.outletActivated) { + CoreNavigator.navigate('../'); + } else { + CoreNavigator.back(); + } + } + /** * Runs when the page is about to leave and no longer be the active page. */ @@ -515,7 +539,7 @@ export class AddonModForumDiscussionPage implements OnInit, AfterViewInit, OnDes this.syncIcon = CoreConstants.ICON_SYNC; if (forceMarkAsRead || (hasUnreadPosts && this.trackPosts)) { - // // Add log in Moodle and mark unread posts as readed. + // Add log in Moodle and mark unread posts as readed. AddonModForum.logDiscussionView(this.discussionId, this.forumId || -1, this.forum.name).catch(() => { // Ignore errors. }).finally(() => { @@ -543,14 +567,8 @@ export class AddonModForumDiscussionPage implements OnInit, AfterViewInit, OnDes return; } - try { - const discussion = await AddonModForumHelper.getDiscussionById(forumId, cmId, discussionId); - - this.discussion = discussion; - this.discussionId = this.discussion.discussion; - } catch (error) { - // Ignore errors. - } + this.discussion = await AddonModForumHelper.getDiscussionById(forumId, cmId, discussionId); + this.discussionId = this.discussion.discussion; } /** @@ -630,7 +648,7 @@ export class AddonModForumDiscussionPage implements OnInit, AfterViewInit, OnDes * @param showErrors Whether to show errors in a modal. * @return Promise resolved when done. */ - refreshPosts(sync?: boolean, showErrors?: boolean): Promise { + async refreshPosts(sync?: boolean, showErrors?: boolean): Promise { this.content.scrollToTop(); this.refreshIcon = CoreConstants.ICON_LOADING; this.syncIcon = CoreConstants.ICON_LOADING; @@ -642,9 +660,9 @@ export class AddonModForumDiscussionPage implements OnInit, AfterViewInit, OnDes AddonModForum.invalidateCanAddDiscussion(this.forumId), ]; - return CoreUtils.allPromises(promises).catch(() => { - // Ignore errors. - }).then(() => this.fetchPosts(sync, showErrors)); + await CoreUtils.ignoreErrors(CoreUtils.allPromises(promises)); + + await this.fetchPosts(sync, showErrors); } /** @@ -668,6 +686,10 @@ export class AddonModForumDiscussionPage implements OnInit, AfterViewInit, OnDes * @param locked True to lock the discussion, false to unlock. */ async setLockState(locked: boolean): Promise { + if (!this.discussion) { + return; + } + const modal = await CoreDomUtils.showModalLoading('core.sending', true); try { @@ -696,6 +718,10 @@ export class AddonModForumDiscussionPage implements OnInit, AfterViewInit, OnDes * @param pinned True to pin the discussion, false to unpin it. */ async setPinState(pinned: boolean): Promise { + if (!this.discussion) { + return; + } + const modal = await CoreDomUtils.showModalLoading('core.sending', true); try { @@ -725,6 +751,10 @@ export class AddonModForumDiscussionPage implements OnInit, AfterViewInit, OnDes * @param starred True to star the discussion, false to unstar it. */ async toggleFavouriteState(starred: boolean): Promise { + if (!this.discussion) { + return; + } + const modal = await CoreDomUtils.showModalLoading('core.sending', true); try { diff --git a/src/addons/mod/forum/pages/new-discussion/new-discussion.page.ts b/src/addons/mod/forum/pages/new-discussion/new-discussion.page.ts index 52995909a..f5495fb71 100644 --- a/src/addons/mod/forum/pages/new-discussion/new-discussion.page.ts +++ b/src/addons/mod/forum/pages/new-discussion/new-discussion.page.ts @@ -103,10 +103,18 @@ export class AddonModForumNewDiscussionPage implements OnInit, OnDestroy, CanLea * Component being initialized. */ ngOnInit(): void { - this.courseId = CoreNavigator.getRouteNumberParam('courseId')!; - this.cmId = CoreNavigator.getRouteNumberParam('cmId')!; - this.forumId = CoreNavigator.getRouteNumberParam('forumId')!; - this.timeCreated = CoreNavigator.getRouteNumberParam('timeCreated')!; + try { + this.courseId = CoreNavigator.getRequiredRouteNumberParam('courseId'); + this.cmId = CoreNavigator.getRequiredRouteNumberParam('cmId'); + this.forumId = CoreNavigator.getRequiredRouteNumberParam('forumId'); + this.timeCreated = CoreNavigator.getRequiredRouteNumberParam('timeCreated'); + } catch (error) { + CoreDomUtils.showErrorModal(error); + + this.goBack(); + + return; + } this.fetchDiscussionData().finally(() => { this.groupsLoaded = true; @@ -595,6 +603,17 @@ export class AddonModForumNewDiscussionPage implements OnInit, OnDestroy, CanLea delete this.syncObserver; } + /** + * Helper function to go back. + */ + protected goBack(): void { + if (this.splitView?.outletActivated) { + CoreNavigator.navigate('../../'); + } else { + CoreNavigator.back(); + } + } + /** * Page destroyed. */ diff --git a/src/addons/mod/glossary/pages/edit/edit.ts b/src/addons/mod/glossary/pages/edit/edit.ts index 854c74247..33f384fc3 100644 --- a/src/addons/mod/glossary/pages/edit/edit.ts +++ b/src/addons/mod/glossary/pages/edit/edit.ts @@ -73,7 +73,7 @@ export class AddonModGlossaryEditPage implements OnInit, CanLeave { }; protected timecreated!: number; - protected concept?: string; + protected concept = ''; protected syncId?: string; protected syncObserver?: CoreEventObserver; protected isDestroyed = false; @@ -86,11 +86,19 @@ export class AddonModGlossaryEditPage implements OnInit, CanLeave { * Component being initialized. */ ngOnInit(): void { - this.cmId = CoreNavigator.getRouteNumberParam('cmId')!; - this.courseId = CoreNavigator.getRouteNumberParam('courseId')!; - this.timecreated = CoreNavigator.getRouteNumberParam('timecreated')!; - this.concept = CoreNavigator.getRouteParam('concept')!; - this.editorExtraParams.timecreated = this.timecreated; + try { + this.cmId = CoreNavigator.getRequiredRouteNumberParam('cmId'); + this.courseId = CoreNavigator.getRequiredRouteNumberParam('courseId'); + this.timecreated = CoreNavigator.getRequiredRouteNumberParam('timecreated'); + this.concept = CoreNavigator.getRouteParam('concept') || ''; + this.editorExtraParams.timecreated = this.timecreated; + } catch (error) { + CoreDomUtils.showErrorModal(error); + + this.goBack(); + + return; + } this.fetchData(); } @@ -116,7 +124,7 @@ export class AddonModGlossaryEditPage implements OnInit, CanLeave { } catch (error) { CoreDomUtils.showErrorModalDefault(error, 'addon.mod_glossary.errorloadingglossary', true); - CoreNavigator.back(); + this.goBack(); } } @@ -126,7 +134,7 @@ export class AddonModGlossaryEditPage implements OnInit, CanLeave { * @return Promise resolved when done. */ protected async loadOfflineData(): Promise { - const entry = await AddonModGlossaryOffline.getNewEntry(this.glossary!.id, this.concept || '', this.timecreated); + const entry = await AddonModGlossaryOffline.getNewEntry(this.glossary!.id, this.concept, this.timecreated); this.entry.concept = entry.concept || ''; this.entry.definition = entry.definition || ''; @@ -367,4 +375,15 @@ export class AddonModGlossaryEditPage implements OnInit, CanLeave { } } + /** + * Helper function to go back. + */ + protected goBack(): void { + if (this.splitView?.outletActivated) { + CoreNavigator.navigate('../../'); + } else { + CoreNavigator.back(); + } + } + } diff --git a/src/addons/mod/glossary/pages/entry/entry.ts b/src/addons/mod/glossary/pages/entry/entry.ts index 5db347e85..ae3c836e4 100644 --- a/src/addons/mod/glossary/pages/entry/entry.ts +++ b/src/addons/mod/glossary/pages/entry/entry.ts @@ -57,10 +57,18 @@ export class AddonModGlossaryEntryPage implements OnInit { * @inheritdoc */ async ngOnInit(): Promise { - this.courseId = CoreNavigator.getRouteNumberParam('courseId')!; - this.entryId = CoreNavigator.getRouteNumberParam('entryId')!; - this.tagsEnabled = CoreTag.areTagsAvailableInSite(); - this.commentsEnabled = !CoreComments.areCommentsDisabledInSite(); + try { + this.courseId = CoreNavigator.getRequiredRouteNumberParam('courseId'); + this.entryId = CoreNavigator.getRequiredRouteNumberParam('entryId'); + this.tagsEnabled = CoreTag.areTagsAvailableInSite(); + this.commentsEnabled = !CoreComments.areCommentsDisabledInSite(); + } catch (error) { + CoreDomUtils.showErrorModal(error); + + CoreNavigator.back(); + + return; + } try { await this.fetchEntry(); diff --git a/src/addons/mod/h5pactivity/pages/attempt-results/attempt-results.ts b/src/addons/mod/h5pactivity/pages/attempt-results/attempt-results.ts index 3f3d79731..113d66430 100644 --- a/src/addons/mod/h5pactivity/pages/attempt-results/attempt-results.ts +++ b/src/addons/mod/h5pactivity/pages/attempt-results/attempt-results.ts @@ -50,9 +50,17 @@ export class AddonModH5PActivityAttemptResultsPage implements OnInit { * @inheritdoc */ async ngOnInit(): Promise { - this.courseId = CoreNavigator.getRouteNumberParam('courseId')!; - this.cmId = CoreNavigator.getRouteNumberParam('cmId')!; - this.attemptId = CoreNavigator.getRouteNumberParam('attemptId')!; + try { + this.courseId = CoreNavigator.getRequiredRouteNumberParam('courseId'); + this.cmId = CoreNavigator.getRequiredRouteNumberParam('cmId'); + this.attemptId = CoreNavigator.getRequiredRouteNumberParam('attemptId'); + } catch (error) { + CoreDomUtils.showErrorModal(error); + + CoreNavigator.back(); + + return; + } try { await this.fetchData(); diff --git a/src/addons/mod/h5pactivity/pages/user-attempts/user-attempts.ts b/src/addons/mod/h5pactivity/pages/user-attempts/user-attempts.ts index 5f09b4c02..cda950b21 100644 --- a/src/addons/mod/h5pactivity/pages/user-attempts/user-attempts.ts +++ b/src/addons/mod/h5pactivity/pages/user-attempts/user-attempts.ts @@ -51,9 +51,18 @@ export class AddonModH5PActivityUserAttemptsPage implements OnInit { * @inheritdoc */ async ngOnInit(): Promise { - this.courseId = CoreNavigator.getRouteNumberParam('courseId')!; - this.cmId = CoreNavigator.getRouteNumberParam('cmId')!; - this.userId = CoreNavigator.getRouteNumberParam('userId') || CoreSites.getCurrentSiteUserId(); + try { + this.courseId = CoreNavigator.getRequiredRouteNumberParam('courseId'); + this.cmId = CoreNavigator.getRequiredRouteNumberParam('cmId'); + this.userId = CoreNavigator.getRouteNumberParam('userId') || CoreSites.getCurrentSiteUserId(); + } catch (error) { + CoreDomUtils.showErrorModal(error); + + CoreNavigator.back(); + + return; + } + this.isCurrentUser = this.userId == CoreSites.getCurrentSiteUserId(); try { diff --git a/src/addons/mod/h5pactivity/pages/users-attempts/users-attempts.ts b/src/addons/mod/h5pactivity/pages/users-attempts/users-attempts.ts index 7dee400fe..65d0f2510 100644 --- a/src/addons/mod/h5pactivity/pages/users-attempts/users-attempts.ts +++ b/src/addons/mod/h5pactivity/pages/users-attempts/users-attempts.ts @@ -49,8 +49,16 @@ export class AddonModH5PActivityUsersAttemptsPage implements OnInit { * @inheritdoc */ async ngOnInit(): Promise { - this.courseId = CoreNavigator.getRouteNumberParam('courseId')!; - this.cmId = CoreNavigator.getRouteNumberParam('cmId')!; + try { + this.courseId = CoreNavigator.getRequiredRouteNumberParam('courseId'); + this.cmId = CoreNavigator.getRequiredRouteNumberParam('cmId'); + } catch (error) { + CoreDomUtils.showErrorModal(error); + + CoreNavigator.back(); + + return; + } try { await this.fetchData(); diff --git a/src/addons/mod/lesson/pages/player/player.page.ts b/src/addons/mod/lesson/pages/player/player.page.ts index 9d5d7db72..e0c4f251d 100644 --- a/src/addons/mod/lesson/pages/player/player.page.ts +++ b/src/addons/mod/lesson/pages/player/player.page.ts @@ -119,12 +119,20 @@ export class AddonModLessonPlayerPage implements OnInit, OnDestroy, CanLeave { * Component being initialized. */ async ngOnInit(): Promise { - this.cmId = CoreNavigator.getRouteNumberParam('cmId')!; - this.courseId = CoreNavigator.getRouteNumberParam('courseId')!; - this.password = CoreNavigator.getRouteParam('password'); - this.review = !!CoreNavigator.getRouteBooleanParam('review'); - this.currentPage = CoreNavigator.getRouteNumberParam('pageId'); - this.retakeToReview = CoreNavigator.getRouteNumberParam('retake'); + try { + this.cmId = CoreNavigator.getRequiredRouteNumberParam('cmId'); + this.courseId = CoreNavigator.getRequiredRouteNumberParam('courseId'); + this.password = CoreNavigator.getRouteParam('password'); + this.review = !!CoreNavigator.getRouteBooleanParam('review'); + this.currentPage = CoreNavigator.getRouteNumberParam('pageId'); + this.retakeToReview = CoreNavigator.getRouteNumberParam('retake'); + } catch (error) { + CoreDomUtils.showErrorModal(error); + + CoreNavigator.back(); + + return; + } try { // Fetch the Lesson data. diff --git a/src/addons/mod/lesson/pages/user-retake/user-retake.page.ts b/src/addons/mod/lesson/pages/user-retake/user-retake.page.ts index 58c826758..634e08740 100644 --- a/src/addons/mod/lesson/pages/user-retake/user-retake.page.ts +++ b/src/addons/mod/lesson/pages/user-retake/user-retake.page.ts @@ -64,10 +64,18 @@ export class AddonModLessonUserRetakePage implements OnInit { * Component being initialized. */ ngOnInit(): void { - this.cmId = CoreNavigator.getRouteNumberParam('cmId')!; - this.courseId = CoreNavigator.getRouteNumberParam('courseId')!; - this.userId = CoreNavigator.getRouteNumberParam('userId') || CoreSites.getCurrentSiteUserId(); - this.retakeNumber = CoreNavigator.getRouteNumberParam('retake'); + try { + this.cmId = CoreNavigator.getRequiredRouteNumberParam('cmId'); + this.courseId = CoreNavigator.getRequiredRouteNumberParam('courseId'); + this.userId = CoreNavigator.getRouteNumberParam('userId') || CoreSites.getCurrentSiteUserId(); + this.retakeNumber = CoreNavigator.getRouteNumberParam('retake'); + } catch (error) { + CoreDomUtils.showErrorModal(error); + + CoreNavigator.back(); + + return; + } // Fetch the data. this.fetchData().finally(() => { diff --git a/src/addons/mod/quiz/pages/attempt/attempt.page.ts b/src/addons/mod/quiz/pages/attempt/attempt.page.ts index 74fa31df3..85481178d 100644 --- a/src/addons/mod/quiz/pages/attempt/attempt.page.ts +++ b/src/addons/mod/quiz/pages/attempt/attempt.page.ts @@ -52,9 +52,17 @@ export class AddonModQuizAttemptPage implements OnInit { * Component being initialized. */ ngOnInit(): void { - this.cmId = CoreNavigator.getRouteNumberParam('cmId')!; - this.courseId = CoreNavigator.getRouteNumberParam('courseId')!; - this.attemptId = CoreNavigator.getRouteNumberParam('attemptId')!; + try { + this.cmId = CoreNavigator.getRequiredRouteNumberParam('cmId'); + this.courseId = CoreNavigator.getRequiredRouteNumberParam('courseId'); + this.attemptId = CoreNavigator.getRequiredRouteNumberParam('attemptId'); + } catch (error) { + CoreDomUtils.showErrorModal(error); + + CoreNavigator.back(); + + return; + } this.fetchQuizData().finally(() => { this.loaded = true; diff --git a/src/addons/mod/quiz/pages/player/player.page.ts b/src/addons/mod/quiz/pages/player/player.page.ts index 27ea916c6..c2da33f1b 100644 --- a/src/addons/mod/quiz/pages/player/player.page.ts +++ b/src/addons/mod/quiz/pages/player/player.page.ts @@ -106,9 +106,17 @@ export class AddonModQuizPlayerPage implements OnInit, OnDestroy, CanLeave { * Component being initialized. */ ngOnInit(): void { - this.cmId = CoreNavigator.getRouteNumberParam('cmId')!; - this.courseId = CoreNavigator.getRouteNumberParam('courseId')!; - this.moduleUrl = CoreNavigator.getRouteParam('moduleUrl'); + try { + this.cmId = CoreNavigator.getRequiredRouteNumberParam('cmId'); + this.courseId = CoreNavigator.getRequiredRouteNumberParam('courseId'); + this.moduleUrl = CoreNavigator.getRouteParam('moduleUrl'); + } catch (error) { + CoreDomUtils.showErrorModal(error); + + CoreNavigator.back(); + + return; + } // Create the auto save instance. this.autoSave = new AddonModQuizAutoSave( diff --git a/src/addons/mod/quiz/pages/review/review.page.ts b/src/addons/mod/quiz/pages/review/review.page.ts index bc7ba288a..98d01d3da 100644 --- a/src/addons/mod/quiz/pages/review/review.page.ts +++ b/src/addons/mod/quiz/pages/review/review.page.ts @@ -83,11 +83,19 @@ export class AddonModQuizReviewPage implements OnInit { * Component being initialized. */ async ngOnInit(): Promise { - this.cmId = CoreNavigator.getRouteNumberParam('cmId')!; - this.courseId = CoreNavigator.getRouteNumberParam('courseId')!; - this.attemptId = CoreNavigator.getRouteNumberParam('attemptId')!; - this.currentPage = CoreNavigator.getRouteNumberParam('page') || -1; - this.showAll = this.currentPage == -1; + try { + this.cmId = CoreNavigator.getRequiredRouteNumberParam('cmId'); + this.courseId = CoreNavigator.getRequiredRouteNumberParam('courseId'); + this.attemptId = CoreNavigator.getRequiredRouteNumberParam('attemptId'); + this.currentPage = CoreNavigator.getRouteNumberParam('page') || -1; + this.showAll = this.currentPage == -1; + } catch (error) { + CoreDomUtils.showErrorModal(error); + + CoreNavigator.back(); + + return; + } try { await this.fetchData(); diff --git a/src/addons/mod/scorm/pages/player/player.ts b/src/addons/mod/scorm/pages/player/player.ts index c24a3d9fd..74bf853c9 100644 --- a/src/addons/mod/scorm/pages/player/player.ts +++ b/src/addons/mod/scorm/pages/player/player.ts @@ -88,14 +88,22 @@ export class AddonModScormPlayerPage implements OnInit, OnDestroy { * @inheritdoc */ async ngOnInit(): Promise { - this.cmId = CoreNavigator.getRouteNumberParam('cmId')!; - this.courseId = CoreNavigator.getRouteNumberParam('courseId')!; - this.mode = CoreNavigator.getRouteParam('mode') || AddonModScormProvider.MODENORMAL; - this.moduleUrl = CoreNavigator.getRouteParam('moduleUrl') || ''; - this.newAttempt = !!CoreNavigator.getRouteBooleanParam('newAttempt'); - this.organizationId = CoreNavigator.getRouteParam('organizationId'); - this.initialScoId = CoreNavigator.getRouteNumberParam('scoId'); - this.siteId = CoreSites.getCurrentSiteId(); + try { + this.cmId = CoreNavigator.getRequiredRouteNumberParam('cmId'); + this.courseId = CoreNavigator.getRequiredRouteNumberParam('courseId'); + this.mode = CoreNavigator.getRouteParam('mode') || AddonModScormProvider.MODENORMAL; + this.moduleUrl = CoreNavigator.getRouteParam('moduleUrl') || ''; + this.newAttempt = !!CoreNavigator.getRouteBooleanParam('newAttempt'); + this.organizationId = CoreNavigator.getRouteParam('organizationId'); + this.initialScoId = CoreNavigator.getRouteNumberParam('scoId'); + this.siteId = CoreSites.getCurrentSiteId(); + } catch (error) { + CoreDomUtils.showErrorModal(error); + + CoreNavigator.back(); + + return; + } try { // Fetch the SCORM data. diff --git a/src/addons/mod/workshop/pages/assessment/assessment.ts b/src/addons/mod/workshop/pages/assessment/assessment.ts index ea8b2ec52..0c074a4f9 100644 --- a/src/addons/mod/workshop/pages/assessment/assessment.ts +++ b/src/addons/mod/workshop/pages/assessment/assessment.ts @@ -117,10 +117,18 @@ export class AddonModWorkshopAssessmentPage implements OnInit, OnDestroy, CanLea * Component being initialized. */ ngOnInit(): void { - this.assessment = CoreNavigator.getRouteParam('assessment')!; - this.submission = CoreNavigator.getRouteParam('submission')!; - this.profile = CoreNavigator.getRouteParam('profile')!; - this.courseId = CoreNavigator.getRouteNumberParam('courseId')!; + try { + this.assessment = CoreNavigator.getRequiredRouteParam('assessment'); + this.submission = CoreNavigator.getRequiredRouteParam('submission'); + this.profile = CoreNavigator.getRequiredRouteParam('profile'); + this.courseId = CoreNavigator.getRequiredRouteNumberParam('courseId'); + } catch (error) { + CoreDomUtils.showErrorModal(error); + + CoreNavigator.back(); + + return; + } this.assessmentId = this.assessment.id; this.workshopId = this.submission.workshopid; diff --git a/src/addons/mod/workshop/pages/edit-submission/edit-submission.ts b/src/addons/mod/workshop/pages/edit-submission/edit-submission.ts index 3cc680eb7..c91033454 100644 --- a/src/addons/mod/workshop/pages/edit-submission/edit-submission.ts +++ b/src/addons/mod/workshop/pages/edit-submission/edit-submission.ts @@ -99,10 +99,18 @@ export class AddonModWorkshopEditSubmissionPage implements OnInit, OnDestroy, Ca * Component being initialized. */ ngOnInit(): void { - this.module = CoreNavigator.getRouteParam('module')!; - this.courseId = CoreNavigator.getRouteNumberParam('courseId')!; - this.access = CoreNavigator.getRouteParam('access')!; - this.submissionId = CoreNavigator.getRouteNumberParam('submissionId') || 0; + try { + this.module = CoreNavigator.getRequiredRouteParam('module'); + this.courseId = CoreNavigator.getRequiredRouteNumberParam('courseId'); + this.access = CoreNavigator.getRequiredRouteParam('access'); + this.submissionId = CoreNavigator.getRouteNumberParam('submissionId') || 0; + } catch (error) { + CoreDomUtils.showErrorModal(error); + + CoreNavigator.back(); + + return; + } if (this.submissionId > 0) { this.editorExtraParams.id = this.submissionId; diff --git a/src/addons/mod/workshop/pages/submission/submission.ts b/src/addons/mod/workshop/pages/submission/submission.ts index 349c00c28..dee18605e 100644 --- a/src/addons/mod/workshop/pages/submission/submission.ts +++ b/src/addons/mod/workshop/pages/submission/submission.ts @@ -130,15 +130,22 @@ export class AddonModWorkshopSubmissionPage implements OnInit, OnDestroy, CanLea * Component being initialized. */ async ngOnInit(): Promise { + try { + this.submissionId = CoreNavigator.getRequiredRouteNumberParam('submissionId'); + this.module = CoreNavigator.getRequiredRouteParam('module'); + this.workshop = CoreNavigator.getRequiredRouteParam('workshop'); + this.access = CoreNavigator.getRequiredRouteParam('access'); + this.courseId = CoreNavigator.getRequiredRouteNumberParam('courseId'); + this.profile = CoreNavigator.getRouteParam('profile'); + this.submissionInfo = CoreNavigator.getRequiredRouteParam('submission'); + this.assessment = CoreNavigator.getRouteParam('assessment'); + } catch (error) { + CoreDomUtils.showErrorModal(error); - this.submissionId = CoreNavigator.getRouteNumberParam('submissionId')!; - this.module = CoreNavigator.getRouteParam('module')!; - this.workshop = CoreNavigator.getRouteParam('workshop')!; - this.access = CoreNavigator.getRouteParam('access')!; - this.courseId = CoreNavigator.getRouteNumberParam('courseId')!; - this.profile = CoreNavigator.getRouteParam('profile'); - this.submissionInfo = CoreNavigator.getRouteParam('submission')!; - this.assessment = CoreNavigator.getRouteParam('assessment'); + CoreNavigator.back(); + + return; + } this.title = this.module.name; this.workshopId = this.module.instance || this.workshop.id; diff --git a/src/addons/notes/pages/list/list.page.ts b/src/addons/notes/pages/list/list.page.ts index 487b86f20..4480fc250 100644 --- a/src/addons/notes/pages/list/list.page.ts +++ b/src/addons/notes/pages/list/list.page.ts @@ -40,7 +40,7 @@ export class AddonNotesListPage implements OnInit, OnDestroy { @ViewChild(IonContent) content?: IonContent; - courseId: number; + courseId!: number; userId?: number; type: AddonNotesPublishState = 'course'; refreshIcon = CoreConstants.ICON_LOADING; @@ -51,13 +51,21 @@ export class AddonNotesListPage implements OnInit, OnDestroy { user?: CoreUserProfile; showDelete = false; canDeleteNotes = false; - currentUserId: number; + currentUserId!: number; - protected syncObserver: CoreEventObserver; + protected syncObserver!: CoreEventObserver; constructor() { - this.courseId = CoreNavigator.getRouteNumberParam('courseId')!; - this.userId = CoreNavigator.getRouteNumberParam('userId'); + try { + this.courseId = CoreNavigator.getRequiredRouteNumberParam('courseId'); + this.userId = CoreNavigator.getRouteNumberParam('userId'); + } catch (error) { + CoreDomUtils.showErrorModal(error); + + CoreNavigator.back(); + + return; + } // Refresh data if notes are synchronized automatically. this.syncObserver = CoreEvents.on(AddonNotesSyncProvider.AUTO_SYNCED, (data) => { diff --git a/src/addons/privatefiles/pages/index/index.ts b/src/addons/privatefiles/pages/index/index.ts index b7a6b0da4..098214023 100644 --- a/src/addons/privatefiles/pages/index/index.ts +++ b/src/addons/privatefiles/pages/index/index.ts @@ -69,19 +69,27 @@ export class AddonPrivateFilesIndexPage implements OnInit, OnDestroy { * Component being initialized. */ ngOnInit(): void { - this.root = CoreNavigator.getRouteParam('root'); - const contextId = CoreNavigator.getRouteNumberParam('contextid'); + try { + this.root = CoreNavigator.getRouteParam('root'); + const contextId = CoreNavigator.getRouteNumberParam('contextid'); - if (contextId) { - // Loading a certain folder. - this.path = { - contextid: contextId, - component: CoreNavigator.getRouteParam('component')!, - filearea: CoreNavigator.getRouteParam('filearea')!, - itemid: CoreNavigator.getRouteNumberParam('itemid')!, - filepath: CoreNavigator.getRouteParam('filepath')!, - filename: CoreNavigator.getRouteParam('filename')!, - }; + if (contextId) { + // Loading a certain folder. + this.path = { + contextid: contextId, + component: CoreNavigator.getRequiredRouteParam('component'), + filearea: CoreNavigator.getRequiredRouteParam('filearea'), + itemid: CoreNavigator.getRequiredRouteNumberParam('itemid'), + filepath: CoreNavigator.getRequiredRouteParam('filepath'), + filename: CoreNavigator.getRequiredRouteParam('filename'), + }; + } + } catch (error) { + CoreDomUtils.showErrorModal(error); + + CoreNavigator.back(); + + return; } this.title = this.path?.filename || Translate.instant('addon.privatefiles.files'); diff --git a/src/addons/storagemanager/pages/course-storage/course-storage.ts b/src/addons/storagemanager/pages/course-storage/course-storage.ts index 2e467f96a..752059a7a 100644 --- a/src/addons/storagemanager/pages/course-storage/course-storage.ts +++ b/src/addons/storagemanager/pages/course-storage/course-storage.ts @@ -42,7 +42,15 @@ export class AddonStorageManagerCourseStoragePage implements OnInit { * View loaded. */ async ngOnInit(): Promise { - this.course = CoreNavigator.getRouteParam('course')!; + try { + this.course = CoreNavigator.getRequiredRouteParam('course'); + } catch (error) { + CoreDomUtils.showErrorModal(error); + + CoreNavigator.back(); + + return; + } this.sections = await CoreCourse.getSections(this.course.id, false, true); CoreCourseHelper.addHandlerDataForModules(this.sections, this.course.id); diff --git a/src/core/components/split-view/split-view.ts b/src/core/components/split-view/split-view.ts index 1a50991a5..a69cc700c 100644 --- a/src/core/components/split-view/split-view.ts +++ b/src/core/components/split-view/split-view.ts @@ -66,14 +66,8 @@ export class CoreSplitViewComponent implements AfterViewInit, OnDestroy { this.disableScrollOnParent(); this.subscriptions = [ - this.contentOutlet.activateEvents.subscribe(() => { - this.updateClasses(); - this.outletRouteSubject.next(this.contentOutlet.activatedRoute.snapshot); - }), - this.contentOutlet.deactivateEvents.subscribe(() => { - this.updateClasses(); - this.outletRouteSubject.next(null); - }), + this.contentOutlet.activateEvents.subscribe(() => this.updateOutletRoute()), + this.contentOutlet.deactivateEvents.subscribe(() => this.updateOutletRoute()), CoreScreen.layoutObservable.subscribe(() => this.updateClasses()), ]; @@ -89,6 +83,17 @@ export class CoreSplitViewComponent implements AfterViewInit, OnDestroy { this.enableScrollOnParent(); } + /** + * Update outlet status. + */ + private updateOutletRoute(): void { + const outletRoute = this.contentOutlet.isActivated ? this.contentOutlet.activatedRoute.snapshot : null; + + this.updateClasses(); + + this.outletRouteSubject.next(outletRoute); + } + /** * Update host classes. */ diff --git a/src/core/features/comments/pages/viewer/viewer.page.ts b/src/core/features/comments/pages/viewer/viewer.page.ts index f0522caca..befbfd691 100644 --- a/src/core/features/comments/pages/viewer/viewer.page.ts +++ b/src/core/features/comments/pages/viewer/viewer.page.ts @@ -110,19 +110,28 @@ export class CoreCommentsViewerPage implements OnInit, OnDestroy { * View loaded. */ async ngOnInit(): Promise { + try { + this.contextLevel = CoreNavigator.getRequiredRouteParam('contextLevel'); + this.instanceId = CoreNavigator.getRequiredRouteNumberParam('instanceId'); + this.componentName = CoreNavigator.getRequiredRouteParam('componentName'); + this.itemId = CoreNavigator.getRequiredRouteNumberParam('itemId'); + this.area = CoreNavigator.getRouteParam('area') || ''; + this.title = CoreNavigator.getRouteNumberParam('title') || + Translate.instant('core.comments.comments'); + this.courseId = CoreNavigator.getRouteNumberParam('courseId'); + } catch (error) { + CoreDomUtils.showErrorModal(error); + + CoreNavigator.back(); + + return; + } + // Is implicit the user can delete if he can add. this.addDeleteCommentsAvailable = await CoreComments.isAddCommentsAvailable(); this.currentUserId = CoreSites.getCurrentSiteUserId(); this.commentsLoaded = false; - this.contextLevel = CoreNavigator.getRouteParam('contextLevel')!; - this.instanceId = CoreNavigator.getRouteNumberParam('instanceId')!; - this.componentName = CoreNavigator.getRouteParam('componentName')!; - this.itemId = CoreNavigator.getRouteNumberParam('itemId')!; - this.area = CoreNavigator.getRouteParam('area') || ''; - this.title = CoreNavigator.getRouteNumberParam('title') || - Translate.instant('core.comments.comments'); - this.courseId = CoreNavigator.getRouteNumberParam('courseId'); await this.fetchComments(true); } diff --git a/src/core/features/course/classes/main-activity-page.ts b/src/core/features/course/classes/main-activity-page.ts index 4c66ba3b0..de7604457 100644 --- a/src/core/features/course/classes/main-activity-page.ts +++ b/src/core/features/course/classes/main-activity-page.ts @@ -14,10 +14,10 @@ import { Component, OnInit } from '@angular/core'; import { CoreNavigator } from '@services/navigator'; +import { CoreDomUtils } from '@services/utils/dom'; import { CoreCourseAnyModuleData } from '../services/course'; import { CoreCourseModuleMainResourceComponent } from './main-resource-component'; -/** /** * Template class to easily create CoreCourseModuleMainComponent of resources (or activities without syncing). */ @@ -36,8 +36,17 @@ export class CoreCourseModuleMainActivityPage('module')!; - this.courseId = CoreNavigator.getRouteNumberParam('courseId')!; + try { + this.module = CoreNavigator.getRequiredRouteParam('module'); + this.courseId = CoreNavigator.getRequiredRouteNumberParam('courseId'); + } catch (error) { + CoreDomUtils.showErrorModal(error); + + CoreNavigator.back(); + + return; + } + this.title = this.module?.name; } diff --git a/src/core/features/courses/pages/categories/categories.ts b/src/core/features/courses/pages/categories/categories.ts index 1714f4ebb..df71c1c88 100644 --- a/src/core/features/courses/pages/categories/categories.ts +++ b/src/core/features/courses/pages/categories/categories.ts @@ -59,7 +59,7 @@ export class CoreCoursesCategoriesPage implements OnInit { * @return Promise resolved when done. */ protected async fetchCategories(): Promise { - try{ + try { const categories: CoreCategoryData[] = await CoreCourses.getCategories(this.categoryId, true); this.currentCategory = undefined; diff --git a/src/core/features/grades/pages/course/course.page.ts b/src/core/features/grades/pages/course/course.page.ts index e47cf8819..ee13e8248 100644 --- a/src/core/features/grades/pages/course/course.page.ts +++ b/src/core/features/grades/pages/course/course.page.ts @@ -40,14 +40,26 @@ import { CoreNavigator } from '@services/navigator'; }) export class CoreGradesCoursePage implements AfterViewInit, OnDestroy { - grades: CoreGradesCourseManager; + grades!: CoreGradesCourseManager; splitViewMode?: CoreSplitViewMode; @ViewChild(CoreSplitViewComponent) splitView!: CoreSplitViewComponent; constructor(protected route: ActivatedRoute) { - const courseId = CoreNavigator.getRouteNumberParam('courseId', { route })!; - const userId = CoreNavigator.getRouteNumberParam('userId', { route }) ?? CoreSites.getCurrentSiteUserId(); + let courseId: number; + let userId: number; + + try { + courseId = CoreNavigator.getRequiredRouteNumberParam('courseId', { route }); + userId = CoreNavigator.getRouteNumberParam('userId', { route }) ?? CoreSites.getCurrentSiteUserId(); + } catch (error) { + CoreDomUtils.showErrorModal(error); + + CoreNavigator.back(); + + return; + } + const useSplitView = route.snapshot.data.useSplitView ?? true; const outsideGradesTab = route.snapshot.data.outsideGradesTab ?? false; diff --git a/src/core/features/grades/pages/grade/grade.page.ts b/src/core/features/grades/pages/grade/grade.page.ts index 4424c1bd2..b77e08ce6 100644 --- a/src/core/features/grades/pages/grade/grade.page.ts +++ b/src/core/features/grades/pages/grade/grade.page.ts @@ -31,16 +31,24 @@ import { CoreNavigator } from '@services/navigator'; }) export class CoreGradesGradePage implements OnInit { - courseId: number; - userId: number; - gradeId: number; + courseId!: number; + userId!: number; + gradeId!: number; grade?: CoreGradesFormattedRow | null; gradeLoaded = false; constructor() { - this.courseId = CoreNavigator.getRouteNumberParam('courseId')!; - this.gradeId = CoreNavigator.getRouteNumberParam('gradeId')!; - this.userId = CoreNavigator.getRouteNumberParam('userId') ?? CoreSites.getCurrentSiteUserId(); + try { + this.courseId = CoreNavigator.getRequiredRouteNumberParam('courseId'); + this.gradeId = CoreNavigator.getRequiredRouteNumberParam('gradeId'); + this.userId = CoreNavigator.getRouteNumberParam('userId') ?? CoreSites.getCurrentSiteUserId(); + } catch (error) { + CoreDomUtils.showErrorModal(error); + + CoreNavigator.back(); + + return; + } } /** diff --git a/src/core/features/user/pages/participants/participants.page.ts b/src/core/features/user/pages/participants/participants.page.ts index bc135514e..b1508eb2b 100644 --- a/src/core/features/user/pages/participants/participants.page.ts +++ b/src/core/features/user/pages/participants/participants.page.ts @@ -33,7 +33,7 @@ import { CoreUtils } from '@services/utils/utils'; }) export class CoreUserParticipantsPage implements OnInit, AfterViewInit, OnDestroy { - participants: CoreUserParticipantsManager; + participants!: CoreUserParticipantsManager; searchQuery: string | null = null; searchInProgress = false; searchEnabled = false; @@ -43,7 +43,17 @@ export class CoreUserParticipantsPage implements OnInit, AfterViewInit, OnDestro @ViewChild(CoreSplitViewComponent) splitView!: CoreSplitViewComponent; constructor() { - const courseId = CoreNavigator.getRouteNumberParam('courseId')!; + let courseId: number; + + try { + courseId = CoreNavigator.getRequiredRouteNumberParam('courseId'); + } catch (error) { + CoreDomUtils.showErrorModal(error); + + CoreNavigator.back(); + + return; + } this.participants = new CoreUserParticipantsManager(CoreUserParticipantsPage, courseId); } diff --git a/src/core/services/navigator.ts b/src/core/services/navigator.ts index 8707bf47d..2e3ad5a0c 100644 --- a/src/core/services/navigator.ts +++ b/src/core/services/navigator.ts @@ -30,6 +30,7 @@ import { makeSingleton, NavController, Router } from '@singletons'; import { CoreScreen } from './screen'; import { CoreApp } from './app'; import { CoreSitePlugins } from '@features/siteplugins/services/siteplugins'; +import { CoreError } from '@classes/errors/error'; const DEFAULT_MAIN_MENU_TAB = CoreMainMenuHomeHandlerService.PAGE_NAME; @@ -269,7 +270,7 @@ export class CoreNavigatorService { const value = route.snapshot.queryParams[name] ?? route.snapshot.params[name]; - if (typeof value != 'undefined') { + if (value !== undefined) { return value; } @@ -300,7 +301,7 @@ export class CoreNavigatorService { value = routeOptions.params[name]; } - if (typeof value == 'undefined') { + if (value === undefined) { return; } @@ -345,7 +346,7 @@ export class CoreNavigatorService { getRouteBooleanParam(name: string, routeOptions: CoreNavigatorCurrentRouteOptions = {}): boolean | undefined { const value = this.getRouteParam(name, routeOptions); - if (typeof value == 'undefined') { + if (value === undefined) { return value; } @@ -360,6 +361,67 @@ export class CoreNavigatorService { return Boolean(value); } + /** + * Get a parameter for the current route. + * Please notice that objects can only be retrieved once. You must call this function only once per page and parameter, + * unless there's a new navigation to the page. + * + * This function will fail if parameter is not found. + * + * @param name Name of the parameter. + * @param routeOptions Optional routeOptions to get the params or route value from. If missing, it will autodetect. + * @return Value of the parameter, undefined if not found. + */ + getRequiredRouteParam(name: string, routeOptions: CoreNavigatorCurrentRouteOptions = {}): T { + const value = this.getRouteParam(name, routeOptions); + + if (value === undefined) { + throw new CoreError(`Required param '${name}' not found.`); + } + + return value; + } + + /** + * Get a number route param. + * Angular router automatically converts numbers to string, this function automatically converts it back to number. + * + * This function will fail if parameter is not found. + * + * @param name Name of the parameter. + * @param routeOptions Optional routeOptions to get the params or route value from. If missing, it will autodetect. + * @return Value of the parameter, undefined if not found. + */ + getRequiredRouteNumberParam(name: string, routeOptions: CoreNavigatorCurrentRouteOptions = {}): number { + const value = this.getRouteNumberParam(name, routeOptions); + + if (value === undefined) { + throw new CoreError(`Required number param '${name}' not found.`); + } + + return value; + } + + /** + * Get a boolean route param. + * Angular router automatically converts booleans to string, this function automatically converts it back to boolean. + * + * This function will fail if parameter is not found. + * + * @param name Name of the parameter. + * @param routeOptions Optional routeOptions to get the params or route value from. If missing, it will autodetect. + * @return Value of the parameter, undefined if not found. + */ + getRequiredRouteBooleanParam(name: string, routeOptions: CoreNavigatorCurrentRouteOptions = {}): boolean { + const value = this.getRouteBooleanParam(name, routeOptions); + + if (value === undefined) { + throw new CoreError(`Required boolean param '${name}' not found.`); + } + + return value; + } + /** * Navigate back. *