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);
}
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);
}
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;
}
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);
}
}
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));
}
Aggregations