use of de.tum.in.www1.artemis.domain.quiz.QuizSubmission in project ArTEMiS by ls1intum.
the class QuizSubmissionResource method submitForLiveMode.
/**
* POST /exercises/:exerciseId/submissions/live : Submit a new quizSubmission for live 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/live")
@PreAuthorize("hasRole('USER')")
public ResponseEntity<QuizSubmission> submitForLiveMode(@PathVariable Long exerciseId, @RequestBody QuizSubmission quizSubmission) {
log.debug("REST request to submit QuizSubmission for live mode : {}", quizSubmission);
String userLogin = SecurityUtils.getCurrentUserLogin().orElseThrow();
try {
// we set the submitted flag to true on the server side
quizSubmission.setSubmitted(true);
QuizSubmission updatedQuizSubmission = quizSubmissionService.saveSubmissionForLiveMode(exerciseId, quizSubmission, userLogin, true);
return ResponseEntity.ok(updatedQuizSubmission);
} catch (QuizSubmissionException e) {
log.warn("QuizSubmissionException: {} for user {} in quiz {}", e.getMessage(), userLogin, exerciseId);
return ResponseEntity.badRequest().headers(HeaderUtil.createFailureAlert(applicationName, true, ENTITY_NAME, "quizSubmissionError", e.getMessage())).body(null);
}
}
use of de.tum.in.www1.artemis.domain.quiz.QuizSubmission in project ArTEMiS by ls1intum.
the class QuizSubmissionResource method submitForPreview.
/**
* POST /exercises/:exerciseId/submissions/preview : Submit a new quizSubmission for preview mode. Note that in this case, nothing will be saved in database.
*
* @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 and body the result or the appropriate error code.
*/
@PostMapping("exercises/{exerciseId}/submissions/preview")
@PreAuthorize("hasRole('TA')")
public ResponseEntity<Result> submitForPreview(@PathVariable Long exerciseId, @RequestBody QuizSubmission quizSubmission) {
log.debug("REST request to submit QuizSubmission for preview : {}", 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);
authCheckService.checkHasAtLeastRoleForExerciseElseThrow(Role.TEACHING_ASSISTANT, quizExercise, null);
// update submission
quizSubmission.setSubmitted(true);
quizSubmission.setType(SubmissionType.MANUAL);
quizSubmission.calculateAndUpdateScores(quizExercise);
// create Participation stub
StudentParticipation participation = new StudentParticipation().exercise(quizExercise);
// create result
Result result = new Result().participation(participation).submission(quizSubmission);
result.setRated(false);
result.setAssessmentType(AssessmentType.AUTOMATIC);
result.setCompletionDate(ZonedDateTime.now());
// calculate score and update result accordingly
result.evaluateQuizSubmission();
return ResponseEntity.ok(result);
}
use of de.tum.in.www1.artemis.domain.quiz.QuizSubmission in project ArTEMiS by ls1intum.
the class QuizSubmissionService method submitForPractice.
/**
* Submit the given submission for practice
*
* @param quizSubmission the submission to submit
* @param quizExercise the exercise to submit in
* @param participation the participation where the result should be saved
* @return the result entity
*/
public Result submitForPractice(QuizSubmission quizSubmission, QuizExercise quizExercise, Participation participation) {
// update submission properties
quizSubmission.setSubmitted(true);
quizSubmission.setType(SubmissionType.MANUAL);
quizSubmission.setSubmissionDate(ZonedDateTime.now());
// calculate scores
quizSubmission.calculateAndUpdateScores(quizExercise);
// save parent submission object
quizSubmission = quizSubmissionRepository.save(quizSubmission);
// create result
Result result = new Result().participation(participation);
result.setRated(false);
result.setAssessmentType(AssessmentType.AUTOMATIC);
result.setCompletionDate(ZonedDateTime.now());
// save result
result = resultRepository.save(result);
// setup result - submission relation
result.setSubmission(quizSubmission);
// calculate score and update result accordingly
result.evaluateQuizSubmission();
quizSubmission.addResult(result);
quizSubmission.setParticipation(participation);
// save submission to set result index column
quizSubmissionRepository.save(quizSubmission);
// save result to store score
resultRepository.save(result);
// result.participation.exercise.quizQuestions turn into proxy objects after saving, so we need to set it again to prevent problems later on
result.setParticipation(participation);
// add result to statistics
quizScheduleService.addResultForStatisticUpdate(quizExercise.getId(), result);
log.debug("submit practice quiz finished: {}", quizSubmission);
return result;
}
use of de.tum.in.www1.artemis.domain.quiz.QuizSubmission in project ArTEMiS by ls1intum.
the class QuizSubmissionService method saveSubmissionForExamMode.
/**
* Updates a submission for the exam mode
*
* @param quizExercise the quiz exercise for which the submission for the exam mode should be done
* @param quizSubmission the quiz submission includes the submitted answers by the student
* @param user the student who wants to submit the quiz during the exam
* @return the updated quiz submission after it has been saved to the database
*/
public QuizSubmission saveSubmissionForExamMode(QuizExercise quizExercise, QuizSubmission quizSubmission, String user) {
// update submission properties
quizSubmission.setSubmitted(true);
quizSubmission.setType(SubmissionType.MANUAL);
quizSubmission.setSubmissionDate(ZonedDateTime.now());
Optional<StudentParticipation> optionalParticipation = participationService.findOneByExerciseAndStudentLoginAnyState(quizExercise, user);
if (optionalParticipation.isEmpty()) {
log.warn("The participation for quiz exercise {}, quiz submission {} and user {} was not found", quizExercise.getId(), quizSubmission.getId(), user);
// TODO: think of better way to handle failure
throw new EntityNotFoundException("Participation for quiz exercise " + quizExercise.getId() + " and quiz submission " + quizSubmission.getId() + " for user " + user + " was not found!");
}
StudentParticipation studentParticipation = optionalParticipation.get();
quizSubmission.setParticipation(studentParticipation);
// remove result from submission (in the unlikely case it is passed here), so that students cannot inject a result
quizSubmission.setResults(new ArrayList<>());
quizSubmissionRepository.save(quizSubmission);
// versioning of submission
try {
submissionVersionService.saveVersionForIndividual(quizSubmission, user);
} catch (Exception ex) {
log.error("Quiz submission version could not be saved", ex);
}
log.debug("submit exam quiz finished: {}", quizSubmission);
return quizSubmission;
}
use of de.tum.in.www1.artemis.domain.quiz.QuizSubmission in project ArTEMiS by ls1intum.
the class ExamQuizServiceTest method evaluateQuiz.
@Test
@WithMockUser(username = "instructor1", roles = "INSTRUCTOR")
public void evaluateQuiz() throws Exception {
for (int i = 0; i < numberOfParticipants; i++) {
exam.addRegisteredUser(users.get(i));
}
exam = examRepository.save(exam);
exerciseGroup.setExam(exam);
exerciseGroup = exerciseGroupRepository.save(exerciseGroup);
exam.setExerciseGroups(List.of(exerciseGroup));
quizExercise.setExerciseGroup(exerciseGroup);
quizExercise = quizExerciseService.save(quizExercise);
exerciseGroup.setExercises(Set.of(quizExercise));
assertThat(studentExamRepository.generateStudentExams(exam)).hasSize(numberOfParticipants);
assertThat(studentExamRepository.findByExamId(exam.getId())).hasSize(numberOfParticipants);
assertThat(studentExamService.startExercises(exam.getId())).isEqualTo(numberOfParticipants);
for (int i = 0; i < numberOfParticipants; i++) {
database.changeUser("student" + (i + 1));
QuizSubmission quizSubmission = database.generateSubmissionForThreeQuestions(quizExercise, i + 1, true, ZonedDateTime.now());
request.put("/api/exercises/" + quizExercise.getId() + "/submissions/exam", quizSubmission, HttpStatus.OK);
}
database.changeUser("instructor1");
// All exams should be over before evaluation
for (StudentExam studentExam : studentExamRepository.findByExamId(exam.getId())) {
studentExam.setWorkingTime(0);
studentExamRepository.save(studentExam);
}
Integer numberOfEvaluatedExercises = request.postWithResponseBody("/api/courses/" + course.getId() + "/exams/" + exam.getId() + "/student-exams/evaluate-quiz-exercises", Optional.empty(), Integer.class, HttpStatus.OK);
assertThat(numberOfEvaluatedExercises).isEqualTo(1);
checkStatistics(quizExercise);
studentExamRepository.deleteAll();
// Make sure delete also works if so many objects have been created before
request.delete("/api/courses/" + course.getId() + "/exams/" + exam.getId(), HttpStatus.OK);
userRepository.deleteAll();
}
Aggregations