diff --git a/addons/survey/controllers/main.py b/addons/survey/controllers/main.py index f3d58ed9cd3..6da5e8e87ad 100644 --- a/addons/survey/controllers/main.py +++ b/addons/survey/controllers/main.py @@ -177,9 +177,45 @@ class WebsiteSurvey(http.Controller): return request.website.render("website.403") # AJAX prefilling of a survey - # @website.route(['/survey/prefill/'], - # type='json', auth='public', multilang=True): - # def prefill(self, survey, **post): + @website.route(['/survey/prefill//', + '/survey/prefill///'], + type='http', auth='public', multilang=True) + def prefill(self, survey, token, page=None, **post): + cr, uid, context = request.cr, request.uid, request.context + user_input_line_obj = request.registry['survey.user_input_line'] + ret = {} + + # Fetch previous answers + if page: + ids = user_input_line_obj.search(cr, uid, [('user_input_id.token', '=', token), ('page_id', '=', page.id)], context=context) + else: + ids = user_input_line_obj.search(cr, uid, [('user_input_id.token', '=', token)], context=context) + previous_answers = user_input_line_obj.browse(cr, uid, ids, context=context) + + # Return non empty answers in a JSON compatible format + for answer in previous_answers: + if not answer.skipped: + answer_tag = '%s_%s_%s' % (answer.survey_id.id, answer.page_id.id, answer.question_id.id) + answer_value = None + if answer.answer_type == 'free_text': + answer_value = answer.value_free_text + elif answer.answer_type == 'text': + answer_value = answer.value_text + elif answer.answer_type == 'number': + answer_value = answer.value_number + elif answer.answer_type == 'date': + answer_value = answer.value_date + # TODO mettre les QCM ici + if answer_value: + ret.update({answer_tag: answer_value}) + else: + _logger.warning("[survey] Fetching None answer for question %s" % answer_tag) + + return json.dumps(ret) + + # load all the user input lines corresponding to token & page (or survey) + # format them into a json dict + # return them to JS # AJAX validation of some questions # @website.route(['/survey/validate/'], diff --git a/addons/survey/static/src/js/survey.js b/addons/survey/static/src/js/survey.js index 699f2990f16..376dfde1a0b 100644 --- a/addons/survey/static/src/js/survey.js +++ b/addons/survey/static/src/js/survey.js @@ -60,9 +60,19 @@ $(document).ready(function () { var validate_controller = the_form.attr("data-validate"); var submit_controller = the_form.attr("data-submit"); - // function prefill(form){ - // return false; - // } + // Pre-filling of the form with previous answers + function prefill(){ + var prefill_def = $.ajax(prefill_controller, {dataType: "json"}) + .done(function(json_data){ + console.log(json_data) + _.each(json_data, function(value, key){ + the_form.find(".form-control[name=" + key + "]").val(value); + }); + }) + .fail(function(){ + console.warn("[survey] Unable to load prefill data"); + }); + }; // function validate(form){ // return false; @@ -105,4 +115,6 @@ $(document).ready(function () { }); console.debug("[survey] Custom JS for survey loaded!"); + + prefill(); }); diff --git a/addons/survey/survey.py b/addons/survey/survey.py index b7a6d3d9522..e7cf0c648ab 100644 --- a/addons/survey/survey.py +++ b/addons/survey/survey.py @@ -745,7 +745,7 @@ class survey_label(osv.osv): required=True) } defaults = { - 'sequence': 10 + 'sequence': 100 } @@ -934,7 +934,8 @@ class survey_user_input_line(osv.osv): 'value_number': fields.float("Numerical answer"), 'value_date': fields.datetime("Date answer"), 'value_free_text': fields.text("Free Text answer"), - 'value_suggested': fields.many2one('survey.label'), + 'value_suggested': fields.many2one('survey.label', "Suggested answer"), + 'value_suggested_row': fields.many2one('survey.label', "Row answer"), } _defaults = { 'skipped': False, @@ -965,7 +966,7 @@ class survey_user_input_line(osv.osv): if answer_tag in post and post[answer_tag].strip() != '': vals.update({'answer_type': 'free_text', 'value_free_text': post[answer_tag]}) else: - vals.update({'skipped': True}) + vals.update({'answer_type': None, 'skipped': True}) old_uil = self.search(cr, uid, [('user_input_id', '=', user_input_id), ('survey_id', '=', question.survey_id.id), ('question_id', '=', question.id)], @@ -986,7 +987,7 @@ class survey_user_input_line(osv.osv): if answer_tag in post and post[answer_tag].strip() != '': vals.update({'answer_type': 'text', 'value_text': post[answer_tag]}) else: - vals.update({'skipped': True}) + vals.update({'answer_type': None, 'skipped': True}) old_uil = self.search(cr, uid, [('user_input_id', '=', user_input_id), ('survey_id', '=', question.survey_id.id), ('question_id', '=', question.id)], @@ -1007,7 +1008,7 @@ class survey_user_input_line(osv.osv): if answer_tag in post and post[answer_tag].strip() != '': vals.update({'answer_type': 'number', 'value_number': float(post[answer_tag])}) else: - vals.update({'skipped': True, 'answer_type': None}) + vals.update({'answer_type': None, 'skipped': True}) old_uil = self.search(cr, uid, [('user_input_id', '=', user_input_id), ('survey_id', '=', question.survey_id.id), ('question_id', '=', question.id)], @@ -1028,7 +1029,7 @@ class survey_user_input_line(osv.osv): if answer_tag in post and post[answer_tag].strip() != '': vals.update({'answer_type': 'date', 'value_date': post[answer_tag]}) else: - vals.update({'skipped': True}) + vals.update({'answer_type': None, 'skipped': True}) old_uil = self.search(cr, uid, [('user_input_id', '=', user_input_id), ('survey_id', '=', question.survey_id.id), ('question_id', '=', question.id)], @@ -1047,9 +1048,9 @@ class survey_user_input_line(osv.osv): 'survey_id': question.survey_id.id, } if answer_tag in post and post[answer_tag].strip() != '': - vals.update({'answer_type': 'date', 'value_date': post[answer_tag]}) + vals.update({'answer_type': 'suggestion', 'value_suggested': post[answer_tag]}) else: - vals.update({'skipped': True}) + vals.update({'answer_type': None, 'skipped': True}) old_uil = self.search(cr, uid, [('user_input_id', '=', user_input_id), ('survey_id', '=', question.survey_id.id), ('question_id', '=', question.id)], diff --git a/addons/survey/views/survey_templates.xml b/addons/survey/views/survey_templates.xml index b2798c0370f..7ad7195ca8e 100644 --- a/addons/survey/views/survey_templates.xml +++ b/addons/survey/views/survey_templates.xml @@ -22,7 +22,7 @@ - +