Search in sources :

Example 6 with Participant

use of de.tum.in.www1.artemis.domain.participation.Participant in project ArTEMiS by ls1intum.

the class DatabaseUtilService method createParticipationSubmissionAndResult.

public Result createParticipationSubmissionAndResult(long exerciseId, Participant participant, Double points, Double bonusPoints, long scoreAwarded, boolean rated) {
    Exercise exercise = exerciseRepo.findById(exerciseId).get();
    if (!exercise.getMaxPoints().equals(points)) {
        exercise.setMaxPoints(points);
    }
    if (!exercise.getBonusPoints().equals(bonusPoints)) {
        exercise.setBonusPoints(bonusPoints);
    }
    exercise = exerciseRepo.saveAndFlush(exercise);
    StudentParticipation studentParticipation = participationService.startExercise(exercise, participant, false);
    return createSubmissionAndResult(studentParticipation, scoreAwarded, rated);
}
Also used : ModelingExercise(de.tum.in.www1.artemis.domain.modeling.ModelingExercise)

Example 7 with Participant

use of de.tum.in.www1.artemis.domain.participation.Participant in project ArTEMiS by ls1intum.

the class ScoreService method updateOrCreateParticipantScore.

/**
 * Either updates an existing participant score or creates a new participant score if a new result comes in
 * The annotation "@Transactional" is ok because it means that this method does not support run in an outer transactional context, instead the outer transaction is paused
 *
 * @param createdOrUpdatedResult newly created or updated result
 */
// ok (see JavaDoc)
@Transactional(propagation = Propagation.NOT_SUPPORTED)
public void updateOrCreateParticipantScore(Result createdOrUpdatedResult) {
    if (createdOrUpdatedResult.getScore() == null || createdOrUpdatedResult.getCompletionDate() == null) {
        return;
    }
    // There is a deadlock problem with programming exercises here if we use the participation from the result (reason unknown at the moment)
    // therefore we get the participation from the database
    Optional<StudentParticipation> studentParticipationOptional = getStudentParticipationForResult(createdOrUpdatedResult);
    if (studentParticipationOptional.isEmpty()) {
        return;
    }
    StudentParticipation studentParticipation = studentParticipationOptional.get();
    // we ignore test runs of exams
    if (studentParticipation.isTestRun()) {
        return;
    }
    Exercise exercise = studentParticipation.getExercise();
    ParticipantScore existingParticipationScoreForExerciseAndParticipant = getExistingParticipationScore(studentParticipation, exercise);
    // there already exists a participant score -> we need to update it
    if (existingParticipationScoreForExerciseAndParticipant != null) {
        updateExistingParticipantScore(existingParticipationScoreForExerciseAndParticipant, createdOrUpdatedResult, exercise);
    } else {
        // there does not already exist a participant score -> we need to create it
        createNewParticipantScore(createdOrUpdatedResult, studentParticipation, exercise);
    }
}
Also used : ParticipantScore(de.tum.in.www1.artemis.domain.scores.ParticipantScore) StudentParticipation(de.tum.in.www1.artemis.domain.participation.StudentParticipation) Transactional(org.springframework.transaction.annotation.Transactional)

Example 8 with Participant

use of de.tum.in.www1.artemis.domain.participation.Participant in project ArTEMiS by ls1intum.

the class ScoreService method getNewLastRatedResultForParticipantScore.

/**
 * Get the result that can replace the currently set last rated result for a participant score
 *
 * @param participantScore participant score
 * @return optional of new result
 */
private Optional<Result> getNewLastRatedResultForParticipantScore(ParticipantScore participantScore) {
    List<Result> ratedResultsOrdered;
    if (participantScore.getClass().equals(StudentScore.class)) {
        StudentScore studentScore = (StudentScore) participantScore;
        ratedResultsOrdered = resultRepository.getRatedResultsOrderedByParticipationIdLegalSubmissionIdResultIdDescForStudent(participantScore.getExercise().getId(), studentScore.getUser().getId()).stream().filter(r -> !participantScore.getLastRatedResult().equals(r)).collect(Collectors.toList());
    } else {
        TeamScore teamScore = (TeamScore) participantScore;
        ratedResultsOrdered = resultRepository.getRatedResultsOrderedByParticipationIdLegalSubmissionIdResultIdDescForTeam(participantScore.getExercise().getId(), teamScore.getTeam().getId()).stream().filter(r -> !participantScore.getLastRatedResult().equals(r)).collect(Collectors.toList());
    }
    // the new last rated result (rated result with the highest id of submission with the highest id) will be at the beginning of the list
    return ratedResultsOrdered.isEmpty() ? Optional.empty() : Optional.of(ratedResultsOrdered.get(0));
}
Also used : StudentScore(de.tum.in.www1.artemis.domain.scores.StudentScore) TeamScore(de.tum.in.www1.artemis.domain.scores.TeamScore)

Example 9 with Participant

use of de.tum.in.www1.artemis.domain.participation.Participant in project ArTEMiS by ls1intum.

the class ScoreService method getNewLastResultForParticipantScore.

/**
 * Get the result that can replace the currently set last result for a participant score
 *
 * @param participantScore participant score
 * @return optional of new result
 */
private Optional<Result> getNewLastResultForParticipantScore(ParticipantScore participantScore) {
    List<Result> resultOrdered;
    if (participantScore.getClass().equals(StudentScore.class)) {
        StudentScore studentScore = (StudentScore) participantScore;
        resultOrdered = resultRepository.getResultsOrderedByParticipationIdLegalSubmissionIdResultIdDescForStudent(participantScore.getExercise().getId(), studentScore.getUser().getId()).stream().filter(r -> !participantScore.getLastResult().equals(r)).collect(Collectors.toList());
    } else {
        TeamScore teamScore = (TeamScore) participantScore;
        resultOrdered = resultRepository.getResultsOrderedByParticipationIdLegalSubmissionIdResultIdDescForTeam(participantScore.getExercise().getId(), teamScore.getTeam().getId()).stream().filter(r -> !participantScore.getLastResult().equals(r)).collect(Collectors.toList());
    }
    // the new last result (result with the highest id of submission with the highest id) will be at the beginning of the list
    return resultOrdered.isEmpty() ? Optional.empty() : Optional.of(resultOrdered.get(0));
}
Also used : StudentScore(de.tum.in.www1.artemis.domain.scores.StudentScore) TeamScore(de.tum.in.www1.artemis.domain.scores.TeamScore)

Example 10 with Participant

use of de.tum.in.www1.artemis.domain.participation.Participant in project ArTEMiS by ls1intum.

the class QuizScheduleService method processCachedQuizSubmissions.

/**
 * // @formatter:off
 * 1. Check cached submissions for new submissions with “isSubmitted() == true”
 *      a. Process each Submission (set submissionType to “SubmissionType.MANUAL”) and create Participation and Result and save them to Database (DB WRITE)
 *      b. Remove processed Submissions from SubmissionHashMap and write Participation with Result into ParticipationHashMap and write Result into ResultHashMap
 * 2. If Quiz has ended:
 *      a. Process all cached Submissions that belong to this quiz i. set “isSubmitted” to “true” and submissionType to “SubmissionType.TIMEOUT”
 *          ii. Create Participation and Result and save to Database (DB WRITE)
 *          iii. Remove processed Submissions from cache and write the Participations with Result and the Results into the cache
 *      b. Send out cached Participations (including QuizExercise and Result) from to each participant and remove them from the cache (WEBSOCKET SEND)
 * 3. Update Statistics with Results from ResultHashMap (DB READ and DB WRITE) and remove from cache
 * 4. Send out new Statistics to instructors (WEBSOCKET SEND)
 */
public void processCachedQuizSubmissions() {
    log.debug("Process cached quiz submissions");
    // global try-catch for error logging
    try {
        for (QuizExerciseCache cachedQuiz : quizCache.getAllQuizExerciseCaches()) {
            // this way near cache is used (values will deserialize new objects)
            Long quizExerciseId = cachedQuiz.getExerciseId();
            // Get fresh QuizExercise from DB
            QuizExercise quizExercise = quizExerciseRepository.findOne(quizExerciseId);
            // check if quiz has been deleted
            if (quizExercise == null) {
                log.debug("Remove quiz {} from resultHashMap", quizExerciseId);
                quizCache.removeAndClear(quizExerciseId);
                continue;
            }
            // Update cached exercise object (use the expensive operation upfront)
            quizExercise = quizExerciseRepository.findByIdWithQuestionsAndStatisticsElseThrow(quizExerciseId);
            Map<Long, QuizBatch> batchCache = quizExercise.getQuizBatches().stream().collect(Collectors.toUnmodifiableMap(QuizBatch::getId, batch -> batch));
            // this is required to ensure that students cannot gain extra attempts this way
            for (var batch : cachedQuiz.getBatches().entrySet()) {
                if (batchCache.get(batch.getValue()).isEnded()) {
                    cachedQuiz.getSubmissions().putIfAbsent(batch.getKey(), new QuizSubmission());
                }
            }
            // (Boolean wrapper is safe to auto-unbox here)
            boolean hasEnded = quizExercise.isQuizEnded();
            // Note that those might not be true later on due to concurrency and a distributed system,
            // do not rely on that for actions upon the whole set, such as clear()
            boolean hasNewSubmissions = !cachedQuiz.getSubmissions().isEmpty();
            boolean hasNewParticipations = !cachedQuiz.getParticipations().isEmpty();
            boolean hasNewResults = !cachedQuiz.getResults().isEmpty();
            // Skip quizzes with no cached changes
            if (!hasNewSubmissions && !hasNewParticipations && !hasNewResults) {
                // Remove quiz if it has ended
                if (hasEnded) {
                    removeCachedQuiz(cachedQuiz);
                }
                continue;
            }
            // Save cached Submissions (this will also generate results and participations and place them in the cache)
            long start = System.nanoTime();
            if (hasNewSubmissions) {
                // Create Participations and Results if the submission was submitted or if the quiz has ended and save them to Database (DB Write)
                Map<String, QuizSubmission> submissions = cachedQuiz.getSubmissions();
                Map<String, Long> batches = cachedQuiz.getBatches();
                // This call will remove the processed Submission map entries itself
                int numberOfSubmittedSubmissions = saveQuizSubmissionWithParticipationAndResultToDatabase(quizExercise, submissions, batches, batchCache);
                // .. and likely generate new participations and results
                if (numberOfSubmittedSubmissions > 0) {
                    // .. so we set the boolean variables here again if some were submitted
                    hasNewParticipations = true;
                    hasNewResults = true;
                    log.info("Saved {} submissions to database in {} in quiz {}", numberOfSubmittedSubmissions, formatDurationFrom(start), quizExercise.getTitle());
                }
            }
            // Send out Participations from ParticipationHashMap to each user if the quiz has ended
            start = System.nanoTime();
            if (hasNewParticipations && hasEnded) {
                // Send the participation with containing result and quiz back to the users via websocket and remove the participation from the ParticipationHashMap
                Collection<Entry<String, StudentParticipation>> finishedParticipations = cachedQuiz.getParticipations().entrySet();
                // TODO maybe find a better way to optimize the performance (use an executor service with e.g. X parallel threads)
                finishedParticipations.parallelStream().forEach(entry -> {
                    StudentParticipation participation = entry.getValue();
                    if (participation.getParticipant() == null || participation.getParticipantIdentifier() == null) {
                        log.error("Participation is missing student (or student is missing username): {}", participation);
                    } else {
                        sendQuizResultToUser(quizExerciseId, participation);
                        cachedQuiz.getParticipations().remove(entry.getKey());
                    }
                });
                if (!finishedParticipations.isEmpty()) {
                    log.info("Sent out {} participations in {} for quiz {}", finishedParticipations.size(), formatDurationFrom(start), quizExercise.getTitle());
                }
            }
            // Update Statistics with Results (DB Read and DB Write) and remove the results from the cache
            start = System.nanoTime();
            if (hasNewResults) {
                // Fetch a new quiz exercise here including deeper attribute paths (this is relatively expensive, so we only do that if necessary)
                try {
                    // Get a Set because QuizStatisticService needs one (currently)
                    Set<Result> newResultsForQuiz = Set.copyOf(cachedQuiz.getResults().values());
                    // Update the statistics
                    quizStatisticService.updateStatistics(newResultsForQuiz, quizExercise);
                    log.info("Updated statistics with {} new results in {} for quiz {}", newResultsForQuiz.size(), formatDurationFrom(start), quizExercise.getTitle());
                    // Remove only processed results
                    for (Result result : newResultsForQuiz) {
                        cachedQuiz.getResults().remove(result.getId());
                    }
                } catch (Exception e) {
                    log.error("Exception in StatisticService.updateStatistics(): {}", e.getMessage(), e);
                }
            }
        }
    } catch (Exception e) {
        log.error("Exception in Quiz Schedule: {}", e.getMessage(), e);
    }
}
Also used : QuizStatisticService(de.tum.in.www1.artemis.service.QuizStatisticService) java.util(java.util) SecurityUtils(de.tum.in.www1.artemis.security.SecurityUtils) de.tum.in.www1.artemis.repository(de.tum.in.www1.artemis.repository) ApplicationReadyEvent(org.springframework.boot.context.event.ApplicationReadyEvent) ZonedDateTime(java.time.ZonedDateTime) LoggerFactory(org.slf4j.LoggerFactory) QuizBatch(de.tum.in.www1.artemis.domain.quiz.QuizBatch) QuizMessagingService(de.tum.in.www1.artemis.service.QuizMessagingService) TimeLogUtil.formatDurationFrom(de.tum.in.www1.artemis.service.util.TimeLogUtil.formatDurationFrom) Service(org.springframework.stereotype.Service) Duration(java.time.Duration) SubmittedAnswer(de.tum.in.www1.artemis.domain.quiz.SubmittedAnswer) Nullable(javax.annotation.Nullable) Config(com.hazelcast.config.Config) HazelcastInstance(com.hazelcast.core.HazelcastInstance) Logger(org.slf4j.Logger) QuizExercise(de.tum.in.www1.artemis.domain.quiz.QuizExercise) ConstraintViolationException(org.hibernate.exception.ConstraintViolationException) com.hazelcast.scheduledexecutor(com.hazelcast.scheduledexecutor) IAtomicReference(com.hazelcast.cp.IAtomicReference) QuizSubmission(de.tum.in.www1.artemis.domain.quiz.QuizSubmission) EventListener(org.springframework.context.event.EventListener) SubmissionType(de.tum.in.www1.artemis.domain.enumeration.SubmissionType) 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) Constants(de.tum.in.www1.artemis.config.Constants) TimeUnit(java.util.concurrent.TimeUnit) Result(de.tum.in.www1.artemis.domain.Result) User(de.tum.in.www1.artemis.domain.User) QuizMode(de.tum.in.www1.artemis.domain.enumeration.QuizMode) Entry(java.util.Map.Entry) StudentParticipation(de.tum.in.www1.artemis.domain.participation.StudentParticipation) SimpMessageSendingOperations(org.springframework.messaging.simp.SimpMessageSendingOperations) StudentParticipation(de.tum.in.www1.artemis.domain.participation.StudentParticipation) ConstraintViolationException(org.hibernate.exception.ConstraintViolationException) QuizExercise(de.tum.in.www1.artemis.domain.quiz.QuizExercise) Result(de.tum.in.www1.artemis.domain.Result) QuizSubmission(de.tum.in.www1.artemis.domain.quiz.QuizSubmission) Entry(java.util.Map.Entry) QuizBatch(de.tum.in.www1.artemis.domain.quiz.QuizBatch)

Aggregations

StudentParticipation (de.tum.in.www1.artemis.domain.participation.StudentParticipation)21 Participant (de.tum.in.www1.artemis.domain.participation.Participant)15 ProgrammingExerciseStudentParticipation (de.tum.in.www1.artemis.domain.participation.ProgrammingExerciseStudentParticipation)14 ParticipantScore (de.tum.in.www1.artemis.domain.scores.ParticipantScore)14 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)14 ParticipantScoreDTO (de.tum.in.www1.artemis.web.rest.dto.ParticipantScoreDTO)12 StudentScore (de.tum.in.www1.artemis.domain.scores.StudentScore)10 TeamScore (de.tum.in.www1.artemis.domain.scores.TeamScore)10 Test (org.junit.jupiter.api.Test)10 WithMockUser (org.springframework.security.test.context.support.WithMockUser)10 Exercise (de.tum.in.www1.artemis.domain.Exercise)8 de.tum.in.www1.artemis.repository (de.tum.in.www1.artemis.repository)8 ParticipantScoreAverageDTO (de.tum.in.www1.artemis.web.rest.dto.ParticipantScoreAverageDTO)8 java.util (java.util)8 Collectors (java.util.stream.Collectors)8 QuizExercise (de.tum.in.www1.artemis.domain.quiz.QuizExercise)7 QuizSubmission (de.tum.in.www1.artemis.domain.quiz.QuizSubmission)7 de.tum.in.www1.artemis.domain (de.tum.in.www1.artemis.domain)6 Course (de.tum.in.www1.artemis.domain.Course)6 ModelingExercise (de.tum.in.www1.artemis.domain.modeling.ModelingExercise)6