From 05f5967ffc7445dac5b18afeaf92baec5d6e2669 Mon Sep 17 00:00:00 2001 From: Dani Palou Date: Thu, 18 Feb 2021 15:17:36 +0100 Subject: [PATCH] MOBILE-3651 core: Specify some nulls in WS responses --- src/addons/messages/services/messages.ts | 11 +++-------- .../mod/lesson/components/index/index.ts | 2 +- src/addons/mod/lesson/services/lesson.ts | 18 +++++++++--------- src/addons/mod/quiz/services/quiz.ts | 12 ++++++------ .../course/components/format/format.ts | 10 +++++----- .../core-courses-course-list-item.html | 2 +- .../course-list-item/course-list-item.ts | 2 +- .../core-courses-course-progress.html | 5 +++-- src/core/features/courses/services/courses.ts | 2 +- .../pages/space-usage/space-usage.html | 2 +- .../user/pages/participants/participants.html | 2 +- src/core/features/user/services/user.ts | 3 +-- 12 files changed, 33 insertions(+), 38 deletions(-) diff --git a/src/addons/messages/services/messages.ts b/src/addons/messages/services/messages.ts index 41903574c..07c9c5f18 100644 --- a/src/addons/messages/services/messages.ts +++ b/src/addons/messages/services/messages.ts @@ -897,10 +897,7 @@ export class AddonMessagesProvider { ): Promise<{members: AddonMessagesConversationMember[]; canLoadMore: boolean}> { const site = await CoreSites.instance.getSite(siteId); userId = userId || site.getUserId(); - - if (typeof limitTo == 'undefined' || limitTo === null) { - limitTo = AddonMessagesProvider.LIMIT_MESSAGES; - } + limitTo = limitTo ?? AddonMessagesProvider.LIMIT_MESSAGES; const preSets: CoreSiteWSPreSets = { cacheKey: this.getCacheKeyForConversationMembers(userId, conversationId), @@ -948,11 +945,9 @@ export class AddonMessagesProvider { options.userId = options.userId || site.getUserId(); options.limitFrom = options.limitFrom || 0; - options.limitTo = options.limitTo === undefined || options.limitTo === null - ? AddonMessagesProvider.LIMIT_MESSAGES - : options.limitTo; + options.limitTo = options.limitTo ?? AddonMessagesProvider.LIMIT_MESSAGES; options.timeFrom = options.timeFrom || 0; - options.newestFirst = options.newestFirst === undefined || options.newestFirst === null ? true : options.newestFirst; + options.newestFirst = options.newestFirst ?? true; const preSets: CoreSiteWSPreSets = { cacheKey: this.getCacheKeyForConversationMessages(options.userId, conversationId), diff --git a/src/addons/mod/lesson/components/index/index.ts b/src/addons/mod/lesson/components/index/index.ts index e9b68dc7c..9a7ff2bff 100644 --- a/src/addons/mod/lesson/components/index/index.ts +++ b/src/addons/mod/lesson/components/index/index.ts @@ -531,7 +531,7 @@ export class AddonModLessonIndexComponent extends CoreCourseModuleMainActivityCo } if (formattedData.lessonscored) { - if (formattedData.numofattempts) { + if (formattedData.numofattempts && formattedData.avescore != null) { formattedData.avescore = CoreTextUtils.instance.roundToDecimals(formattedData.avescore, 2); } if (formattedData.highscore != null) { diff --git a/src/addons/mod/lesson/services/lesson.ts b/src/addons/mod/lesson/services/lesson.ts index d6d787921..d73b3985a 100644 --- a/src/addons/mod/lesson/services/lesson.ts +++ b/src/addons/mod/lesson/services/lesson.ts @@ -2231,14 +2231,14 @@ export class AddonModLessonProvider { * @param data Data containing the user answer. * @return User response. */ - protected getUserResponseMultichoice(data: Record): number[] | null { + protected getUserResponseMultichoice(data: Record): number[] | undefined { if (data.answer) { // The data is already stored as expected. If it's valid, parse the values to int. if (Array.isArray(data.answer)) { return data.answer.map((value) => parseInt(value, 10)); } - return null; + return undefined; } // Data is stored in properties like 'answer[379]'. Recreate the answer array. @@ -3979,12 +3979,12 @@ export type AddonModLessonGetAttemptsOverviewWSResponse = { export type AddonModLessonAttemptsOverviewWSData = { lessonscored: boolean; // True if the lesson was scored. numofattempts: number; // Number of attempts. - avescore: number; // Average score. - highscore: number; // High score. - lowscore: number; // Low score. - avetime: number; // Average time (spent in taking the lesson). - hightime: number; // High time. - lowtime: number; // Low time. + avescore: number | null; // Average score. + highscore: number | null; // High score. + lowscore: number | null; // Low score. + avetime: number | null; // Average time (spent in taking the lesson). + hightime: number | null; // High time. + lowtime: number | null; // Low time. students?: AddonModLessonAttemptsOverviewsStudentWSData[]; // Students data, including attempts. }; @@ -4003,7 +4003,7 @@ export type AddonModLessonAttemptsOverviewsStudentWSData = { */ export type AddonModLessonAttemptsOverviewsAttemptWSData = { try: number; // Attempt number. - grade: number; // Attempt grade. + grade: number | null; // Attempt grade. timestart: number; // Attempt time started. timeend: number; // Attempt last time continued. end: number; // Attempt time ended. diff --git a/src/addons/mod/quiz/services/quiz.ts b/src/addons/mod/quiz/services/quiz.ts index 19fbe96f5..86fd2e27e 100644 --- a/src/addons/mod/quiz/services/quiz.ts +++ b/src/addons/mod/quiz/services/quiz.ts @@ -85,8 +85,8 @@ export class AddonModQuizProvider { * @param decimals Decimals to use. * @return Grade to display. */ - formatGrade(grade?: number, decimals?: number): string { - if (typeof grade == 'undefined' || grade == -1 || grade === null || isNaN(grade)) { + formatGrade(grade?: number | null, decimals?: number): string { + if (grade === undefined || grade == -1 || grade === null || isNaN(grade)) { return Translate.instance.instant('addon.mod_quiz.notyetgraded'); } @@ -1796,14 +1796,14 @@ export class AddonModQuizProvider { * @return Grade to display. */ rescaleGrade( - rawGrade: string | number | undefined, + rawGrade: string | number | undefined | null, quiz: AddonModQuizQuizWSData, format: boolean | string = true, ): string | undefined { let grade: number | undefined; const rawGradeNum = typeof rawGrade == 'string' ? parseFloat(rawGrade) : rawGrade; - if (rawGradeNum !== undefined && !isNaN(rawGradeNum)) { + if (rawGradeNum !== undefined && rawGradeNum !== null && !isNaN(rawGradeNum)) { if (quiz.sumgrades! >= 0.000005) { grade = rawGradeNum * quiz.grade! / quiz.sumgrades!; } else { @@ -2055,7 +2055,7 @@ export type AddonModQuizAttemptWSData = { timemodified?: number; // Last modified time. timemodifiedoffline?: number; // Last modified time via webservices. timecheckstate?: number; // Next time quiz cron should check attempt for state changes. NULL means never check. - sumgrades?: number; // Total marks for this attempt. + sumgrades?: number | null; // Total marks for this attempt. }; /** @@ -2215,7 +2215,7 @@ export type AddonModQuizQuizWSData = { questionsperpage?: number; // How often to insert a page break when editing the quiz, or when shuffling the question order. navmethod?: string; // Any constraints on how the user is allowed to navigate around the quiz. shuffleanswers?: number; // Whether the parts of the question should be shuffled, in those question types that support it. - sumgrades?: number; // The total of all the question instance maxmarks. + sumgrades?: number | null; // The total of all the question instance maxmarks. grade?: number; // The total that the quiz overall grade is scaled to be out of. timecreated?: number; // The time when the quiz was added to the course. timemodified?: number; // Last modified time. diff --git a/src/core/features/course/components/format/format.ts b/src/core/features/course/components/format/format.ts index 59e5c6d85..060906f96 100644 --- a/src/core/features/course/components/format/format.ts +++ b/src/core/features/course/components/format/format.ts @@ -160,9 +160,9 @@ export class CoreCourseFormatComponent implements OnInit, OnChanges, OnDestroy { let section: CoreCourseSection | undefined; - if (typeof data.sectionId != 'undefined' && data.sectionId != null && this.sections) { + if (typeof data.sectionId != 'undefined' && this.sections) { section = this.sections.find((section) => section.id == data.sectionId); - } else if (typeof data.sectionNumber != 'undefined' && data.sectionNumber != null && this.sections) { + } else if (typeof data.sectionNumber != 'undefined' && this.sections) { section = this.sections.find((section) => section.section == data.sectionNumber); } @@ -184,8 +184,8 @@ export class CoreCourseFormatComponent implements OnInit, OnChanges, OnDestroy { this.displaySectionSelector = CoreCourseFormatDelegate.instance.displaySectionSelector(this.course); this.displayBlocks = CoreCourseFormatDelegate.instance.displayBlocks(this.course); - this.progress = 'progress' in this.course && this.course.progress !== undefined && this.course.progress >= 0 && - this.course.completionusertracked !== false ? this.course.progress : undefined; + this.progress = 'progress' in this.course && typeof this.course.progress == 'number' && + this.course.progress >= 0 && this.course.completionusertracked !== false ? this.course.progress : undefined; if ('overviewfiles' in this.course) { this.imageThumb = this.course.overviewfiles?.[0]?.fileurl; } @@ -600,7 +600,7 @@ export class CoreCourseFormatComponent implements OnInit, OnChanges, OnDestroy { this.completionChanged.emit(completionData); if (completionData.valueused !== false || !this.course || !('progress' in this.course) || - typeof this.course.progress == 'undefined') { + typeof this.course.progress != 'number') { return; } diff --git a/src/core/features/courses/components/course-list-item/core-courses-course-list-item.html b/src/core/features/courses/components/course-list-item/core-courses-course-list-item.html index ccf64943c..4a1b9718f 100644 --- a/src/core/features/courses/components/course-list-item/core-courses-course-list-item.html +++ b/src/core/features/courses/components/course-list-item/core-courses-course-list-item.html @@ -23,7 +23,7 @@ -

+

diff --git a/src/core/features/courses/components/course-list-item/course-list-item.ts b/src/core/features/courses/components/course-list-item/course-list-item.ts index 0ec9214c0..aeefb3804 100644 --- a/src/core/features/courses/components/course-list-item/course-list-item.ts +++ b/src/core/features/courses/components/course-list-item/course-list-item.ts @@ -34,7 +34,7 @@ export class CoreCoursesCourseListItemComponent implements OnInit { @Input() course!: CoreCourseSearchedData & CoreCourseWithImageAndColor & { completionusertracked?: boolean; // If the user is completion tracked. - progress?: number; // Progress percentage. + progress?: number | null; // Progress percentage. }; // The course to render. icons: CoreCoursesEnrolmentIcons[] = []; diff --git a/src/core/features/courses/components/course-progress/core-courses-course-progress.html b/src/core/features/courses/components/course-progress/core-courses-course-progress.html index 84acd224b..db78a223d 100644 --- a/src/core/features/courses/components/course-progress/core-courses-course-progress.html +++ b/src/core/features/courses/components/course-progress/core-courses-course-progress.html @@ -5,7 +5,7 @@ + [class.core-course-more-than-title]="course.progress! >= 0"> - + diff --git a/src/core/features/courses/services/courses.ts b/src/core/features/courses/services/courses.ts index 84ce04a4f..28cdf1e72 100644 --- a/src/core/features/courses/services/courses.ts +++ b/src/core/features/courses/services/courses.ts @@ -1227,7 +1227,7 @@ export type CoreEnrolledCourseData = CoreEnrolledCourseBasicData & { enrolledusercount?: number; // Number of enrolled users in this course. completionhascriteria?: boolean; // If completion criteria is set. completionusertracked?: boolean; // If the user is completion tracked. - progress?: number; // Progress percentage. + progress?: number | null; // Progress percentage. completed?: boolean; // Whether the course is completed. marker?: number; // Course section marker. lastaccess?: number; // Last access to the course (timestamp). diff --git a/src/core/features/settings/pages/space-usage/space-usage.html b/src/core/features/settings/pages/space-usage/space-usage.html index 8d9294c40..582fa1773 100644 --- a/src/core/features/settings/pages/space-usage/space-usage.html +++ b/src/core/features/settings/pages/space-usage/space-usage.html @@ -26,7 +26,7 @@

{{ site.fullName }}

{{ site.siteUrl }}

-

+

{{ site.spaceUsage | coreBytesToSize }}

{{ participant.fullname }}

{{ 'core.lastaccess' | translate }}: {{ participant.lastcourseaccess | coreTimeAgo }}

-

{{ 'core.lastaccess' | translate }}: {{ participant.lastaccess | coreTimeAgo }}

+

{{ 'core.lastaccess' | translate }}: {{ participant.lastaccess | coreTimeAgo }}

diff --git a/src/core/features/user/services/user.ts b/src/core/features/user/services/user.ts index 4ed4e8ee3..0d34f14d2 100644 --- a/src/core/features/user/services/user.ts +++ b/src/core/features/user/services/user.ts @@ -964,7 +964,7 @@ export type CoreUserParticipant = CoreUserBasicData & { interests?: string; // User interests (separated by commas). firstaccess?: number; // First access to the site (0 if never). lastaccess?: number; // Last access to the site (0 if never). - lastcourseaccess?: number; // Last access to the course (0 if never). + lastcourseaccess?: number | null; // Last access to the course (0 if never). description?: string; // User profile description. descriptionformat?: number; // Description format (1 = HTML, 0 = MOODLE, 2 = PLAIN or 4 = MARKDOWN). city?: string; // Home city of the user. @@ -1114,4 +1114,3 @@ type CoreEnrolSearchUsersWSParams = { * Data returned by core_enrol_search_users WS. */ type CoreEnrolSearchUsersWSResponse = CoreUserData[]; -