diff --git a/src/main/webapp/app/entities/exercise-category.model.ts b/src/main/webapp/app/entities/exercise-category.model.ts index bc134d0b7f29..68f09c914d2d 100644 --- a/src/main/webapp/app/entities/exercise-category.model.ts +++ b/src/main/webapp/app/entities/exercise-category.model.ts @@ -1,6 +1,30 @@ export class ExerciseCategory { public color?: string; + + // TODO should be renamed to "name" -> accessing variable via "category.name" instead of "category.category" - requires database migration (stored as json in database, see the table "exercise_categories") public category?: string; - constructor() {} + constructor(category: string | undefined, color: string | undefined) { + this.color = color; + this.category = category; + } + + equals(otherExerciseCategory: ExerciseCategory): boolean { + return this.color === otherExerciseCategory.color && this.category === otherExerciseCategory.category; + } + + /** + * @param otherExerciseCategory + * @returns the alphanumerical order of the two exercise categories based on their display text + */ + compare(otherExerciseCategory: ExerciseCategory): number { + if (this.category === otherExerciseCategory.category) { + return 0; + } + + const displayText = this.category?.toLowerCase() ?? ''; + const otherCategoryDisplayText = otherExerciseCategory.category?.toLowerCase() ?? ''; + + return displayText < otherCategoryDisplayText ? -1 : 1; + } } diff --git a/src/main/webapp/app/exercises/shared/exercise/exercise.service.ts b/src/main/webapp/app/exercises/shared/exercise/exercise.service.ts index dffb26604cc7..50c2227751e3 100644 --- a/src/main/webapp/app/exercises/shared/exercise/exercise.service.ts +++ b/src/main/webapp/app/exercises/shared/exercise/exercise.service.ts @@ -347,7 +347,7 @@ export class ExerciseService { * @param exercise the exercise */ static stringifyExerciseCategories(exercise: Exercise) { - return exercise.categories?.map((category) => JSON.stringify(category) as ExerciseCategory); + return exercise.categories?.map((category) => JSON.stringify(category) as unknown as ExerciseCategory); } /** @@ -362,12 +362,15 @@ export class ExerciseService { } /** - * Parses the exercise categories JSON string into ExerciseCategory objects. + * Parses the exercise categories JSON string into {@link ExerciseCategory} objects. * @param exercise - the exercise */ static parseExerciseCategories(exercise?: Exercise) { if (exercise?.categories) { - exercise.categories = exercise.categories.map((category) => JSON.parse(category as string) as ExerciseCategory); + exercise.categories = exercise.categories.map((category) => { + const categoryObj = JSON.parse(category as unknown as string); + return new ExerciseCategory(categoryObj.category, categoryObj.color); + }); } } diff --git a/src/main/webapp/app/exercises/shared/participation/participation.utils.ts b/src/main/webapp/app/exercises/shared/participation/participation.utils.ts index 7c7a99d3830c..5fc349f22b27 100644 --- a/src/main/webapp/app/exercises/shared/participation/participation.utils.ts +++ b/src/main/webapp/app/exercises/shared/participation/participation.utils.ts @@ -102,7 +102,11 @@ export const isParticipationInDueTime = (participation: Participation, exercise: * @param participation * @param showUngradedResults */ -export function getLatestResultOfStudentParticipation(participation: StudentParticipation, showUngradedResults: boolean): Result | undefined { +export function getLatestResultOfStudentParticipation(participation: StudentParticipation | undefined, showUngradedResults: boolean): Result | undefined { + if (!participation) { + return undefined; + } + // Sort participation results by completionDate desc. if (participation.results) { participation.results = _orderBy(participation.results, 'completionDate', 'desc'); diff --git a/src/main/webapp/app/overview/course-exercises/course-exercises.component.html b/src/main/webapp/app/overview/course-exercises/course-exercises.component.html index 2de6ec14a991..6be118bf7475 100644 --- a/src/main/webapp/app/overview/course-exercises/course-exercises.component.html +++ b/src/main/webapp/app/overview/course-exercises/course-exercises.component.html @@ -1,7 +1,7 @@