liquid_feedback_frontend
diff app/main/survey/_action/answer.lua @ 1735:5a8a09119865
Added survey feature
author | bsw |
---|---|
date | Fri Oct 08 00:09:23 2021 +0200 (2021-10-08) |
parents | |
children | 3d61fa0d6474 |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/app/main/survey/_action/answer.lua Fri Oct 08 00:09:23 2021 +0200 1.3 @@ -0,0 +1,73 @@ 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 +local given_answer = param.get("answer") 1.38 + 1.39 +if question.answer_type == "radio" then 1.40 + if not given_answer then 1.41 + slot.put_into("error", _"Please choose an option!") 1.42 + return false 1.43 + end 1.44 + local answer_valid = false 1.45 + for i, answer_option in ipairs(question.answer_options) do 1.46 + if given_answer == answer_option then 1.47 + answer_valid = true 1.48 + end 1.49 + end 1.50 + if not answer_valid then 1.51 + slot.put_into("error", _"Internal error 1") 1.52 + return false 1.53 + end 1.54 +end 1.55 + 1.56 +answer.answer = given_answer 1.57 +answer:save() 1.58 + 1.59 +local question 1.60 +local answers_by_question_id = {} 1.61 +for i, answer in ipairs(answer_set.answers) do 1.62 + answers_by_question_id[answer.survey_question_id] = answer 1.63 +end 1.64 +for i, q in ipairs(survey.questions) do 1.65 + if not question and not answers_by_question_id[q.id] then 1.66 + question = q 1.67 + end 1.68 +end 1.69 + 1.70 +if not question then 1.71 + survey_member.survey_answer_set_ident = nil 1.72 + survey_member.finished = 'now' 1.73 + survey_member:save() 1.74 +end 1.75 + 1.76 +return true