-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathQuestions.elm
executable file
·432 lines (358 loc) · 10.8 KB
/
Questions.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
module Questions exposing (..) --where
import Html exposing (..)
import Html.Attributes exposing (..)
import Html.Events exposing (..)
import Json.Decode as Json
import Data exposing (..)
import Format exposing (..)
import Markdown
import Defaults
import String
import Material
import Material.Scheme
import Material.Textfield as Textfield
import Material.Toggles as Toggles
import Material.Options exposing (css)
import Material.Button as Button
-- MODEL
type alias Model =
{ content : Content
, mdl : Material.Model
, displayCompletionMessage : Bool
}
type alias Content =
{ title : String
, instructions : String
, questions : List Question
, numberOfParagraphs : Int
, percentageComplete : Int
}
init : Model
init =
let
createQuestion id' question =
{ question = question.question
, answer = ""
, completed = False
, editing = False
, id = id'
, paragraphId = question.paragraphId
, rows = question.rows
, maxlength = question.maxlength
, format = question.format
}
numberOfParagraphs' =
let
paragraphIds =
List.map (\question -> question.paragraphId) Data.questions
in
List.maximum paragraphIds |> Maybe.withDefault 0
firstQuestion =
List.head model'.content.questions
|> Maybe.withDefault
{ question = ""
, answer = "This is a test"
, completed = False
, editing = False
, id = 0
, paragraphId = 0
, rows = 0
, maxlength = 0
, format = Format.Normal
}
firstAnswer =
firstQuestion.answer
content' =
{ title = Data.title
, instructions = Data.instructions
, questions = List.indexedMap createQuestion Data.questions
, numberOfParagraphs = numberOfParagraphs'
, percentageComplete = 0
}
model' =
{ content = content'
, mdl = Material.model
, displayCompletionMessage = False
}
in
model'
-- Question
type alias Question =
{ question : String
, answer : String
, completed : Bool
, editing : Bool
, id : Int
, paragraphId : Int
, rows : Int
, maxlength : Int
, format : Format.FormatStyle
}
-- Sentence
type alias Sentence =
{ content : String
, completed : Bool
, editing : Bool
--, id : Int
}
-- Paragraph
type alias Paragraph =
{ sentences : List Sentence
--, id : Int
}
-- Essay
type alias Essay =
{ paragraphs : List Paragraph
}
-- UPDATE
type Msg
= UpdateField Question String
| MDL Material.Msg
| CheckForCompletion
| UpdatePercentageComplete
| NoOp
update : Msg -> Model -> (Model, Cmd Msg)
update msg model =
case msg of
MDL msg ->
Material.update MDL msg model
CheckForCompletion ->
let
-- Find out which questions remain to be answered
remainingAnswers =
List.filter (\question -> not question.completed) model.content.questions
-- Find out what the id numbers of the remaining unanswered questions are
remainingAnswerIDs =
List.map (\question -> question.id) remainingAnswers
-- Add 1 to the remaining unanswered question id numbers to that they
-- match the displayed question numbers
remainingQuestionNumbers =
List.map (\id -> id + 1) remainingAnswerIDs
remainingQuestionNumbersAsStrings =
List.map (\number -> toString number) remainingQuestionNumbers
percentageComplete =
let
remaining =
List.length remainingQuestionNumbers |> toFloat
total =
List.length model.content.questions |> toFloat
completed =
total - remaining
|> (*) 100
in
completed / total |> floor
completionMessage' =
"Percentage Complete: "
++ toString percentageComplete
++ "%"
++ ". Please complete the remainig questions: "
++ String.join ", " remainingQuestionNumbersAsStrings
in
{ model
| displayCompletionMessage = True
}
! []
UpdateField question inputString ->
let
-- Set the `completed` Boolean flag on the answer
completed' =
if String.isEmpty inputString then
False
else
True
-- Update the current answer with textfield input value
questions' currentQuestion =
if currentQuestion.id == question.id then
{ currentQuestion
| answer = inputString
, completed = completed'
}
else
currentQuestion
content' modelContent =
{ modelContent
| questions = List.map questions' modelContent.questions
--, percentageComplete = percentageComplete'
}
model' =
{ model
| content = content' model.content
, displayCompletionMessage = False
}
in
update UpdatePercentageComplete model'
UpdatePercentageComplete ->
let
-- Find out which questions remain to be answered
remainingAnswers =
List.filter (\question -> not question.completed) model.content.questions
-- Find out what the id numbers of the remaining unanswered questions are
remainingAnswerIDs =
List.map (\question -> question.id) remainingAnswers
-- Add 1 to the remaining unanswered question id numbers to that they
-- match the displayed question numbers
remainingQuestionNumbers =
List.map (\id -> id + 1) remainingAnswerIDs
remaining =
List.length remainingQuestionNumbers |> toFloat
total =
List.length model.content.questions |> toFloat
completed =
total - remaining
|> (*) 100
percentageComplete' =
completed / total |> floor
content' modelContent =
{ modelContent
| percentageComplete = percentageComplete'
}
in
{ model
| content = content' model.content
}
! []
NoOp ->
model
! []
-- A function to check whether the user has pressed the Enter key (code 13).
-- If so, the Add message is returned
onEnter : msg -> msg -> Attribute msg
onEnter fail success =
let
tagger code =
if code == 13 then success
else fail
in
on "keyup" (Json.map tagger keyCode)
-- VIEW
(=>) : a -> b -> ( a, b )
(=>) = (,)
-- `questionsView` is the view for the left side of the app, which
-- includes all the questions
view : Model -> Html Msg
view model =
let
textfieldStyle =
style
[ "width" => "100%"
, "font-family" => Defaults.essayFont
, "font-size" => "1em"
, "line-height" => "1.5em"
, "padding" => "1em"
]
questionStyle =
style
[ "font-size" => "1.2em"
, "margin-top" => "-1.4em"
]
listItemStyle =
style
[ "padding-top" => "4em"
]
toggleStyle =
style
[ "background-color" => "red"
, "display" => "inline"
, "height" => "0px"
]
checkboxStyle =
style
[ "margin-left" => "-3em"
]
completionMessageStyle =
style
[ "color" => "mediumSeaGreen"
, "font-weight" => "bold"
, "width" => "80%"
]
completionMessageContainerStyle =
style
[ "padding-top" => "2em"
]
getMaxLength maxlength =
if maxlength /= 0 then
maxlength
else
500
setAutoFocus question =
if question.id == 0 then
True
else
False
checkbox state =
if state == True then
Defaults.imagesLocation ++ "checkboxTrue.png"
else
Defaults.imagesLocation ++ "checkboxFalse.png"
completionMessage =
let
-- Find out which questions remain to be answered
remainingAnswers =
List.filter (\question -> not question.completed) model.content.questions
-- Find out what the id numbers of the remaining unanswered questions are
remainingAnswerIDs =
List.map (\question -> question.id) remainingAnswers
-- Add 1 to the remaining unanswered question id numbers to that they
-- match the displayed question numbers
remainingQuestionNumbers =
List.map (\id -> id + 1) remainingAnswerIDs
remainingQuestionNumbersAsStrings =
List.map (\number -> toString number) remainingQuestionNumbers
in
case model.displayCompletionMessage of
True ->
if model.content.percentageComplete == 100 then
"""
You're done! Carefully read through your finished work on the right and make any further
changes that you need to by updating your answers above.
"""
else
"Please complete the remaining questions: " ++ String.join ", " remainingQuestionNumbersAsStrings
False ->
""
percentageCompleteMessage =
"Percentage Complete: "
++ toString model.content.percentageComplete
++ "%"
questions question =
li [ listItemStyle ]
[ img [ (src <| checkbox question.completed), checkboxStyle ] []
, Markdown.toHtml [ questionStyle ] question.question
, div [ ]
[ textarea
[ on "input" (Json.map (UpdateField question) targetValue)
--, onBlur UpdatePercentageComplete
, class "mdl-textfield__input"
, rows question.rows
, textfieldStyle
, maxlength <| getMaxLength question.maxlength
, autofocus <| setAutoFocus question
]
[ text question.answer ]
]
]
answers item =
div []
[ p [ ] [ text <| toString(item.id + 1) ++ ". " ++ item.answer ]
]
doneButton =
Button.render MDL [0] model.mdl
[ Button.ripple
, Button.raised
, Button.colored
, Button.onClick CheckForCompletion
, css "background-color" "rgb(0, 127, 163)"
, css "float" "right"
, css "margin-top" "2em"
]
[ text "Done" ]
in
div []
[ ol [] (List.map questions model.content.questions)
, doneButton
, div [ completionMessageContainerStyle ]
[ p [ completionMessageStyle ] [ text percentageCompleteMessage ]
, p [ completionMessageStyle ] [ text completionMessage ]
]
]
--|> Material.Scheme.top