Search in sources :

Example 16 with TutorParticipation

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

the class TutorParticipationResource method assessExampleSubmissionForTutorParticipation.

/**
 * POST /exercises/:exerciseId/assess-example-submission: Add an example submission to the tutor participation of the given exercise.
 * If it is just for review (not used for tutorial), the method just records that the tutor has read it.
 * If it is a tutorial, the method checks if the assessment given by the tutor matches the instructor one.
 * If yes, then it returns the participation, if not, it returns an error.
 *
 * @param exerciseId the id of the exercise of the tutorParticipation
 * @param exampleSubmission the example submission that will be added
 * @return the ResponseEntity with status 200 (OK) and with body the exercise, or with status 404 (Not Found)
 */
@PostMapping(value = "/exercises/{exerciseId}/assess-example-submission")
@PreAuthorize("hasRole('TA')")
public ResponseEntity<TutorParticipation> assessExampleSubmissionForTutorParticipation(@PathVariable Long exerciseId, @RequestBody ExampleSubmission exampleSubmission) {
    log.debug("REST request to add example submission to exercise id : {}", exerciseId);
    Exercise exercise = this.exerciseRepository.findByIdElseThrow(exerciseId);
    User user = userRepository.getUserWithGroupsAndAuthorities();
    authorizationCheckService.checkHasAtLeastRoleForExerciseElseThrow(Role.TEACHING_ASSISTANT, exercise, user);
    TutorParticipation resultTutorParticipation = tutorParticipationService.addExampleSubmission(exercise, exampleSubmission, user);
    // Avoid infinite recursion for JSON
    resultTutorParticipation.getTrainedExampleSubmissions().forEach(trainedExampleSubmission -> {
        trainedExampleSubmission.setTutorParticipations(null);
        trainedExampleSubmission.setExercise(null);
    });
    return ResponseEntity.ok().body(resultTutorParticipation);
}
Also used : Exercise(de.tum.in.www1.artemis.domain.Exercise) User(de.tum.in.www1.artemis.domain.User) TutorParticipation(de.tum.in.www1.artemis.domain.participation.TutorParticipation) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 17 with TutorParticipation

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

the class ExampleSubmissionService method deleteById.

/**
 * Deletes a ExampleSubmission with the given ID, cleans up the tutor participations, removes the result and the submission
 * @param exampleSubmissionId the ID of the ExampleSubmission which should be deleted
 */
// ok
@Transactional
public void deleteById(long exampleSubmissionId) {
    Optional<ExampleSubmission> optionalExampleSubmission = exampleSubmissionRepository.findByIdWithResultsAndTutorParticipations(exampleSubmissionId);
    if (optionalExampleSubmission.isPresent()) {
        ExampleSubmission exampleSubmission = optionalExampleSubmission.get();
        for (TutorParticipation tutorParticipation : exampleSubmission.getTutorParticipations()) {
            tutorParticipation.getTrainedExampleSubmissions().remove(exampleSubmission);
        }
        Long exerciseId = exampleSubmission.getExercise().getId();
        Optional<Exercise> exerciseWithExampleSubmission = exerciseRepository.findByIdWithEagerExampleSubmissions(exerciseId);
        // Remove the reference to the exercise when the example submission is deleted
        exerciseWithExampleSubmission.ifPresent(exercise -> exercise.removeExampleSubmission(exampleSubmission));
        // due to Cascade.Remove this will also remove the submission and the result(s) in case they exist
        exampleSubmissionRepository.delete(exampleSubmission);
    }
}
Also used : ModelingExercise(de.tum.in.www1.artemis.domain.modeling.ModelingExercise) TutorParticipation(de.tum.in.www1.artemis.domain.participation.TutorParticipation) Transactional(org.springframework.transaction.annotation.Transactional)

Example 18 with TutorParticipation

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

the class TutorParticipationService method createNewParticipation.

/**
 * Given an exercise and a tutor it creates the participation of the tutor to that exercise The tutor starts a participation when she reads the grading instruction. If no
 * grading instructions are available, then she starts her participation clicking on "Start participation". Usually, the first step is `REVIEWED_INSTRUCTIONS`: after that, she
 * has to train reviewing some example submissions, and assessing others. If no example submissions are available, because the instructor hasn't created any, then she goes
 * directly to the next step, that allows her to assess students' participations
 *
 * @param exercise the exercise the tutor is going to participate in
 * @param tutor    the tutor who is going to participate in the exercise
 * @return a TutorParticipation for the exercise
 */
public TutorParticipation createNewParticipation(Exercise exercise, User tutor) {
    TutorParticipation tutorParticipation = new TutorParticipation();
    Long exampleSubmissionsCount = exampleSubmissionRepository.countAllByExerciseId(exercise.getId());
    tutorParticipation.setStatus(exampleSubmissionsCount == 0 ? TRAINED : REVIEWED_INSTRUCTIONS);
    tutorParticipation.setTutor(tutor);
    tutorParticipation.setAssessedExercise(exercise);
    return tutorParticipationRepository.saveAndFlush(tutorParticipation);
}
Also used : TutorParticipation(de.tum.in.www1.artemis.domain.participation.TutorParticipation)

Example 19 with TutorParticipation

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

the class TutorParticipationService method addExampleSubmission.

/**
 * Given an exercise, it adds to the tutor participation of that exercise the example submission passed as argument.
 * If it is valid (e.g: if it is an example submission used for tutorial, we check the result is close enough to the one of the instructor)
 *
 * @param exercise               - the exercise we are referring to
 * @param tutorExampleSubmission - the example submission to add
 * @param user                   - the user who invokes this request
 * @return the updated tutor participation
 * @throws EntityNotFoundException if example submission or tutor participation is not found
 * @throws BadRequestAlertException if tutor didn't review the instructions before assessing example submissions
 */
public TutorParticipation addExampleSubmission(Exercise exercise, ExampleSubmission tutorExampleSubmission, User user) throws EntityNotFoundException, BadRequestAlertException {
    TutorParticipation existingTutorParticipation = this.findByExerciseAndTutor(exercise, user);
    // Do not trust the user input
    Optional<ExampleSubmission> exampleSubmissionFromDatabase = exampleSubmissionRepository.findByIdWithResultsAndTutorParticipations(tutorExampleSubmission.getId());
    if (existingTutorParticipation == null || exampleSubmissionFromDatabase.isEmpty()) {
        throw new EntityNotFoundException("There isn't such example submission, or there isn't any tutor participation for this exercise");
    }
    ExampleSubmission originalExampleSubmission = exampleSubmissionFromDatabase.get();
    // Cannot start an example submission if the tutor hasn't participated in the exercise yet
    if (existingTutorParticipation.getStatus() == NOT_PARTICIPATED) {
        throw new BadRequestAlertException("The tutor needs review the instructions before assessing example submissions", ENTITY_NAME, "wrongStatus");
    }
    // Check if it is a tutorial or not
    boolean isTutorial = Boolean.TRUE.equals(originalExampleSubmission.isUsedForTutorial());
    // If it is a tutorial we check the assessment
    if (isTutorial) {
        validateTutorialExampleSubmission(tutorExampleSubmission);
    }
    List<ExampleSubmission> alreadyAssessedSubmissions = new ArrayList<>(existingTutorParticipation.getTrainedExampleSubmissions());
    // If the example submission was already assessed, we do not assess it again, we just return the current participation
    if (alreadyAssessedSubmissions.contains(tutorExampleSubmission)) {
        return existingTutorParticipation;
    }
    long numberOfExampleSubmissionsForTutor = exampleSubmissionRepository.findAllWithResultByExerciseId(exercise.getId()).stream().filter(exSub -> exSub.getSubmission() != null && exSub.getSubmission().getLatestResult() != null && Boolean.TRUE.equals(exSub.getSubmission().getLatestResult().isExampleResult())).count();
    // +1 because we haven't added yet the one we just did
    int numberOfAlreadyAssessedSubmissions = alreadyAssessedSubmissions.size() + 1;
    /*
         * When the tutor has read and assessed all the exercises, the tutor status goes to the next step.
         */
    if (numberOfAlreadyAssessedSubmissions >= numberOfExampleSubmissionsForTutor) {
        existingTutorParticipation.setStatus(TRAINED);
    }
    // keep example submission set reference with loaded submission.results to reconnect after save response from DB
    var exampleSubmissionSet = existingTutorParticipation.getTrainedExampleSubmissions();
    existingTutorParticipation = existingTutorParticipation.addTrainedExampleSubmissions(originalExampleSubmission);
    exampleSubmissionService.save(originalExampleSubmission);
    existingTutorParticipation = tutorParticipationRepository.saveAndFlush(existingTutorParticipation);
    existingTutorParticipation.setTrainedExampleSubmissions(exampleSubmissionSet);
    existingTutorParticipation.getTrainedExampleSubmissions().add(originalExampleSubmission);
    return existingTutorParticipation;
}
Also used : BadRequestAlertException(de.tum.in.www1.artemis.web.rest.errors.BadRequestAlertException) java.util(java.util) JsonProcessingException(org.springframework.cloud.cloudfoundry.com.fasterxml.jackson.core.JsonProcessingException) Logger(org.slf4j.Logger) BadRequestAlertException(de.tum.in.www1.artemis.web.rest.errors.BadRequestAlertException) FeedbackCorrectionErrorType(de.tum.in.www1.artemis.service.TutorParticipationService.FeedbackCorrectionErrorType) ObjectMapper(org.springframework.cloud.cloudfoundry.com.fasterxml.jackson.databind.ObjectMapper) LoggerFactory(org.slf4j.LoggerFactory) Collectors(java.util.stream.Collectors) EntityNotFoundException(de.tum.in.www1.artemis.web.rest.errors.EntityNotFoundException) Stream(java.util.stream.Stream) de.tum.in.www1.artemis.domain(de.tum.in.www1.artemis.domain) Service(org.springframework.stereotype.Service) TutorParticipationStatus(de.tum.in.www1.artemis.domain.enumeration.TutorParticipationStatus) ExampleSubmissionRepository(de.tum.in.www1.artemis.repository.ExampleSubmissionRepository) TutorParticipation(de.tum.in.www1.artemis.domain.participation.TutorParticipation) TutorParticipationRepository(de.tum.in.www1.artemis.repository.TutorParticipationRepository) FeedbackType(de.tum.in.www1.artemis.domain.enumeration.FeedbackType) TutorParticipation(de.tum.in.www1.artemis.domain.participation.TutorParticipation) EntityNotFoundException(de.tum.in.www1.artemis.web.rest.errors.EntityNotFoundException)

Example 20 with TutorParticipation

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

the class TutorParticipationService method removeTutorParticipations.

/**
 * This method removes the tutor participation for the example submission of an exercise
 * @param exercise  the exercise to which the example submission and tutor participation are linked to
 * @param user  the user for which the tutor participation should be removed
 */
public void removeTutorParticipations(Exercise exercise, User user) {
    if (!tutorParticipationRepository.existsByAssessedExerciseIdAndTutorId(exercise.getId(), user.getId())) {
        return;
    }
    Set<ExampleSubmission> exampleSubmissions = exampleSubmissionRepository.findAllByExerciseId(exercise.getId());
    TutorParticipation tutorParticipation = tutorParticipationRepository.findWithEagerExampleSubmissionAndResultsByAssessedExerciseAndTutor(exercise, user);
    for (ExampleSubmission exampleSubmission : exampleSubmissions) {
        Optional<ExampleSubmission> exampleSubmissionWithTutorParticipation = exampleSubmissionRepository.findByIdWithResultsAndTutorParticipations(exampleSubmission.getId());
        if (exampleSubmissionWithTutorParticipation.isPresent()) {
            exampleSubmissionWithTutorParticipation.get().removeTutorParticipations(tutorParticipation);
            tutorParticipationRepository.delete(tutorParticipation);
        }
    }
}
Also used : TutorParticipation(de.tum.in.www1.artemis.domain.participation.TutorParticipation)

Aggregations

TutorParticipation (de.tum.in.www1.artemis.domain.participation.TutorParticipation)22 Exercise (de.tum.in.www1.artemis.domain.Exercise)8 User (de.tum.in.www1.artemis.domain.User)8 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)8 ExampleSubmission (de.tum.in.www1.artemis.domain.ExampleSubmission)4 TutorParticipationStatus (de.tum.in.www1.artemis.domain.enumeration.TutorParticipationStatus)4 ModelingExercise (de.tum.in.www1.artemis.domain.modeling.ModelingExercise)4 java.util (java.util)4 Collectors (java.util.stream.Collectors)4 Logger (org.slf4j.Logger)4 LoggerFactory (org.slf4j.LoggerFactory)4 WithMockUser (org.springframework.security.test.context.support.WithMockUser)4 Service (org.springframework.stereotype.Service)4 JsonParser.parseString (com.google.gson.JsonParser.parseString)2 de.tum.in.www1.artemis.domain (de.tum.in.www1.artemis.domain)2 ProgrammingExercise (de.tum.in.www1.artemis.domain.ProgrammingExercise)2 ExerciseMapEntry (de.tum.in.www1.artemis.domain.assessment.dashboard.ExerciseMapEntry)2 FeedbackType (de.tum.in.www1.artemis.domain.enumeration.FeedbackType)2 Exam (de.tum.in.www1.artemis.domain.exam.Exam)2 ExerciseGroup (de.tum.in.www1.artemis.domain.exam.ExerciseGroup)2