chore(tutorial): WIP templates advancement

This commit is contained in:
Steffen Jost 2023-05-23 17:28:22 +02:00
parent 5446ca5406
commit c2521df20b
5 changed files with 55 additions and 52 deletions

View File

@ -289,37 +289,34 @@ upsertNewTutorial cid tutorialName = do
audit $ TransactionTutorialEdit tutId audit $ TransactionTutorialEdit tutId
return tutId return tutId
-- tutorialTemplates :: [CI Text] tutorialTemplateNames :: Maybe (CI Text) -> [CI Text]
-- tutorialTemplates = ["Vorlage", "Template"] tutorialTemplateNames Nothing = ["Vorlage", "Template"]
tutorialTemplateNames (Just name) = [prefixes <> suffixes | prefixes <- tutorialTemplateNames Nothing, suffixes <- ["", Text.cons '_' name]]
{- upsertNewTutorialTemplate :: CourseId -> TutorialName -> Maybe (CI Text) -> Maybe Day -> Handler TutorialId
upsertNewTutorialTemplate :: CourseId -> TutorialName -> Maybe Day -> Handler TutorialId upsertNewTutorialTemplate cid newTutorialName newTutorialType anchorDay = runDB $ do
upsertNewTutorialTemplate cid tutorialName anchorDay = runDB $ do
now <- liftIO getCurrentTime now <- liftIO getCurrentTime
existingTut <- getBy $ UniqueTutorial cid tutorialName existingTut <- getBy $ UniqueTutorial cid tutorialName
templateEnt <- selectFirst [TutorialType <-. tutorialTemplates] [Desc TutorialType] templateEnt <- selectFirst [TutorialType <-. tutorialTemplateNames newTutorialType] [Desc TutorialType]
case (existingTut, anchorDay, templateEnt) of case (existingTut, anchorDay, templateEnt) of
(Just (Entity{entityKey=tid}),_,_) -> return tid -- no need to update, we ignore the anchor day (Just (Entity{entityKey=tid}),_,_) -> return tid -- no need to update, we ignore the anchor day
(Nothing, Just firstDay, Just Entity{entityVal=Tutorial{tutorialFirstDay=Just tmplFirstDay}}) -> do (Nothing, Just newFirstDay, Just Entity{entityVal=Tutorial{..}}) -> do
Course{..} <- get404 cid Course{..} <- get404 cid
Term{termLectureStart} <- get404 courseTerm term <- get404 courseTerm
let dayDiff = diffDays firstDay tmplFirstDay let newTime = occurrencesAddBusinessDays term (tutorialFirstDay, newFirstDay)
-- addBusinessDays
Entity tutId _ <- upsert Entity tutId _ <- upsert
Tutorial Tutorial
{ tutorialCourse = cid { tutorialCourse = cid
, tutorialType = CI.mk "Schulung" , tutorialType = fromMaybe (CI.mk "Schulung") newTutorialType
, tutorialCapacity = Nothing , tutorialTime = newTime
, tutorialRoom = Nothing , tutorialFirstDay = newFirstDay
, tutorialRoomHidden = False , tutorialName = newTutorialName
, tutorialTime = Occurrences mempty mempty -- TODO
, tutorialRegGroup = Nothing -- TODO: remove
, tutorialRegisterFrom = Nothing , tutorialRegisterFrom = Nothing
, tutorialRegisterTo = Nothing , tutorialRegisterTo = Nothing
, tutorialDeregisterUntil = Nothing , tutorialDeregisterUntil = Nothing
, tutorialLastChanged = now , tutorialLastChanged = now
, tutorialTutorControlled = False
, tutorialFirstDay = anchorDay
, .. , ..
} [] } []
-- error "TODO" -- CONTINUE HERE -- error "TODO" -- CONTINUE HERE

View File

@ -20,14 +20,14 @@ import Handler.Tutorial.TutorInvite
getCTutorialNewR, postCTutorialNewR :: TermId -> SchoolId -> CourseShorthand -> Handler Html getCTutorialNewR, postCTutorialNewR :: TermId -> SchoolId -> CourseShorthand -> Handler Html
getCTutorialNewR = postCTutorialNewR getCTutorialNewR = postCTutorialNewR
postCTutorialNewR tid ssh csh = do postCTutorialNewR tid ssh csh = do
cid <- runDB . getKeyBy404 $ TermSchoolCourseShort tid ssh csh Entity{entityKey=cid, entityVal=course} <- runDB . getBy404 $ TermSchoolCourseShort tid ssh csh -- TODO: use getKeyBy404 if was optimized to no longer retrieve the full entity from the DB anyway
((newTutResult, newTutWidget), newTutEnctype) <- runFormPost $ tutorialForm cid Nothing ((newTutResult, newTutWidget), newTutEnctype) <- runFormPost $ tutorialForm cid Nothing
formResult newTutResult $ \TutorialForm{..} -> do formResult newTutResult $ \TutorialForm{..} -> do
insertRes <- runDBJobs $ do insertRes <- runDBJobs $ do
now <- liftIO getCurrentTime now <- liftIO getCurrentTime
term <- fetchTermByCID cid term <- get404 $ course ^. CourseTerm
insertRes <- insertUnique Tutorial insertRes <- insertUnique Tutorial
{ tutorialName = tfName { tutorialName = tfName
, tutorialCourse = cid , tutorialCourse = cid

View File

@ -5,14 +5,14 @@
module Handler.Utils.Occurrences module Handler.Utils.Occurrences
( occurrencesWidget ( occurrencesWidget
, occurrencesBounds , occurrencesBounds
-- , occurrencesAddBusinessDays , occurrencesAddBusinessDays
) where ) where
import Import import Import
import qualified Data.Set as Set import qualified Data.Set as Set
-- import Utils.Holidays (isWeekend) import Utils.Holidays (isWeekend)
import Utils.Occurrences import Utils.Occurrences
import Handler.Utils.DateTime import Handler.Utils.DateTime
@ -51,30 +51,29 @@ occurrencesBounds Term{..} Occurrences{..} = (Set.lookupMin occDays, Set.lookupM
getOccDays :: OccurrenceSchedule -> Set Day -> Set Day getOccDays :: OccurrenceSchedule -> Set Day -> Set Day
getOccDays ScheduleWeekly{scheduleDayOfWeek=wday} = Set.union $ daysOfWeekBetween (termLectureStart,termLectureEnd) wday getOccDays ScheduleWeekly{scheduleDayOfWeek=wday} = Set.union $ daysOfWeekBetween (termLectureStart,termLectureEnd) wday
-- occurrencesAddBusinessDays :: Term -> (Day,Day) -> Occurrences -> Occurrences occurrencesAddBusinessDays :: Term -> (Day,Day) -> Occurrences -> Occurrences
-- occurrencesAddBusinessDays Term{..} (dayOld, dayNew) Occurrences{..} = Occurrences newSchedule newExceptions occurrencesAddBusinessDays Term{..} (dayOld, dayNew) Occurrences{..} = Occurrences newSchedule newExceptions
-- where where
-- newSchedule = Set.map switchDayOfWeek occurrencesScheduled newSchedule = Set.map switchDayOfWeek occurrencesScheduled
-- dayDiff = diffDays dayNew dayOld dayDiff = diffDays dayNew dayOld
-- switchDayOfWeek :: OccurrenceSchedule -> OccurrenceSchedule offDays = Set.fromList $ termHolidays <> weekends
-- switchDayOfWeek _ | 0 == dayDiff `mod` 7 = id weekends = [d | d <- [(min termLectureStart termStart)..(max termEnd termLectureEnd)], isWeekend d]
-- switchDayOfWeek os@ScheduleWeekly{scheduleDayOfWeek=wday} = os{scheduleDayOfWeek= toEnum (dayDiff + fromEnum wday)}
-- newExceptions = snd $ Set.foldr advanceExceptions (dayDiff,mempty) occurrencesExceptions switchDayOfWeek :: OccurrenceSchedule -> OccurrenceSchedule
switchDayOfWeek _ | 0 == dayDiff `mod` 7 = id
switchDayOfWeek os@ScheduleWeekly{scheduleDayOfWeek=wday} = os{scheduleDayOfWeek= toEnum (dayDiff + fromEnum wday)}
-- advanceExceptions :: OccurrenceException -> (Integer, Set OccurrenceException) -> (Integer, Set OccurrenceException) newExceptions = snd $ Set.foldr advanceExceptions (dayDiff,mempty) occurrencesExceptions
-- advanceExceptions ex@ExceptOccur{ exceptDay = ed } (offset, acc) =
-- | add
-- we assume that instance Ord OccurrenceException is ordered chronologically
-- advanceExceptions ex@ExceptOccur{ exceptDay = ed } = ex{ exceptDay = pushSkip ed } advanceExceptions :: OccurrenceException -> (Integer, Set OccurrenceException) -> (Integer, Set OccurrenceException)
-- advanceExceptions ex@ExceptNoOccur{ exceptTime = et@LocalTime { localDay = ed } } = ex{ exceptDay = et{ localDay = pushSkip ed}} advanceExceptions ex (offset, acc)
| ed `Set.notMember` offDays -- skip term-holidays and weekends, unless the original day was a holiday or weekend
-- pushSkip , nd `Set.member` offDays
-- pushSkip :: Day -> Day = advanceExceptions ex (succ offset, acc)
-- pushSkip = id -- TODO | otherwise
-- -- pushSkip = let weekends = [d | d <- [(min termLectureStart termStart)..(max termEnd termLectureEnd)], isWeekend d] = (offset, Set.insert (setDayOfOccurrenceException nd ex) acc)
-- -- offDays = Set.fromList $ termHolidays <> weekends where
ed = dayOfOccurrenceException ex
-- -- in nd = addDays offset ed

View File

@ -69,7 +69,7 @@ fetchTermByCID :: ( MonadHandler m
) )
=> CourseId -> ReaderT backend m Term => CourseId -> ReaderT backend m Term
fetchTermByCID cid = do fetchTermByCID cid = do
termList <- E.select . E.from $ \(course `E.InnerJoin` term) -> do termList <- E.select . E.distinct . E.from $ \(course `E.InnerJoin` term) -> do
E.on $ course E.^. CourseTerm E.==. term E.^. TermId E.on $ course E.^. CourseTerm E.==. term E.^. TermId
E.where_ $ course E.^. CourseId E.==. E.val cid E.where_ $ course E.^. CourseId E.==. E.val cid
return term return term

View File

@ -188,15 +188,14 @@ data OccurrenceException = ExceptOccur
deriving (Eq, Read, Show, Generic) deriving (Eq, Read, Show, Generic)
deriving anyclass (NFData) deriving anyclass (NFData)
-- Handler.Utils.Occurrences.occurrencesAddBusinessDays assumes that OccurrenceException is ordered chronologically
instance Ord OccurrenceException where instance Ord OccurrenceException where
compare ExceptOccur{exceptDay=ad, exceptStart=as, exceptEnd=ae} ExceptOccur{exceptDay=bd, exceptStart=bs, exceptEnd=be} compare ExceptOccur{exceptDay=ad, exceptStart=as, exceptEnd=ae} ExceptOccur{exceptDay=bd, exceptStart=bs, exceptEnd=be}
= compare (ad,as,ae) (bd,bs,be) = compare (ad,as,ae) (bd,bs,be)
compare ExceptOccur{exceptDay=d, exceptStart=s} ExceptNoOccur{exceptTime=e} compare ExceptOccur{exceptDay=d, exceptStart=s} ExceptNoOccur{exceptTime=e}
= -- replaceEq GT $ = replaceEq LT $ compare (LocalTime d s) e
compare (LocalTime d s) e
compare ExceptNoOccur{exceptTime=e } ExceptOccur{exceptDay=d, exceptStart=s} compare ExceptNoOccur{exceptTime=e } ExceptOccur{exceptDay=d, exceptStart=s}
= -- replaceEq LT $ = replaceEq GT $ compare e (LocalTime d s)
compare e (LocalTime d s)
compare ExceptNoOccur{exceptTime=ae } ExceptNoOccur{exceptTime=be } compare ExceptNoOccur{exceptTime=ae } ExceptNoOccur{exceptTime=be }
= compare ae be = compare ae be
@ -206,6 +205,14 @@ deriveJSON defaultOptions
, sumEncoding = TaggedObject "exception" "for" , sumEncoding = TaggedObject "exception" "for"
} ''OccurrenceException } ''OccurrenceException
dayOfOccurrenceException :: OccurrenceException -> Day
dayOfOccurrenceException ExceptOccur{exceptDay=d} = d
dayOfOccurrenceException ExceptNoOccur{exceptTime=LocalTime{localDay=d}} = d
setDayOfOccurrenceException :: Day -> OccurrenceException -> OccurrenceException
setDayOfOccurrenceException d ex@ExceptOccur{} = ex{exceptDay=d}
setDayOfOccurrenceException d ExceptNoOccur{exceptTime=lt} = ExceptNoOccur{exceptTime = lt{localDay=d}}
data Occurrences = Occurrences data Occurrences = Occurrences
{ occurrencesScheduled :: Set OccurrenceSchedule { occurrencesScheduled :: Set OccurrenceSchedule
, occurrencesExceptions :: Set OccurrenceException , occurrencesExceptions :: Set OccurrenceException