module Ideas.Encoding.Options
( Options, makeOptions, optionBaseUrl
, script, request, qcGen, baseUrl, maxTime
, cgiBin, optionCgiBin, optionHtml
) where
import Control.Applicative
import Data.Monoid
import Ideas.Common.Library (Exercise, getId)
import Ideas.Encoding.Request
import Ideas.Service.DomainReasoner
import Ideas.Service.FeedbackScript.Parser (parseScriptSafe, Script)
import Test.QuickCheck.Random
cgiBin :: Options -> Maybe String
cgiBin = cgiBinary . request
optionCgiBin :: String -> Options
optionCgiBin s = mempty {request = mempty {cgiBinary = Just s}}
data Options = Options
{ request :: Request
, qcGen :: Maybe QCGen
, script :: Script
, baseUrl :: Maybe String
, maxTime :: Maybe Int
}
instance Monoid Options where
mempty = Options mempty Nothing mempty Nothing Nothing
mappend x y = Options
{ request = request x <> request y
, qcGen = make qcGen
, script = script x <> script y
, baseUrl = make baseUrl
, maxTime = make maxTime
}
where
make f = f x <|> f y
optionHtml :: Options
optionHtml = mempty
{ request = mempty {encoding = [EncHTML]} }
optionBaseUrl :: String -> Options
optionBaseUrl base = mempty {baseUrl = Just base}
makeOptions :: DomainReasoner -> Exercise a -> Request -> IO Options
makeOptions dr ex req = do
gen <- maybe newQCGen (return . mkQCGen) (randomSeed req)
scr <- case feedbackScript req of
Just s -> parseScriptSafe s
Nothing -> defaultScript dr (getId ex)
return $ mempty
{ request = req
, qcGen = Just gen
, script = scr
}