diff --git a/ChangeLog.md b/ChangeLog.md index 9f07bd783..64cf0b8f7 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -1,3 +1,7 @@ + * Version 20.05.2019 + + Komplett überarbeitete Funktionalität zur automatischen Verteilung von Korrekturen + * Version 13.05.2019 Kursverwalter können Teilnehmer hinzufügen diff --git a/messages/uniworx/de.msg b/messages/uniworx/de.msg index 70f005dfc..d7a6a484b 100644 --- a/messages/uniworx/de.msg +++ b/messages/uniworx/de.msg @@ -394,6 +394,10 @@ UpdatedAssignedCorrectorsAuto num@Int64: #{display num} Abgaben wurden unter den CouldNotAssignCorrectorsAuto num@Int64: #{display num} Abgaben konnten nicht automatisch zugewiesen werden: SelfCorrectors num@Int64: #{display num} Abgaben wurden Abgebenden als eigenem Korrektor zugeteilt! +AssignSubmissionExceptionNoCorrectors: Es sind keine Korrektoren eingestellt +AssignSubmissionExceptionNoCorrectorsByProportion: Es sind keine Korrektoren mit Anteil ungleich Null eingestellt +AssignSubmissionExceptionSubmissionsNotFound n@Int: #{tshow n} Abgaben konnten nicht gefunden werden + CorrectionsUploaded num@Int64: #{display num} Korrekturen wurden gespeichert: NoCorrectionsUploaded: In der hochgeladenen Datei wurden keine Korrekturen gefunden. @@ -521,6 +525,7 @@ UploadModeUnpackZipsTip: Wenn die Abgabe mehrerer Dateien erlaubt ist, werden au UploadModeExtensionRestriction: Zulässige Dateiendungen UploadModeExtensionRestrictionTip: Komma-separiert. Wenn keine Dateiendungen angegeben werden erfolgt keine Einschränkung. +UploadModeExtensionRestrictionEmpty: Liste von zulässigen Dateiendungen darf nicht leer sein UploadSpecificFiles: Vorgegebene Dateinamen NoUploadSpecificFilesConfigured: Wenn der Abgabemodus vorgegebene Dateinamen vorsieht, muss mindestens ein vorgegebener Dateiname konfiguriert werden. diff --git a/models/tutorials b/models/tutorials index 444d988cd..4961e0bd5 100644 --- a/models/tutorials +++ b/models/tutorials @@ -11,6 +11,7 @@ Tutorial json deregisterUntil UTCTime Maybe lastChanged UTCTime default=now() UniqueTutorial course name + deriving Generic Tutor tutorial TutorialId user UserId diff --git a/package.yaml b/package.yaml index 098fb0bec..417b74e26 100644 --- a/package.yaml +++ b/package.yaml @@ -243,6 +243,7 @@ tests: - uniworx - hspec >=2.0.0 - QuickCheck + - HUnit - yesod-test - conduit-extra - quickcheck-classes diff --git a/src/Handler/Corrections.hs b/src/Handler/Corrections.hs index 78b5d187a..ca358a335 100644 --- a/src/Handler/Corrections.hs +++ b/src/Handler/Corrections.hs @@ -432,7 +432,18 @@ correctionsR whereClause displayColumns dbtFilterUI psValidator actions = do redirect currentRoute FormSuccess (CorrAutoSetCorrectorData shid, subs') -> do subs <- mapM decrypt $ Set.toList subs' - runDB $ do + let + assignExceptions :: AssignSubmissionException -> Handler () + assignExceptions NoCorrectors = addMessageI Error MsgAssignSubmissionExceptionNoCorrectors + assignExceptions NoCorrectorsByProportion = addMessageI Error MsgAssignSubmissionExceptionNoCorrectorsByProportion + assignExceptions (SubmissionsNotFound subIds) = do + subCIDs <- mapM encrypt . Set.toList $ toNullable subIds :: Handler [CryptoFileNameSubmission] + let errorModal = msgModal + [whamlet|_{MsgAssignSubmissionExceptionSubmissionsNotFound (length subCIDs)}|] + (Right $(widgetFile "messages/submissionsAssignNotFound")) + addMessageWidget Error errorModal + + handle assignExceptions . runDB $ do alreadyAssigned <- selectList [SubmissionId <-. subs, SubmissionRatingBy !=. Nothing] [] unless (null alreadyAssigned) $ do mr <- (toHtml . ) <$> getMessageRender diff --git a/src/Handler/Utils/Form.hs b/src/Handler/Utils/Form.hs index a9dbe1ede..12fdc847c 100644 --- a/src/Handler/Utils/Form.hs +++ b/src/Handler/Utils/Form.hs @@ -359,15 +359,15 @@ uploadModeForm prev = multiActionA actions (fslI MsgSheetUploadMode) (classifyUp , ( UploadModeAny , UploadAny <$> apreq checkBoxField (fslI MsgUploadModeUnpackZips & setTooltip MsgUploadModeUnpackZipsTip) (prev ^? _Just . _unpackZips) - <*> apreq extensionRestrictionField (fslI MsgUploadModeExtensionRestriction & setTooltip MsgUploadModeExtensionRestrictionTip) ((prev ^? _Just . _extensionRestriction) <|> fmap Just defaultExtensionRestriction) + <*> aopt extensionRestrictionField (fslI MsgUploadModeExtensionRestriction & setTooltip MsgUploadModeExtensionRestrictionTip) ((prev ^? _Just . _extensionRestriction) <|> fmap Just defaultExtensionRestriction) ) , ( UploadModeSpecific , UploadSpecific <$> specificFileForm ) ] - extensionRestrictionField :: Field Handler (Maybe (NonNull (Set Extension))) - extensionRestrictionField = convertField (fromNullable . toSet) (maybe "" $ intercalate ", " . Set.toList . toNullable) textField + extensionRestrictionField :: Field Handler (NonNull (Set Extension)) + extensionRestrictionField = checkMMap (return . maybe (Left MsgUploadModeExtensionRestrictionEmpty) Right . fromNullable . toSet) (intercalate ", " . Set.toList . toNullable) textField where toSet = Set.fromList . filter (not . Text.null) . map (stripDot . Text.strip) . Text.splitOn "," stripDot ext diff --git a/src/Handler/Utils/Submission.hs b/src/Handler/Utils/Submission.hs index 09c59f6b3..be6745a6a 100644 --- a/src/Handler/Utils/Submission.hs +++ b/src/Handler/Utils/Submission.hs @@ -13,27 +13,25 @@ module Handler.Utils.Submission import Import hiding (joinPath) import Jobs.Queue -import Prelude (lcm) import Yesod.Core.Types (HandlerContents(..), ErrorResponse(..)) import Utils.Lens -import Control.Monad.State hiding (forM_, mapM_,foldM) +import Control.Monad.State as State (StateT) +import Control.Monad.State.Class as State import Control.Monad.Writer (MonadWriter(..), execWriterT, execWriter) -import Control.Monad.RWS.Lazy (RWST) +import Control.Monad.RWS.Lazy (MonadRWS, RWST, execRWST) import qualified Control.Monad.Random as Rand import qualified System.Random.Shuffle as Rand (shuffleM) import Data.Maybe () -import qualified Data.List as List import Data.Set (Set) import qualified Data.Set as Set -import Data.Map (Map) +import Data.Map (Map, (!), (!?)) import qualified Data.Map as Map import qualified Data.Text as Text -import Data.Ratio import Data.Monoid (Monoid, Any(..), Sum(..)) import Generics.Deriving.Monoid (memptydefault, mappenddefault) @@ -56,155 +54,178 @@ import Text.Hamlet (ihamletFile) import qualified Control.Monad.Catch as E (Handler(..)) -data AssignSubmissionException = NoCorrectorsByProportion - deriving (Typeable, Show) +data AssignSubmissionException = NoCorrectors + | NoCorrectorsByProportion + | SubmissionsNotFound (NonNull (Set SubmissionId)) + deriving (Eq, Ord, Read, Show, Generic, Typeable) instance Exception AssignSubmissionException -- | Assigns all submissions according to sheet corrector loads -assignSubmissions :: SheetId -- ^ Sheet do distribute to correction +assignSubmissions :: SheetId -- ^ Sheet to distribute to correctors -> Maybe (Set SubmissionId) -- ^ Optionally restrict submission to consider -> YesodDB UniWorX ( Set SubmissionId , Set SubmissionId ) -- ^ Returns assigned and unassigned submissions; unassigned submissions occur only if no tutors have an assigned load assignSubmissions sid restriction = do Sheet{..} <- getJust sid - correctors <- selectList [ SheetCorrectorSheet ==. sid, SheetCorrectorState ==. CorrectorNormal ] [] - let - -- byTutorial' uid = join . Map.lookup uid $ Map.fromList [ (sheetCorrectorUser, byTutorial sheetCorrectorLoad) | Entity _ SheetCorrector{..} <- corrsTutorial ] - corrsTutorial = filter hasTutorialLoad correctors -- needed as List within Esqueleto - corrsProp = filter hasPositiveLoad correctors - countsToLoad' :: UserId -> Bool - countsToLoad' uid = Map.findWithDefault True uid loadMap - loadMap :: Map UserId Bool - loadMap = Map.fromList [(sheetCorrectorUser,b) | Entity _ SheetCorrector{ sheetCorrectorLoad = (Load {byTutorial = Just b}), .. } <- corrsTutorial] - - currentSubs <- E.select . E.from $ \(submission `E.LeftOuterJoin` tutor') -> do - let tutors = E.subList_select . E.from $ \(submissionUser `E.InnerJoin` tutorialUser `E.InnerJoin` tutorial `E.InnerJoin` tutor) -> do - -- Uncomment next line for equal chance between tutors, irrespective of the number of students per tutor per submission group - -- E.distinctOn [E.don $ tutorial E.^. TutorialTutor] $ do - E.on (tutorial E.^. TutorialId E.==. tutor E.^. TutorTutorial) - E.on (tutorial E.^. TutorialId E.==. tutorialUser E.^. TutorialParticipantTutorial) - E.on (submissionUser E.^. SubmissionUserUser E.==. tutorialUser E.^. TutorialParticipantUser) - E.where_ (tutor E.^. TutorUser `E.in_` E.valList (map (sheetCorrectorUser . entityVal) corrsTutorial)) - return $ tutor E.^. TutorUser - E.on $ tutor' E.?. UserId `E.in_` E.justList tutors - E.where_ $ submission E.^. SubmissionSheet E.==. E.val sid - E.&&. maybe (E.val True) (submission E.^. SubmissionId `E.in_`) (E.valList . Set.toList <$> restriction) - return (submission E.^. SubmissionId, tutor' E.?. UserId) - - let subTutor' :: Map SubmissionId (Set UserId) - subTutor' = Map.fromListWith Set.union $ currentSubs - & mapped._2 %~ (maybe Set.empty Set.singleton . E.unValue) - & mapped._1 %~ E.unValue - - prevSubs <- E.select . E.from $ \((sheet `E.InnerJoin` sheetCorrector) `E.LeftOuterJoin` submission) -> do - E.on $ E.joinV (submission E.?. SubmissionRatingBy) E.==. E.just (sheetCorrector E.^. SheetCorrectorUser) - E.on $ sheetCorrector E.^. SheetCorrectorSheet E.==. sheet E.^. SheetId - let isByTutorial = E.exists . E.from $ \(submissionUser `E.InnerJoin` tutorialUser `E.InnerJoin` tutorial `E.InnerJoin` tutor) -> do - E.on (tutorial E.^. TutorialId E.==. tutor E.^. TutorTutorial) - E.on $ tutorial E.^. TutorialId E.==. tutorialUser E.^. TutorialParticipantTutorial - E.on $ submissionUser E.^. SubmissionUserUser E.==. tutorialUser E.^. TutorialParticipantUser - E.where_ $ tutor E.^. TutorUser E.==. sheetCorrector E.^. SheetCorrectorUser - E.&&. submission E.?. SubmissionId E.==. E.just (submissionUser E.^. SubmissionUserSubmission) - E.where_ $ sheet E.^. SheetCourse E.==. E.val sheetCourse - E.&&. sheetCorrector E.^. SheetCorrectorUser `E.in_` E.valList (map (sheetCorrectorUser . entityVal) correctors) - return (sheetCorrector, isByTutorial, E.isNothing (submission E.?. SubmissionId)) + correctorsRaw <- E.select . E.from $ \(sheet `E.InnerJoin` sheetCorrector) -> do + E.on $ sheet E.^. SheetId E.==. sheetCorrector E.^. SheetCorrectorSheet + E.where_ $ sheetCorrector E.^. SheetCorrectorState `E.in_` E.valList [CorrectorNormal, CorrectorMissing] + return (sheet E.^. SheetId, sheetCorrector) let - prevSubs' :: Map SheetId (Map UserId (Rational, Integer)) - prevSubs' = Map.unionsWith (Map.unionWith $ \(prop, n) (_, n') -> (prop, n + n')) $ do - (Entity _ SheetCorrector{ sheetCorrectorLoad = Load{..}, .. }, E.Value isByTutorial, E.Value isPlaceholder) <- prevSubs - guard $ maybe True (not isByTutorial ||) byTutorial - let proportion - | CorrectorExcused <- sheetCorrectorState = 0 - | otherwise = byProportion - return . Map.singleton sheetCorrectorSheet $ Map.singleton sheetCorrectorUser (proportion, bool 1 0 isPlaceholder) + correctors :: Map SheetId (Map UserId (Load, CorrectorState)) + correctors = Map.fromList $ do + E.Value sheetId <- Set.toList $ setOf (folded . _1) correctorsRaw + let loads = Map.fromList $ do + (E.Value sheetId', Entity _ SheetCorrector{..}) + <- correctorsRaw + guard $ sheetId' == sheetId + return (sheetCorrectorUser, (sheetCorrectorLoad, sheetCorrectorState)) + return (sheetId, loads) - deficit :: Map UserId Integer - deficit = Map.filter (> 0) $ Map.foldr (Map.unionWith (+) . toDeficit) Map.empty prevSubs' - - toDeficit :: Map UserId (Rational, Integer) -> Map UserId Integer - toDeficit assignments = toDeficit' <$> assignments + sheetCorrectors :: Map UserId Load + sheetCorrectors = Map.mapMaybe filterLoad $ correctors ! sid where - assigned' = getSum $ foldMap (Sum . snd) assignments - props = getSum $ foldMap (Sum . fst) assignments + filterLoad (l@Load{..}, CorrectorNormal) = l <$ guard (isJust byTutorial || byProportion /= 0) + filterLoad _ = Nothing - toDeficit' (prop, assigned) = let - target - | props == 0 = 0 - | otherwise = round $ fromInteger assigned' * (prop / props) - in target - assigned + unless (Map.member sid correctors) $ + throwM NoCorrectors - $logDebugS "assignSubmissions" $ "Previous submissions: " <> tshow prevSubs' - $logDebugS "assignSubmissions" $ "Current deficit: " <> tshow deficit + submissionDataRaw <- E.select . E.from $ \((sheet `E.InnerJoin` submission `E.InnerJoin` submissionUser) `E.LeftOuterJoin` (tutorial `E.InnerJoin` tutorialUser `E.InnerJoin` tutor)) -> do + E.on $ tutor E.?. TutorTutorial E.==. tutorial E.?. TutorialId + E.on $ tutorialUser E.?. TutorialParticipantTutorial E.==. tutorial E.?. TutorialId + E.on $ tutorialUser E.?. TutorialParticipantUser E.==. E.just (submissionUser E.^. SubmissionUserUser) + E.&&. tutor E.?. TutorUser `E.in_` E.justList (E.valList $ foldMap Map.keys correctors) + E.&&. tutorial E.?. TutorialCourse E.==. E.just (E.val sheetCourse) + E.on $ submission E.^. SubmissionId E.==. submissionUser E.^. SubmissionUserSubmission + E.on $ submission E.^. SubmissionSheet E.==. sheet E.^. SheetId + + E.where_ $ sheet E.^. SheetCourse E.==. E.val sheetCourse + + return (sheet E.^. SheetId, submission, tutor E.?. TutorUser) let - lcd :: Integer - lcd = foldr lcm 1 $ map (denominator . byProportion . sheetCorrectorLoad . entityVal) corrsProp - wholeProps :: Map UserId Integer - wholeProps = Map.fromList [ ( sheetCorrectorUser, round $ byProportion * fromInteger lcd ) | Entity _ SheetCorrector{ sheetCorrectorLoad = Load{..}, .. } <- corrsProp ] - detQueueLength = fromIntegral (Map.size $ Map.filter (\tuts -> all countsToLoad' tuts) subTutor') - sum deficit - detQueue = concat . List.genericReplicate (detQueueLength `div` sum wholeProps) . concatMap (uncurry $ flip List.genericReplicate) $ Map.toList wholeProps + -- | All submissions in this course so far + submissionData :: Map SubmissionId + ( Maybe UserId -- Corrector + , Map UserId (Sum Natural) -- Tutors + , SheetId + ) + submissionData = Map.fromListWith merge $ map process submissionDataRaw + where + process (E.Value sheetId, Entity subId Submission{..}, E.Value mTutId) = (subId, (submissionRatingBy, maybe Map.empty (flip Map.singleton $ Sum 1) $ assertM isCorrectorByTutorial mTutId, sheetId)) + merge (corrA, tutorsA, sheetA) (corrB, tutorsB, sheetB) + | corrA /= corrB = error "Same submission seen with different correctors" + | sheetA /= sheetB = error "Same submission seen with different sheets" + | otherwise = (corrA, Map.unionWith mappend tutorsA tutorsB, sheetA) - $logDebugS "assignSubmissions" $ "Deterministic Queue: " <> tshow detQueue + -- Not done in esqueleto, since inspection of `Load`-Values is difficult + isCorrectorByTutorial = maybe False (\Load{..} -> is _Just byTutorial) . flip Map.lookup sheetCorrectors - queue <- liftIO . Rand.evalRandIO . execWriterT $ do - tell $ map Just detQueue - forever $ - tell . pure =<< Rand.weightedMay [ (sheetCorrectorUser, byProportion sheetCorrectorLoad) | Entity _ SheetCorrector{..} <- corrsProp ] + targetSubmissions = Set.fromList $ do + (E.Value sheetId, Entity subId Submission{..}, _) <- submissionDataRaw + guard $ sheetId == sid + case restriction of + Just restriction' -> + guard $ subId `Set.member` restriction' + Nothing -> + guard $ is _Nothing submissionRatingBy + return subId - $logDebugS "assignSubmissions" $ "Queue: " <> tshow (take (Map.size subTutor') queue) + targetSubmissionData = set _1 Nothing <$> Map.restrictKeys submissionData targetSubmissions + oldSubmissionData = Map.withoutKeys submissionData targetSubmissions + + whenIsJust (fromNullable =<< fmap (`Set.difference` targetSubmissions) restriction) $ \missing -> + throwM $ SubmissionsNotFound missing let - assignSubmission :: MonadState (Map SubmissionId UserId, [Maybe UserId], Map UserId Integer) m => Bool -> SubmissionId -> UserId -> m () - assignSubmission countsToLoad smid tutid = do - _1 %= Map.insert smid tutid - _3 . at tutid %= assertM' (> 0) . maybe (-1) pred - when countsToLoad $ - _2 %= List.delete (Just tutid) + withSubmissionData :: MonadRWS (Map SubmissionId a) w (Map SubmissionId a) m + => (Map SubmissionId a -> b) + -> m b + withSubmissionData f = f <$> (mappend <$> ask <*> State.get) + + -- | How many additional submission should the given corrector be assigned, if possible? + calculateDeficit :: UserId -> Map SubmissionId (Maybe UserId, Map UserId _, SheetId) -> Rational + calculateDeficit corrector submissionState = getSum $ foldMap Sum deficitBySheet + where + sheetSizes :: Map SheetId Integer + -- ^ Number of assigned submissions (to anyone) per sheet + sheetSizes = Map.map getSum . Map.fromListWith mappend $ do + (_, (Just _, _, sheetId)) <- Map.toList submissionState + return (sheetId, Sum 1) - maximumDeficit :: (MonadState (_a, _b, Map UserId Integer) m, MonadIO m) => m (Maybe UserId) - maximumDeficit = do - transposed <- uses _3 invertMap - traverse (liftIO . Rand.evalRandIO . Rand.uniform . snd) (Map.lookupMax transposed) + deficitBySheet :: Map SheetId Rational + -- ^ Deficite of @corrector@ per sheet + deficitBySheet = flip Map.mapMaybeWithKey sheetSizes $ \sheetId sheetSize -> do + let assigned :: Rational + assigned = fromIntegral . Map.size $ Map.filter (\(mCorr, _, sheetId') -> mCorr == Just corrector && sheetId == sheetId') submissionState + proportionSum :: Rational + proportionSum = getSum . foldMap corrProportion . fromMaybe Map.empty $ correctors !? sheetId + where corrProportion (_, CorrectorExcused) = mempty + corrProportion (Load{..}, _) = Sum byProportion + extra + | Just (Load{..}, corrState) <- correctors !? sheetId >>= Map.lookup corrector + = sum + [ assigned + , fromMaybe 0 $ do -- If corrections assigned by tutorial do not count against proportion, substract them from deficit + tutCounts <- byTutorial + guard $ not tutCounts + guard $ corrState /= CorrectorExcused + return . negate . fromIntegral . Map.size $ Map.filter (\(mCorr, tutors, sheetId') -> mCorr == Just corrector && sheetId == sheetId' && Map.member corrector tutors) submissionState + , fromMaybe 0 $ do + guard $ corrState /= CorrectorExcused + return . negate $ (byProportion / proportionSum) * fromIntegral sheetSize + ] + | otherwise + = assigned + return $ negate extra - subTutor'' <- liftIO . Rand.evalRandIO . Rand.shuffleM $ Map.toList subTutor' + -- Sort target submissions by those that have tutors first and otherwise random + -- + -- Deficit produced by restriction to tutors can thus be fixed by later submissions + targetSubmissions' <- liftIO . unstableSortBy (comparing $ \subId -> Map.null . view _2 $ submissionData ! subId) $ Set.toList targetSubmissions - subTutor <- fmap (view _1) . flip execStateT (Map.empty, queue, deficit) . forM_ subTutor'' $ \(smid, tuts) -> do - let - restrictTuts - | Set.null tuts = id - | otherwise = flip Map.restrictKeys tuts - byDeficit <- withStateT (over _3 restrictTuts) maximumDeficit - case byDeficit of - Just q' -> do - $logDebugS "assignSubmissions" $ tshow smid <> " -> " <> tshow q' <> " (byDeficit)" - assignSubmission False smid q' - Nothing - | Set.null tuts -> do - q <- preuse $ _2 . _head . _Just - case q of - Just q' -> do - $logDebugS "assignSubmissions" $ tshow smid <> " -> " <> tshow q' <> " (queue)" - assignSubmission True smid q' - Nothing -> return () - | otherwise -> do - q <- liftIO . Rand.evalRandIO $ Rand.uniform tuts - $logDebugS "assignSubmissions" $ tshow smid <> " -> " <> tshow q <> " (tutorial)" - assignSubmission (countsToLoad' q) smid q + (newSubmissionData, ()) <- (\act -> execRWST act oldSubmissionData targetSubmissionData) . forM_ (zip [1..] targetSubmissions') $ \(i, subId) -> do + tutors <- gets $ view _2 . (! subId) -- :: Map UserId (Sum Natural) + let acceptableCorrectors + | correctorsByTut <- Map.filter (is _Just . view _byTutorial) $ sheetCorrectors `Map.restrictKeys` Map.keysSet tutors + , not $ null correctorsByTut + = Map.keysSet correctorsByTut + | otherwise + = Map.keysSet $ Map.filter (views _byProportion (/= 0)) sheetCorrectors + + when (not $ null acceptableCorrectors) $ do + deficits <- sequence . flip Map.fromSet acceptableCorrectors $ withSubmissionData . calculateDeficit + let + bestCorrectors :: Set UserId + bestCorrectors = acceptableCorrectors + & maximumsBy (deficits !) + & maximumsBy (tutors !?) + + $logDebugS "assignSubmissions" [st|#{tshow i} Tutors for #{tshow subId}: #{tshow tutors}|] + $logDebugS "assignSubmissions" [st|#{tshow i} Current (#{tshow subId}) relevant deficits: #{tshow deficits}|] + $logDebugS "assignSubmissions" [st|#{tshow i} Assigning #{tshow subId} to one of #{tshow bestCorrectors}|] + + ix subId . _1 <~ Just <$> liftIO (Rand.uniform bestCorrectors) now <- liftIO getCurrentTime - forM_ (Map.toList subTutor) $ - \(smid, tutid) -> update smid [ SubmissionRatingBy =. Just tutid - , SubmissionRatingAssigned =. Just now ] + execWriterT . forM_ (Map.toList newSubmissionData) $ \(subId, (mCorrector, _, _)) -> case mCorrector of + Just corrector -> do + lift $ update subId [ SubmissionRatingBy =. Just corrector + , SubmissionRatingAssigned =. Just now + ] + tell (Set.singleton subId, mempty) + Nothing -> + tell (mempty, Set.singleton subId) + where + maximumsBy :: (Ord a, Ord b) => (a -> b) -> Set a -> Set a + maximumsBy f xs = flip Set.filter xs $ \x -> maybe True (((==) `on` f) x . maximumBy (comparing f)) $ fromNullable xs - let assignedSubmissions = Map.keysSet subTutor - unassigendSubmissions = Map.keysSet subTutor' \\ assignedSubmissions - return (assignedSubmissions, unassigendSubmissions) - where - hasPositiveLoad = (> 0) . byProportion . sheetCorrectorLoad . entityVal - hasTutorialLoad = isJust . byTutorial . sheetCorrectorLoad . entityVal + unstableSortBy :: MonadRandom m => (a -> a -> Ordering) -> [a] -> m [a] + unstableSortBy cmp = fmap concat . mapM Rand.shuffleM . groupBy (\a b -> cmp a b == EQ) . sortBy cmp submissionFileSource :: SubmissionId -> Source (YesodDB UniWorX) (Entity File) diff --git a/src/Model/Types/Sheet.hs b/src/Model/Types/Sheet.hs index 6ec4ae4f0..426e375c5 100644 --- a/src/Model/Types/Sheet.hs +++ b/src/Model/Types/Sheet.hs @@ -250,7 +250,9 @@ defaultExtensionRestriction :: Maybe (NonNull (Set Extension)) defaultExtensionRestriction = fromNullable $ Set.fromList ["txt", "pdf"] deriveJSON defaultOptions - { constructorTagModifier = camelToPathPiece + { constructorTagModifier = \c -> if + | c == "UploadAny" -> "upload" + | otherwise -> camelToPathPiece c , fieldLabelModifier = camelToPathPiece , sumEncoding = TaggedObject "mode" "settings" , omitNothingFields = True diff --git a/src/Utils/Form.hs b/src/Utils/Form.hs index c2797980d..2c04192ec 100644 --- a/src/Utils/Form.hs +++ b/src/Utils/Form.hs @@ -690,23 +690,32 @@ mforced Field{..} FieldSettings{..} val = do aforced :: (RenderMessage site FormMessage, HandlerSite m ~ site, MonadHandler m) => Field m a -> FieldSettings site -> a -> AForm m a -aforced field settings val = formToAForm $ second pure <$> mforced field settings val - -apreq :: (RenderMessage site FormMessage, HandlerSite m ~ site, MonadHandler m) - => Field m a -> FieldSettings site -> Maybe a -> AForm m a --- ^ Pseudo required -apreq f fs mx = formToAForm $ do - mr <- getMessageRender - over _1 (maybe (FormFailure [mr MsgValueRequired]) return =<<) . over _2 (pure . (\fv -> fv { fvRequired = True } )) <$> mopt f fs (Just <$> mx) +aforced field settings val = formToAForm $ over _2 pure <$> mforced field settings val mpreq :: (RenderMessage site FormMessage, HandlerSite m ~ site, MonadHandler m) => Field m a -> FieldSettings site -> Maybe a -> MForm m (FormResult a, FieldView site) +-- ^ Pseudo required +-- +-- `FieldView` has `fvRequired` set to `True` and @FormSuccess Nothing@ is cast to `FormFailure`. +-- Otherwise acts exactly like `mopt`. mpreq f fs mx = do mr <- getMessageRender - over _1 (maybe (FormFailure [mr MsgValueRequired]) return =<<) . over _2 (\fv -> fv { fvRequired = True } ) <$> mopt f fs (Just <$> mx) + (res, fv) <- mopt f fs (Just <$> mx) + let fv' = fv { fvRequired = True } + return $ case res of + FormSuccess (Just res') + -> (FormSuccess res', fv') + FormSuccess Nothing + -> (FormFailure [mr MsgValueRequired], fv' { fvErrors = Just . toHtml $ mr MsgValueRequired }) + FormFailure errs + -> (FormFailure errs, fv') + FormMissing + -> (FormMissing, fv') + +apreq :: (RenderMessage site FormMessage, HandlerSite m ~ site, MonadHandler m) + => Field m a -> FieldSettings site -> Maybe a -> AForm m a +apreq f fs mx = formToAForm $ over _2 pure <$> mpreq f fs mx wpreq :: (RenderMessage site FormMessage, HandlerSite m ~ site, MonadHandler m) => Field m a -> FieldSettings site -> Maybe a -> WForm m (FormResult a) -wpreq f fs mx = mFormToWForm $ do - mr <- getMessageRender - over _1 (maybe (FormFailure [mr MsgValueRequired]) return =<<) . over _2 (\fv -> fv { fvRequired = True } ) <$> mopt f fs (Just <$> mx) +wpreq f fs mx = mFormToWForm $ mpreq f fs mx diff --git a/src/Utils/Lens.hs b/src/Utils/Lens.hs index 51aa57fd0..b4cd5a572 100644 --- a/src/Utils/Lens.hs +++ b/src/Utils/Lens.hs @@ -77,6 +77,8 @@ hasEntityUser = hasEntity makeLenses_ ''SheetCorrector +makeLenses_ ''Load + makeLenses_ ''SubmissionGroup makeLenses_ ''SheetGrading diff --git a/templates/messages/submissionsAssignNotFound.hamlet b/templates/messages/submissionsAssignNotFound.hamlet new file mode 100644 index 000000000..570e81459 --- /dev/null +++ b/templates/messages/submissionsAssignNotFound.hamlet @@ -0,0 +1,4 @@ +

_{MsgAssignSubmissionExceptionSubmissionsNotFound (length subCIDs)} +