From 34f14e357c38e570e7862155333cf7835e98a6f2 Mon Sep 17 00:00:00 2001 From: "Parth Gajjar (Open ERP)" Date: Thu, 6 Feb 2014 19:10:53 +0530 Subject: [PATCH] [ADD] added filter for matrix and choice bzr revid: pga@tinyerp.com-20140206134053-mjlfe7qdf07bk8me --- addons/survey/controllers/main.py | 36 ++++++++++++++----- .../survey/static/src/css/survey_result.css | 9 +++++ addons/survey/static/src/js/survey_result.js | 19 ++++++++++ addons/survey/views/survey_result.xml | 17 ++++----- 4 files changed, 65 insertions(+), 16 deletions(-) diff --git a/addons/survey/controllers/main.py b/addons/survey/controllers/main.py index 0987e79ed8c..812038efadb 100644 --- a/addons/survey/controllers/main.py +++ b/addons/survey/controllers/main.py @@ -294,21 +294,40 @@ class WebsiteSurvey(http.Controller): 'prepare_result':self.prepare_result, 'get_input_summary':self.get_input_summary, 'get_graph_data':self.get_graph_data, - 'page_range':self.page_range + 'page_range':self.page_range, + 'current_filters':post and self.filter_input_ids(post) }) + def filter_input_ids(self,post): + cr, uid, context = request.cr, request.uid, request.context + input_line_obj = request.registry['survey.user_input_line'] + input_obj = request.registry['survey.user_input_line'] + domain_filter,choice = [],[] + for ids in post: + row_id,answer_id = ids.split(',') + if row_id == '0': + choice.append(int(answer_id)) + else: + domain_filter.extend(['|',('value_suggested_row.id','=',int(row_id)),('value_suggested.id','=',int(answer_id))]) + if choice: + domain_filter.insert(0,('value_suggested.id','in',choice)) + else: + domain_filter = domain_filter[1:] + line_ids = input_line_obj.search(cr, uid, domain_filter, context=context) + filtered_input_ids = [input.user_input_id.id for input in input_obj.browse(cr, uid, line_ids, context=context)] + return filtered_input_ids def page_range(self, total_record, limit): '''Returns number of pages required for pagination''' total = math.ceil( total_record/float(limit) ) return range(1, int( total+1 )) - def prepare_result(self, question): + def prepare_result(self, question, current_filters=[]): '''Prepare statistical data for questions by counting number of vote per choice''' if question.type in ['simple_choice', 'multiple_choice'] : result_summary = {} - [ result_summary.update({ label.id : {'text':label.value, 'count':0} }) for label in question.labels_ids ] + [ result_summary.update({ label.id : {'text':label.value, 'count':0, 'answer_id':label.id} }) for label in question.labels_ids ] for input_line in question.user_input_line_ids: - if result_summary.get(input_line.value_suggested.id) and not input_line.skipped: + if result_summary.get(input_line.value_suggested.id) and (not(current_filters) or input_line.user_input_id.id in current_filters): result_summary[input_line.value_suggested.id]['count'] += 1 result_summary = result_summary.values() if question.type == 'matrix': @@ -318,7 +337,7 @@ class WebsiteSurvey(http.Controller): for cell in itertools.product(rows.keys(), answers.keys()): res[cell] = 0 for input_line in question.user_input_line_ids: - if not input_line.skipped: + if not(current_filters) or input_line.user_input_id.id in current_filters: res[(input_line.value_suggested_row.id,input_line.value_suggested.id)] += 1 result_summary= {'answers':answers,'rows':rows,'result':res} if question.type == 'numerical_box': @@ -336,14 +355,15 @@ class WebsiteSurvey(http.Controller): return result_summary @http.route(['/survey/results/graph/'],type='http', auth='user', multilang=True, website=True) - def get_graph_data(self, question): + def get_graph_data(self, question, **post): '''Returns appropriate formated data required by graph library''' + current_filters = eval(post.get('current_filters','[]')) result = [] if question.type in ['simple_choice', 'multiple_choice']: result.append({'key':str(question.question), - 'values':self.prepare_result(question)}) + 'values':self.prepare_result(question, current_filters)}) if question.type == 'matrix': - data = self.prepare_result(question) + data = self.prepare_result(question, current_filters) for answer in data['answers']: values = [] for res in data['result']: diff --git a/addons/survey/static/src/css/survey_result.css b/addons/survey/static/src/css/survey_result.css index 1e0ff4eb34b..48b818065d3 100644 --- a/addons/survey/static/src/css/survey_result.css +++ b/addons/survey/static/src/css/survey_result.css @@ -18,3 +18,12 @@ -moz-border-radius: 5px 5px 0 0; border-radius: 5px 5px 0 0; } + + +.survey_answer i{ + padding:3px; + cursor:pointer; +} +.survey_answer i.invisible{ + visibility: hidden!important; +} \ No newline at end of file diff --git a/addons/survey/static/src/js/survey_result.js b/addons/survey/static/src/js/survey_result.js index d1a5d20dbd2..5a276bddac8 100644 --- a/addons/survey/static/src/js/survey_result.js +++ b/addons/survey/static/src/js/survey_result.js @@ -101,10 +101,12 @@ $(document).ready(function () { $.each(survey_graphs, function(index, graph){ var question_id = $(graph).attr("data-question_id"); var graph_type = $(graph).attr("data-graph_type"); + var current_filters = $(graph).attr("data-current_filters"); $.ajax({ url: '/survey/results/graph/'+question_id, type: 'POST', dataType: 'json', + data:{'current_filters':current_filters}, success: function(response, status, xhr, wfe){ if(graph_type == 'multi_bar'){ nv.addGraph(function(){ @@ -122,5 +124,22 @@ $(document).ready(function () { }); }); + // Script for filter + $('td.survey_answer').hover(function(){$(this).find('i.fa-filter').removeClass('invisible');},function(){$(this).find('i.fa-filter').addClass('invisible');}); + $('td.survey_answer i.fa-filter').click(function(){ + var cell=$(this); + var question_id = cell.attr('data-question_id'); + var row_id = cell.attr('data-row_id') | 0; + var answer_id = cell.attr('data-answer_id'); + if(document.URL.indexOf("?") == -1) + { + window.location.href = document.URL+'?' + encodeURI(row_id + ','+answer_id + '=' + question_id); + } + else{ + window.location.href = document.URL+'&' + encodeURI(row_id + ','+answer_id + '=' + question_id); + + } + }); + console.debug("[survey] Survey Result JS loaded!"); }); \ No newline at end of file diff --git a/addons/survey/views/survey_result.xml b/addons/survey/views/survey_result.xml index e0ca4fda5bb..b0e3d45e8cd 100644 --- a/addons/survey/views/survey_result.xml +++ b/addons/survey/views/survey_result.xml @@ -116,7 +116,7 @@
-
+
@@ -129,13 +129,14 @@ - +

- + % Vote + @@ -147,7 +148,7 @@