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