Search in sources :

Example 91 with Exam

use of de.tum.in.www1.artemis.domain.exam.Exam in project ArTEMiS by ls1intum.

the class QuizSubmissionResource method submitQuizForExam.

/**
 * PUT /exercises/:exerciseId/submissions/exam : Update a QuizSubmission for exam mode
 *
 * @param exerciseId        the id of the exercise for which to update the submission
 * @param quizSubmission    the quizSubmission to update
 * @return                  the ResponseEntity with status 200 and body the result or the appropriate error code.
 */
@PutMapping("exercises/{exerciseId}/submissions/exam")
@PreAuthorize("hasRole('USER')")
public ResponseEntity<QuizSubmission> submitQuizForExam(@PathVariable Long exerciseId, @RequestBody QuizSubmission quizSubmission) {
    long start = System.currentTimeMillis();
    log.debug("REST request to submit QuizSubmission for exam : {}", quizSubmission);
    // recreate pointers back to submission in each submitted answer
    for (SubmittedAnswer submittedAnswer : quizSubmission.getSubmittedAnswers()) {
        submittedAnswer.setSubmission(quizSubmission);
    }
    QuizExercise quizExercise = quizExerciseRepository.findByIdWithQuestionsElseThrow(exerciseId);
    User user = userRepository.getUserWithGroupsAndAuthorities();
    // Apply further checks if it is an exam submission
    examSubmissionService.checkSubmissionAllowanceElseThrow(quizExercise, user);
    // Prevent multiple submissions (currently only for exam submissions)
    quizSubmission = (QuizSubmission) examSubmissionService.preventMultipleSubmissions(quizExercise, quizSubmission, user);
    QuizSubmission updatedQuizSubmission = quizSubmissionService.saveSubmissionForExamMode(quizExercise, quizSubmission, user.getLogin());
    long end = System.currentTimeMillis();
    log.info("submitQuizForExam took {}ms for exercise {} and user {}", end - start, exerciseId, user.getLogin());
    return ResponseEntity.ok(updatedQuizSubmission);
}
Also used : SubmittedAnswer(de.tum.in.www1.artemis.domain.quiz.SubmittedAnswer) QuizSubmission(de.tum.in.www1.artemis.domain.quiz.QuizSubmission) User(de.tum.in.www1.artemis.domain.User) QuizExercise(de.tum.in.www1.artemis.domain.quiz.QuizExercise) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 92 with Exam

use of de.tum.in.www1.artemis.domain.exam.Exam 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);
}
Also used : SubmittedAnswer(de.tum.in.www1.artemis.domain.quiz.SubmittedAnswer) User(de.tum.in.www1.artemis.domain.User) 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 93 with Exam

use of de.tum.in.www1.artemis.domain.exam.Exam in project ArTEMiS by ls1intum.

the class ParticipantScoreIntegrationTest method createIndividualTextExerciseForExam.

private void createIndividualTextExerciseForExam() {
    Exam exam;
    exam = examRepository.findWithExerciseGroupsAndExercisesById(idOfExam).get();
    var exerciseGroup0 = exam.getExerciseGroups().get(0);
    TextExercise textExercise = ModelFactory.generateTextExerciseForExam(exerciseGroup0);
    textExercise.setMaxPoints(10.0);
    textExercise.setBonusPoints(0.0);
    textExercise.setKnowledge(textAssessmentKnowledgeService.createNewKnowledge());
    textExercise = exerciseRepository.save(textExercise);
    getIdOfIndividualTextExerciseOfExam = textExercise.getId();
}
Also used : Exam(de.tum.in.www1.artemis.domain.exam.Exam)

Example 94 with Exam

use of de.tum.in.www1.artemis.domain.exam.Exam in project ArTEMiS by ls1intum.

the class ModelingExerciseIntegrationTest method createModelingExerciseForExam.

@Test
@WithMockUser(username = "instructor1", roles = "INSTRUCTOR")
public void createModelingExerciseForExam() throws Exception {
    ExerciseGroup exerciseGroup = database.addExerciseGroupWithExamAndCourse(true);
    ModelingExercise modelingExercise = ModelFactory.generateModelingExerciseForExam(DiagramType.ClassDiagram, exerciseGroup);
    String title = "New Exam Modeling Exercise";
    DifficultyLevel difficulty = DifficultyLevel.HARD;
    modelingExercise.setTitle(title);
    modelingExercise.setDifficulty(difficulty);
    ModelingExercise newModelingExercise = request.postWithResponseBody("/api/modeling-exercises/", modelingExercise, ModelingExercise.class, HttpStatus.CREATED);
    assertThat(newModelingExercise.getTitle()).as("modeling exercise title was correctly set").isEqualTo(title);
    assertThat(newModelingExercise.getDifficulty()).as("modeling exercise difficulty was correctly set").isEqualTo(difficulty);
    assertThat(newModelingExercise.isCourseExercise()).as("course was not set for exam exercise").isFalse();
    assertThat(newModelingExercise.getExerciseGroup()).as("exerciseGroup was set for exam exercise").isNotNull();
    assertThat(newModelingExercise.getExerciseGroup().getId()).as("exerciseGroupId was set correctly").isEqualTo(exerciseGroup.getId());
}
Also used : ModelingExercise(de.tum.in.www1.artemis.domain.modeling.ModelingExercise) ExerciseGroup(de.tum.in.www1.artemis.domain.exam.ExerciseGroup) WithMockUser(org.springframework.security.test.context.support.WithMockUser) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 95 with Exam

use of de.tum.in.www1.artemis.domain.exam.Exam in project ArTEMiS by ls1intum.

the class ExerciseTest method getExamViaExerciseGroupOrCourseMember_withoutExamExercise.

@Test
public void getExamViaExerciseGroupOrCourseMember_withoutExamExercise() {
    Exercise examExercise = mock(Exercise.class, CALLS_REAL_METHODS);
    when(examExercise.isExamExercise()).thenReturn(false);
    Exam result = examExercise.getExamViaExerciseGroupOrCourseMember();
    assertThat(result).isNull();
}
Also used : ModelingExercise(de.tum.in.www1.artemis.domain.modeling.ModelingExercise) Exam(de.tum.in.www1.artemis.domain.exam.Exam) Test(org.junit.jupiter.api.Test)

Aggregations

Exam (de.tum.in.www1.artemis.domain.exam.Exam)228 StudentExam (de.tum.in.www1.artemis.domain.exam.StudentExam)180 WithMockUser (org.springframework.security.test.context.support.WithMockUser)164 Test (org.junit.jupiter.api.Test)158 ExerciseGroup (de.tum.in.www1.artemis.domain.exam.ExerciseGroup)92 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)75 ModelingExercise (de.tum.in.www1.artemis.domain.modeling.ModelingExercise)52 EntityNotFoundException (de.tum.in.www1.artemis.web.rest.errors.EntityNotFoundException)48 AbstractSpringIntegrationBambooBitbucketJiraTest (de.tum.in.www1.artemis.AbstractSpringIntegrationBambooBitbucketJiraTest)46 StudentParticipation (de.tum.in.www1.artemis.domain.participation.StudentParticipation)46 QuizExercise (de.tum.in.www1.artemis.domain.quiz.QuizExercise)42 ZonedDateTime (java.time.ZonedDateTime)40 BadRequestAlertException (de.tum.in.www1.artemis.web.rest.errors.BadRequestAlertException)38 Course (de.tum.in.www1.artemis.domain.Course)36 GradingScale (de.tum.in.www1.artemis.domain.GradingScale)34 BeforeEach (org.junit.jupiter.api.BeforeEach)30 User (de.tum.in.www1.artemis.domain.User)27 Collectors (java.util.stream.Collectors)26 ModelingSubmission (de.tum.in.www1.artemis.domain.modeling.ModelingSubmission)24 de.tum.in.www1.artemis.repository (de.tum.in.www1.artemis.repository)24