<commit_before>package de.tum.in.www1.artemis.service;

import de.tum.in.www1.artemis.domain.*;
import de.tum.in.www1.artemis.repository.CourseRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.security.Principal;
import java.time.ZonedDateTime;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;


/**
 * Service Implementation for managing Course.
 */
@Service
@Transactional
public class CourseService {

    private final Logger log = LoggerFactory.getLogger(CourseService.class);

    private final CourseRepository courseRepository;
    private final UserService userService;
    private final ExerciseService exerciseService;
    private final AuthorizationCheckService authCheckService;

    public CourseService(CourseRepository courseRepository,
                         UserService userService,
                         ExerciseService exerciseService,
                         AuthorizationCheckService authCheckService) {
        this.courseRepository = courseRepository;
        this.userService = userService;
        this.exerciseService = exerciseService;
        this.authCheckService = authCheckService;
    }

    /**
     * Save a course.
     *
     * @param course the entity to save
     * @return the persisted entity
     */
    public Course save(Course course) {
        log.debug("Request to save Course : {}", course);
        return courseRepository.save(course);
    }

    /**
     * Get all the courses.
     *
     * @return the list of entities
     */
    @Transactional(readOnly = true)
    public List<Course> findAll() {
        log.debug("Request to get all Courses");
        return courseRepository.findAll();
    }

    /**
     * Check if the current user has at least Student-level permissions for the given course
     *
     * @param course the course to check permissions for
     * @return true, if the user has the required permissions, false otherwise
     */
    public boolean userHasStudentPermissions(Course course) {
        User user = userService.getUserWithGroupsAndAuthorities();
        return authCheckService.isStudentInCourse(course, user) ||
            authCheckService.isTeachingAssistantInCourse(course, user) ||
            authCheckService.isInstructorInCourse(course, user) ||
            authCheckService.isAdmin();
    }

    /**
     * Check if the current user has at least TA-level permissions for the given course
     *
     * @param course the course to check permissions for
     * @return true, if the user has the required permissions, false otherwise
     */
    public boolean userHasTAPermissions(Course course) {
        User user = userService.getUserWithGroupsAndAuthorities();
        return authCheckService.isTeachingAssistantInCourse(course, user) ||
            authCheckService.isInstructorInCourse(course, user) ||
            authCheckService.isAdmin();
    }

    /**
     * Check if the current user has at least Instructor-level permissions for the given course
     *
     * @param course the course to check permissions for
     * @return true, if the user has the required permissions, false otherwise
     */
    public boolean userHasInstructorPermissions(Course course) {
        User user = userService.getUserWithGroupsAndAuthorities();
        return authCheckService.isInstructorInCourse(course, user) ||
            authCheckService.isAdmin();
    }

    /**
     * Get all the courses with exercises.
     *
     * @return the list of entities
     */
    @Transactional(readOnly = true)
    public List<Course> findAllWithExercises() {
        log.debug("Request to get all Courses with Exercises");
        return courseRepository.findAllWithEagerExercises();
    }

    /**
     * Get all courses with exercises (filtered for given user)
     *
     * @param principal the user principal
     * @param user      the user entity
     * @return the list of all courses including exercises for the user
     */
    @Transactional(readOnly = true)
    public List<Course> findAllWithExercisesForUser(Principal principal, User user) {
        if (authCheckService.isAdmin()) {
            // admin => fetch all courses with all exercises immediately
            List<Course> allCourses = findAllWithExercises();
            List<Course> userCourses = new ArrayList<Course>();
            // filter old courses and unnecessary information anyway
            for (Course course : allCourses) {
                if (course.getEndDate() != null && course.getEndDate().isBefore(ZonedDateTime.now())) {
                    //skip old courses
                    continue;
                }
                userCourses.add(course);
                for (Exercise exercise : course.getExercises()) {
                    if (exercise instanceof QuizExercise) {
                        QuizExercise quizExercise = (QuizExercise) exercise;
                        quizExercise.filterSensitiveInformation();
                    }
                }
            }
            return userCourses;
        } else {
            // not admin => fetch visible courses first
            List<Course> allCourses = findAll();
            List<Course> userCourses = new ArrayList<Course>();
            // filter old courses and courses the user should not be able to see
            for (Course course : allCourses) {
                if (course.getEndDate() != null && course.getEndDate().isBefore(ZonedDateTime.now())) {
                    //skip old courses
                    continue;
                }
                //Instructors and TAs see all courses that have not yet finished
                if (user.getGroups().contains(course.getTeachingAssistantGroupName()) ||
                    user.getGroups().contains(course.getInstructorGroupName())) {

                    userCourses.add(course);
                }
                //Students see all courses that have already startet
                if (user.getGroups().contains(course.getStudentGroupName())) {
                    if (course.getStartDate() != null && course.getStartDate().isAfter(ZonedDateTime.now())) {
                        userCourses.add(course);
                    }
                }
            }
            for (Course course : userCourses) {
                // fetch visible exercises for each course after filtering
                List<Exercise> exercises = exerciseService.findAllForCourse(course, true, principal, user);
                course.setExercises(new HashSet<>(exercises));
            }<commit_msg>fix logic error when displaying courses<commit_after>package de.tum.in.www1.artemis.service;

import de.tum.in.www1.artemis.domain.*;
import de.tum.in.www1.artemis.repository.CourseRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.security.Principal;
import java.time.ZonedDateTime;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;


/**
 * Service Implementation for managing Course.
 */
@Service
@Transactional
public class CourseService {

    private final Logger log = LoggerFactory.getLogger(CourseService.class);

    private final CourseRepository courseRepository;
    private final UserService userService;
    private final ExerciseService exerciseService;
    private final AuthorizationCheckService authCheckService;

    public CourseService(CourseRepository courseRepository,
                         UserService userService,
                         ExerciseService exerciseService,
                         AuthorizationCheckService authCheckService) {
        this.courseRepository = courseRepository;
        this.userService = userService;
        this.exerciseService = exerciseService;
        this.authCheckService = authCheckService;
    }

    /**
     * Save a course.
     *
     * @param course the entity to save
     * @return the persisted entity
     */
    public Course save(Course course) {
        log.debug("Request to save Course : {}", course);
        return courseRepository.save(course);
    }

    /**
     * Get all the courses.
     *
     * @return the list of entities
     */
    @Transactional(readOnly = true)
    public List<Course> findAll() {
        log.debug("Request to get all Courses");
        return courseRepository.findAll();
    }

    /**
     * Check if the current user has at least Student-level permissions for the given course
     *
     * @param course the course to check permissions for
     * @return true, if the user has the required permissions, false otherwise
     */
    public boolean userHasStudentPermissions(Course course) {
        User user = userService.getUserWithGroupsAndAuthorities();
        return authCheckService.isStudentInCourse(course, user) ||
            authCheckService.isTeachingAssistantInCourse(course, user) ||
            authCheckService.isInstructorInCourse(course, user) ||
            authCheckService.isAdmin();
    }

    /**
     * Check if the current user has at least TA-level permissions for the given course
     *
     * @param course the course to check permissions for
     * @return true, if the user has the required permissions, false otherwise
     */
    public boolean userHasTAPermissions(Course course) {
        User user = userService.getUserWithGroupsAndAuthorities();
        return authCheckService.isTeachingAssistantInCourse(course, user) ||
            authCheckService.isInstructorInCourse(course, user) ||
            authCheckService.isAdmin();
    }

    /**
     * Check if the current user has at least Instructor-level permissions for the given course
     *
     * @param course the course to check permissions for
     * @return true, if the user has the required permissions, false otherwise
     */
    public boolean userHasInstructorPermissions(Course course) {
        User user = userService.getUserWithGroupsAndAuthorities();
        return authCheckService.isInstructorInCourse(course, user) ||
            authCheckService.isAdmin();
    }

    /**
     * Get all the courses with exercises.
     *
     * @return the list of entities
     */
    @Transactional(readOnly = true)
    public List<Course> findAllWithExercises() {
        log.debug("Request to get all Courses with Exercises");
        return courseRepository.findAllWithEagerExercises();
    }

    /**
     * Get all courses with exercises (filtered for given user)
     *
     * @param principal the user principal
     * @param user      the user entity
     * @return the list of all courses including exercises for the user
     */
    @Transactional(readOnly = true)
    public List<Course> findAllWithExercisesForUser(Principal principal, User user) {
        if (authCheckService.isAdmin()) {
            // admin => fetch all courses with all exercises immediately
            List<Course> allCourses = findAllWithExercises();
            List<Course> userCourses = new ArrayList<Course>();
            // filter old courses and unnecessary information anyway
            for (Course course : allCourses) {
                if (course.getEndDate() != null && course.getEndDate().isBefore(ZonedDateTime.now())) {
                    //skip old courses
                    continue;
                }
                userCourses.add(course);
                for (Exercise exercise : course.getExercises()) {
                    if (exercise instanceof QuizExercise) {
                        QuizExercise quizExercise = (QuizExercise) exercise;
                        quizExercise.filterSensitiveInformation();
                    }
                }
            }
            return userCourses;
        } else {
            // not admin => fetch visible courses first
            List<Course> allCourses = findAll();
            List<Course> userCourses = new ArrayList<Course>();
            // filter old courses and courses the user should not be able to see
            for (Course course : allCourses) {
                if (course.getEndDate() != null && course.getEndDate().isBefore(ZonedDateTime.now())) {
                    //skip old courses
                    continue;
                }
                //Instructors and TAs see all courses that have not yet finished
                if (user.getGroups().contains(course.getTeachingAssistantGroupName()) ||
                    user.getGroups().contains(course.getInstructorGroupName())) {

                    userCourses.add(course);
                }
                //Students see all courses that have already startet
                if (user.getGroups().contains(course.getStudentGroupName())) {
                    if (course.getStartDate() != null && course.getStartDate().isBefore(ZonedDateTime.now())) {
                        userCourses.add(course);
                    }
                }
            }
            for (Course course : userCourses) {
                // fetch visible exercises for each course after filtering
                List<Exercise> exercises = exerciseService.findAllForCourse(course, true, principal, user);
                course.setExercises(new HashSet<>(exercises));
            }
{'fraction_non_alphanumeric': 0.08241717149053923, 'fraction_numerical': 0.000747887218607434, 'mean_word_length': 2.2903543307086616, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 6, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6078721', 'n_tokens_mistral': 3347, 'n_tokens_neox': 3168, 'n_words': 923}