liquid_feedback_frontend

view app/main/survey/_action/answer.lua @ 1743:3d61fa0d6474

Add checkboxes to survey
author bsw
date Mon Oct 11 10:47:25 2021 +0200 (2021-10-11)
parents 5a8a09119865
children f5c41bffb3ff
line source
1 local id = param.get("question_id", atom.integer)
3 local question = SurveyQuestion:by_id(id)
5 local survey = Survey:get_open()
7 if question.survey_id ~= survey.id then
8 slot.put_into("error", _"Internal error 2")
9 return false
10 end
12 if not question or not question.survey.open then
13 slot.put_into("error", _"Internal error 3")
14 return false
15 end
17 local survey_member = SurveyMember:by_pk(question.survey.id, app.session.member_id)
18 if not survey_member then
19 return execute.view { module = "index", view = "404" }
20 end
22 local answer_set = survey_member.answer_set
23 if not answer_set then
24 return execute.view { module = "index", view = "404" }
25 end
27 local answer = SurveyAnswer:by_pk(answer_set.ident, question.id)
28 if not answer then
29 answer = SurveyAnswer:new()
30 answer.survey_answer_set_ident = answer_set.ident
31 answer.survey_question_id = question.id
32 end
34 if question.answer_type == "radio" then
35 local given_answer = param.get("answer")
36 if not given_answer then
37 slot.put_into("error", _"Please choose an option!")
38 return false
39 end
40 local answer_valid = false
41 for i, answer_option in ipairs(question.answer_options) do
42 if given_answer == answer_option then
43 answer_valid = true
44 end
45 end
46 if not answer_valid then
47 slot.put_into("error", _"Internal error 1")
48 return false
49 end
50 answer.answer = given_answer
52 if question.answer_type == "checkbox" then
53 local answers = json.array()
54 for i, answer_option in ipairs(question.answer_options) do
55 local answer = param.get("answer_" .. answer_option)
56 if answer then
57 table.insert(answers, answer)
58 end
59 end
60 answer.answer = answers
61 end
63 answer:save()
65 local question
66 local answers_by_question_id = {}
67 for i, answer in ipairs(answer_set.answers) do
68 answers_by_question_id[answer.survey_question_id] = answer
69 end
70 for i, q in ipairs(survey.questions) do
71 if not question and not answers_by_question_id[q.id] then
72 question = q
73 end
74 end
76 if not question then
77 survey_member.survey_answer_set_ident = nil
78 survey_member.finished = 'now'
79 survey_member:save()
80 end
82 return true

Impressum / About Us