Advertisement
Guest User

Untitled

a guest
Aug 29th, 2016
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.62 KB | None | 0 0
  1. module Preprojectform exposing (Model, init, view, update, Msg(..), myVariable, myTotalScore, getScoreMessage)
  2.  
  3. import Html exposing (Html, Attribute, text, div, input, button, br)
  4. import Html.Attributes exposing (..)
  5. import Html.App
  6. import Html.Events exposing (onClick, onCheck)
  7. import Dict exposing (..)
  8. import Debug
  9.  
  10.  
  11. -- import Debug
  12.  
  13.  
  14. main : Program Never
  15. main =
  16. Html.App.program
  17. { init = init
  18. , view = view
  19. , subscriptions = subscriptions
  20. , update = update
  21. }
  22.  
  23.  
  24.  
  25. -- MODEL
  26.  
  27.  
  28. type alias Model =
  29. { myScoreList : Dict Int Int
  30. , listOfAnswers : List { myText : String, myValue : Int }
  31. , listOfQuestions : List { questionId : Int, questionText : String }
  32. , scoreMessage : String
  33. }
  34.  
  35.  
  36. init : ( Model, Cmd Msg )
  37. init =
  38. ( Model Dict.empty
  39. [ { myText = "Fully agree", myValue = 3 }
  40. , { myText = "Agree", myValue = 2 }
  41. , { myText = "Neutral", myValue = 1 }
  42. , { myText = "Disagree", myValue = 0 }
  43. ]
  44. [ { questionId = 1
  45. , questionText = "Is the business co-located with the developers?"
  46. }
  47. , { questionId = 2, questionText = "Is the scope flexible?" }
  48. , { questionId = 3, questionText = "Is the Scrum Master a Servant-Leader?" }
  49. , { questionId = 4, questionText = "Does the team make frequent retrospectives?" }
  50. ]
  51. ""
  52. , Cmd.none
  53. )
  54.  
  55.  
  56.  
  57. --MESSAGES
  58.  
  59.  
  60. type Msg
  61. = QuestionAnswered Int Int
  62. | Send Int
  63. | NoOp
  64.  
  65.  
  66.  
  67. --VIEW
  68.  
  69.  
  70. view :
  71. Model
  72. -> Html Msg
  73. view model =
  74. let
  75. displayQuestionsandAnswers =
  76. List.concatMap (\question -> div [ style [ ( "background-color", "lightgrey" ), ( "width", "50%" ), ( "margin-left", "100px" ) ] ] [ text question.questionText ] :: List.map (createAnswerButtons question.questionId) model.listOfAnswers) model.listOfQuestions
  77.  
  78. lengthOfListofQuestions =
  79. List.length model.listOfQuestions
  80.  
  81. maxPointsPossible : Int
  82. maxPointsPossible =
  83. 1
  84.  
  85. myCurrentNumberofPoints =
  86. List.sum <| Dict.values <| model.myScoreList
  87.  
  88. myTotalScore3 =
  89. myTotalScore myCurrentNumberofPoints maxPointsPossible
  90.  
  91. formTitle =
  92. div [] [ text "Questionnaire" ]
  93.  
  94. sendScore : Result String Int -> Msg
  95. sendScore result =
  96. case result of
  97. Ok points ->
  98. Send points
  99. Err _ ->
  100. NoOp
  101. in
  102. div []
  103. [ div [ style [ ( "width", "50px" ), ( "margin-left", "300px" ) ] ] [ text "Questionnaire" ]
  104. , br [] []
  105. , div [ style [ ( "background-color", "#FFFFFF" ) ] ] <|
  106. displayQuestionsandAnswers
  107. ++ [ br [] []
  108. , button [ onClick (sendScore <| myTotalScore3), style [ ( "background-color", "gray" ), ( "color", "#FFFFFF" ), ( "margin-left", "300px" ) ] ] [ text "Send" ]
  109. , br [] []
  110. , br [] []
  111. , div [ style [ ( "margin-left", "200px" ) ] ] [ text model.scoreMessage ]
  112. ]
  113. ]
  114.  
  115. myTotalScore : Int -> Int -> Result String Int
  116. myTotalScore points maxPoints =
  117. if ( points > maxPoints ) then
  118. Err "Developer made an error : Cannot have more points than max"
  119. else
  120. Ok ((points * 100) // maxPoints)
  121.  
  122. {-myTotalScore : Int -> Int -> Int
  123. myTotalScore points maxPoints =
  124. if ( points > maxPoints ) then
  125. Debug.crash "Developer made an error : Cannot have more points than max"
  126. else
  127. (points * 100) // maxPoints-}
  128.  
  129. {-
  130. let
  131. nextPoints = getPoints points maxPoints
  132. in
  133. case nextPoints of
  134. Ok points ->
  135. ( { model | points = points }, … )
  136.  
  137. Err msg ->
  138. ( { model | error = msg }, … )
  139. -}
  140.  
  141. {-
  142. let
  143. nextPoints = getPoints points maxPoints
  144.  
  145. tooManyPoints = nextPoints > maxPoints
  146.  
  147. points = if tooManyPoints then model.points else nextPoints
  148.  
  149. error = if tooManyPoints then Just "Too many points!" else Nothing
  150. in
  151. ( { model | points = points, error = error }, … )
  152. -}
  153.  
  154. getScoreMessage : Int -> Int -> Int -> String
  155. getScoreMessage myAnsweredQuestions finalScore lengthListofQuestions2 =
  156. if myAnsweredQuestions == lengthListofQuestions2 then
  157. if (finalScore > 100 || finalScore <= 0) then
  158. "The developer made an error"
  159. else if finalScore >= 75 then
  160. "Your score is " ++ toString (finalScore) ++ "% " ++ "Bravo"
  161. else
  162. "Your score is " ++ toString finalScore ++ "% " ++ "Not so bad"
  163. else if myAnsweredQuestions > lengthListofQuestions2 then
  164. "The developer made an error"
  165. else if (myAnsweredQuestions < lengthListofQuestions2 && myAnsweredQuestions > 0) then
  166. "You must answer all questions!"
  167. else if (myAnsweredQuestions == 0 && finalScore /= 0) then
  168. "The developer made an error"
  169. else if (myAnsweredQuestions == 0 && finalScore == 0) then
  170. "You must answer all questions!"
  171. else
  172. "The developer made an error"
  173.  
  174.  
  175. myVariable : number
  176. myVariable =
  177. 10
  178.  
  179.  
  180. createAnswerButtons : Int -> { a | myText : String, myValue : Int } -> Html Msg
  181. createAnswerButtons questionId answersPair =
  182. div
  183. [ style
  184. [ ( "background-color", "#FFFFFF" )
  185. , ( "width", "50%" )
  186. , ( "padding", "2px" )
  187. , ( "margin-left", "150px" )
  188. , ( "font-size", "1em" )
  189. ]
  190. ]
  191. [ input [ type' "radio", name ("myChoice" ++ toString questionId), onCheck (\_ -> QuestionAnswered questionId answersPair.myValue) ] []
  192. , text (answersPair.myText)
  193. ]
  194.  
  195.  
  196.  
  197. --UPDATE
  198.  
  199.  
  200. update :
  201. Msg
  202. -> Model
  203. -> ( Model, Cmd b )
  204. update msg model =
  205. case msg of
  206. QuestionAnswered questionId answersId ->
  207. let
  208. newScoreList =
  209. Dict.insert questionId answersId model.myScoreList
  210. in
  211. ( { model | myScoreList = Debug.log "myscorelist" newScoreList }, Cmd.none )
  212.  
  213. Send finalScore ->
  214. let
  215. myAnsweredQuestions =
  216. List.length <| Dict.values <| model.myScoreList
  217.  
  218. lengthListofQuestions2 =
  219. List.length model.listOfQuestions
  220.  
  221. newScoreMessage =
  222. getScoreMessage myAnsweredQuestions finalScore lengthListofQuestions2
  223.  
  224. in
  225. ( { model | scoreMessage = newScoreMessage }, Cmd.none )
  226.  
  227. NoOp ->
  228. let
  229. errorMessage = "BUG!"
  230. in
  231. ( { model | scoreMessage = errorMessage }, Cmd.none )
  232.  
  233. --SUBSCRIPTIONS
  234.  
  235.  
  236. subscriptions : Model -> Sub Msg
  237. subscriptions model =
  238. Sub.none
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement