bsw@1735: local survey = Survey:get_open()
bsw@1735: if not survey then
bsw@1735:   return execute.view { module = "index", view = "404" }
bsw@1735: end
bsw@1735: 
bsw@1735: local survey_member = SurveyMember:by_pk(survey.id, app.session.member_id)
bsw@1735: if not survey_member then
bsw@1735:   return execute.view { module = "index", view = "404" }
bsw@1735: end
bsw@1735: 
bsw@1735: local question
bsw@1735: 
bsw@1735: if survey_member then
bsw@1735:   local answer_set = survey_member.answer_set
bsw@1735:   if answer_set then
bsw@1735:     local answers_by_question_id = {}
bsw@1735:     for i, answer in ipairs(answer_set.answers) do
bsw@1735:       answers_by_question_id[answer.survey_question_id] = answer
bsw@1735:     end
bsw@1735:     for i, q in ipairs(survey.questions) do
bsw@1735:       if not question and not answers_by_question_id[q.id] then
bsw@1735:         question = q
bsw@1735:       end
bsw@1735:     end
bsw@1735:   end
bsw@1735: end
bsw@1735: 
bsw@1735: ui.title(_"Survey")
bsw@1735: ui.grid{ content = function()
bsw@1735:   ui.cell_main{ content = function()
bsw@1735: 
bsw@1735:     ui.container{ attr = { class = "mdl-card mdl-card__fullwidth mdl-shadow--2dp" }, content = function()
bsw@1735:       ui.container{ attr = { class = "mdl-card__title mdl-card--border" }, content = function()
bsw@1735:         ui.heading { attr = { class = "mdl-card__title-text" }, level = 2, content = survey.title }
bsw@1735:         ui.container{ 
bsw@1735:           content = _(
bsw@1735:             "This survey closes in #{closing}.", 
bsw@1735:             { closing = format.interval_text(survey.time_left) }
bsw@1735:           )
bsw@1735:         }
bsw@1735:       end }
bsw@1735:       ui.container{ attr = { class = "mdl-card__content mdl-card--border" }, content = function()
bsw@1735:         if survey_member.finished then
bsw@1735:           ui.container{ content = function()
bsw@1735:             slot.put(survey.finished_text)
bsw@1735:           end }
bsw@1735:           slot.put("
")
bsw@1735:           ui.link{
bsw@1735:             attr = { class = "mdl-button mdl-js-button mdl-button--raised mdl-button--colored" },
bsw@1735:             module = "index", view = "index", content = _"Go to start page"
bsw@1735:           }
bsw@1735:           return
bsw@1735:         else
bsw@1735:           ui.heading{ level = 2, content = question.question }
bsw@1735:           if question.description then
bsw@1735:             ui.container{ content = question.description }
bsw@1735:           end
bsw@1735:           ui.form{
bsw@1735:             module = "survey", action = "answer",
bsw@1735:             routing = {
bsw@1735:               ok = { mode = "redirect", module = "survey", view = "participate" },
bsw@1735:               error = { mode = "forward", module = "survey", view = "participate" },
bsw@1735:             },
bsw@1735:             content = function()
bsw@1735:               ui.field.hidden{ name = "question_id", value = question.id }
bsw@1735:               if question.answer_type == "radio" then
bsw@1735:                 for i, answer_option in ipairs(question.answer_options) do
bsw@1735:                   ui.container{ content = function()
bsw@1735:                     ui.tag{ tag = "label", attr = {
bsw@1735:                         class = "mdl-radio mdl-js-radio mdl-js-ripple-effect",
bsw@1735:                         ["for"] = "answer_" .. i
bsw@1735:                       },
bsw@1735:                       content = function()
bsw@1735:                         ui.tag{
bsw@1735:                           tag = "input",
bsw@1735:                           attr = {
bsw@1735:                             id = "answer_" .. i,
bsw@1735:                             class = "mdl-radio__button",
bsw@1735:                             type = "radio",
bsw@1735:                             name = "answer",
bsw@1735:                             value = answer_option,
bsw@1735:                             checked = param.get("answer") == answer_option and "checked" or nil,
bsw@1735:                           }
bsw@1735:                         }
bsw@1735:                         ui.tag{
bsw@1735:                           attr = { class = "mdl-radio__label", ['for'] = "answer_" .. i },
bsw@1735:                           content = answer_option
bsw@1735:                         }
bsw@1735:                       end
bsw@1735:                     }
bsw@1735:                   end }
bsw@1735:                 end
bsw@1735:                 slot.put("
")
bsw@1735:                 ui.tag{
bsw@1735:                   tag = "input",
bsw@1735:                   attr = {
bsw@1735:                     type = "submit",
bsw@1735:                     class = "mdl-button mdl-js-button mdl-button--raised mdl-button--colored",
bsw@1735:                     value = _"Next step"
bsw@1735:                   },
bsw@1735:                   content = ""
bsw@1735:                 }
bsw@1735:               end
bsw@1735:             end
bsw@1735:           }
bsw@1735:         end
bsw@1735:       end }
bsw@1735:     end }
bsw@1735:   end }
bsw@1735: end }