Search in sources :

Example 11 with StudentExam

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

the class ExamIntegrationTest method testRemovingAllStudentsAndParticipations.

@Test
@WithMockUser(username = "admin", roles = "ADMIN")
public void testRemovingAllStudentsAndParticipations() throws Exception {
    Exam exam = database.setupExamWithExerciseGroupsExercisesRegisteredStudents(course1);
    // Generate student exams
    List<StudentExam> studentExams = request.postListWithResponseBody("/api/courses/" + course1.getId() + "/exams/" + exam.getId() + "/generate-student-exams", Optional.empty(), StudentExam.class, HttpStatus.OK);
    assertThat(studentExams).hasSize(4);
    assertThat(exam.getRegisteredUsers()).hasSize(4);
    // /courses/{courseId}/exams/{examId}/student-exams/start-exercises
    Integer numberOfGeneratedParticipations = request.postWithResponseBody("/api/courses/" + course1.getId() + "/exams/" + exam.getId() + "/student-exams/start-exercises", Optional.empty(), Integer.class, HttpStatus.OK);
    assertThat(numberOfGeneratedParticipations).isEqualTo(16);
    // Fetch student exams
    List<StudentExam> studentExamsDB = request.getList("/api/courses/" + course1.getId() + "/exams/" + exam.getId() + "/student-exams", HttpStatus.OK, StudentExam.class);
    assertThat(studentExamsDB).hasSize(4);
    List<StudentParticipation> participationList = new ArrayList<>();
    Exercise[] exercises = examRepository.findAllExercisesByExamId(exam.getId()).toArray(new Exercise[0]);
    for (Exercise value : exercises) {
        participationList.addAll(studentParticipationRepository.findByExerciseId(value.getId()));
    }
    assertThat(participationList).hasSize(16);
    // TODO there should be some participation but no submissions unfortunately
    // remove all students
    var paramsParticipations = new LinkedMultiValueMap<String, String>();
    paramsParticipations.add("withParticipationsAndSubmission", "true");
    request.delete("/api/courses/" + course1.getId() + "/exams/" + exam.getId() + "/students", HttpStatus.OK, paramsParticipations);
    // Get the exam with all registered users
    var params = new LinkedMultiValueMap<String, String>();
    params.add("withStudents", "true");
    Exam storedExam = request.get("/api/courses/" + course1.getId() + "/exams/" + exam.getId(), HttpStatus.OK, Exam.class, params);
    assertThat(storedExam.getRegisteredUsers()).isEmpty();
    // Fetch student exams
    studentExamsDB = request.getList("/api/courses/" + course1.getId() + "/exams/" + exam.getId() + "/student-exams", HttpStatus.OK, StudentExam.class);
    assertThat(studentExamsDB).isEmpty();
    // Fetch participations
    exercises = examRepository.findAllExercisesByExamId(exam.getId()).toArray(new Exercise[0]);
    participationList = new ArrayList<>();
    for (Exercise exercise : exercises) {
        participationList.addAll(studentParticipationRepository.findByExerciseId(exercise.getId()));
    }
    assertThat(participationList).isEmpty();
}
Also used : QuizExercise(de.tum.in.www1.artemis.domain.quiz.QuizExercise) ModelingExercise(de.tum.in.www1.artemis.domain.modeling.ModelingExercise) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) StudentExam(de.tum.in.www1.artemis.domain.exam.StudentExam) StudentExam(de.tum.in.www1.artemis.domain.exam.StudentExam) Exam(de.tum.in.www1.artemis.domain.exam.Exam) StudentParticipation(de.tum.in.www1.artemis.domain.participation.StudentParticipation) WithMockUser(org.springframework.security.test.context.support.WithMockUser) Test(org.junit.jupiter.api.Test)

Example 12 with StudentExam

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

the class ExamIntegrationTest method testStartExercisesWithModelingExercise.

@Test
@WithMockUser(username = "instructor1", roles = "INSTRUCTOR")
public void testStartExercisesWithModelingExercise() throws Exception {
    // TODO IMPORTANT test more complex exam configurations (mixed exercise type, more variants and more registered students)
    // registering users
    var student1 = database.getUserByLogin("student1");
    var student2 = database.getUserByLogin("student2");
    var registeredUsers = Set.of(student1, student2);
    exam2.setRegisteredUsers(registeredUsers);
    // setting dates
    exam2.setStartDate(now().plusHours(2));
    exam2.setEndDate(now().plusHours(3));
    exam2.setVisibleDate(now().plusHours(1));
    // creating exercise
    ModelingExercise modelingExercise = ModelFactory.generateModelingExerciseForExam(DiagramType.ClassDiagram, exam2.getExerciseGroups().get(0));
    exam2.getExerciseGroups().get(0).addExercise(modelingExercise);
    exerciseGroupRepository.save(exam2.getExerciseGroups().get(0));
    modelingExercise = exerciseRepo.save(modelingExercise);
    List<StudentExam> createdStudentExams = new ArrayList<>();
    // creating student exams
    for (User user : registeredUsers) {
        StudentExam studentExam = new StudentExam();
        studentExam.addExercise(modelingExercise);
        studentExam.setUser(user);
        exam2.addStudentExam(studentExam);
        createdStudentExams.add(studentExamRepository.save(studentExam));
    }
    exam2 = examRepository.save(exam2);
    // invoke start exercises
    Integer noGeneratedParticipations = request.postWithResponseBody("/api/courses/" + course1.getId() + "/exams/" + exam2.getId() + "/student-exams/start-exercises", Optional.empty(), Integer.class, HttpStatus.OK);
    assertThat(noGeneratedParticipations).isEqualTo(exam2.getStudentExams().size());
    List<Participation> studentParticipations = participationTestRepository.findAllWithSubmissions();
    for (Participation participation : studentParticipations) {
        assertThat(participation.getExercise()).isEqualTo(modelingExercise);
        assertThat(participation.getExercise().getCourseViaExerciseGroupOrCourseMember()).isNotNull();
        assertThat(participation.getExercise().getExerciseGroup()).isEqualTo(exam2.getExerciseGroups().get(0));
        assertThat(participation.getSubmissions()).hasSize(1);
        var textSubmission = (ModelingSubmission) participation.getSubmissions().iterator().next();
        assertThat(textSubmission.getModel()).isNull();
        assertThat(textSubmission.getExplanationText()).isNull();
    }
    // Cleanup of Bidirectional Relationships
    for (StudentExam studentExam : createdStudentExams) {
        exam2.removeStudentExam(studentExam);
    }
    examRepository.save(exam2);
}
Also used : Participation(de.tum.in.www1.artemis.domain.participation.Participation) StudentParticipation(de.tum.in.www1.artemis.domain.participation.StudentParticipation) WithMockUser(org.springframework.security.test.context.support.WithMockUser) ModelingSubmission(de.tum.in.www1.artemis.domain.modeling.ModelingSubmission) ModelingExercise(de.tum.in.www1.artemis.domain.modeling.ModelingExercise) StudentExam(de.tum.in.www1.artemis.domain.exam.StudentExam) WithMockUser(org.springframework.security.test.context.support.WithMockUser) Test(org.junit.jupiter.api.Test)

Example 13 with StudentExam

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

the class ExamRegistrationService method unregisterAllStudentFromExam.

/**
 * Unregisters all students from the exam
 *
 * @param examId the exam for which a student should be unregistered
 * @param deleteParticipationsAndSubmission whether the participations and submissions of the student should be deleted
 */
public void unregisterAllStudentFromExam(Long examId, boolean deleteParticipationsAndSubmission) {
    var exam = examRepository.findWithRegisteredUsersById(examId).orElseThrow(() -> new EntityNotFoundException("Exam", examId));
    // remove all registered students
    List<Long> userIds = new ArrayList<>();
    exam.getRegisteredUsers().forEach(user -> userIds.add(user.getId()));
    List<User> registeredStudentsList = userRepository.findAllById(userIds);
    registeredStudentsList.forEach(exam::removeRegisteredUser);
    examRepository.save(exam);
    // remove all students exams
    Set<StudentExam> studentExams = studentExamRepository.findAllWithExercisesByExamId(examId);
    studentExams.forEach(studentExam -> removeStudentExam(studentExam, deleteParticipationsAndSubmission));
    User currentUser = userRepository.getUserWithGroupsAndAuthorities();
    AuditEvent auditEvent = new AuditEvent(currentUser.getLogin(), Constants.REMOVE_ALL_USERS_FROM_EXAM, "exam=" + exam.getTitle());
    auditEventRepository.add(auditEvent);
    log.info("User {} has removed all users from the exam {} with id {}. This also deleted potentially existing student exams with all its participations and submissions.", currentUser.getLogin(), exam.getTitle(), exam.getId());
}
Also used : User(de.tum.in.www1.artemis.domain.User) AuditEvent(org.springframework.boot.actuate.audit.AuditEvent) EntityNotFoundException(de.tum.in.www1.artemis.web.rest.errors.EntityNotFoundException) StudentExam(de.tum.in.www1.artemis.domain.exam.StudentExam)

Example 14 with StudentExam

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

the class ExamRegistrationService method unregisterStudentFromExam.

/**
 * @param examId the exam for which a student should be unregistered
 * @param deleteParticipationsAndSubmission whether the participations and submissions of the student should be deleted
 * @param student the user object that should be unregistered
 */
public void unregisterStudentFromExam(Long examId, boolean deleteParticipationsAndSubmission, User student) {
    var exam = examRepository.findWithRegisteredUsersById(examId).orElseThrow(() -> new EntityNotFoundException("Exam", examId));
    exam.removeRegisteredUser(student);
    // Note: we intentionally do not remove the user from the course, because the student might just have "unregistered" from the exam, but should
    // still have access to the course.
    examRepository.save(exam);
    // The student exam might already be generated, then we need to delete it
    Optional<StudentExam> optionalStudentExam = studentExamRepository.findWithExercisesByUserIdAndExamId(student.getId(), exam.getId());
    optionalStudentExam.ifPresent(studentExam -> removeStudentExam(studentExam, deleteParticipationsAndSubmission));
    User currentUser = userRepository.getUserWithGroupsAndAuthorities();
    AuditEvent auditEvent = new AuditEvent(currentUser.getLogin(), Constants.REMOVE_USER_FROM_EXAM, "exam=" + exam.getTitle(), "user=" + student.getLogin());
    auditEventRepository.add(auditEvent);
    log.info("User {} has removed user {} from the exam {} with id {}. This also deleted a potentially existing student exam with all its participations and submissions.", currentUser.getLogin(), student.getLogin(), exam.getTitle(), exam.getId());
}
Also used : User(de.tum.in.www1.artemis.domain.User) AuditEvent(org.springframework.boot.actuate.audit.AuditEvent) EntityNotFoundException(de.tum.in.www1.artemis.web.rest.errors.EntityNotFoundException) StudentExam(de.tum.in.www1.artemis.domain.exam.StudentExam)

Example 15 with StudentExam

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

the class ExamSessionService method startExamSession.

/**
 * Creates and saves an exam session for given student exam
 *
 * @param studentExam student exam for which an exam session shall be created
 * @param fingerprint the browser fingerprint reported by the client, can be null
 * @param userAgent the user agent of the client, can be null
 * @param instanceId the instance id of the client, can be null
 * @param ipAddress the ip address of the client, can be null
 * @return the newly create exam session
 */
public ExamSession startExamSession(StudentExam studentExam, @Nullable String fingerprint, @Nullable String userAgent, @Nullable String instanceId, @Nullable IPAddress ipAddress) {
    log.debug("Exam session started");
    String sessionToken = generateSafeToken();
    ExamSession examSession = new ExamSession();
    examSession.setSessionToken(sessionToken);
    examSession.setStudentExam(studentExam);
    examSession.setBrowserFingerprintHash(fingerprint);
    examSession.setUserAgent(userAgent);
    examSession.setInstanceId(instanceId);
    examSession.setIpAddress(ipAddress);
    examSession = examSessionRepository.save(examSession);
    return examSession;
}
Also used : ExamSession(de.tum.in.www1.artemis.domain.exam.ExamSession)

Aggregations

StudentExam (de.tum.in.www1.artemis.domain.exam.StudentExam)69 WithMockUser (org.springframework.security.test.context.support.WithMockUser)44 Test (org.junit.jupiter.api.Test)42 Exam (de.tum.in.www1.artemis.domain.exam.Exam)37 StudentParticipation (de.tum.in.www1.artemis.domain.participation.StudentParticipation)22 ModelingExercise (de.tum.in.www1.artemis.domain.modeling.ModelingExercise)20 QuizExercise (de.tum.in.www1.artemis.domain.quiz.QuizExercise)18 EntityNotFoundException (de.tum.in.www1.artemis.web.rest.errors.EntityNotFoundException)18 AbstractSpringIntegrationBambooBitbucketJiraTest (de.tum.in.www1.artemis.AbstractSpringIntegrationBambooBitbucketJiraTest)16 ModelingSubmission (de.tum.in.www1.artemis.domain.modeling.ModelingSubmission)14 ZonedDateTime (java.time.ZonedDateTime)14 ExerciseGroup (de.tum.in.www1.artemis.domain.exam.ExerciseGroup)12 Participation (de.tum.in.www1.artemis.domain.participation.Participation)12 de.tum.in.www1.artemis.repository (de.tum.in.www1.artemis.repository)12 java.util (java.util)12 Collectors (java.util.stream.Collectors)12 LinkedMultiValueMap (org.springframework.util.LinkedMultiValueMap)12 de.tum.in.www1.artemis.domain (de.tum.in.www1.artemis.domain)10 AssessmentType (de.tum.in.www1.artemis.domain.enumeration.AssessmentType)10 de.tum.in.www1.artemis.web.rest.dto (de.tum.in.www1.artemis.web.rest.dto)10