module Handler.Term ( getTermShowR , getTermEditR, postTermEditR , getTermEditExistR, postTermEditExistR ) where import Import import Utils.Course (mayViewCourse) import Utils.Holidays (bankHolidaysAreaSet, Feiertagsgebiet(..)) import Handler.Utils import qualified Data.Map.Strict as Map import Data.Map.Strict ((!)) import qualified Database.Esqueleto.Legacy as E import qualified Data.Set as Set import qualified Control.Monad.State.Class as State validateTerm :: (MonadHandler m, HandlerSite m ~ UniWorX) => FormValidator TermForm m () validateTerm = do TermForm{..} <- State.get guardValidation MsgTermEndMustBeAfterStart $ tfStart < tfEnd guardValidation MsgTermLectureEndMustBeAfterStart $ tfLectureStart < tfLectureEnd guardValidation MsgTermStartMustBeBeforeLectureStart $ tfStart <= tfLectureStart guardValidation MsgTermEndMustBeAfterLectureEnd $ tfEnd >= tfLectureEnd getTermShowR :: Handler Html getTermShowR = do muid <- maybeAuthId now <- liftIO getCurrentTime ata <- getSessionActiveAuthTags table <- runDB $ let termDBTable = DBTable{..} where dbtSQLQuery term = return (term, courseCount, isActive) where courseCount = E.subSelectCount . E.from $ \(course `E.LeftOuterJoin` allocation) -> do E.on . E.exists . E.from $ \allocationCourse -> E.where_ $ allocationCourse E.^. AllocationCourseCourse E.==. course E.^. CourseId E.&&. E.just (allocationCourse E.^. AllocationCourseAllocation) E.==. allocation E.?. AllocationId E.where_ $ term E.^. TermId E.==. course E.^. CourseTerm E.&&. mayViewCourse muid ata now course (allocation E.?. AllocationId) isActive = termIsActiveE (E.val now) (E.val muid) (term E.^. TermId) dbtRowKey = (E.^. TermId) dbtProj = dbrOutput <$> dbtProjId dbtColonnade = widgetColonnade $ mconcat [ sortable (Just "term-id") (i18nCell MsgTermShort) $ \(Entity tid _, _, _) -> cell $ do mayEdit <- hasWriteAccessTo $ TermEditExistR tid [whamlet| $newline never #{toPathPiece tid} $if mayEdit   #{iconMenuAdmin} |] , sortable (Just "lecture-start") (i18nCell MsgLectureStart) $ \(Entity _ Term{..},_,_) -> cell $ formatTime SelFormatDate termLectureStart >>= toWidget , sortable (Just "lecture-end") (i18nCell MsgTermLectureEnd) $ \(Entity _ Term{..},_,_) -> cell $ formatTime SelFormatDate termLectureEnd >>= toWidget , sortable Nothing (i18nCell MsgTermActive) $ \(_, _, E.Value isActive) -> tickmarkCell isActive , sortable Nothing (i18nCell MsgTermCourseCount) $ \(_, E.Value numCourses, _) -> cell [whamlet|_{MsgNumCourses numCourses}|] , sortable (Just "start") (i18nCell MsgTermStart) $ \(Entity _ Term{..},_, _) -> cell $ formatTime SelFormatDate termStart >>= toWidget , sortable (Just "end") (i18nCell MsgTermEnd) $ \(Entity _ Term{..},_, _) -> cell $ formatTime SelFormatDate termEnd >>= toWidget , sortable Nothing (i18nCell MsgTermHolidays) $ \(Entity _ Term{..},_, _) -> cell $ do let termHolidays' = groupHolidays termHolidays [whamlet| $newline never