From 13a5d8cb9b54a13665a198a772f7cb43faa1e729 Mon Sep 17 00:00:00 2001 From: Timor Morrien Date: Wed, 23 Oct 2024 17:10:47 +0200 Subject: [PATCH 1/3] Load submission of last rated result instead of a general participation submission --- .../quiz/repository/QuizSubmissionRepository.java | 9 +++++++++ .../aet/artemis/quiz/web/QuizParticipationResource.java | 9 ++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/main/java/de/tum/cit/aet/artemis/quiz/repository/QuizSubmissionRepository.java b/src/main/java/de/tum/cit/aet/artemis/quiz/repository/QuizSubmissionRepository.java index 4202d525190b..7514ac2818b6 100644 --- a/src/main/java/de/tum/cit/aet/artemis/quiz/repository/QuizSubmissionRepository.java +++ b/src/main/java/de/tum/cit/aet/artemis/quiz/repository/QuizSubmissionRepository.java @@ -39,6 +39,15 @@ public interface QuizSubmissionRepository extends ArtemisJpaRepository findWithEagerSubmittedAnswersByParticipationId(long participationId); + @Query(""" + SELECT submission + FROM QuizSubmission submission + LEFT JOIN FETCH submission.submittedAnswers + JOIN submission.results r + WHERE r.id = :resultId + """) + Optional findWithEagerSubmittedAnswersByResultId(@Param("resultId") long resultId); + /** * Retrieve QuizSubmission for given quiz batch and studentLogin * diff --git a/src/main/java/de/tum/cit/aet/artemis/quiz/web/QuizParticipationResource.java b/src/main/java/de/tum/cit/aet/artemis/quiz/web/QuizParticipationResource.java index 41c6fc8173c9..1876f76a45df 100644 --- a/src/main/java/de/tum/cit/aet/artemis/quiz/web/QuizParticipationResource.java +++ b/src/main/java/de/tum/cit/aet/artemis/quiz/web/QuizParticipationResource.java @@ -92,7 +92,14 @@ public ResponseEntity startParticipation(@PathVariable Long // NOTE: starting exercise prevents that two participation will exist, but ensures that a submission is created var result = resultRepository.findFirstByParticipationIdAndRatedOrderByCompletionDateDesc(participation.getId(), true).orElse(new Result()); - result.setSubmission(quizSubmissionRepository.findWithEagerSubmittedAnswersByParticipationId(participation.getId()).orElseThrow()); + if (result.getId() == null) { + // Load the live submission of the participation + result.setSubmission(quizSubmissionRepository.findWithEagerSubmittedAnswersByParticipationId(participation.getId()).orElseThrow()); + } + else { + // Load the actual submission of the result + result.setSubmission(quizSubmissionRepository.findWithEagerSubmittedAnswersByResultId(result.getId()).orElseThrow()); + } participation.setResults(Set.of(result)); participation.setExercise(exercise); From b3fc4044ba408279ef4cf4c253e87bb44628cc8e Mon Sep 17 00:00:00 2001 From: Timor Morrien Date: Wed, 23 Oct 2024 17:23:51 +0200 Subject: [PATCH 2/3] Add refactoring TODO --- .../tum/cit/aet/artemis/quiz/web/QuizParticipationResource.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/java/de/tum/cit/aet/artemis/quiz/web/QuizParticipationResource.java b/src/main/java/de/tum/cit/aet/artemis/quiz/web/QuizParticipationResource.java index 1876f76a45df..c6b9f3b64669 100644 --- a/src/main/java/de/tum/cit/aet/artemis/quiz/web/QuizParticipationResource.java +++ b/src/main/java/de/tum/cit/aet/artemis/quiz/web/QuizParticipationResource.java @@ -69,6 +69,8 @@ public QuizParticipationResource(QuizExerciseRepository quizExerciseRepository, /** * POST /quiz-exercises/{exerciseId}/start-participation : start the quiz exercise participation + * TODO: This endpoint is also called when viewing the result of a quiz exercise. + * TODO: This does not make any sense, as the participation is already started. * * @param exerciseId the id of the quiz exercise * @return The created participation From 50d6229b254f343d0c9fe3c97eda4a17e3117efa Mon Sep 17 00:00:00 2001 From: Timor Morrien Date: Wed, 23 Oct 2024 21:59:16 +0200 Subject: [PATCH 3/3] Fix another edge-case --- .../aet/artemis/quiz/repository/QuizSubmissionRepository.java | 3 ++- .../cit/aet/artemis/quiz/service/QuizSubmissionService.java | 2 +- .../cit/aet/artemis/quiz/web/QuizParticipationResource.java | 2 +- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/main/java/de/tum/cit/aet/artemis/quiz/repository/QuizSubmissionRepository.java b/src/main/java/de/tum/cit/aet/artemis/quiz/repository/QuizSubmissionRepository.java index 7514ac2818b6..39cd106f67b2 100644 --- a/src/main/java/de/tum/cit/aet/artemis/quiz/repository/QuizSubmissionRepository.java +++ b/src/main/java/de/tum/cit/aet/artemis/quiz/repository/QuizSubmissionRepository.java @@ -3,6 +3,7 @@ import static de.tum.cit.aet.artemis.core.config.Constants.PROFILE_CORE; import static org.springframework.data.jpa.repository.EntityGraph.EntityGraphType.LOAD; +import java.util.List; import java.util.Optional; import java.util.Set; @@ -37,7 +38,7 @@ public interface QuizSubmissionRepository extends ArtemisJpaRepository findWithEagerSubmittedAnswersByParticipationId(long participationId); + List findWithEagerSubmittedAnswersByParticipationId(long participationId); @Query(""" SELECT submission diff --git a/src/main/java/de/tum/cit/aet/artemis/quiz/service/QuizSubmissionService.java b/src/main/java/de/tum/cit/aet/artemis/quiz/service/QuizSubmissionService.java index 3892a0e8e44e..481648fb9636 100644 --- a/src/main/java/de/tum/cit/aet/artemis/quiz/service/QuizSubmissionService.java +++ b/src/main/java/de/tum/cit/aet/artemis/quiz/service/QuizSubmissionService.java @@ -147,7 +147,7 @@ public void calculateAllResults(long quizExerciseId) { log.info("Calculating results for quiz {}", quizExercise.getId()); studentParticipationRepository.findByExerciseId(quizExercise.getId()).forEach(participation -> { participation.setExercise(quizExercise); - Optional quizSubmissionOptional = quizSubmissionRepository.findWithEagerSubmittedAnswersByParticipationId(participation.getId()); + Optional quizSubmissionOptional = quizSubmissionRepository.findWithEagerSubmittedAnswersByParticipationId(participation.getId()).stream().findFirst(); if (quizSubmissionOptional.isEmpty()) { return; diff --git a/src/main/java/de/tum/cit/aet/artemis/quiz/web/QuizParticipationResource.java b/src/main/java/de/tum/cit/aet/artemis/quiz/web/QuizParticipationResource.java index c6b9f3b64669..fc2b4d3b3c94 100644 --- a/src/main/java/de/tum/cit/aet/artemis/quiz/web/QuizParticipationResource.java +++ b/src/main/java/de/tum/cit/aet/artemis/quiz/web/QuizParticipationResource.java @@ -96,7 +96,7 @@ public ResponseEntity startParticipation(@PathVariable Long var result = resultRepository.findFirstByParticipationIdAndRatedOrderByCompletionDateDesc(participation.getId(), true).orElse(new Result()); if (result.getId() == null) { // Load the live submission of the participation - result.setSubmission(quizSubmissionRepository.findWithEagerSubmittedAnswersByParticipationId(participation.getId()).orElseThrow()); + result.setSubmission(quizSubmissionRepository.findWithEagerSubmittedAnswersByParticipationId(participation.getId()).stream().findFirst().orElseThrow()); } else { // Load the actual submission of the result