rev |
line source |
bsw@1735
|
1 local id = param.get("question_id", atom.integer)
|
bsw@1735
|
2
|
bsw@1735
|
3 local question = SurveyQuestion:by_id(id)
|
bsw@1735
|
4
|
bsw@1735
|
5 local survey = Survey:get_open()
|
bsw@1735
|
6
|
bsw@1735
|
7 if question.survey_id ~= survey.id then
|
bsw@1735
|
8 slot.put_into("error", _"Internal error 2")
|
bsw@1735
|
9 return false
|
bsw@1735
|
10 end
|
bsw@1735
|
11
|
bsw@1735
|
12 if not question or not question.survey.open then
|
bsw@1735
|
13 slot.put_into("error", _"Internal error 3")
|
bsw@1735
|
14 return false
|
bsw@1735
|
15 end
|
bsw@1735
|
16
|
bsw@1735
|
17 local survey_member = SurveyMember:by_pk(question.survey.id, app.session.member_id)
|
bsw@1735
|
18 if not survey_member then
|
bsw@1735
|
19 return execute.view { module = "index", view = "404" }
|
bsw@1735
|
20 end
|
bsw@1735
|
21
|
bsw@1735
|
22 local answer_set = survey_member.answer_set
|
bsw@1735
|
23 if not answer_set then
|
bsw@1735
|
24 return execute.view { module = "index", view = "404" }
|
bsw@1735
|
25 end
|
bsw@1735
|
26
|
bsw@1735
|
27 local answer = SurveyAnswer:by_pk(answer_set.ident, question.id)
|
bsw@1735
|
28 if not answer then
|
bsw@1735
|
29 answer = SurveyAnswer:new()
|
bsw@1735
|
30 answer.survey_answer_set_ident = answer_set.ident
|
bsw@1735
|
31 answer.survey_question_id = question.id
|
bsw@1735
|
32 end
|
bsw@1735
|
33
|
bsw@1735
|
34 local given_answer = param.get("answer")
|
bsw@1735
|
35
|
bsw@1735
|
36 if question.answer_type == "radio" then
|
bsw@1735
|
37 if not given_answer then
|
bsw@1735
|
38 slot.put_into("error", _"Please choose an option!")
|
bsw@1735
|
39 return false
|
bsw@1735
|
40 end
|
bsw@1735
|
41 local answer_valid = false
|
bsw@1735
|
42 for i, answer_option in ipairs(question.answer_options) do
|
bsw@1735
|
43 if given_answer == answer_option then
|
bsw@1735
|
44 answer_valid = true
|
bsw@1735
|
45 end
|
bsw@1735
|
46 end
|
bsw@1735
|
47 if not answer_valid then
|
bsw@1735
|
48 slot.put_into("error", _"Internal error 1")
|
bsw@1735
|
49 return false
|
bsw@1735
|
50 end
|
bsw@1735
|
51 end
|
bsw@1735
|
52
|
bsw@1735
|
53 answer.answer = given_answer
|
bsw@1735
|
54 answer:save()
|
bsw@1735
|
55
|
bsw@1735
|
56 local question
|
bsw@1735
|
57 local answers_by_question_id = {}
|
bsw@1735
|
58 for i, answer in ipairs(answer_set.answers) do
|
bsw@1735
|
59 answers_by_question_id[answer.survey_question_id] = answer
|
bsw@1735
|
60 end
|
bsw@1735
|
61 for i, q in ipairs(survey.questions) do
|
bsw@1735
|
62 if not question and not answers_by_question_id[q.id] then
|
bsw@1735
|
63 question = q
|
bsw@1735
|
64 end
|
bsw@1735
|
65 end
|
bsw@1735
|
66
|
bsw@1735
|
67 if not question then
|
bsw@1735
|
68 survey_member.survey_answer_set_ident = nil
|
bsw@1735
|
69 survey_member.finished = 'now'
|
bsw@1735
|
70 survey_member:save()
|
bsw@1735
|
71 end
|
bsw@1735
|
72
|
bsw@1735
|
73 return true
|