Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Quiz exercises: Fix an error after using the practice mode #9571

Merged
merged 3 commits into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -37,7 +38,16 @@ public interface QuizSubmissionRepository extends ArtemisJpaRepository<QuizSubmi
QuizSubmission findWithEagerSubmittedAnswersById(long submissionId);

@EntityGraph(type = LOAD, attributePaths = { "submittedAnswers" })
Optional<QuizSubmission> findWithEagerSubmittedAnswersByParticipationId(long participationId);
List<QuizSubmission> findWithEagerSubmittedAnswersByParticipationId(long participationId);

@Query("""
SELECT submission
FROM QuizSubmission submission
LEFT JOIN FETCH submission.submittedAnswers
JOIN submission.results r
WHERE r.id = :resultId
""")
Optional<QuizSubmission> findWithEagerSubmittedAnswersByResultId(@Param("resultId") long resultId);

/**
* Retrieve QuizSubmission for given quiz batch and studentLogin
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<QuizSubmission> quizSubmissionOptional = quizSubmissionRepository.findWithEagerSubmittedAnswersByParticipationId(participation.getId());
Optional<QuizSubmission> quizSubmissionOptional = quizSubmissionRepository.findWithEagerSubmittedAnswersByParticipationId(participation.getId()).stream().findFirst();

if (quizSubmissionOptional.isEmpty()) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -92,7 +94,14 @@ public ResponseEntity<MappingJacksonValue> 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()).stream().findFirst().orElseThrow());
}
else {
// Load the actual submission of the result
result.setSubmission(quizSubmissionRepository.findWithEagerSubmittedAnswersByResultId(result.getId()).orElseThrow());
}

participation.setResults(Set.of(result));
participation.setExercise(exercise);
Expand Down
Loading