From 171a122ec931c4b1e6df98564ee54290af0f63ae Mon Sep 17 00:00:00 2001 From: Dani Palou Date: Wed, 15 Dec 2021 09:23:08 +0100 Subject: [PATCH] MOBILE-3946 h5pactivity: Fix non-null assertion eslint warnings --- .../mod/h5pactivity/components/index/index.ts | 72 ++++++++++++++----- .../pages/attempt-results/attempt-results.ts | 10 ++- .../pages/user-attempts/user-attempts.ts | 10 ++- .../pages/users-attempts/users-attempts.ts | 22 ++++-- .../mod/h5pactivity/services/h5pactivity.ts | 14 ++-- 5 files changed, 94 insertions(+), 34 deletions(-) diff --git a/src/addons/mod/h5pactivity/components/index/index.ts b/src/addons/mod/h5pactivity/components/index/index.ts index d54c0db3e..f8cb4d015 100644 --- a/src/addons/mod/h5pactivity/components/index/index.ts +++ b/src/addons/mod/h5pactivity/components/index/index.ts @@ -166,7 +166,11 @@ export class AddonModH5PActivityIndexComponent extends CoreCourseModuleMainActiv * @return Promise resolved when done. */ protected async checkHasOffline(): Promise { - this.hasOffline = await CoreXAPIOffline.contextHasStatements(this.h5pActivity!.context, this.siteId); + if (!this.h5pActivity) { + return; + } + + this.hasOffline = await CoreXAPIOffline.contextHasStatements(this.h5pActivity.context, this.siteId); } /** @@ -175,7 +179,11 @@ export class AddonModH5PActivityIndexComponent extends CoreCourseModuleMainActiv * @return Promise resolved when done. */ protected async fetchAccessInfo(): Promise { - this.accessInfo = await AddonModH5PActivity.getAccessInformation(this.h5pActivity!.id, { + if (!this.h5pActivity) { + return; + } + + this.accessInfo = await AddonModH5PActivity.getAccessInformation(this.h5pActivity.id, { cmId: this.module.id, siteId: this.siteId, }); @@ -187,12 +195,12 @@ export class AddonModH5PActivityIndexComponent extends CoreCourseModuleMainActiv * @return Promise resolved when done. */ protected async fetchDeployedFileData(): Promise { - if (!this.siteCanDownload) { + if (!this.siteCanDownload || !this.h5pActivity) { // Cannot download the file, no need to fetch the file data. return; } - this.deployedFile = await AddonModH5PActivity.getDeployedFile(this.h5pActivity!, { + this.deployedFile = await AddonModH5PActivity.getDeployedFile(this.h5pActivity, { displayOptions: this.displayOptions, siteId: this.siteId, }); @@ -217,10 +225,14 @@ export class AddonModH5PActivityIndexComponent extends CoreCourseModuleMainActiv * @return Promise resolved when done. */ protected async calculateFileState(): Promise { + if (!this.fileUrl || !this.deployedFile) { + return; + } + this.state = await CoreFilepool.getFileStateByUrl( this.site.getId(), - this.fileUrl!, - this.deployedFile!.timemodified, + this.fileUrl, + this.deployedFile.timemodified, ); this.showFileState(); @@ -268,6 +280,10 @@ export class AddonModH5PActivityIndexComponent extends CoreCourseModuleMainActiv event?.preventDefault(); event?.stopPropagation(); + if (!this.deployedFile) { + return; + } + if (!CoreApp.isOnline()) { CoreDomUtils.showErrorModal('core.networkerrormsg', true); @@ -276,7 +292,7 @@ export class AddonModH5PActivityIndexComponent extends CoreCourseModuleMainActiv try { // Confirm the download if needed. - await CoreDomUtils.confirmDownloadSize({ size: this.deployedFile!.filesize!, total: true }); + await CoreDomUtils.confirmDownloadSize({ size: this.deployedFile.filesize || 0, total: true }); await this.downloadDeployedFile(); @@ -317,17 +333,22 @@ export class AddonModH5PActivityIndexComponent extends CoreCourseModuleMainActiv * @return Promise resolved when done. */ protected async downloadDeployedFile(): Promise { + if (!this.fileUrl || !this.deployedFile) { + return; + } + + const deployedFile = this.deployedFile; this.downloading = true; this.progressMessage = 'core.downloading'; try { await CoreFilepool.downloadUrl( this.site.getId(), - this.fileUrl!, + this.fileUrl, false, this.component, this.componentId, - this.deployedFile!.timemodified, + deployedFile.timemodified, (data: DownloadProgressData) => { if (!data) { return; @@ -341,7 +362,7 @@ export class AddonModH5PActivityIndexComponent extends CoreCourseModuleMainActiv this.progressMessage = data.message; } else if (data.loaded !== undefined) { // Downloading or unzipping. - const totalSize = this.progressMessage == 'core.downloading' ? this.deployedFile!.filesize : data.total; + const totalSize = this.progressMessage == 'core.downloading' ? deployedFile.filesize : data.total; if (totalSize !== undefined) { const percentageNumber = (Number(data.loaded / totalSize) * 100); @@ -364,10 +385,14 @@ export class AddonModH5PActivityIndexComponent extends CoreCourseModuleMainActiv * Play the package. */ async play(): Promise { + if (!this.h5pActivity) { + return; + } + this.playing = true; // Mark the activity as viewed. - await AddonModH5PActivity.logView(this.h5pActivity!.id, this.h5pActivity!.name, this.siteId); + await AddonModH5PActivity.logView(this.h5pActivity.id, this.h5pActivity.name, this.siteId); CoreCourse.checkModuleCompletion(this.courseId, this.module.completiondata); } @@ -411,7 +436,7 @@ export class AddonModH5PActivityIndexComponent extends CoreCourseModuleMainActiv */ protected async onIframeMessage(event: MessageEvent): Promise { const data = event.data; - if (!data || !CoreXAPI.canPostStatementsInSite(this.site) || !this.isCurrentXAPIPost(data)) { + if (!data || !this.h5pActivity || !CoreXAPI.canPostStatementsInSite(this.site) || !this.isCurrentXAPIPost(data)) { return; } @@ -419,12 +444,12 @@ export class AddonModH5PActivityIndexComponent extends CoreCourseModuleMainActiv const options = { offline: this.hasOffline, courseId: this.courseId, - extra: this.h5pActivity!.name, + extra: this.h5pActivity.name, siteId: this.site.getId(), }; const sent = await CoreXAPI.postStatements( - this.h5pActivity!.context, + this.h5pActivity.context, data.component, JSON.stringify(data.statements), options, @@ -435,7 +460,7 @@ export class AddonModH5PActivityIndexComponent extends CoreCourseModuleMainActiv if (sent) { try { // Invalidate attempts. - await AddonModH5PActivity.invalidateUserAttempts(this.h5pActivity!.id, undefined, this.siteId); + await AddonModH5PActivity.invalidateUserAttempts(this.h5pActivity.id, undefined, this.siteId); } catch (error) { // Ignore errors. } @@ -459,6 +484,10 @@ export class AddonModH5PActivityIndexComponent extends CoreCourseModuleMainActiv */ // eslint-disable-next-line @typescript-eslint/no-explicit-any protected isCurrentXAPIPost(data: any): data is AddonModH5PActivityXAPIData { + if (!this.h5pActivity) { + return false; + } + if (data.environment != 'moodleapp' || data.context != 'h5p' || data.action != 'xapi_post_statement' || !data.statements) { return false; } @@ -476,14 +505,21 @@ export class AddonModH5PActivityIndexComponent extends CoreCourseModuleMainActiv const match = trackingUrl.match(/xapi\/activity\/(\d+)/); - return match && match[1] == this.h5pActivity!.context; + return match && match[1] == this.h5pActivity.context; } /** * @inheritdoc */ - protected sync(): Promise { - return AddonModH5PActivitySync.syncActivity(this.h5pActivity!.context, this.site.getId()); + protected async sync(): Promise { + if (!this.h5pActivity) { + return { + updated: false, + warnings: [], + }; + } + + return await AddonModH5PActivitySync.syncActivity(this.h5pActivity.context, this.site.getId()); } /** 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 113d66430..ad89f085f 100644 --- a/src/addons/mod/h5pactivity/pages/attempt-results/attempt-results.ts +++ b/src/addons/mod/h5pactivity/pages/attempt-results/attempt-results.ts @@ -65,7 +65,9 @@ export class AddonModH5PActivityAttemptResultsPage implements OnInit { try { await this.fetchData(); - await AddonModH5PActivity.logViewReport(this.h5pActivity!.id, this.h5pActivity!.name, { attemptId: this.attemptId }); + if (this.h5pActivity) { + await AddonModH5PActivity.logViewReport(this.h5pActivity.id, this.h5pActivity.name, { attemptId: this.attemptId }); + } } catch (error) { CoreDomUtils.showErrorModalDefault(error, 'Error loading attempt.'); } finally { @@ -105,8 +107,12 @@ export class AddonModH5PActivityAttemptResultsPage implements OnInit { * @return Promise resolved when done. */ protected async fetchUserProfile(): Promise { + if (!this.attempt) { + return; + } + try { - this.user = await CoreUser.getProfile(this.attempt!.userid, this.courseId, true); + this.user = await CoreUser.getProfile(this.attempt.userid, this.courseId, true); } catch (error) { // Ignore errors. } 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 cda950b21..0165feb15 100644 --- a/src/addons/mod/h5pactivity/pages/user-attempts/user-attempts.ts +++ b/src/addons/mod/h5pactivity/pages/user-attempts/user-attempts.ts @@ -68,7 +68,9 @@ export class AddonModH5PActivityUserAttemptsPage implements OnInit { try { await this.fetchData(); - await AddonModH5PActivity.logViewReport(this.h5pActivity!.id, this.h5pActivity!.name, { userId: this.userId }); + if (this.h5pActivity) { + await AddonModH5PActivity.logViewReport(this.h5pActivity.id, this.h5pActivity.name, { userId: this.userId }); + } } catch (error) { CoreDomUtils.showErrorModalDefault(error, 'Error loading attempts.'); } finally { @@ -107,7 +109,11 @@ export class AddonModH5PActivityUserAttemptsPage implements OnInit { * @return Promise resolved when done. */ protected async fetchAttempts(): Promise { - this.attemptsData = await AddonModH5PActivity.getUserAttempts(this.h5pActivity!.id, { + if (!this.h5pActivity) { + return; + } + + this.attemptsData = await AddonModH5PActivity.getUserAttempts(this.h5pActivity.id, { cmId: this.cmId, userId: this.userId, }); 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 65d0f2510..0d45c30b5 100644 --- a/src/addons/mod/h5pactivity/pages/users-attempts/users-attempts.ts +++ b/src/addons/mod/h5pactivity/pages/users-attempts/users-attempts.ts @@ -63,7 +63,9 @@ export class AddonModH5PActivityUsersAttemptsPage implements OnInit { try { await this.fetchData(); - await AddonModH5PActivity.logViewReport(this.h5pActivity!.id, this.h5pActivity!.name); + if (this.h5pActivity) { + await AddonModH5PActivity.logViewReport(this.h5pActivity.id, this.h5pActivity.name); + } } catch (error) { CoreDomUtils.showErrorModalDefault(error, 'Error loading attempts.'); } finally { @@ -103,16 +105,20 @@ export class AddonModH5PActivityUsersAttemptsPage implements OnInit { * @return Promise resolved when done. */ protected async fetchUsers(refresh?: boolean): Promise { + if (!this.h5pActivity) { + return; + } + if (refresh) { this.page = 0; } - const result = await AddonModH5PActivity.getUsersAttempts(this.h5pActivity!.id, { + const result = await AddonModH5PActivity.getUsersAttempts(this.h5pActivity.id, { cmId: this.cmId, page: this.page, }); - const formattedUsers = await this.formatUsers(result.users); + const formattedUsers = await this.formatUsers(this.h5pActivity, result.users); if (this.page === 0) { this.users = formattedUsers; @@ -127,17 +133,21 @@ export class AddonModH5PActivityUsersAttemptsPage implements OnInit { /** * Format users data. * + * @param h5pActivity Activity data. * @param users Users to format. * @return Formatted users. */ - protected async formatUsers(users: AddonModH5PActivityUserAttempts[]): Promise { + protected async formatUsers( + h5pActivity: AddonModH5PActivityData, + users: AddonModH5PActivityUserAttempts[], + ): Promise { return await Promise.all(users.map(async (user: AddonModH5PActivityUserAttemptsFormatted) => { user.user = await CoreUser.getProfile(user.userid, this.courseId, true); // Calculate the score of the user. - if (this.h5pActivity!.grademethod === AddonModH5PActivityProvider.GRADEMANUAL) { + if (h5pActivity.grademethod === AddonModH5PActivityProvider.GRADEMANUAL) { // No score. - } else if (this.h5pActivity!.grademethod === AddonModH5PActivityProvider.GRADEAVERAGEATTEMPT) { + } else if (h5pActivity.grademethod === AddonModH5PActivityProvider.GRADEAVERAGEATTEMPT) { if (user.attempts.length) { // Calculate the average. const sumScores = user.attempts.reduce((sumScores, attempt) => diff --git a/src/addons/mod/h5pactivity/services/h5pactivity.ts b/src/addons/mod/h5pactivity/services/h5pactivity.ts index e43b39bdd..d8a80aee4 100644 --- a/src/addons/mod/h5pactivity/services/h5pactivity.ts +++ b/src/addons/mod/h5pactivity/services/h5pactivity.ts @@ -216,8 +216,8 @@ export class AddonModH5PActivityProvider { const optionsWithPage: AddonModH5PActivityGetAllUsersAttemptsOptions = { ...options, - page: 0, }; + optionsWithPage.page = 0; let canLoadMore = true; let users: AddonModH5PActivityUserAttempts[] = []; @@ -225,7 +225,7 @@ export class AddonModH5PActivityProvider { try { const result = await this.getUsersAttempts(id, optionsWithPage); - optionsWithPage.page = optionsWithPage.page! + 1; + optionsWithPage.page = optionsWithPage.page + 1; users = users.concat(result.users); canLoadMore = result.canLoadMore; } catch (error) { @@ -359,10 +359,11 @@ export class AddonModH5PActivityProvider { const params: AddonModH5pactivityGetResultsWSParams = { h5pactivityid: id, - attemptids: [attemptId], }; + params.attemptids = [attemptId]; + const preSets: CoreSiteWSPreSets = { - cacheKey: this.getAttemptResultsCacheKey(id, params.attemptids!), + cacheKey: this.getAttemptResultsCacheKey(id, params.attemptids), updateFrequency: CoreSite.FREQUENCY_SOMETIMES, component: AddonModH5PActivityProvider.COMPONENT, componentId: options.cmId, @@ -610,10 +611,11 @@ export class AddonModH5PActivityProvider { try { const params: AddonModH5pactivityGetAttemptsWSParams = { h5pactivityid: id, - userids: [userId], }; + params.userids = [userId]; + const preSets: CoreSiteWSPreSets = { - cacheKey: this.getUserAttemptsCacheKey(id, params.userids!), + cacheKey: this.getUserAttemptsCacheKey(id, params.userids), updateFrequency: CoreSite.FREQUENCY_SOMETIMES, component: AddonModH5PActivityProvider.COMPONENT, componentId: options.cmId,