# HG changeset patch # User bsw # Date 1633942045 -7200 # Node ID 3d61fa0d6474bcdadf25705a1d44fe404e0e3ac4 # Parent 9f96b7e6e5f24c894f5bfb797508c43edbaebbbe Add checkboxes to survey diff -r 9f96b7e6e5f2 -r 3d61fa0d6474 app/main/survey/_action/answer.lua --- a/app/main/survey/_action/answer.lua Mon Oct 11 10:39:23 2021 +0200 +++ b/app/main/survey/_action/answer.lua Mon Oct 11 10:47:25 2021 +0200 @@ -31,9 +31,8 @@ answer.survey_question_id = question.id end -local given_answer = param.get("answer") - if question.answer_type == "radio" then + local given_answer = param.get("answer") if not given_answer then slot.put_into("error", _"Please choose an option!") return false @@ -48,9 +47,19 @@ slot.put_into("error", _"Internal error 1") return false end + answer.answer = given_answer + +if question.answer_type == "checkbox" then + local answers = json.array() + for i, answer_option in ipairs(question.answer_options) do + local answer = param.get("answer_" .. answer_option) + if answer then + table.insert(answers, answer) + end + end + answer.answer = answers end -answer.answer = given_answer answer:save() local question diff -r 9f96b7e6e5f2 -r 3d61fa0d6474 app/main/survey/participate.lua --- a/app/main/survey/participate.lua Mon Oct 11 10:39:23 2021 +0200 +++ b/app/main/survey/participate.lua Mon Oct 11 10:47:25 2021 +0200 @@ -56,6 +56,7 @@ }, content = function() ui.field.hidden{ name = "question_id", value = question.id } + if question.answer_type == "radio" then for i, answer_option in ipairs(question.answer_options) do ui.container{ content = function() @@ -82,7 +83,35 @@ end } end } + + if question.answer_type == "checkbox" then + for i, answer_option in ipairs(question.answer_options) do + ui.container{ content = function() + ui.tag{ tag = "label", attr = { + class = "mdl-checkbox mdl-js-checkbox mdl-js-ripple-effect", + ["for"] = "answer_" .. i + }, + content = function() + ui.tag{ + tag = "input", + attr = { + id = "answer_" .. i, + class = "mdl-checkbox__button", + type = "checkbox", + name = "answer_" .. answer_option, + value = "1", + checked = param.get("answer_" .. answer_option) and "checked" or nil, + } + } + ui.tag{ + attr = { class = "mdl-checkbox__label", ['for'] = "answer_" .. i }, + content = answer_option + } + end + } + end } end + slot.put("
") ui.tag{ tag = "input",