Skip to content

Commit

Permalink
Fix query and only update necessary users
Browse files Browse the repository at this point in the history
  • Loading branch information
JohannesStoehr committed Jul 13, 2024
1 parent f6f40a1 commit 198445e
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -104,15 +104,15 @@ GROUP BY ex.maxPoints, ex.difficulty, TYPE(ex), sS.lastScore, tS.lastScore, sS.l
SELECT c.id
FROM CourseCompetency c
LEFT JOIN c.exercises ex
WHERE :learningObject = ex
WHERE :exercise = ex
""")
Set<Long> findAllIdsByExercise(@Param("exercise") Exercise exercise);

@Query("""
SELECT c.id
FROM CourseCompetency c
LEFT JOIN c.lectureUnits lu
WHERE :learningObject = lu
WHERE :lectureUnit = lu
""")
Set<Long> findAllIdsByLectureUnit(@Param("lectureUnit") LectureUnit lectureUnit);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import de.tum.in.www1.artemis.domain.User;
import de.tum.in.www1.artemis.domain.lecture.LectureUnit;
import de.tum.in.www1.artemis.domain.lecture.LectureUnitCompletion;
import de.tum.in.www1.artemis.repository.base.ArtemisJpaRepository;
Expand Down Expand Up @@ -44,4 +45,12 @@ SELECT COUNT(lectureUnitCompletion)
AND lectureUnitCompletion.user.id = :userId
""")
int countByLectureUnitIdsAndUserId(@Param("lectureUnitIds") Collection<Long> lectureUnitIds, @Param("userId") Long userId);

@Query("""
SELECT user
FROM LectureUnitCompletion lectureUnitCompletion
LEFT JOIN lectureUnitCompletion.user user
WHERE lectureUnitCompletion.lectureUnit = :lectureUnit
""")
Set<User> findCompletedUsersForLectureUnit(@Param("lectureUnit") LectureUnit lectureUnit);
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@ public interface StudentScoreRepository extends ArtemisJpaRepository<StudentScor
""")
List<StudentScore> findAllByExerciseAndUserWithEagerExercise(@Param("exercises") Set<Exercise> exercises, @Param("user") User user);

@Query("""
SELECT stud
FROM StudentScore s
LEFT JOIN s.user stud
WHERE s.exercise = :exercise
""")
Set<User> findAllUsersWithScoresByExercise(@Param("exercise") Exercise exercise);

@Transactional // ok because of delete
@Modifying
void deleteByExerciseAndUser(Exercise exercise, User user);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,15 @@ public interface TeamScoreRepository extends ArtemisJpaRepository<TeamScore, Lon
""")
List<TeamScore> findAllByExercisesAndUser(@Param("exercises") Set<Exercise> exercises, @Param("user") User user);

@Query("""
SELECT studs
FROM TeamScore s
LEFT JOIN s.team t
LEFT JOIN t.students studs
WHERE s.exercise = :exercise
""")
Set<User> findAllUsersWithScoresByExercise(@Param("exercise") Exercise exercise);

@Transactional // ok because of delete
@Modifying
void deleteByExerciseAndTeam(Exercise exercise, Team team);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,4 +216,13 @@ public double getAverageOfAverageScores(Set<Exercise> exercises) {
return participantScoreRepository.findAverageScoreForExercises(exercises).stream().mapToDouble(exerciseInfo -> (double) exerciseInfo.get("averageScore")).average()
.orElse(0.0);
}

public Set<User> getAllParticipatedUsersInExercise(Exercise exercise) {
if (exercise.isTeamMode()) {
return teamScoreRepository.findAllUsersWithScoresByExercise(exercise);
}
else {
return studentScoreRepository.findAllUsersWithScoresByExercise(exercise);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.springframework.stereotype.Service;

import de.tum.in.www1.artemis.domain.Course;
import de.tum.in.www1.artemis.domain.Exercise;
import de.tum.in.www1.artemis.domain.LearningObject;
import de.tum.in.www1.artemis.domain.User;
import de.tum.in.www1.artemis.domain.competency.CompetencyProgress;
Expand Down Expand Up @@ -158,8 +159,7 @@ public void updateProgressForUpdatedLearningObjectAsync(LearningObject originalL

updateProgressByCompetencyIds(removedCompetencyIds);
if (!addedCompetencyIds.isEmpty()) {
Course course = updatedCompetencies.stream().findAny().map(CourseCompetency::getCourse).orElseThrow();
updateProgressByCompetencyIdsAndUsersInCourse(addedCompetencyIds, course);
updateProgressByCompetencyIdsAndLearningObject(addedCompetencyIds, originalLearningObject);
}
}

Expand All @@ -171,11 +171,18 @@ private void updateProgressByCompetencyIds(Set<Long> competencyIds) {
}
}

private void updateProgressByCompetencyIdsAndUsersInCourse(Set<Long> competencyIds, Course course) {
private void updateProgressByCompetencyIdsAndLearningObject(Set<Long> competencyIds, LearningObject learningObject) {
for (long competencyId : competencyIds) {
Set<User> users = userRepository.getUsersInCourse(course);
log.debug("Updating competency progress for {} users.", users.size());
users.forEach(user -> updateCompetencyProgress(competencyId, user));
Set<User> existingCompetencyUsers = competencyProgressRepository.findAllByCompetencyId(competencyId).stream().map(CompetencyProgress::getUser)
.collect(Collectors.toSet());
Set<User> existingLearningObjectUsers = switch (learningObject) {
case Exercise exercise -> participantScoreService.getAllParticipatedUsersInExercise(exercise);
case LectureUnit lectureUnit -> lectureUnitCompletionRepository.findCompletedUsersForLectureUnit(lectureUnit);
default -> throw new IllegalStateException("Unexpected value: " + learningObject);
};
existingCompetencyUsers.addAll(existingLearningObjectUsers);
log.debug("Updating competency progress for {} users.", existingCompetencyUsers.size());
existingCompetencyUsers.forEach(user -> updateCompetencyProgress(competencyId, user));
}
}

Expand Down

0 comments on commit 198445e

Please sign in to comment.