-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNovaSurveyClient.elm
222 lines (202 loc) · 6 KB
/
NovaSurveyClient.elm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
import Html exposing (..)
import Html.Attributes exposing (..)
import Html.App exposing (beginnerProgram)
import Html.Events exposing (onClick, onInput)
type alias QuestionChoiceOption = String
type alias ChoiceQuestion =
{ description : String
, options : List QuestionChoiceOption
}
type Question
= FreeText String
| SingleChoice ChoiceQuestion
| MultipleChoice ChoiceQuestion
type Response
= FreeTextResponse String
| SingleChoiceResponse Int
| MultipleChoiceResponse (List Int)
type alias AnsweredQuestion =
{ question: Question
, response: Response
}
type Quiz
= EmptyQuiz (List Question)
| PartialQuiz (List Question) (List AnsweredQuestion)
| CompletedQuiz (List AnsweredQuestion)
draw_response answer =
div []
[ p []
[ text ((toString answer.question) ++ (toString answer.response)) ]
]
getFreeTextResponse response =
case response of
Just (FreeTextResponse text) ->
text
_ ->
""
draw_question q response =
case q of
FreeText description ->
div []
[ p [] [ text description ]
, input [ name "response"
, type' "text"
, value (getFreeTextResponse response)
, onInput UpdateFreeTextResponse
][]
, button [ onClick NextQuestion ] [ text "Next" ]
]
SingleChoice question ->
div []
[ p []
[ text question.description ]
, ul []
(List.map
(\option ->
li []
[ input [ id "response"
, name "response"
, type' "radio"
, value option
]
[]
, label [ for "response" ]
[ text option ]
]
)
question.options
)
, button [] [ text "Next" ]
]
MultipleChoice question ->
div []
[ p []
[ text question.description ]
, ul []
(List.map
(\option ->
li []
[ input [ id "response"
, name "response"
, type' "checkbox"
, value option
]
[]
, label [ for "response" ]
[ text option ]
]
)
question.options
)
, button [] [ text "Next" ]
]
q1 = FreeText "What is your name?"
q2 = SingleChoice
{ description = "What is your city?"
, options = ["Sao Paulo", "Rio de Janeiro", "Belo Horizonte"]
}
q3 = MultipleChoice
{ description = "What are your favorite foods?"
, options = [ "Ham", "Bacon", "Pork" ]
}
q4 = FreeText "Do you want to participate?"
r4 = FreeTextResponse "Yep"
aq = { question=q4, response=r4}
q5 = FreeText "Where were you born?"
q6 = FreeText "What is your age?"
--quiz = EmptyQuiz [q1, q2, q3]
--quiz = PartialQuiz [q1, q2, q3] [aq]
--quiz = CompletedQuiz [aq]
quiz = EmptyQuiz [q1, q5, q6]
type alias Model =
{ quiz: Quiz
, currentQuestion: Question
, currentResponse: Maybe Response
, replied: Bool
, feedback: String
}
model = Model quiz q1 Nothing False ""
main =
beginnerProgram { model = model, view = view, update = update }
view model =
case model.quiz of
EmptyQuiz questions ->
div []
[ (draw_question model.currentQuestion model.currentResponse)
, p [] [ text ("Feedback: " ++ model.feedback) ]
]
PartialQuiz questions replied_questions ->
div []
(
[(draw_question model.currentQuestion model.currentResponse)]
++ [ p [] [ text ("Feedback: " ++ model.feedback) ] ]
++ (List.map draw_response replied_questions)
)
CompletedQuiz replied_questions ->
div [] [ p [] [ text "over" ] ]
type Msg
= UpdateFreeTextResponse String
| NextQuestion
update : Msg -> Model -> Model
update msg model =
case msg of
UpdateFreeTextResponse text ->
{ model |
currentResponse = (Just (FreeTextResponse text))
, feedback = "Replying..."
}
NextQuestion ->
case model.currentResponse of
Nothing ->
{ model | feedback = "No response" }
Just response ->
let
repliedQuestion = AnsweredQuestion model.currentQuestion response
newQuiz = updateQuiz model.quiz repliedQuestion
in
case newQuiz of
Ok quiz ->
{ model |
quiz = quiz
, currentQuestion = findNextQuestion quiz
, currentResponse = Nothing
, feedback = "???"
}
Err feedback ->
{ model | feedback = feedback }
updateQuiz : Quiz -> AnsweredQuestion -> Result String Quiz
updateQuiz quiz answeredQuestion =
case quiz of
EmptyQuiz questions ->
if (List.length questions) == 1 then
Ok (CompletedQuiz [answeredQuestion])
else
Ok (PartialQuiz questions [answeredQuestion])
PartialQuiz questions repliedQuestions ->
if (List.length questions) == (List.length repliedQuestions) + 1 then
Ok (CompletedQuiz (repliedQuestions ++ [answeredQuestion]))
else
Ok (PartialQuiz questions (repliedQuestions ++ [answeredQuestion]))
CompletedQuiz replied_questions ->
Err "Quiz is already completed!"
findNextQuestion : Quiz -> Question
findNextQuestion quiz =
case quiz of
EmptyQuiz questions ->
case (List.head questions) of
Just question ->
question
Nothing ->
FreeText "Empty Question for now"
PartialQuiz questions responses ->
let
repliedQuestions = List.map (\r -> r.question) responses
unrepliedQuestions = List.filter (\q -> not (List.member q repliedQuestions)) questions
in
case (List.head unrepliedQuestions) of
Just question ->
question
Nothing ->
FreeText "Empty Question for now"
CompletedQuiz replied_questions ->
FreeText "Empty Question for now"