| 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 if question.answer_type == "radio" then | 
| bsw@1743 | 35   local given_answer = param.get("answer") | 
| bsw@1735 | 36   if not given_answer then | 
| bsw@1735 | 37     slot.put_into("error", _"Please choose an option!") | 
| bsw@1735 | 38     return false | 
| bsw@1735 | 39   end | 
| bsw@1735 | 40   local answer_valid = false | 
| bsw@1735 | 41   for i, answer_option in ipairs(question.answer_options) do | 
| bsw@1735 | 42     if given_answer == answer_option then | 
| bsw@1735 | 43       answer_valid = true | 
| bsw@1735 | 44     end | 
| bsw@1735 | 45   end | 
| bsw@1735 | 46   if not answer_valid then | 
| bsw@1735 | 47     slot.put_into("error", _"Internal error 1") | 
| bsw@1735 | 48     return false | 
| bsw@1735 | 49   end | 
| bsw@1743 | 50   answer.answer = given_answer | 
| bsw@1743 | 51 | 
| bsw@1744 | 52 elseif question.answer_type == "checkbox" then | 
| bsw@1743 | 53   local answers = json.array() | 
| bsw@1743 | 54   for i, answer_option in ipairs(question.answer_options) do | 
| bsw@1743 | 55     local answer = param.get("answer_" .. answer_option) | 
| bsw@1743 | 56     if answer then | 
| bsw@1748 | 57       table.insert(answers, answer_option) | 
| bsw@1743 | 58     end | 
| bsw@1743 | 59   end | 
| bsw@1743 | 60   answer.answer = answers | 
| bsw@1735 | 61 end | 
| bsw@1735 | 62 | 
| bsw@1735 | 63 answer:save() | 
| bsw@1735 | 64 | 
| bsw@1735 | 65 local question | 
| bsw@1735 | 66 local answers_by_question_id = {} | 
| bsw@1735 | 67 for i, answer in ipairs(answer_set.answers) do | 
| bsw@1735 | 68   answers_by_question_id[answer.survey_question_id] = answer | 
| bsw@1735 | 69 end | 
| bsw@1735 | 70 for i, q in ipairs(survey.questions) do | 
| bsw@1735 | 71   if not question and not answers_by_question_id[q.id] then | 
| bsw@1735 | 72     question = q | 
| bsw@1735 | 73   end | 
| bsw@1735 | 74 end | 
| bsw@1735 | 75 | 
| bsw@1735 | 76 if not question then | 
| bsw@1735 | 77   survey_member.survey_answer_set_ident = nil | 
| bsw@1735 | 78   survey_member.finished = 'now' | 
| bsw@1735 | 79   survey_member:save() | 
| bsw@1735 | 80 end | 
| bsw@1735 | 81 | 
| bsw@1735 | 82 return true |