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);
}
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);
}
}
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);
}
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;
}
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);
}
}
}
Aggregations