module Handler.Tutorial.List ( getCTutorialListR ) where import Import import Handler.Utils import Handler.Utils.Tutorial import qualified Database.Esqueleto as E import qualified Database.Esqueleto.Utils as E import Database.Esqueleto.Utils.TH import qualified Data.Map as Map import qualified Data.CaseInsensitive as CI getCTutorialListR :: TermId -> SchoolId -> CourseShorthand -> Handler Html getCTutorialListR tid ssh csh = do muid <- maybeAuthId cid <- runDB . getKeyBy404 $ TermSchoolCourseShort tid ssh csh MsgRenderer mr <- getMsgRenderer let tutorialDBTable = DBTable{..} where resultTutorial :: Lens' (DBRow (Entity Tutorial, Int, Bool)) (Entity Tutorial) resultTutorial = _dbrOutput . _1 resultParticipants = _dbrOutput . _2 resultShowRoom = _dbrOutput . _3 dbtSQLQuery tutorial = do E.where_ $ tutorial E.^. TutorialCourse E.==. E.val cid let participants :: E.SqlExpr (E.Value Int) participants = E.subSelectCount . E.from $ \tutorialParticipant -> E.where_ $ tutorialParticipant E.^. TutorialParticipantTutorial E.==. tutorial E.^. TutorialId let showRoom = maybe E.false (flip showTutorialRoom tutorial . E.val) muid E.||. E.not_ (tutorial E.^. TutorialRoomHidden) return (tutorial, participants, showRoom) dbtRowKey = (E.^. TutorialId) dbtProj = over (_dbrOutput . _2) E.unValue . over (_dbrOutput . _3) E.unValue <$> dbtProjId dbtColonnade = dbColonnade $ mconcat [ sortable (Just "type") (i18nCell MsgTutorialType) $ \(view $ resultTutorial . _entityVal -> Tutorial{..}) -> textCell $ CI.original tutorialType , sortable (Just "name") (i18nCell MsgTutorialName) $ \(view $ resultTutorial . _entityVal -> Tutorial{..}) -> anchorCell (CTutorialR tid ssh csh tutorialName TUsersR) [whamlet|#{tutorialName}|] , sortable Nothing (i18nCell MsgTutorialTutors) $ \(view $ resultTutorial . _entityKey -> tutid) -> sqlCell $ do tutors <- fmap (map $(unValueN 3)) . E.select . E.from $ \(tutor `E.InnerJoin` user) -> do E.on $ tutor E.^. TutorUser E.==. user E.^. UserId E.where_ $ tutor E.^. TutorTutorial E.==. E.val tutid return (user E.^. UserEmail, user E.^. UserDisplayName, user E.^. UserSurname) return [whamlet| $newline never