liquid_feedback_frontend
view app/main/survey/participate.lua @ 1735:5a8a09119865
Added survey feature
| author | bsw | 
|---|---|
| date | Fri Oct 08 00:09:23 2021 +0200 (2021-10-08) | 
| parents | |
| children | b94cf0ea5942 | 
 line source
     1 local survey = Survey:get_open()
     2 if not survey then
     3   return execute.view { module = "index", view = "404" }
     4 end
     6 local survey_member = SurveyMember:by_pk(survey.id, app.session.member_id)
     7 if not survey_member then
     8   return execute.view { module = "index", view = "404" }
     9 end
    11 local question
    13 if survey_member then
    14   local answer_set = survey_member.answer_set
    15   if answer_set then
    16     local answers_by_question_id = {}
    17     for i, answer in ipairs(answer_set.answers) do
    18       answers_by_question_id[answer.survey_question_id] = answer
    19     end
    20     for i, q in ipairs(survey.questions) do
    21       if not question and not answers_by_question_id[q.id] then
    22         question = q
    23       end
    24     end
    25   end
    26 end
    28 ui.title(_"Survey")
    29 ui.grid{ content = function()
    30   ui.cell_main{ content = function()
    32     ui.container{ attr = { class = "mdl-card mdl-card__fullwidth mdl-shadow--2dp" }, content = function()
    33       ui.container{ attr = { class = "mdl-card__title mdl-card--border" }, content = function()
    34         ui.heading { attr = { class = "mdl-card__title-text" }, level = 2, content = survey.title }
    35         ui.container{ 
    36           content = _(
    37             "This survey closes in #{closing}.", 
    38             { closing = format.interval_text(survey.time_left) }
    39           )
    40         }
    41       end }
    42       ui.container{ attr = { class = "mdl-card__content mdl-card--border" }, content = function()
    43         if survey_member.finished then
    44           ui.container{ content = function()
    45             slot.put(survey.finished_text)
    46           end }
    47           slot.put("<br>")
    48           ui.link{
    49             attr = { class = "mdl-button mdl-js-button mdl-button--raised mdl-button--colored" },
    50             module = "index", view = "index", content = _"Go to start page"
    51           }
    52           return
    53         else
    54           ui.heading{ level = 2, content = question.question }
    55           if question.description then
    56             ui.container{ content = question.description }
    57           end
    58           ui.form{
    59             module = "survey", action = "answer",
    60             routing = {
    61               ok = { mode = "redirect", module = "survey", view = "participate" },
    62               error = { mode = "forward", module = "survey", view = "participate" },
    63             },
    64             content = function()
    65               ui.field.hidden{ name = "question_id", value = question.id }
    66               if question.answer_type == "radio" then
    67                 for i, answer_option in ipairs(question.answer_options) do
    68                   ui.container{ content = function()
    69                     ui.tag{ tag = "label", attr = {
    70                         class = "mdl-radio mdl-js-radio mdl-js-ripple-effect",
    71                         ["for"] = "answer_" .. i
    72                       },
    73                       content = function()
    74                         ui.tag{
    75                           tag = "input",
    76                           attr = {
    77                             id = "answer_" .. i,
    78                             class = "mdl-radio__button",
    79                             type = "radio",
    80                             name = "answer",
    81                             value = answer_option,
    82                             checked = param.get("answer") == answer_option and "checked" or nil,
    83                           }
    84                         }
    85                         ui.tag{
    86                           attr = { class = "mdl-radio__label", ['for'] = "answer_" .. i },
    87                           content = answer_option
    88                         }
    89                       end
    90                     }
    91                   end }
    92                 end
    93                 slot.put("<br>")
    94                 ui.tag{
    95                   tag = "input",
    96                   attr = {
    97                     type = "submit",
    98                     class = "mdl-button mdl-js-button mdl-button--raised mdl-button--colored",
    99                     value = _"Next step"
   100                   },
   101                   content = ""
   102                 }
   103               end
   104             end
   105           }
   106         end
   107       end }
   108     end }
   109   end }
   110 end }
