bsw@1735: local id = param.get("question_id", atom.integer) bsw@1735: bsw@1735: local question = SurveyQuestion:by_id(id) bsw@1735: bsw@1735: local survey = Survey:get_open() bsw@1735: bsw@1735: if question.survey_id ~= survey.id then bsw@1735: slot.put_into("error", _"Internal error 2") bsw@1735: return false bsw@1735: end bsw@1735: bsw@1735: if not question or not question.survey.open then bsw@1735: slot.put_into("error", _"Internal error 3") bsw@1735: return false bsw@1735: end bsw@1735: bsw@1735: local survey_member = SurveyMember:by_pk(question.survey.id, app.session.member_id) bsw@1735: if not survey_member then bsw@1735: return execute.view { module = "index", view = "404" } bsw@1735: end bsw@1735: bsw@1735: local answer_set = survey_member.answer_set bsw@1735: if not answer_set then bsw@1735: return execute.view { module = "index", view = "404" } bsw@1735: end bsw@1735: bsw@1735: local answer = SurveyAnswer:by_pk(answer_set.ident, question.id) bsw@1735: if not answer then bsw@1735: answer = SurveyAnswer:new() bsw@1735: answer.survey_answer_set_ident = answer_set.ident bsw@1735: answer.survey_question_id = question.id bsw@1735: end bsw@1735: bsw@1735: if question.answer_type == "radio" then bsw@1743: local given_answer = param.get("answer") bsw@1735: if not given_answer then bsw@1735: slot.put_into("error", _"Please choose an option!") bsw@1735: return false bsw@1735: end bsw@1735: local answer_valid = false bsw@1735: for i, answer_option in ipairs(question.answer_options) do bsw@1735: if given_answer == answer_option then bsw@1735: answer_valid = true bsw@1735: end bsw@1735: end bsw@1735: if not answer_valid then bsw@1735: slot.put_into("error", _"Internal error 1") bsw@1735: return false bsw@1735: end bsw@1743: answer.answer = given_answer bsw@1743: bsw@1744: elseif question.answer_type == "checkbox" then bsw@1743: local answers = json.array() bsw@1743: for i, answer_option in ipairs(question.answer_options) do bsw@1743: local answer = param.get("answer_" .. answer_option) bsw@1743: if answer then bsw@1748: table.insert(answers, answer_option) bsw@1743: end bsw@1743: end bsw@1743: answer.answer = answers bsw@1735: end bsw@1735: bsw@1735: answer:save() bsw@1735: bsw@1735: local question bsw@1735: local answers_by_question_id = {} bsw@1735: for i, answer in ipairs(answer_set.answers) do bsw@1735: answers_by_question_id[answer.survey_question_id] = answer bsw@1735: end bsw@1735: for i, q in ipairs(survey.questions) do bsw@1735: if not question and not answers_by_question_id[q.id] then bsw@1735: question = q bsw@1735: end bsw@1735: end bsw@1735: bsw@1735: if not question then bsw@1735: survey_member.survey_answer_set_ident = nil bsw@1735: survey_member.finished = 'now' bsw@1735: survey_member:save() bsw@1735: end bsw@1735: bsw@1735: return true