use of de.tum.in.www1.artemis.domain.metis.Post in project ArTEMiS by ls1intum.
the class QuizSubmissionResource method submitForPractice.
/**
* POST /exercises/:exerciseId/submissions/practice : Submit a new quizSubmission for practice mode.
*
* @param exerciseId the id of the exercise for which to init a participation
* @param quizSubmission the quizSubmission to submit
* @return the ResponseEntity with status 200 (OK) and the Result as its body, or with status 4xx if the request is invalid
*/
@PostMapping("/exercises/{exerciseId}/submissions/practice")
@PreAuthorize("hasRole('USER')")
public ResponseEntity<Result> submitForPractice(@PathVariable Long exerciseId, @RequestBody QuizSubmission quizSubmission) {
log.debug("REST request to submit QuizSubmission for practice : {}", quizSubmission);
// recreate pointers back to submission in each submitted answer
for (SubmittedAnswer submittedAnswer : quizSubmission.getSubmittedAnswers()) {
submittedAnswer.setSubmission(quizSubmission);
}
if (quizSubmission.getId() != null) {
return ResponseEntity.badRequest().headers(HeaderUtil.createFailureAlert(applicationName, true, ENTITY_NAME, "idexists", "A new quizSubmission cannot already have an ID.")).body(null);
}
QuizExercise quizExercise = quizExerciseRepository.findByIdWithQuestionsElseThrow(exerciseId);
User user = userRepository.getUserWithGroupsAndAuthorities();
if (!authCheckService.isAllowedToSeeExercise(quizExercise, user)) {
return ResponseEntity.status(403).headers(HeaderUtil.createFailureAlert(applicationName, true, "submission", "Forbidden", "You are not allowed to participate in this exercise.")).body(null);
}
// Note that exam quiz exercises do not have an end date, so we need to check in that order
if (!Boolean.TRUE.equals(quizExercise.isIsOpenForPractice()) || !quizExercise.isQuizEnded()) {
return ResponseEntity.badRequest().headers(HeaderUtil.createFailureAlert(applicationName, true, "submission", "exerciseNotOpenForPractice", "The exercise is not open for practice or hasn't ended yet.")).body(null);
}
// the following method either reuses an existing participation or creates a new one
StudentParticipation participation = participationService.startExercise(quizExercise, user, false);
// we set the exercise again to prevent issues with lazy loaded quiz questions
participation.setExercise(quizExercise);
// update and save submission
Result result = quizSubmissionService.submitForPractice(quizSubmission, quizExercise, participation);
// The quizScheduler is usually responsible for updating the participation to FINISHED in the database. If quizzes where the student did not participate are used for
// practice, the QuizScheduler does not update the participation, that's why we update it manually here
participation.setInitializationState(InitializationState.FINISHED);
studentParticipationRepository.saveAndFlush(participation);
// remove some redundant or unnecessary data that is not needed on client side
for (SubmittedAnswer answer : quizSubmission.getSubmittedAnswers()) {
answer.getQuizQuestion().setQuizQuestionStatistic(null);
}
quizExercise.setQuizPointStatistic(null);
quizExercise.setCourse(null);
messagingService.broadcastNewResult(result.getParticipation(), result);
// return result with quizSubmission, participation and quiz exercise (including the solution)
return ResponseEntity.ok(result);
}
use of de.tum.in.www1.artemis.domain.metis.Post in project ArTEMiS by ls1intum.
the class TutorParticipationResource method initTutorParticipation.
/**
* POST /exercises/:exerciseId/tutor-participations : start the "id" exercise for the current tutor. A tutor participation will be created and returned for the exercise given by
* the exercise id. The tutor participation status will be assigned based on which features are available for the exercise (e.g. grading instructions) The method is valid only
* for tutors, since it inits the tutor participation to the exercise, which is different from a standard participation
*
* @param exerciseId the id of the exercise for which to init a tutorParticipations
* @return the ResponseEntity with status 200 (OK) and with body the exercise, or with status 404 (Not Found)
* @throws URISyntaxException if URI path can't be created
*/
@PostMapping(value = "/exercises/{exerciseId}/tutor-participations")
@PreAuthorize("hasRole('TA')")
public ResponseEntity<TutorParticipation> initTutorParticipation(@PathVariable Long exerciseId) throws URISyntaxException {
log.debug("REST request to start tutor participation : {}", exerciseId);
Exercise exercise = exerciseRepository.findByIdElseThrow(exerciseId);
User user = userRepository.getUserWithGroupsAndAuthorities();
authorizationCheckService.checkHasAtLeastRoleForExerciseElseThrow(Role.TEACHING_ASSISTANT, exercise, user);
if (tutorParticipationRepository.existsByAssessedExerciseIdAndTutorId(exerciseId, user.getId())) {
// tutorParticipation already exists
return ResponseEntity.badRequest().headers(HeaderUtil.createFailureAlert(applicationName, true, ENTITY_NAME, "tutorParticipationAlreadyExists", "There is already a tutorParticipations for the given exercise and user.")).body(null);
}
TutorParticipation tutorParticipation = tutorParticipationService.createNewParticipation(exercise, user);
return ResponseEntity.created(new URI("/api/exercises/" + exerciseId + "tutor-participations/" + tutorParticipation.getId())).body(tutorParticipation);
}
use of de.tum.in.www1.artemis.domain.metis.Post in project ArTEMiS by ls1intum.
the class ModelingSubmissionIntegrationTest method getModelSubmissionWithResult_involved_allowed.
@Test
@WithMockUser(username = "student1", roles = "USER")
public void getModelSubmissionWithResult_involved_allowed() throws Exception {
ModelingSubmission submission = ModelFactory.generateModelingSubmission(validModel, true);
submission = database.addModelingSubmission(classExercise, submission, "student1");
PlagiarismComparison<ModelingSubmissionElement> plagiarismComparison = new PlagiarismComparison<>();
PlagiarismSubmission<ModelingSubmissionElement> submissionA = new PlagiarismSubmission<>();
submissionA.setStudentLogin("student1");
submissionA.setSubmissionId(submission.getId());
plagiarismComparison.setSubmissionA(submissionA);
PlagiarismCase plagiarismCase = new PlagiarismCase();
plagiarismCase = plagiarismCaseRepository.save(plagiarismCase);
Post post = new Post();
post.setAuthor(userRepo.getUserByLoginElseThrow("instructor1"));
post.setTitle("Title Plagiarism Case Post");
post.setContent("Content Plagiarism Case Post");
post.setVisibleForStudents(true);
post.setPlagiarismCase(plagiarismCase);
postRepository.save(post);
submissionA.setPlagiarismCase(plagiarismCase);
plagiarismComparisonRepository.save(plagiarismComparison);
var submissionResult = request.get("/api/modeling-submissions/" + submission.getId(), HttpStatus.OK, ModelingSubmission.class);
assertThat(submissionResult.getParticipation()).as("Should anonymize participation").isNull();
assertThat(submissionResult.getResults()).as("Should anonymize results").isEmpty();
assertThat(submissionResult.getSubmissionDate()).as("Should anonymize submission date").isNull();
}
use of de.tum.in.www1.artemis.domain.metis.Post in project ArTEMiS by ls1intum.
the class PostIntegrationTest method testValidatePostContextConstraintViolation.
@Test
@WithMockUser(username = "student1", roles = "USER")
public void testValidatePostContextConstraintViolation() throws Exception {
Post invalidPost = createPostWithoutContext();
request.postWithResponseBody("/api/courses/" + courseId + "/posts", invalidPost, Post.class, HttpStatus.BAD_REQUEST);
invalidPost = createPostWithoutContext();
invalidPost.setCourseWideContext(CourseWideContext.ORGANIZATION);
invalidPost.setLecture(existingLecturePosts.get(0).getLecture());
request.postWithResponseBody("/api/courses/" + courseId + "/posts", invalidPost, Post.class, HttpStatus.BAD_REQUEST);
Set<ConstraintViolation<Post>> constraintViolations = validator.validate(invalidPost);
assertThat(constraintViolations).hasSize(1);
invalidPost = createPostWithoutContext();
invalidPost.setCourseWideContext(CourseWideContext.ORGANIZATION);
invalidPost.setExercise(existingExercisePosts.get(0).getExercise());
request.postWithResponseBody("/api/courses/" + courseId + "/posts", invalidPost, Post.class, HttpStatus.BAD_REQUEST);
constraintViolations = validator.validate(invalidPost);
assertThat(constraintViolations).hasSize(1);
invalidPost = createPostWithoutContext();
invalidPost.setLecture(existingLecturePosts.get(0).getLecture());
invalidPost.setExercise(existingExercisePosts.get(0).getExercise());
request.postWithResponseBody("/api/courses/" + courseId + "/posts", invalidPost, Post.class, HttpStatus.BAD_REQUEST);
constraintViolations = validator.validate(invalidPost);
assertThat(constraintViolations).hasSize(1);
}
use of de.tum.in.www1.artemis.domain.metis.Post in project ArTEMiS by ls1intum.
the class PostIntegrationTest method testEditPostByChangingContext2_asTutor.
@Test
@WithMockUser(username = "tutor1", roles = "TA")
public void testEditPostByChangingContext2_asTutor() throws Exception {
// update lecture post
Post postToUpdate = existingLecturePosts.get(0);
// change to context to exercise
postToUpdate.setLecture(null);
postToUpdate.setExercise(this.existingExercisePosts.get(0).getExercise());
Post updatedPost = request.putWithResponseBody("/api/courses/" + courseId + "/posts/" + postToUpdate.getId(), postToUpdate, Post.class, HttpStatus.OK);
database.assertSensitiveInformationHidden(updatedPost);
assertThat(updatedPost).isEqualTo(postToUpdate);
}
Aggregations