MOBILE-3946 h5pactivity: Fix non-null assertion eslint warnings

main
Dani Palou 2021-12-15 09:23:08 +01:00
parent c7a32b3e09
commit 171a122ec9
5 changed files with 94 additions and 34 deletions

View File

@ -166,7 +166,11 @@ export class AddonModH5PActivityIndexComponent extends CoreCourseModuleMainActiv
* @return Promise resolved when done.
*/
protected async checkHasOffline(): Promise<void> {
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<void> {
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<void> {
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<void> {
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<void> {
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<void> {
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<void> {
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<AddonModH5PActivitySyncResult> {
return AddonModH5PActivitySync.syncActivity(this.h5pActivity!.context, this.site.getId());
protected async sync(): Promise<AddonModH5PActivitySyncResult> {
if (!this.h5pActivity) {
return {
updated: false,
warnings: [],
};
}
return await AddonModH5PActivitySync.syncActivity(this.h5pActivity.context, this.site.getId());
}
/**

View File

@ -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<void> {
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.
}

View File

@ -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<void> {
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,
});

View File

@ -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<void> {
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<AddonModH5PActivityUserAttemptsFormatted[]> {
protected async formatUsers(
h5pActivity: AddonModH5PActivityData,
users: AddonModH5PActivityUserAttempts[],
): Promise<AddonModH5PActivityUserAttemptsFormatted[]> {
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) =>

View File

@ -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,