-- © 2002-2005 Peter Thiemann module Main where import WASH.CGI.CGI hiding (head, div, span, map) import qualified WASH.CGI.CGI as CGI import Random import qualified WASH.CGI.Persistent2 as P import qualified WASH.CGI.Cookie as C import WASH.CGI.Types -- for nhc98 main = run helo0 -- helo0 = standardQuery "Select an Example" $ activate exAction (selectSingle exName Nothing examples) empty -- do exampleF <- selectSingle exName Nothing examples empty -- submit exampleF dispatch (attr "value" "GO") data Example = Example {exName :: String, exAction :: CGI ()} instance Eq Example where e1 == e2 = exName e1 == exName e2 examples = [Example "Simple Hello World" helo1 ,Example "Hello World with a little HTML" helo2 ,Example "Hello World with a little Color" helo4 ,Example "Hello World Personalized" helo5 ,Example "Multiplication Table" helo6 ,Example "Multiplication Drill" helo7 ,Example "Multiplication Drill with Selection Box" helo8 ,Example "Multiplication Drill with Radio Buttons" helo9 ,Example "Multiplication Drill with Email Address" helo10 ,Example "Multiplication Drill with Cookies" helo11 ] -- helo1 = standardQuery "Hello" <#>This is my first CGI program!#> -- helo2 = standardQuery "Hello" $ do
This is my second CGI program!
My hobbies are
Hi there! What's your name? <% activate greeting textInputField empty %>
greeting :: String -> CGI () greeting name = standardQuery "Hello" $ <#>Hello <%= name %>. This is my first interactive CGI program!#> -- helo6 = standardQuery "What's your name?" $Hi there! What's your name? <% activate mtable textInputField empty %>
mtable name = standardQuery "Multiplication Table" $ doHello <%= name %>!
Let's see a multiplication table!
Give me a multiplier <% activate ptable inputField empty %>
ptable :: Integer -> CGI () ptable mpy = standardQuery "Multiplication Table" $Hi there! What's your name? <% activate mdrill textInputField empty %>
mdrill name = standardQuery "Multiplication" $ <#>Hello <%= name %>!
Let's exercise some multiplication!
Give me a multiplier
Number of exercises
#> firstExercise :: String -> (Int, Int) -> CGI () firstExercise name (mpy, rpt) = runExercises 1 [] [] where -- runExercises nr successes failures = if nr > rpt then finalReport else do factor <- io (randomRIO (0,12)) standardQuery ("Question " ++ show nr ++ " of " ++ show rpt) $ do text (show factor ++ " * " ++ show mpy ++ " = ") activate (checkAnswer factor) inputField empty where checkAnswer factor answer = let correct = answer == factor * mpy message = if correct then "correct! " else "wrong! " continue F0 = if correct then runExercises (nr+1) (factor:successes) failures else runExercises (nr+1) successes (factor:failures) in standardQuery ("Answer " ++ show nr ++ " of " ++ show rpt) $ do p (text (show factor ++ " * " ++ show mpy ++ " = " ++ show (factor * mpy))) text ("Your answer " ++ show answer ++ " was " ++ message) submit F0 continue (attr "value" "CONTINUE") -- finalReport = let lenSucc = length successes pItem (m, l, r) = li (text ("Multiplier " ++ show m ++ " : " ++ show l ++ " correct out of " ++ show r)) in do initialHandle <- P.init ("multi-" ++ name) [] currentHandle <- P.add initialHandle (mpy, lenSucc, rpt) hiScores <- P.get currentHandle standardQuery "Final Report" $ do p (text "Here are your recent scores.") ul (mapM_ pItem hiScores) -- helo8 = standardQuery "What's your name?" $ p (do text "Hi there! What's your name?" activate mdrillSelect textInputField empty) mdrillSelect name = standardQuery "Multiplication" $ <#>Hello <%= name %>!
Let's exercise some multiplication!
Give me a multiplier <% mpyF <- selectSingle show Nothing [2..12] empty %>
Number of exercises
#> -- helo9 = standardQuery "What's your name?" $ p (do text "Hi there! What's your name?" activate mdrillRadio textInputField empty) mdrillRadio name = standardQuery "Multiplication" $ <#>Hello <%= name %>!
Let's exercise some multiplication!
Give me a multiplier <% mpyF <- selectSingle show Nothing [2..12] empty %>
<% rptF <- radioGroup %>Number of exercises 5 <% radioButton rptF 5 empty %> 10 <% radioButton rptF 10 empty %> 20 <% radioButton rptF 20 empty %> <% radioError rptF %>
#> -- helo10 = standardQuery "What's your name?" $ p (do text "Hi there! What's your email address?" activate mdrillEmail inputField empty -- inf <- inputField empty -- submit inf mdrillEmailHandle empty ) mdrillEmailHandle emailH = mdrillEmail (value emailH) mdrillEmail email = let name = unEmailAddress email in standardQuery "Multiplication" $ <#>Hello <%= name %>!
Let's exercise some multiplication!
Give me a multiplier <% mpyF <- selectSingle show Nothing [2..12] empty %>
Number of exercises
#> -- helo11 = C.check "name" >>= \mNameH -> case mNameH of Nothing -> standardQuery "What's your name?" $ p (do text "Hi there! What's your name?" activate mdrillCookie textInputField empty) Just nameC -> do mname <- C.get nameC case mname of Nothing -> helo11 -- retry if outdated Just name -> mdrill name mdrillCookie name = do C.create "name" name mdrill name