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();
}
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);
}
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());
}
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());
}
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;
}
Aggregations