Search in sources :

Example 1 with ParticipantScore

use of de.tum.in.www1.artemis.domain.scores.ParticipantScore in project ArTEMiS by ls1intum.

the class ScoreService method updateOrCreateParticipantScore.

/**
 * Either updates an existing participant score or creates a new participant score if a new result comes in
 * The annotation "@Transactional" is ok because it means that this method does not support run in an outer transactional context, instead the outer transaction is paused
 *
 * @param createdOrUpdatedResult newly created or updated result
 */
// ok (see JavaDoc)
@Transactional(propagation = Propagation.NOT_SUPPORTED)
public void updateOrCreateParticipantScore(Result createdOrUpdatedResult) {
    if (createdOrUpdatedResult.getScore() == null || createdOrUpdatedResult.getCompletionDate() == null) {
        return;
    }
    // There is a deadlock problem with programming exercises here if we use the participation from the result (reason unknown at the moment)
    // therefore we get the participation from the database
    Optional<StudentParticipation> studentParticipationOptional = getStudentParticipationForResult(createdOrUpdatedResult);
    if (studentParticipationOptional.isEmpty()) {
        return;
    }
    StudentParticipation studentParticipation = studentParticipationOptional.get();
    // we ignore test runs of exams
    if (studentParticipation.isTestRun()) {
        return;
    }
    Exercise exercise = studentParticipation.getExercise();
    ParticipantScore existingParticipationScoreForExerciseAndParticipant = getExistingParticipationScore(studentParticipation, exercise);
    // there already exists a participant score -> we need to update it
    if (existingParticipationScoreForExerciseAndParticipant != null) {
        updateExistingParticipantScore(existingParticipationScoreForExerciseAndParticipant, createdOrUpdatedResult, exercise);
    } else {
        // there does not already exist a participant score -> we need to create it
        createNewParticipantScore(createdOrUpdatedResult, studentParticipation, exercise);
    }
}
Also used : ParticipantScore(de.tum.in.www1.artemis.domain.scores.ParticipantScore) StudentParticipation(de.tum.in.www1.artemis.domain.participation.StudentParticipation) Transactional(org.springframework.transaction.annotation.Transactional)

Example 2 with ParticipantScore

use of de.tum.in.www1.artemis.domain.scores.ParticipantScore in project ArTEMiS by ls1intum.

the class ScoreService method getNewLastRatedResultForParticipantScore.

/**
 * Get the result that can replace the currently set last rated result for a participant score
 *
 * @param participantScore participant score
 * @return optional of new result
 */
private Optional<Result> getNewLastRatedResultForParticipantScore(ParticipantScore participantScore) {
    List<Result> ratedResultsOrdered;
    if (participantScore.getClass().equals(StudentScore.class)) {
        StudentScore studentScore = (StudentScore) participantScore;
        ratedResultsOrdered = resultRepository.getRatedResultsOrderedByParticipationIdLegalSubmissionIdResultIdDescForStudent(participantScore.getExercise().getId(), studentScore.getUser().getId()).stream().filter(r -> !participantScore.getLastRatedResult().equals(r)).collect(Collectors.toList());
    } else {
        TeamScore teamScore = (TeamScore) participantScore;
        ratedResultsOrdered = resultRepository.getRatedResultsOrderedByParticipationIdLegalSubmissionIdResultIdDescForTeam(participantScore.getExercise().getId(), teamScore.getTeam().getId()).stream().filter(r -> !participantScore.getLastRatedResult().equals(r)).collect(Collectors.toList());
    }
    // the new last rated result (rated result with the highest id of submission with the highest id) will be at the beginning of the list
    return ratedResultsOrdered.isEmpty() ? Optional.empty() : Optional.of(ratedResultsOrdered.get(0));
}
Also used : StudentScore(de.tum.in.www1.artemis.domain.scores.StudentScore) TeamScore(de.tum.in.www1.artemis.domain.scores.TeamScore)

Example 3 with ParticipantScore

use of de.tum.in.www1.artemis.domain.scores.ParticipantScore in project ArTEMiS by ls1intum.

the class ScoreService method getNewLastResultForParticipantScore.

/**
 * Get the result that can replace the currently set last result for a participant score
 *
 * @param participantScore participant score
 * @return optional of new result
 */
private Optional<Result> getNewLastResultForParticipantScore(ParticipantScore participantScore) {
    List<Result> resultOrdered;
    if (participantScore.getClass().equals(StudentScore.class)) {
        StudentScore studentScore = (StudentScore) participantScore;
        resultOrdered = resultRepository.getResultsOrderedByParticipationIdLegalSubmissionIdResultIdDescForStudent(participantScore.getExercise().getId(), studentScore.getUser().getId()).stream().filter(r -> !participantScore.getLastResult().equals(r)).collect(Collectors.toList());
    } else {
        TeamScore teamScore = (TeamScore) participantScore;
        resultOrdered = resultRepository.getResultsOrderedByParticipationIdLegalSubmissionIdResultIdDescForTeam(participantScore.getExercise().getId(), teamScore.getTeam().getId()).stream().filter(r -> !participantScore.getLastResult().equals(r)).collect(Collectors.toList());
    }
    // the new last result (result with the highest id of submission with the highest id) will be at the beginning of the list
    return resultOrdered.isEmpty() ? Optional.empty() : Optional.of(resultOrdered.get(0));
}
Also used : StudentScore(de.tum.in.www1.artemis.domain.scores.StudentScore) TeamScore(de.tum.in.www1.artemis.domain.scores.TeamScore)

Example 4 with ParticipantScore

use of de.tum.in.www1.artemis.domain.scores.ParticipantScore in project ArTEMiS by ls1intum.

the class ParticipantScoreDTO method generateFromParticipantScore.

/**
 * Generates a {@link ParticipantScoreDTO} from a {@link ParticipantScore}
 *
 * @param participantScore ParticipantScore input
 * @return {@link ParticipantScoreDTO}
 */
public static ParticipantScoreDTO generateFromParticipantScore(ParticipantScore participantScore) {
    String userName = null;
    Long userId = null;
    String teamName = null;
    Long teamId = null;
    if (participantScore.getClass().equals(StudentScore.class)) {
        StudentScore studentScore = (StudentScore) participantScore;
        if (studentScore.getUser() != null) {
            userName = studentScore.getUser().getLogin();
            userId = studentScore.getUser().getId();
        }
    } else {
        TeamScore teamScore = (TeamScore) participantScore;
        if (teamScore.getTeam() != null) {
            teamName = teamScore.getTeam().getName();
            teamId = teamScore.getTeam().getId();
        }
    }
    Long id = participantScore.getId();
    String exerciseTitle = participantScore.getExercise() != null && participantScore.getExercise().getTitle() != null ? participantScore.getExercise().getTitle() : null;
    Long exerciseId = participantScore.getExercise() != null ? participantScore.getExercise().getId() : null;
    Long lastResultId = participantScore.getLastResult() != null ? participantScore.getLastResult().getId() : null;
    Double lastResultScore = participantScore.getLastScore();
    Long lastRatedResultId = participantScore.getLastRatedResult() != null ? participantScore.getLastRatedResult().getId() : null;
    Double lastRatedResultScore = participantScore.getLastRatedScore();
    Double lastPoints = participantScore.getLastPoints();
    Double lastRatedPoints = participantScore.getLastRatedPoints();
    return new ParticipantScoreDTO(id, userId, userName, teamId, teamName, exerciseId, exerciseTitle, lastResultId, lastResultScore, lastRatedResultId, lastRatedResultScore, lastPoints, lastRatedPoints);
}
Also used : StudentScore(de.tum.in.www1.artemis.domain.scores.StudentScore) TeamScore(de.tum.in.www1.artemis.domain.scores.TeamScore)

Example 5 with ParticipantScore

use of de.tum.in.www1.artemis.domain.scores.ParticipantScore in project ArTEMiS by ls1intum.

the class ResultListenerIntegrationTest method saveRatedResult_then_removeSavedResult_ShouldRemoveAssociatedStudentScore.

@ParameterizedTest(name = "{displayName} [{index}] {argumentsWithNames}")
@ValueSource(booleans = { true, false })
@WithMockUser(username = "instructor1", roles = "INSTRUCTOR")
public void saveRatedResult_then_removeSavedResult_ShouldRemoveAssociatedStudentScore(boolean isTeamTest) {
    ParticipantScore originalParticipantScore = setupTestScenarioWithOneResultSaved(true, isTeamTest);
    Result persistedResult = originalParticipantScore.getLastResult();
    // removing the result should trigger the entity listener and remove the associated student score
    resultRepository.deleteById(persistedResult.getId());
    List<StudentScore> savedStudentScores = studentScoreRepository.findAll();
    List<Result> savedResults = resultRepository.findAll();
    assertThat(savedStudentScores).isEmpty();
    assertThat(savedResults).isEmpty();
    verify(scoreService, times(1)).removeOrUpdateAssociatedParticipantScore(any(Result.class));
}
Also used : ParticipantScore(de.tum.in.www1.artemis.domain.scores.ParticipantScore) StudentScore(de.tum.in.www1.artemis.domain.scores.StudentScore) WithMockUser(org.springframework.security.test.context.support.WithMockUser) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

ParticipantScore (de.tum.in.www1.artemis.domain.scores.ParticipantScore)40 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)28 ValueSource (org.junit.jupiter.params.provider.ValueSource)28 WithMockUser (org.springframework.security.test.context.support.WithMockUser)28 StudentScore (de.tum.in.www1.artemis.domain.scores.StudentScore)14 TeamScore (de.tum.in.www1.artemis.domain.scores.TeamScore)10 Participant (de.tum.in.www1.artemis.domain.participation.Participant)4 Transactional (org.springframework.transaction.annotation.Transactional)4 StudentParticipation (de.tum.in.www1.artemis.domain.participation.StudentParticipation)2 ExerciseScoresAggregatedInformation (de.tum.in.www1.artemis.web.rest.dto.ExerciseScoresAggregatedInformation)2 ExerciseScoresDTO (de.tum.in.www1.artemis.web.rest.dto.ExerciseScoresDTO)2 Authentication (org.springframework.security.core.Authentication)2