Search in sources :

Example 26 with QuizSubmission

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);
    }
}
Also used : QuizSubmission(de.tum.in.www1.artemis.domain.quiz.QuizSubmission) QuizSubmissionException(de.tum.in.www1.artemis.exception.QuizSubmissionException) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 27 with QuizSubmission

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);
}
Also used : StudentParticipation(de.tum.in.www1.artemis.domain.participation.StudentParticipation) QuizExercise(de.tum.in.www1.artemis.domain.quiz.QuizExercise) Result(de.tum.in.www1.artemis.domain.Result) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 28 with QuizSubmission

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;
}
Also used : Result(de.tum.in.www1.artemis.domain.Result)

Example 29 with QuizSubmission

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;
}
Also used : EntityNotFoundException(de.tum.in.www1.artemis.web.rest.errors.EntityNotFoundException) StudentParticipation(de.tum.in.www1.artemis.domain.participation.StudentParticipation) QuizSubmissionException(de.tum.in.www1.artemis.exception.QuizSubmissionException) EntityNotFoundException(de.tum.in.www1.artemis.web.rest.errors.EntityNotFoundException)

Example 30 with 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();
}
Also used : StudentExam(de.tum.in.www1.artemis.domain.exam.StudentExam) WithMockUser(org.springframework.security.test.context.support.WithMockUser) Test(org.junit.jupiter.api.Test) AbstractSpringIntegrationBambooBitbucketJiraTest(de.tum.in.www1.artemis.AbstractSpringIntegrationBambooBitbucketJiraTest)

Aggregations

QuizSubmission (de.tum.in.www1.artemis.domain.quiz.QuizSubmission)28 QuizExercise (de.tum.in.www1.artemis.domain.quiz.QuizExercise)20 StudentParticipation (de.tum.in.www1.artemis.domain.participation.StudentParticipation)19 Result (de.tum.in.www1.artemis.domain.Result)17 SubmittedAnswer (de.tum.in.www1.artemis.domain.quiz.SubmittedAnswer)11 User (de.tum.in.www1.artemis.domain.User)10 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)10 StudentExam (de.tum.in.www1.artemis.domain.exam.StudentExam)8 QuizSubmissionException (de.tum.in.www1.artemis.exception.QuizSubmissionException)8 WithMockUser (org.springframework.security.test.context.support.WithMockUser)8 AbstractSpringIntegrationBambooBitbucketJiraTest (de.tum.in.www1.artemis.AbstractSpringIntegrationBambooBitbucketJiraTest)6 ModelingExercise (de.tum.in.www1.artemis.domain.modeling.ModelingExercise)6 ModelingSubmission (de.tum.in.www1.artemis.domain.modeling.ModelingSubmission)6 EntityNotFoundException (de.tum.in.www1.artemis.web.rest.errors.EntityNotFoundException)6 Test (org.junit.jupiter.api.Test)6 Submission (de.tum.in.www1.artemis.domain.Submission)4 AssessmentType (de.tum.in.www1.artemis.domain.enumeration.AssessmentType)4 InitializationState (de.tum.in.www1.artemis.domain.enumeration.InitializationState)4 ModelingPlagiarismResult (de.tum.in.www1.artemis.domain.plagiarism.modeling.ModelingPlagiarismResult)4 TextPlagiarismResult (de.tum.in.www1.artemis.domain.plagiarism.text.TextPlagiarismResult)4