-
[clean-tests.git] / old / afp / a5 / a5.icl
1 module a5
2
3 import Data.List
4 import Data.Func
5 import Text
6
7 import iTasks
8
9 derive class iTask Function, Question
10 :: Function = Student | Teacher | Admin
11 :: Question =
12 { question :: String
13 , answers :: [String]
14 , correct :: Int
15 }
16
17 questions :: Shared [Question]
18 questions = sharedStore "questions"
19 [{question="Afp cool?"
20 ,answers=["yes", "no"]
21 ,correct=0}
22 ,{question="Mart the awesomest TA?"
23 ,answers=["yes", "no"]
24 ,correct=0}
25 ]
26
27 Start w = doTasks login w
28
29 login :: Task [Question]
30 login = enterInformation "Enter your function" []
31 >>~ \function->case function of
32 Teacher = teacher
33 Admin = admin
34 Student = student
35
36 student :: Task [Question]
37 student = get questions
38 >>= \qs->sequence (map makeQuestion qs) @ distillResult
39 >>= viewInformation "Result" []
40 >>* [OnAction (Action "Quit") (always login)]
41 where
42 makeQuestion :: Question -> Task Bool
43 makeQuestion q = enterChoice q.question [ChooseFromList snd] (zip2 [0..] q.answers)
44 @? \v->case v of
45 NoValue = NoValue
46 Value (idx, _) _ = Value (idx == q.correct) True
47
48 distillResult :: [Bool] -> String
49 distillResult [] = "No questions answered!"
50 distillResult b = concat
51 [ "You had "
52 , toString good
53 , " answers correct and "
54 , toString bad
55 , " answers incorrect which results in a score of: "
56 , toString $ good*100 / (good+bad)
57 , "/100"
58 ]
59 where
60 good = length [()\\True<-b]
61 bad = length [()\\False<-b]
62
63 admin :: Task [Question]
64 admin = updateSharedInformation "Questions" [] questions
65 >>* [OnAction (Action "Quit") (always login)]
66
67 teacher :: Task [Question]
68 teacher = forever
69 $ enterChoiceWithShared "Choose an item to edit" [ChooseFromList id] questions
70 >>*
71 [ OnAction (Action "Append") (withValue $ Just o append)
72 , OnAction (Action "Delete") (withValue $ Just o delete)
73 , OnAction (Action "Edit") (withValue $ Just o edit)
74 , OnAction (Action "Clear") (withValue $ Just o clear)
75 , OnAction (Action "First") (always first)
76 , OnAction (Action "Quit") (always login)
77 ]
78 where
79 append choice = orCancel (enterInformation () []) \nq->upd (insertAfter choice nq) questions
80 delete choice = upd (deleteBy (===) choice) questions
81 edit choice = orCancel (updateInformation () [] choice) $ replace choice
82 clear choice = orCancel (enterInformation () []) $ replace choice
83 first = orCancel (enterInformation () []) \nq->upd (\x->[nq:x]) questions
84
85 replace choice nq = upd (deleteBy (===) choice o insertAfter choice nq) questions
86
87 insertAfter :: a a [a] -> [a] | gEq{|*|} a
88 insertAfter after el [] = [el]
89 insertAfter after el [e:es]
90 | e === after = [e,el:es]
91 = insertAfter after el es
92
93 orCancel do done = do >>*
94 [ OnAction (Action "Cancel") (always teacher)
95 , OnAction (Action "Continue") (withValue (Just o done))
96 ]