Search in sources :

Example 96 with Lecture

use of de.tum.in.www1.artemis.domain.Lecture in project Artemis by ls1intum.

the class LectureUnitService method removeLectureUnit.

/**
 * Deletes a lecture unit correctly in the database
 *
 * @param lectureUnit lecture unit to delete
 */
// ok because of delete
@Transactional
public void removeLectureUnit(LectureUnit lectureUnit) {
    if (Objects.isNull(lectureUnit)) {
        return;
    }
    // update associated learning goals
    Set<LearningGoal> associatedLearningGoals = new HashSet<>(lectureUnit.getLearningGoals());
    for (LearningGoal learningGoal : associatedLearningGoals) {
        disconnectLectureUnitAndLearningGoal(lectureUnit, learningGoal);
    }
    Lecture lecture = lectureRepository.findByIdWithPostsAndLectureUnitsAndLearningGoalsElseThrow(lectureUnit.getLecture().getId());
    // Creating a new list of lecture units without the one we want to remove
    List<LectureUnit> lectureUnitsUpdated = new ArrayList<>();
    for (LectureUnit unit : lecture.getLectureUnits()) {
        if (Objects.nonNull(unit) && !unit.getId().equals(lectureUnit.getId())) {
            lectureUnitsUpdated.add(unit);
        }
    }
    lecture.getLectureUnits().clear();
    lecture.getLectureUnits().addAll(lectureUnitsUpdated);
    lectureRepository.save(lecture);
}
Also used : Lecture(de.tum.in.www1.artemis.domain.Lecture) LectureUnit(de.tum.in.www1.artemis.domain.lecture.LectureUnit) LearningGoal(de.tum.in.www1.artemis.domain.LearningGoal) Transactional(org.springframework.transaction.annotation.Transactional)

Example 97 with Lecture

use of de.tum.in.www1.artemis.domain.Lecture in project Artemis by ls1intum.

the class FileService method publicPathForActualPath.

/**
 * Generate the public path for the file at the given path
 *
 * @param actualPath the path to the file in the local filesystem
 * @param entityId   the id of the entity associated with the file
 * @return the public file url that can be used by users to access the file from outside
 */
public String publicPathForActualPath(String actualPath, @Nullable Long entityId) {
    // first extract filename
    String filename = Path.of(actualPath).getFileName().toString();
    // generate part for id
    String id = entityId == null ? Constants.FILEPATH_ID_PLACEHOLDER : entityId.toString();
    // check for known path to convert
    if (actualPath.contains(FilePathService.getTempFilePath())) {
        return "/api/files/temp/" + filename;
    }
    if (actualPath.contains(FilePathService.getDragAndDropBackgroundFilePath())) {
        return "/api/files/drag-and-drop/backgrounds/" + id + "/" + filename;
    }
    if (actualPath.contains(FilePathService.getDragItemFilePath())) {
        return "/api/files/drag-and-drop/drag-items/" + id + "/" + filename;
    }
    if (actualPath.contains(FilePathService.getCourseIconFilePath())) {
        return "/api/files/course/icons/" + id + "/" + filename;
    }
    if (actualPath.contains(FilePathService.getLectureAttachmentFilePath())) {
        return "/api/files/attachments/lecture/" + id + "/" + filename;
    }
    if (actualPath.contains(FilePathService.getAttachmentUnitFilePath())) {
        return "/api/files/attachments/attachment-unit/" + id + "/" + filename;
    }
    if (actualPath.contains(FilePathService.getFileUploadExercisesFilePath())) {
        final var path = Path.of(actualPath);
        final long exerciseId;
        try {
            // The last name is the file name, the one before that is the submissionId and the one before that is the exerciseId, in which we are interested
            final var shouldBeExerciseId = path.getName(path.getNameCount() - 3).toString();
            exerciseId = Long.parseLong(shouldBeExerciseId);
        } catch (IllegalArgumentException e) {
            throw new FilePathParsingException("Unexpected String in upload file path. Exercise ID should be present here: " + actualPath);
        }
        return "/api/files/file-upload-exercises/" + exerciseId + "/submissions/" + id + "/" + filename;
    }
    // path is unknown => cannot convert
    throw new FilePathParsingException("Unknown Filepath: " + actualPath);
}
Also used : FilePathParsingException(de.tum.in.www1.artemis.exception.FilePathParsingException)

Example 98 with Lecture

use of de.tum.in.www1.artemis.domain.Lecture in project Artemis by ls1intum.

the class PostService method getAllLecturePosts.

/**
 * Checks course, user, lecture and post validity,
 * retrieves and filters posts for a lecture by its id
 * and ensures that sensitive information is filtered out
 *
 * @param postContextFilter filter object
 * @return page of posts that belong to the lecture
 */
public List<Post> getAllLecturePosts(PostContextFilter postContextFilter) {
    final User user = userRepository.getUserWithGroupsAndAuthorities();
    // checks
    preCheckUserAndCourse(user, postContextFilter.getCourseId());
    preCheckLecture(user, postContextFilter.getCourseId(), postContextFilter.getLectureId());
    // retrieve posts
    List<Post> lecturePosts;
    lecturePosts = postRepository.findPostsByLectureId(postContextFilter.getLectureId(), postContextFilter.getFilterToUnresolved(), postContextFilter.getFilterToOwn(), postContextFilter.getFilterToAnsweredOrReacted(), user.getId());
    // protect sample solution, grading instructions, etc.
    lecturePosts.stream().map(Post::getExercise).filter(Objects::nonNull).forEach(Exercise::filterSensitiveInformation);
    return lecturePosts;
}
Also used : User(de.tum.in.www1.artemis.domain.User) Exercise(de.tum.in.www1.artemis.domain.Exercise) Post(de.tum.in.www1.artemis.domain.metis.Post)

Example 99 with Lecture

use of de.tum.in.www1.artemis.domain.Lecture in project Artemis by ls1intum.

the class PostService method sendNotification.

/**
 * Sends notification to affected groups
 *
 * @param post post that triggered the notification
 */
void sendNotification(Post post, Course course) {
    // create post for notification
    Post postForNotification = new Post();
    postForNotification.setId(post.getId());
    postForNotification.setAuthor(post.getAuthor());
    postForNotification.setCourse(course);
    postForNotification.setCourseWideContext(post.getCourseWideContext());
    postForNotification.setLecture(post.getLecture());
    postForNotification.setExercise(post.getExercise());
    postForNotification.setCreationDate(post.getCreationDate());
    postForNotification.setTitle(post.getTitle());
    // create html content
    Parser parser = Parser.builder().build();
    String htmlPostContent;
    try {
        Node document = parser.parse(post.getContent());
        HtmlRenderer renderer = HtmlRenderer.builder().build();
        htmlPostContent = renderer.render(document);
    } catch (Exception e) {
        htmlPostContent = "";
    }
    postForNotification.setContent(htmlPostContent);
    // notify via course
    if (post.getCourseWideContext() != null) {
        if (post.getCourseWideContext() == CourseWideContext.ANNOUNCEMENT) {
            groupNotificationService.notifyAllGroupsAboutNewAnnouncement(postForNotification, course);
            return;
        }
        groupNotificationService.notifyAllGroupsAboutNewCoursePost(postForNotification, course);
        return;
    }
    // notify via exercise
    if (post.getExercise() != null) {
        groupNotificationService.notifyAllGroupsAboutNewPostForExercise(postForNotification, course);
        // protect sample solution, grading instructions, etc.
        post.getExercise().filterSensitiveInformation();
        return;
    }
    // notify via lecture
    if (post.getLecture() != null) {
        groupNotificationService.notifyAllGroupsAboutNewPostForLecture(postForNotification, course);
    }
}
Also used : Post(de.tum.in.www1.artemis.domain.metis.Post) Node(org.commonmark.node.Node) HtmlRenderer(org.commonmark.renderer.html.HtmlRenderer) BadRequestAlertException(de.tum.in.www1.artemis.web.rest.errors.BadRequestAlertException) ForbiddenException(javax.ws.rs.ForbiddenException) Parser(org.commonmark.parser.Parser)

Example 100 with Lecture

use of de.tum.in.www1.artemis.domain.Lecture in project Artemis by ls1intum.

the class LearningGoalResource method getLearningGoals.

/**
 * GET /courses/:courseId/goals : gets all the learning goals of a course
 * @param courseId the id of the course for which the learning goals should be fetched
 * @return the ResponseEntity with status 200 (OK) and with body the found learning goals
 */
@GetMapping("/courses/{courseId}/goals")
@PreAuthorize("hasRole('USER')")
public ResponseEntity<List<LearningGoal>> getLearningGoals(@PathVariable Long courseId) {
    log.debug("REST request to get learning goals for course with id: {}", courseId);
    Course course = courseRepository.findByIdElseThrow(courseId);
    User user = userRepository.getUserWithGroupsAndAuthorities();
    authorizationCheckService.checkHasAtLeastRoleInCourseElseThrow(Role.STUDENT, course, user);
    Set<LearningGoal> learningGoals = learningGoalRepository.findAllByCourseIdWithLectureUnitsUnidirectional(courseId);
    // if the user is a student the not yet released lecture units need to be filtered out
    if (authorizationCheckService.isOnlyStudentInCourse(course, user)) {
        for (LearningGoal learningGoal : learningGoals) {
            Set<LectureUnit> visibleLectureUnits = learningGoal.getLectureUnits().parallelStream().filter(LectureUnit::isVisibleToStudents).collect(Collectors.toSet());
            learningGoal.setLectureUnits(visibleLectureUnits);
        }
    }
    return ResponseEntity.ok(new ArrayList<>(learningGoals));
}
Also used : User(de.tum.in.www1.artemis.domain.User) LectureUnit(de.tum.in.www1.artemis.domain.lecture.LectureUnit) LearningGoal(de.tum.in.www1.artemis.domain.LearningGoal) Course(de.tum.in.www1.artemis.domain.Course) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Aggregations

PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)50 Lecture (de.tum.in.www1.artemis.domain.Lecture)42 LectureUnit (de.tum.in.www1.artemis.domain.lecture.LectureUnit)34 WithMockUser (org.springframework.security.test.context.support.WithMockUser)32 Post (de.tum.in.www1.artemis.domain.metis.Post)31 Test (org.junit.jupiter.api.Test)30 ConflictException (de.tum.in.www1.artemis.web.rest.errors.ConflictException)28 ExerciseUnit (de.tum.in.www1.artemis.domain.lecture.ExerciseUnit)22 Course (de.tum.in.www1.artemis.domain.Course)20 TextUnit (de.tum.in.www1.artemis.domain.lecture.TextUnit)18 BadRequestAlertException (de.tum.in.www1.artemis.web.rest.errors.BadRequestAlertException)18 AbstractSpringIntegrationBambooBitbucketJiraTest (de.tum.in.www1.artemis.AbstractSpringIntegrationBambooBitbucketJiraTest)16 AttachmentUnit (de.tum.in.www1.artemis.domain.lecture.AttachmentUnit)16 URI (java.net.URI)14 User (de.tum.in.www1.artemis.domain.User)13 BadRequestException (javax.ws.rs.BadRequestException)12 BeforeEach (org.junit.jupiter.api.BeforeEach)12 LearningGoal (de.tum.in.www1.artemis.domain.LearningGoal)10 AnswerPost (de.tum.in.www1.artemis.domain.metis.AnswerPost)10 EntityNotFoundException (de.tum.in.www1.artemis.web.rest.errors.EntityNotFoundException)10