liquid_feedback_frontend
diff app/main/survey/_action/answer.lua @ 1843:b01d9920371b
merge
author | jbe |
---|---|
date | Thu Feb 03 15:57:22 2022 +0100 (2022-02-03) |
parents | 8d7c4d542c3d |
children |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/app/main/survey/_action/answer.lua Thu Feb 03 15:57:22 2022 +0100 1.3 @@ -0,0 +1,82 @@ 1.4 +local id = param.get("question_id", atom.integer) 1.5 + 1.6 +local question = SurveyQuestion:by_id(id) 1.7 + 1.8 +local survey = Survey:get_open() 1.9 + 1.10 +if question.survey_id ~= survey.id then 1.11 + slot.put_into("error", _"Internal error 2") 1.12 + return false 1.13 +end 1.14 + 1.15 +if not question or not question.survey.open then 1.16 + slot.put_into("error", _"Internal error 3") 1.17 + return false 1.18 +end 1.19 + 1.20 +local survey_member = SurveyMember:by_pk(question.survey.id, app.session.member_id) 1.21 +if not survey_member then 1.22 + return execute.view { module = "index", view = "404" } 1.23 +end 1.24 + 1.25 +local answer_set = survey_member.answer_set 1.26 +if not answer_set then 1.27 + return execute.view { module = "index", view = "404" } 1.28 +end 1.29 + 1.30 +local answer = SurveyAnswer:by_pk(answer_set.ident, question.id) 1.31 +if not answer then 1.32 + answer = SurveyAnswer:new() 1.33 + answer.survey_answer_set_ident = answer_set.ident 1.34 + answer.survey_question_id = question.id 1.35 +end 1.36 + 1.37 +if question.answer_type == "radio" then 1.38 + local given_answer = param.get("answer") 1.39 + if not given_answer then 1.40 + slot.put_into("error", _"Please choose an option!") 1.41 + return false 1.42 + end 1.43 + local answer_valid = false 1.44 + for i, answer_option in ipairs(question.answer_options) do 1.45 + if given_answer == answer_option then 1.46 + answer_valid = true 1.47 + end 1.48 + end 1.49 + if not answer_valid then 1.50 + slot.put_into("error", _"Internal error 1") 1.51 + return false 1.52 + end 1.53 + answer.answer = given_answer 1.54 + 1.55 +elseif question.answer_type == "checkbox" then 1.56 + local answers = json.array() 1.57 + for i, answer_option in ipairs(question.answer_options) do 1.58 + local answer = param.get("answer_" .. answer_option) 1.59 + if answer then 1.60 + table.insert(answers, answer_option) 1.61 + end 1.62 + end 1.63 + answer.answer = answers 1.64 +end 1.65 + 1.66 +answer:save() 1.67 + 1.68 +local question 1.69 +local answers_by_question_id = {} 1.70 +for i, answer in ipairs(answer_set.answers) do 1.71 + answers_by_question_id[answer.survey_question_id] = answer 1.72 +end 1.73 +for i, q in ipairs(survey.questions) do 1.74 + if not question and not answers_by_question_id[q.id] then 1.75 + question = q 1.76 + end 1.77 +end 1.78 + 1.79 +if not question then 1.80 + survey_member.survey_answer_set_ident = nil 1.81 + survey_member.finished = 'now' 1.82 + survey_member:save() 1.83 +end 1.84 + 1.85 +return true