Skip to content
Snippets Groups Projects
Commit 1c523f5c authored by Otto Visser's avatar Otto Visser
Browse files

Merge branch '207-ghost-courses-for-teachers' into 'development'

Update active course filter for dashboard

Closes #207

See merge request !147
parents d5e7c9b3 3d98d826
Branches
No related tags found
2 merge requests!183Use Real IPs instead of Proxy IP for Sentry (#250),!147Update active course filter for dashboard
...@@ -19,7 +19,10 @@ import java.util.List; ...@@ -19,7 +19,10 @@ import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import nl.tudelft.ewi.queue.annotation.AuthenticatedUser; import nl.tudelft.ewi.queue.annotation.AuthenticatedUser;
import nl.tudelft.ewi.queue.model.*; import nl.tudelft.ewi.queue.model.Course;
import nl.tudelft.ewi.queue.model.Role;
import nl.tudelft.ewi.queue.model.User;
import nl.tudelft.ewi.queue.model.UserPrincipal;
import nl.tudelft.ewi.queue.repository.CourseRepository; import nl.tudelft.ewi.queue.repository.CourseRepository;
import nl.tudelft.ewi.queue.repository.UserRepository; import nl.tudelft.ewi.queue.repository.UserRepository;
...@@ -46,41 +49,59 @@ public class HomeController { ...@@ -46,41 +49,59 @@ public class HomeController {
return "main"; return "main";
} }
/**
* Maps the index/root url to a page. When the user is logged in correctly with an
* AnonymousAuthenticationToken, the user is directed to a dashboard view of the Queue. When the user is
* not correctly logged in, they are redirected to the introductory page of the Queue.
*
* @param model The model to be filled out when the user has courses and/or labs to show on their
* dashboard.
* @return The path to the corresponding Thymeleaf template.
*/
@RequestMapping("/") @RequestMapping("/")
public String index(Model model) { public String index(Model model) {
if (SecurityContextHolder.getContext().getAuthentication() != null) { if (SecurityContextHolder.getContext().getAuthentication() != null
if (SecurityContextHolder.getContext().getAuthentication().isAuthenticated()) { && SecurityContextHolder.getContext().getAuthentication().isAuthenticated()
if (!(SecurityContextHolder.getContext() && !(SecurityContextHolder.getContext()
.getAuthentication() instanceof AnonymousAuthenticationToken)) { .getAuthentication() instanceof AnonymousAuthenticationToken)) {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
UserPrincipal userPrincipal = (UserPrincipal) authentication.getPrincipal(); UserPrincipal userPrincipal = (UserPrincipal) authentication.getPrincipal();
return dashboard(userRepository.findByUsername(userPrincipal.getUsername()), model); return dashboard(userRepository.findByUsername(userPrincipal.getUsername()), model);
} }
}
}
return "home/index"; return "home/index";
} }
/**
* Maps the user to the dashboard page and inserts the courses and user attributes corresponding to the
* given user.
*
* @param user The user to fill the dashboard model for.
* @param model The model to fill.
* @return The path to the dashboard Thymeleaf template.
*/
private String dashboard(@AuthenticatedUser User user, Model model) { private String dashboard(@AuthenticatedUser User user, Model model) {
model.addAttribute("user", user);
List<Role> availableCourses = user.getRoles().stream() List<Role> availableCourses = user.getRoles().stream()
.filter(role -> !role.getCourse().getIsArchived() .filter(role -> !role.getCourse().isDeleted()
|| role.getUser().isTeacher()) && (!role.getCourse().getIsArchived()
|| role.getUser().isTeacher()))
.collect(Collectors.toList()); .collect(Collectors.toList());
// System.out.println(availableCourses);
model.addAttribute("availableCourses", availableCourses);
// TODO this is probably not efficient // TODO this is probably not efficient
if (user.isAdmin()) { if (user.isAdmin()) {
List<Course> runningCourses = courseRepository.findAll().stream() List<Course> runningCourses = courseRepository.findAll().stream()
.filter(course -> !course.getTodaysLabs().isEmpty()) .filter(course -> !course.getTodaysLabs().isEmpty())
.collect(Collectors.toList()); .collect(Collectors.toList());
model.addAttribute("runningCourses", runningCourses); model.addAttribute("runningCourses", runningCourses);
} }
model.addAttribute("user", user);
model.addAttribute("availableCourses", availableCourses);
return "home/dashboard"; return "home/dashboard";
} }
} }
...@@ -394,11 +394,6 @@ public class Course implements Serializable { ...@@ -394,11 +394,6 @@ public class Course implements Serializable {
return latestClosed; return latestClosed;
} }
@Override
public String toString() {
return name + " (" + code + ")";
}
public Boolean getIsArchived() { public Boolean getIsArchived() {
return isArchived; return isArchived;
} }
...@@ -406,4 +401,16 @@ public class Course implements Serializable { ...@@ -406,4 +401,16 @@ public class Course implements Serializable {
public void setIsArchived(Boolean archived) { public void setIsArchived(Boolean archived) {
isArchived = archived; isArchived = archived;
} }
/**
* @return Whether this course is deleted.
*/
public Boolean isDeleted() {
return deletedAt != null;
}
@Override
public String toString() {
return name + " (" + code + ")";
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment