Search in sources :

Example 41 with Participant

use of de.tum.in.www1.artemis.domain.participation.Participant in project ArTEMiS by ls1intum.

the class ScoreService method removeOrUpdateAssociatedParticipantScore.

/**
 * Either updates or removes an existing participant score when a result is removed
 * 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 resultToBeDeleted result that will be removes
 */
// ok (see JavaDoc)
@Transactional(propagation = Propagation.NOT_SUPPORTED)
public void removeOrUpdateAssociatedParticipantScore(Result resultToBeDeleted) {
    // In this method we use custom @Query methods that will fail if no authentication is available, therefore
    // we check this here and set a dummy authentication if none is available (this is the case in a scheduled service or
    // websocket)
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    if (authentication == null) {
        SecurityUtils.setAuthorizationObject();
    }
    Optional<ParticipantScore> associatedParticipantScoreOptional;
    if (resultToBeDeleted.isRated() != null && resultToBeDeleted.isRated()) {
        associatedParticipantScoreOptional = participantScoreRepository.findParticipantScoreByLastRatedResult(resultToBeDeleted);
    } else {
        associatedParticipantScoreOptional = participantScoreRepository.findParticipantScoresByLastResult(resultToBeDeleted);
    }
    if (associatedParticipantScoreOptional.isEmpty()) {
        return;
    }
    // There is a participant score connected to the result that will be deleted
    ParticipantScore associatedParticipantScore = associatedParticipantScoreOptional.get();
    Exercise exercise = associatedParticipantScore.getExercise();
    String originalParticipantScoreStructure = associatedParticipantScore.toString();
    // There are two possibilities now:
    // A: Another result exists for the exercise and the student / team -> update participant score with the newest one
    // B: No other result exists for the exercise and the student / team -> remove participant score
    tryToFindNewLastResult(resultToBeDeleted, associatedParticipantScore, exercise);
    if (associatedParticipantScore.getLastResult() == null && associatedParticipantScore.getLastRatedResult() == null) {
        participantScoreRepository.deleteById(associatedParticipantScore.getId());
        logger.info("Deleted an existing participant score: " + originalParticipantScoreStructure);
    } else {
        ParticipantScore updatedParticipantScore = participantScoreRepository.saveAndFlush(associatedParticipantScore);
        logger.info("Updated an existing participant score. Was: " + originalParticipantScoreStructure + ". Is: " + updatedParticipantScore);
    }
}
Also used : ParticipantScore(de.tum.in.www1.artemis.domain.scores.ParticipantScore) Authentication(org.springframework.security.core.Authentication) Transactional(org.springframework.transaction.annotation.Transactional)

Example 42 with Participant

use of de.tum.in.www1.artemis.domain.participation.Participant in project ArTEMiS by ls1intum.

the class ScoreService method getExistingParticipationScore.

/**
 * Gets the existing participation score for an exercise and a participant or null if none can be found
 *
 * @param studentParticipation participation containing the information about the participant
 * @param exercise             exercise for which to find the participation score of the participant
 * @return existing participation score or null if none can be found
 */
private ParticipantScore getExistingParticipationScore(StudentParticipation studentParticipation, Exercise exercise) {
    ParticipantScore existingParticipationScoreForExerciseAndParticipant = null;
    if (exercise.isTeamMode()) {
        Team team = studentParticipation.getTeam().get();
        Optional<TeamScore> teamScoreOptional = teamScoreRepository.findTeamScoreByExerciseAndTeam(exercise, team);
        if (teamScoreOptional.isPresent()) {
            existingParticipationScoreForExerciseAndParticipant = teamScoreOptional.get();
        }
    } else {
        User user = studentParticipation.getStudent().get();
        Optional<StudentScore> studentScoreOptional = studentScoreRepository.findStudentScoreByExerciseAndUser(exercise, user);
        if (studentScoreOptional.isPresent()) {
            existingParticipationScoreForExerciseAndParticipant = studentScoreOptional.get();
        }
    }
    return existingParticipationScoreForExerciseAndParticipant;
}
Also used : ParticipantScore(de.tum.in.www1.artemis.domain.scores.ParticipantScore) StudentScore(de.tum.in.www1.artemis.domain.scores.StudentScore) TeamScore(de.tum.in.www1.artemis.domain.scores.TeamScore)

Example 43 with Participant

use of de.tum.in.www1.artemis.domain.participation.Participant in project ArTEMiS by ls1intum.

the class ProgrammingExerciseTestService method startProgrammingExercise_correctInitializationState.

// TEST
public void startProgrammingExercise_correctInitializationState() throws Exception {
    var user = userRepo.findOneByLogin(studentLogin).orElseThrow();
    user.setLogin("edx_student1");
    user.setInternal(true);
    user = userRepo.save(user);
    final Course course = setupCourseWithProgrammingExercise(ExerciseMode.INDIVIDUAL);
    Participant participant = user;
    mockDelegate.mockConnectorRequestsForStartParticipation(exercise, participant.getParticipantIdentifier(), participant.getParticipants(), true, HttpStatus.CREATED);
    final var path = ParticipationResource.Endpoints.ROOT + ParticipationResource.Endpoints.START_PARTICIPATION.replace("{courseId}", String.valueOf(course.getId())).replace("{exerciseId}", String.valueOf(exercise.getId()));
    final var participation = request.postWithResponseBody(path, null, ProgrammingExerciseStudentParticipation.class, HttpStatus.CREATED);
    assertThat(participation.getInitializationState()).as("Participation should be initialized").isEqualTo(InitializationState.INITIALIZED);
}
Also used : Participant(de.tum.in.www1.artemis.domain.participation.Participant)

Example 44 with Participant

use of de.tum.in.www1.artemis.domain.participation.Participant in project ArTEMiS by ls1intum.

the class ProgrammingExerciseTestService method resumeProgrammingExerciseByTriggeringInstructorBuild_correctInitializationState.

// TEST
public void resumeProgrammingExerciseByTriggeringInstructorBuild_correctInitializationState(ExerciseMode exerciseMode) throws Exception {
    var participation = createStudentParticipationWithSubmission(exerciseMode);
    var participant = participation.getParticipant();
    mockDelegate.mockTriggerInstructorBuildAll(participation);
    // We need to mock the call again because we are triggering the build twice in order to verify that the submission isn't re-created
    mockDelegate.mockTriggerInstructorBuildAll(participation);
    mockDelegate.mockDefaultBranch(participation.getProgrammingExercise());
    // These will be updated triggering a failed build
    participation.setInitializationState(InitializationState.INACTIVE);
    participation.setBuildPlanId(null);
    programmingExerciseStudentParticipationRepository.saveAndFlush(participation);
    var url = "/api/programming-exercises/" + exercise.getId() + "/trigger-instructor-build-all";
    request.postWithoutLocation(url, null, HttpStatus.OK, new HttpHeaders());
    Awaitility.setDefaultTimeout(Duration.ofSeconds(20));
    await().until(() -> programmingExerciseRepository.findWithTemplateAndSolutionParticipationTeamAssignmentConfigCategoriesById(exercise.getId()).isPresent());
    // Fetch updated participation and assert
    ProgrammingExerciseStudentParticipation updatedParticipation = (ProgrammingExerciseStudentParticipation) participationRepository.findByIdElseThrow(participation.getId());
    assertThat(updatedParticipation.getInitializationState()).as("Participation should be initialized").isEqualTo(InitializationState.INITIALIZED);
    assertThat(updatedParticipation.getBuildPlanId()).as("Build Plan Id should be set").isEqualTo(exercise.getProjectKey().toUpperCase() + "-" + participant.getParticipantIdentifier().toUpperCase());
    // Trigger the build again and make sure no new submission is created
    request.postWithoutLocation(url, null, HttpStatus.OK, new HttpHeaders());
    var submissions = submissionRepository.findAll();
    assertThat(submissions).hasSize(1);
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) ProgrammingExerciseStudentParticipation(de.tum.in.www1.artemis.domain.participation.ProgrammingExerciseStudentParticipation)

Example 45 with Participant

use of de.tum.in.www1.artemis.domain.participation.Participant in project ArTEMiS by ls1intum.

the class ProgrammingExerciseTestService method resumeProgrammingExerciseByTriggeringBuild_correctInitializationState.

// TEST
public void resumeProgrammingExerciseByTriggeringBuild_correctInitializationState(ExerciseMode exerciseMode, SubmissionType submissionType) throws Exception {
    var participation = createStudentParticipationWithSubmission(exerciseMode);
    var participant = participation.getParticipant();
    mockDelegate.mockTriggerParticipationBuild(participation);
    // We need to mock the call again because we are triggering the build twice in order to verify that the submission isn't re-created
    mockDelegate.mockTriggerParticipationBuild(participation);
    mockDelegate.mockDefaultBranch(participation.getProgrammingExercise());
    // These will be updated when triggering a build
    participation.setInitializationState(InitializationState.INACTIVE);
    participation.setBuildPlanId(null);
    programmingExerciseStudentParticipationRepository.saveAndFlush(participation);
    // Construct trigger-build url and execute request
    submissionType = submissionType == null ? SubmissionType.MANUAL : submissionType;
    String url = "/api/programming-submissions/" + participation.getId() + "/trigger-build?submissionType=" + submissionType.name();
    request.postWithoutLocation(url, null, HttpStatus.OK, new HttpHeaders());
    // Fetch updated participation and assert
    ProgrammingExerciseStudentParticipation updatedParticipation = (ProgrammingExerciseStudentParticipation) participationRepository.findByIdElseThrow(participation.getId());
    assertThat(updatedParticipation.getInitializationState()).as("Participation should be initialized").isEqualTo(InitializationState.INITIALIZED);
    assertThat(updatedParticipation.getBuildPlanId()).as("Build Plan Id should be set").isEqualTo(exercise.getProjectKey().toUpperCase() + "-" + participant.getParticipantIdentifier().toUpperCase());
    // Trigger the build again and make sure no new submission is created
    request.postWithoutLocation(url, null, HttpStatus.OK, new HttpHeaders());
    var submissions = submissionRepository.findAll();
    assertThat(submissions).hasSize(1);
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) ProgrammingExerciseStudentParticipation(de.tum.in.www1.artemis.domain.participation.ProgrammingExerciseStudentParticipation)

Aggregations

StudentParticipation (de.tum.in.www1.artemis.domain.participation.StudentParticipation)21 Participant (de.tum.in.www1.artemis.domain.participation.Participant)15 ProgrammingExerciseStudentParticipation (de.tum.in.www1.artemis.domain.participation.ProgrammingExerciseStudentParticipation)14 ParticipantScore (de.tum.in.www1.artemis.domain.scores.ParticipantScore)14 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)14 ParticipantScoreDTO (de.tum.in.www1.artemis.web.rest.dto.ParticipantScoreDTO)12 StudentScore (de.tum.in.www1.artemis.domain.scores.StudentScore)10 TeamScore (de.tum.in.www1.artemis.domain.scores.TeamScore)10 Test (org.junit.jupiter.api.Test)10 WithMockUser (org.springframework.security.test.context.support.WithMockUser)10 Exercise (de.tum.in.www1.artemis.domain.Exercise)8 de.tum.in.www1.artemis.repository (de.tum.in.www1.artemis.repository)8 ParticipantScoreAverageDTO (de.tum.in.www1.artemis.web.rest.dto.ParticipantScoreAverageDTO)8 java.util (java.util)8 Collectors (java.util.stream.Collectors)8 QuizExercise (de.tum.in.www1.artemis.domain.quiz.QuizExercise)7 QuizSubmission (de.tum.in.www1.artemis.domain.quiz.QuizSubmission)7 de.tum.in.www1.artemis.domain (de.tum.in.www1.artemis.domain)6 Course (de.tum.in.www1.artemis.domain.Course)6 ModelingExercise (de.tum.in.www1.artemis.domain.modeling.ModelingExercise)6