use of de.tum.in.www1.artemis.domain.exam.StudentExam in project Artemis by ls1intum.
the class ExamSubmissionService method isSubmissionInTime.
private boolean isSubmissionInTime(Exercise exercise, StudentExam studentExam, boolean withGracePeriod) {
// The attributes of the exam (e.g. startDate) are missing. Therefore we need to load it.
Exam exam = examRepository.findByIdElseThrow(exercise.getExerciseGroup().getExam().getId());
ZonedDateTime calculatedEndDate = withGracePeriod ? exam.getEndDate().plusSeconds(exam.getGracePeriod()) : exam.getEndDate();
if (studentExam.getWorkingTime() != null && studentExam.getWorkingTime() > 0) {
calculatedEndDate = withGracePeriod ? studentExam.getIndividualEndDateWithGracePeriod() : studentExam.getIndividualEndDate();
}
return exam.getStartDate().isBefore(ZonedDateTime.now()) && calculatedEndDate.isAfter(ZonedDateTime.now());
}
use of de.tum.in.www1.artemis.domain.exam.StudentExam in project Artemis by ls1intum.
the class StudentExamAccessService method checkStudentExamAccessElseThrow.
/**
* Checks if the current user is allowed to see the requested student exam.
*
* @param courseId the if of the course
* @param examId the id of the exam
* @param studentExamId the id of the student exam
* @param currentUser the current user
* @param isTestRun flag to determine if this is a test run or not
*/
public void checkStudentExamAccessElseThrow(Long courseId, Long examId, Long studentExamId, User currentUser, boolean isTestRun) {
checkCourseAndExamAccessElseThrow(courseId, examId, currentUser, isTestRun);
// Check that the student exam exists
StudentExam studentExam = studentExamRepository.findByIdElseThrow(studentExamId);
// Check that the examId equals the id of the exam of the student exam
if (!studentExam.getExam().getId().equals(examId)) {
throw new ConflictException("The student exam does not belong to the exam", "StudentExam", "studentExamExamConflict");
}
// Check that the student of the required student exam (from the database) is the current user
if (!studentExam.getUser().equals(currentUser)) {
throw new AccessForbiddenException();
}
}
use of de.tum.in.www1.artemis.domain.exam.StudentExam in project Artemis by ls1intum.
the class ExamActivityResource method updatePerformedExamActions.
/**
* PUT /courses/{courseId}/exams/{examId}/student-exams/{studentExamId}/actions: Adds the performed actions by
* the user
*
* @param courseId the course to which the student exams belong to
* @param examId the exam to which the student exams belong to
* @param studentExamId the student exam id where we want to add the actions
* @param actions list of actions performed by the user after the last synchronisation
* @return 200 if successful
*/
@PutMapping("/courses/{courseId}/exams/{examId}/student-exams/{studentExamId}/actions")
@PreAuthorize("hasRole('USER')")
public ResponseEntity<Void> updatePerformedExamActions(@PathVariable Long courseId, @PathVariable Long examId, @PathVariable Long studentExamId, @RequestBody List<ExamAction> actions) {
Exam exam = examRepository.findByIdElseThrow(examId);
if (!exam.isMonitoring()) {
throw new BadRequestException("Monitoring is not enabled for the exam with the id " + examId);
}
StudentExam studentExam = studentExamRepository.findByIdWithExercisesElseThrow(studentExamId);
User currentUser = userRepository.getUserWithGroupsAndAuthorities();
boolean isTestRun = studentExam.isTestRun();
this.studentExamAccessService.checkStudentExamAccessElseThrow(courseId, examId, studentExamId, currentUser, isTestRun);
// TODO: Validate actions
actions.forEach(action -> examMonitoringScheduleService.addExamAction(examId, studentExamId, action));
log.info("REST request by user: {} for exam with id {} to add {} actions for student-exam {}", currentUser.getLogin(), examId, actions.size(), studentExamId);
return ResponseEntity.ok().build();
}
use of de.tum.in.www1.artemis.domain.exam.StudentExam in project Artemis by ls1intum.
the class ExamResource method generateStudentExams.
/**
* POST /courses/:courseId/exams/:examId/generate-student-exams : Generates the student exams randomly based on the exam configuration and the exercise groups
*
* @param courseId the id of the course
* @param examId the id of the exam
* @return the list of student exams with their corresponding users
*/
@PostMapping(value = "/courses/{courseId}/exams/{examId}/generate-student-exams")
@PreAuthorize("hasRole('INSTRUCTOR')")
public ResponseEntity<List<StudentExam>> generateStudentExams(@PathVariable Long courseId, @PathVariable Long examId) {
long start = System.nanoTime();
log.info("REST request to generate student exams for exam {}", examId);
final Exam exam = examRepository.findByIdWithRegisteredUsersExerciseGroupsAndExercisesElseThrow(examId);
examAccessService.checkCourseAndExamAccessForInstructorElseThrow(courseId, exam);
// Validate settings of the exam
examService.validateForStudentExamGeneration(exam);
examService.combineTemplateCommitsOfAllProgrammingExercisesInExam(exam);
List<StudentExam> studentExams = studentExamRepository.generateStudentExams(exam);
// we need to break a cycle for the serialization
for (StudentExam studentExam : studentExams) {
studentExam.getExam().setRegisteredUsers(null);
studentExam.getExam().setExerciseGroups(null);
studentExam.getExam().setStudentExams(null);
}
// Reschedule after creation (possible longer working time)
examMonitoringScheduleService.scheduleExamActivitySave(examId);
log.info("Generated {} student exams in {} for exam {}", studentExams.size(), formatDurationFrom(start), examId);
return ResponseEntity.ok().body(studentExams);
}
use of de.tum.in.www1.artemis.domain.exam.StudentExam in project Artemis by ls1intum.
the class StudentExamAccessServiceTest method testExamIdEqualsExamOfStudentExam.
@Test
@WithMockUser(username = "student1", roles = "USER")
public void testExamIdEqualsExamOfStudentExam() {
StudentExam studentExamNotRelatedToExam1 = database.addStudentExam(exam2);
assertThrows(ConflictException.class, () -> studentExamAccessService.checkStudentExamAccessElseThrow(course1.getId(), exam1.getId(), studentExamNotRelatedToExam1.getId(), false));
assertThrows(ConflictException.class, () -> studentExamAccessService.checkStudentExamAccessElseThrow(course1.getId(), exam1.getId(), studentExamNotRelatedToExam1.getId(), users.get(0), false));
}
Aggregations