module Handler.Term where import Import import Handler.Utils import qualified Data.Map as Map import qualified Database.Esqueleto as E import qualified Data.Set as Set import qualified Control.Monad.State.Class as State -- | Default start day of term for season, -- @True@: start of term, @False@: end of term defaultDay :: Bool -> Season -> Day defaultDay True Winter = fromGregorian 2020 10 1 defaultDay False Winter = fromGregorian 2020 3 31 defaultDay True Summer = fromGregorian 2020 4 1 defaultDay False Summer = fromGregorian 2020 9 30 validateTerm :: (MonadHandler m, HandlerSite m ~ UniWorX) => FormValidator Term m () validateTerm = do Term{..} <- State.get guardValidation MsgTermStartMustMatchName $ termStart `withinTerm` termName guardValidation MsgTermEndMustBeAfterStart $ termStart < termEnd guardValidation MsgTermLectureEndMustBeAfterStart $ termLectureStart < termLectureEnd guardValidation MsgTermStartMustBeBeforeLectureStart $ termStart <= termLectureStart guardValidation MsgTermEndMustBeAfterLectureEnd $ termEnd >= termLectureEnd getTermShowR :: Handler TypedContent getTermShowR = do -- terms <- runDB $ selectList [] [Desc TermStart] ------- ÄQUIVALENT: -- term <- runDB $ E.select . E.from $ \(term) -> do -- E.orderBy [E.desc $ term E.^. TermStart ] -- return term -- let termData :: E.SqlExpr (Entity Term) -> E.SqlQuery (E.SqlExpr (Entity Term), E.SqlExpr (E.Value Int64)) termData term = do -- E.orderBy [E.desc $ term E.^. TermStart ] let courseCount = E.subSelectCount . E.from $ \course -> E.where_ $ term E.^. TermId E.==. course E.^. CourseTerm return (term, courseCount) selectRep $ do provideRep $ toJSON . map fst <$> runDB (E.select $ E.from termData) provideRep $ do let colonnadeTerms = widgetColonnade $ mconcat [ sortable (Just "term-id") (i18nCell MsgTermShort) $ \(Entity tid _, _) -> anchorCell (TermCourseListR tid) [whamlet|#{toPathPiece tid}|] , 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) $ \(Entity _ Term{..},_) -> tickmarkCell termActive , 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 termHolidays' <- mapM (formatTime SelFormatDate) termHolidays [whamlet| $newline never