Search in sources :

Example 31 with StudentExam

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);
}
Also used : User(de.tum.in.www1.artemis.domain.User) WithMockUser(org.springframework.security.test.context.support.WithMockUser) Course(de.tum.in.www1.artemis.domain.Course) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 32 with 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);
        }
    }
}
Also used : QuizStatisticService(de.tum.in.www1.artemis.service.QuizStatisticService) java.util(java.util) Logger(org.slf4j.Logger) QuizExercise(de.tum.in.www1.artemis.domain.quiz.QuizExercise) QuizSubmission(de.tum.in.www1.artemis.domain.quiz.QuizSubmission) de.tum.in.www1.artemis.repository(de.tum.in.www1.artemis.repository) TimeLogUtil(de.tum.in.www1.artemis.service.util.TimeLogUtil) ZonedDateTime(java.time.ZonedDateTime) LoggerFactory(org.slf4j.LoggerFactory) Submission(de.tum.in.www1.artemis.domain.Submission) StudentExam(de.tum.in.www1.artemis.domain.exam.StudentExam) NotNull(javax.validation.constraints.NotNull) AssessmentType(de.tum.in.www1.artemis.domain.enumeration.AssessmentType) InitializationState(de.tum.in.www1.artemis.domain.enumeration.InitializationState) Collectors(java.util.stream.Collectors) ResultService(de.tum.in.www1.artemis.service.ResultService) Result(de.tum.in.www1.artemis.domain.Result) EntityNotFoundException(de.tum.in.www1.artemis.web.rest.errors.EntityNotFoundException) Service(org.springframework.stereotype.Service) StudentParticipation(de.tum.in.www1.artemis.domain.participation.StudentParticipation) QuizSubmission(de.tum.in.www1.artemis.domain.quiz.QuizSubmission) EntityNotFoundException(de.tum.in.www1.artemis.web.rest.errors.EntityNotFoundException) QuizExercise(de.tum.in.www1.artemis.domain.quiz.QuizExercise) Result(de.tum.in.www1.artemis.domain.Result)

Example 33 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 34 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)

Example 35 with StudentExam

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;
}
Also used : EntityNotFoundException(de.tum.in.www1.artemis.web.rest.errors.EntityNotFoundException) 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)

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