use of de.tum.in.www1.artemis.domain.exam.StudentExam in project Artemis by ls1intum.
the class ExamSubmissionServiceTest method init.
@BeforeEach
void init() {
List<User> users = database.addUsers(1, 0, 0, 1);
user = users.get(0);
exercise = database.addCourseExamExerciseGroupWithOneTextExercise();
Course course = exercise.getCourseViaExerciseGroupOrCourseMember();
exam = examRepository.findByCourseId(course.getId()).get(0);
studentExam = database.addStudentExam(exam);
// 2 hours
studentExam.setWorkingTime(7200);
studentExam.setUser(user);
studentExam.addExercise(exercise);
studentExam = studentExamRepository.save(studentExam);
}
use of de.tum.in.www1.artemis.domain.exam.StudentExam in project Artemis by ls1intum.
the class ExamQuizService method evaluateQuizParticipationsForTestRun.
/**
* This method is intended to be called after a user submits a test run. We calculate the achieved score in the quiz exercises immediately and attach a result.
* Note: We do not insert the result of this test run quiz participation into the quiz statistics.
* @param testRun The test run containing the users participations in all exam exercises
*/
public void evaluateQuizParticipationsForTestRun(StudentExam testRun) {
final var participations = testRun.getExercises().stream().flatMap(exercise -> exercise.getStudentParticipations().stream().filter(StudentParticipation::isTestRun).filter(participation -> participation.getExercise() instanceof QuizExercise)).collect(Collectors.toSet());
for (final var participation : participations) {
var quizExercise = (QuizExercise) participation.getExercise();
final var optionalExistingSubmission = participation.findLatestSubmission();
if (optionalExistingSubmission.isPresent()) {
QuizSubmission submission = (QuizSubmission) submissionRepository.findWithEagerResultAndFeedbackById(optionalExistingSubmission.get().getId()).orElseThrow(() -> new EntityNotFoundException("Submission with id \"" + optionalExistingSubmission.get().getId() + "\" does not exist"));
participation.setExercise(quizExerciseRepository.findByIdWithQuestionsElseThrow(quizExercise.getId()));
quizExercise = (QuizExercise) participation.getExercise();
Result result;
if (submission.getLatestResult() == null) {
result = new Result();
result.setParticipation(participation);
result.setAssessmentType(AssessmentType.AUTOMATIC);
// set submission to calculate scores
result.setSubmission(submission);
// calculate scores and update result and submission accordingly
submission.calculateAndUpdateScores(quizExercise);
result.evaluateQuizSubmission();
// remove submission to follow save order for ordered collections
result.setSubmission(null);
result = resultRepository.save(result);
participation.setResults(Set.of(result));
studentParticipationRepository.save(participation);
result.setSubmission(submission);
submission.addResult(result);
} else {
result = submission.getLatestResult();
// set submission to calculate scores
result.setSubmission(submission);
// calculate scores and update result and submission accordingly
submission.calculateAndUpdateScores(quizExercise);
// prevent a lazy exception in the evaluateQuizSubmission method
result.setParticipation(participation);
result.evaluateQuizSubmission();
resultRepository.save(result);
}
submissionRepository.save(submission);
}
}
}
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;
}
use of de.tum.in.www1.artemis.domain.exam.StudentExam in project Artemis by ls1intum.
the class ExamSubmissionService method isAllowedToSubmitDuringExam.
/**
* Check if the user is allowed to submit (submission is in time & user's student exam has the exercise or it is a test run).
* Note: if the exercise is not an exam, this method will return true
*
* @param exercise the exercise for which a submission should be saved
* @param user the user that wants to submit
* @param withGracePeriod whether the grace period should be taken into account or not
* @return true if it is not an exam of if it is an exam and the submission is in time and the exercise is part of
* the user's student exam
*/
public boolean isAllowedToSubmitDuringExam(Exercise exercise, User user, boolean withGracePeriod) {
if (isExamSubmission(exercise)) {
// Get the student exam if it was not passed to the function
Exam exam = exercise.getExerciseGroup().getExam();
Optional<StudentExam> optionalStudentExam = studentExamRepository.findWithExercisesByUserIdAndExamId(user.getId(), exam.getId());
if (optionalStudentExam.isEmpty()) {
// unnecessary database calls
if (!isExamTestRunSubmission(exercise, user, exam)) {
throw new EntityNotFoundException("Student exam with for userId \"" + user.getId() + "\" and examId \"" + exam.getId() + "\" does not exist");
}
return true;
}
StudentExam studentExam = optionalStudentExam.get();
// Check that the current user is allowed to submit to this exercise
if (!studentExam.getExercises().contains(exercise)) {
return false;
}
// if the student exam was already submitted, the user cannot save anymore
if (Boolean.TRUE.equals(studentExam.isSubmitted()) || studentExam.getSubmissionDate() != null) {
return false;
}
// Check that the submission is in time
return isSubmissionInTime(exercise, studentExam, withGracePeriod);
}
return true;
}
Aggregations