Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module Preprojectform exposing (Model, init, view, update, Msg(..), myVariable, myTotalScore, getScoreMessage)
- import Html exposing (Html, Attribute, text, div, input, button, br)
- import Html.Attributes exposing (..)
- import Html.App
- import Html.Events exposing (onClick, onCheck)
- import Dict exposing (..)
- import Debug
- -- import Debug
- main : Program Never
- main =
- Html.App.program
- { init = init
- , view = view
- , subscriptions = subscriptions
- , update = update
- }
- -- MODEL
- type alias Model =
- { myScoreList : Dict Int Int
- , listOfAnswers : List { myText : String, myValue : Int }
- , listOfQuestions : List { questionId : Int, questionText : String }
- , scoreMessage : String
- }
- init : ( Model, Cmd Msg )
- init =
- ( Model Dict.empty
- [ { myText = "Fully agree", myValue = 3 }
- , { myText = "Agree", myValue = 2 }
- , { myText = "Neutral", myValue = 1 }
- , { myText = "Disagree", myValue = 0 }
- ]
- [ { questionId = 1
- , questionText = "Is the business co-located with the developers?"
- }
- , { questionId = 2, questionText = "Is the scope flexible?" }
- , { questionId = 3, questionText = "Is the Scrum Master a Servant-Leader?" }
- , { questionId = 4, questionText = "Does the team make frequent retrospectives?" }
- ]
- ""
- , Cmd.none
- )
- --MESSAGES
- type Msg
- = QuestionAnswered Int Int
- | Send Int
- | NoOp
- --VIEW
- view :
- Model
- -> Html Msg
- view model =
- let
- displayQuestionsandAnswers =
- 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
- lengthOfListofQuestions =
- List.length model.listOfQuestions
- maxPointsPossible : Int
- maxPointsPossible =
- 1
- myCurrentNumberofPoints =
- List.sum <| Dict.values <| model.myScoreList
- myTotalScore3 =
- myTotalScore myCurrentNumberofPoints maxPointsPossible
- formTitle =
- div [] [ text "Questionnaire" ]
- sendScore : Result String Int -> Msg
- sendScore result =
- case result of
- Ok points ->
- Send points
- Err _ ->
- NoOp
- in
- div []
- [ div [ style [ ( "width", "50px" ), ( "margin-left", "300px" ) ] ] [ text "Questionnaire" ]
- , br [] []
- , div [ style [ ( "background-color", "#FFFFFF" ) ] ] <|
- displayQuestionsandAnswers
- ++ [ br [] []
- , button [ onClick (sendScore <| myTotalScore3), style [ ( "background-color", "gray" ), ( "color", "#FFFFFF" ), ( "margin-left", "300px" ) ] ] [ text "Send" ]
- , br [] []
- , br [] []
- , div [ style [ ( "margin-left", "200px" ) ] ] [ text model.scoreMessage ]
- ]
- ]
- myTotalScore : Int -> Int -> Result String Int
- myTotalScore points maxPoints =
- if ( points > maxPoints ) then
- Err "Developer made an error : Cannot have more points than max"
- else
- Ok ((points * 100) // maxPoints)
- {-myTotalScore : Int -> Int -> Int
- myTotalScore points maxPoints =
- if ( points > maxPoints ) then
- Debug.crash "Developer made an error : Cannot have more points than max"
- else
- (points * 100) // maxPoints-}
- {-
- let
- nextPoints = getPoints points maxPoints
- in
- case nextPoints of
- Ok points ->
- ( { model | points = points }, … )
- Err msg ->
- ( { model | error = msg }, … )
- -}
- {-
- let
- nextPoints = getPoints points maxPoints
- tooManyPoints = nextPoints > maxPoints
- points = if tooManyPoints then model.points else nextPoints
- error = if tooManyPoints then Just "Too many points!" else Nothing
- in
- ( { model | points = points, error = error }, … )
- -}
- getScoreMessage : Int -> Int -> Int -> String
- getScoreMessage myAnsweredQuestions finalScore lengthListofQuestions2 =
- if myAnsweredQuestions == lengthListofQuestions2 then
- if (finalScore > 100 || finalScore <= 0) then
- "The developer made an error"
- else if finalScore >= 75 then
- "Your score is " ++ toString (finalScore) ++ "% " ++ "Bravo"
- else
- "Your score is " ++ toString finalScore ++ "% " ++ "Not so bad"
- else if myAnsweredQuestions > lengthListofQuestions2 then
- "The developer made an error"
- else if (myAnsweredQuestions < lengthListofQuestions2 && myAnsweredQuestions > 0) then
- "You must answer all questions!"
- else if (myAnsweredQuestions == 0 && finalScore /= 0) then
- "The developer made an error"
- else if (myAnsweredQuestions == 0 && finalScore == 0) then
- "You must answer all questions!"
- else
- "The developer made an error"
- myVariable : number
- myVariable =
- 10
- createAnswerButtons : Int -> { a | myText : String, myValue : Int } -> Html Msg
- createAnswerButtons questionId answersPair =
- div
- [ style
- [ ( "background-color", "#FFFFFF" )
- , ( "width", "50%" )
- , ( "padding", "2px" )
- , ( "margin-left", "150px" )
- , ( "font-size", "1em" )
- ]
- ]
- [ input [ type' "radio", name ("myChoice" ++ toString questionId), onCheck (\_ -> QuestionAnswered questionId answersPair.myValue) ] []
- , text (answersPair.myText)
- ]
- --UPDATE
- update :
- Msg
- -> Model
- -> ( Model, Cmd b )
- update msg model =
- case msg of
- QuestionAnswered questionId answersId ->
- let
- newScoreList =
- Dict.insert questionId answersId model.myScoreList
- in
- ( { model | myScoreList = Debug.log "myscorelist" newScoreList }, Cmd.none )
- Send finalScore ->
- let
- myAnsweredQuestions =
- List.length <| Dict.values <| model.myScoreList
- lengthListofQuestions2 =
- List.length model.listOfQuestions
- newScoreMessage =
- getScoreMessage myAnsweredQuestions finalScore lengthListofQuestions2
- in
- ( { model | scoreMessage = newScoreMessage }, Cmd.none )
- NoOp ->
- let
- errorMessage = "BUG!"
- in
- ( { model | scoreMessage = errorMessage }, Cmd.none )
- --SUBSCRIPTIONS
- subscriptions : Model -> Sub Msg
- subscriptions model =
- Sub.none
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement