91 lines
4.4 KiB
Haskell
91 lines
4.4 KiB
Haskell
module Utils.Course
|
|
( mayViewCourse, mayEditCourse
|
|
, mayEditCourse'
|
|
, isCourseLecturer, isCourseTutor, isCourseCorrector, isCourseParticipant, isCourseAssociated
|
|
, isCourseLecturer'
|
|
, courseIsVisible
|
|
, numCourseParticipants
|
|
) where
|
|
|
|
import Import.NoFoundation
|
|
|
|
import qualified Database.Esqueleto as E
|
|
import qualified Database.Esqueleto.Utils as E
|
|
|
|
|
|
-- TODO switch from E.SqlExpr (Entity Course) to CourseId
|
|
|
|
|
|
mayViewCourse :: Maybe UserId -> UTCTime -> E.SqlExpr (Entity Course) -> E.SqlExpr (E.Value Bool)
|
|
mayViewCourse muid now course =
|
|
mayEditCourse muid course
|
|
E.||. isCourseAssociated muid course
|
|
E.||. courseIsVisible now course
|
|
|
|
mayEditCourse :: Maybe UserId -> E.SqlExpr (Entity Course) -> E.SqlExpr (E.Value Bool)
|
|
mayEditCourse muid course = (E.exists . E.from $ \(user `E.InnerJoin` userFunction) -> do
|
|
E.on $ user E.^. UserId E.==. userFunction E.^. UserFunctionUser
|
|
E.where_ $ E.just (user E.^. UserId) E.==. E.val muid
|
|
E.&&. userFunction E.^. UserFunctionFunction E.==. E.val SchoolAdmin
|
|
E.&&. userFunction E.^. UserFunctionSchool E.==. course E.^. CourseSchool
|
|
) E.||. isCourseLecturer muid course
|
|
|
|
mayEditCourse' :: Maybe UserId -> Entity Course -> E.SqlExpr (E.Value Bool)
|
|
mayEditCourse' muid (Entity cid Course{..}) = (E.exists . E.from $ \(user `E.InnerJoin` userFunction) -> do
|
|
E.on $ user E.^. UserId E.==. userFunction E.^. UserFunctionUser
|
|
E.where_ $ E.just (user E.^. UserId) E.==. E.val muid
|
|
E.&&. userFunction E.^. UserFunctionFunction E.==. E.val SchoolAdmin
|
|
E.&&. userFunction E.^. UserFunctionSchool E.==. E.val courseSchool
|
|
) E.||. isCourseLecturer' muid cid
|
|
|
|
isCourseLecturer :: Maybe UserId -> E.SqlExpr (Entity Course) -> E.SqlExpr (E.Value Bool)
|
|
isCourseLecturer muid course = E.exists . E.from $ \(user `E.InnerJoin` lecturer) -> do
|
|
E.on $ user E.^. UserId E.==. lecturer E.^. LecturerUser
|
|
E.where_ $ E.just (user E.^. UserId) E.==. E.val muid
|
|
E.&&. lecturer E.^. LecturerCourse E.==. course E.^. CourseId
|
|
|
|
isCourseLecturer' :: Maybe UserId -> CourseId -> E.SqlExpr (E.Value Bool)
|
|
isCourseLecturer' muid cid = E.exists . E.from $ \(user `E.InnerJoin` lecturer) -> do
|
|
E.on $ user E.^. UserId E.==. lecturer E.^. LecturerUser
|
|
E.where_ $ E.just (user E.^. UserId) E.==. E.val muid
|
|
E.&&. lecturer E.^. LecturerCourse E.==. E.val cid
|
|
|
|
isCourseTutor :: Maybe UserId -> E.SqlExpr (Entity Course) -> E.SqlExpr (E.Value Bool)
|
|
isCourseTutor muid course = E.exists . E.from $ \(tutor `E.InnerJoin` tutorial) -> do
|
|
E.on $ tutor E.^. TutorTutorial E.==. tutorial E.^. TutorialId
|
|
E.where_ $ E.just (tutor E.^. TutorUser) E.==. E.val muid
|
|
E.&&. tutorial E.^. TutorialCourse E.==. course E.^. CourseId
|
|
|
|
isCourseCorrector :: Maybe UserId -> E.SqlExpr (Entity Course) -> E.SqlExpr (E.Value Bool)
|
|
isCourseCorrector muid course = E.exists . E.from $ \(sheetCorrector `E.InnerJoin` sheet) -> do
|
|
E.on $ sheetCorrector E.^. SheetCorrectorSheet E.==. sheet E.^. SheetId
|
|
E.where_ $ E.just (sheetCorrector E.^. SheetCorrectorUser) E.==. E.val muid
|
|
E.&&. sheet E.^. SheetCourse E.==. course E.^. CourseId
|
|
|
|
isCourseParticipant :: Maybe UserId -> E.SqlExpr (Entity Course) -> E.SqlExpr (E.Value Bool)
|
|
isCourseParticipant muid course = E.exists . E.from $ \courseParticipant -> do
|
|
E.where_ $ E.just (courseParticipant E.^. CourseParticipantUser) E.==. E.val muid
|
|
E.&&. courseParticipant E.^. CourseParticipantCourse E.==. course E.^. CourseId
|
|
E.&&. courseParticipant E.^. CourseParticipantState E.==. E.val CourseParticipantActive
|
|
|
|
isCourseAssociated :: Maybe UserId -> E.SqlExpr (Entity Course) -> E.SqlExpr (E.Value Bool)
|
|
isCourseAssociated muid course =
|
|
isCourseLecturer muid course
|
|
E.||. isCourseTutor muid course
|
|
E.||. isCourseCorrector muid course
|
|
E.||. isCourseParticipant muid course
|
|
|
|
courseIsVisible :: UTCTime -> E.SqlExpr (Entity Course) -> E.SqlExpr (E.Value Bool)
|
|
courseIsVisible now course =
|
|
E.isJust (course E.^. CourseVisibleFrom)
|
|
E.&&. course E.^. CourseVisibleFrom E.<=. E.val (Just now)
|
|
E.&&. (
|
|
E.isNothing (course E.^. CourseVisibleTo)
|
|
E.||. E.val (Just now) E.<=. course E.^. CourseVisibleTo
|
|
)
|
|
|
|
numCourseParticipants :: E.SqlExpr (Entity Course) -> E.SqlExpr (E.Value Int)
|
|
numCourseParticipants course = E.subSelectCount . E.from $ \courseParticipant ->
|
|
E.where_ $ courseParticipant E.^. CourseParticipantCourse E.==. course E.^. CourseId
|
|
E.&&. courseParticipant E.^. CourseParticipantState E.==. E.val CourseParticipantActive
|