[FIX] survey: display labels in the right order in survey reporting views

This commit is contained in:
Richard Mathot 2014-06-18 17:08:08 +02:00
parent d5c386f04a
commit 6e93af4774
2 changed files with 27 additions and 20 deletions

View File

@ -318,8 +318,9 @@ class WebsiteSurvey(http.Controller):
'filter_finish': filter_finish 'filter_finish': filter_finish
}) })
def prepare_result_dict(self,survey, current_filters=[]): def prepare_result_dict(self,survey, current_filters=None):
"""Returns dictionary having values for rendering template""" """Returns dictionary having values for rendering template"""
current_filters = current_filters if current_filters else []
survey_obj = request.registry['survey.survey'] survey_obj = request.registry['survey.survey']
result = {'survey':survey, 'page_ids': []} result = {'survey':survey, 'page_ids': []}
for page in survey.page_ids: for page in survey.page_ids:
@ -347,8 +348,10 @@ class WebsiteSurvey(http.Controller):
total = ceil(total_record / float(limit)) total = ceil(total_record / float(limit))
return range(1, int(total + 1)) return range(1, int(total + 1))
def get_graph_data(self, question, current_filters=[]): def get_graph_data(self, question, current_filters=None):
'''Returns formatted data required by graph library on basis of filter''' '''Returns formatted data required by graph library on basis of filter'''
# TODO refactor this terrible method and merge it with prepare_result_dict
current_filters = current_filters if current_filters else []
survey_obj = request.registry['survey.survey'] survey_obj = request.registry['survey.survey']
result = [] result = []
if question.type == 'multiple_choice': if question.type == 'multiple_choice':
@ -360,9 +363,8 @@ class WebsiteSurvey(http.Controller):
data = survey_obj.prepare_result(request.cr, request.uid, question, current_filters, context=request.context) data = survey_obj.prepare_result(request.cr, request.uid, question, current_filters, context=request.context)
for answer in data['answers']: for answer in data['answers']:
values = [] values = []
for res in data['result']: for row in data['rows']:
if res[1] == answer: values.append({'text': data['rows'].get(row), 'count': data['result'].get((row, answer))})
values.append({'text': data['rows'][res[0]], 'count': data['result'][res]})
result.append({'key': data['answers'].get(answer), 'values': values}) result.append({'key': data['answers'].get(answer), 'values': values})
return json.dumps(result) return json.dumps(result)

View File

@ -26,6 +26,7 @@ from openerp.addons.website.models.website import slug
from urlparse import urljoin from urlparse import urljoin
from itertools import product from itertools import product
from collections import Counter from collections import Counter
from collections import OrderedDict
import datetime import datetime
import logging import logging
@ -289,8 +290,7 @@ class survey_survey(osv.Model):
:param finished: True for completely filled survey,Falser otherwise. :param finished: True for completely filled survey,Falser otherwise.
:returns list of filtered user_input_ids. :returns list of filtered user_input_ids.
''' '''
if context is None: context = context if context else {}
context = {}
if filters: if filters:
input_line_obj = self.pool.get('survey.user_input_line') input_line_obj = self.pool.get('survey.user_input_line')
domain_filter, choice, filter_display_data = [], [], [] domain_filter, choice, filter_display_data = [], [], []
@ -339,22 +339,27 @@ class survey_survey(osv.Model):
filter_display_data.append({'question_text': question.question, 'labels': [label.value for label in labels]}) filter_display_data.append({'question_text': question.question, 'labels': [label.value for label in labels]})
return filter_display_data return filter_display_data
def prepare_result(self, cr, uid, question, current_filters=[], context=None): def prepare_result(self, cr, uid, question, current_filters=None, context=None):
''' Compute statistical data for questions by counting number of vote per choice on basis of filter ''' ''' Compute statistical data for questions by counting number of vote per choice on basis of filter '''
if context is None: current_filters = current_filters if current_filters else []
context = {} context = context if context else {}
#Calculate and return statistics for choice #Calculate and return statistics for choice
if question.type in ['simple_choice', 'multiple_choice']: if question.type in ['simple_choice', 'multiple_choice']:
result_summary = {} result_summary = []
[result_summary.update({label.id: {'text': label.value, 'count': 0, 'answer_id': label.id}}) for label in question.labels_ids] for label in question.labels_ids:
for input_line in question.user_input_line_ids: count = 0
if input_line.answer_type == 'suggestion' and result_summary.get(input_line.value_suggested.id) and (not(current_filters) or input_line.user_input_id.id in current_filters): for input_line in question.user_input_line_ids:
result_summary[input_line.value_suggested.id]['count'] += 1 if input_line.answer_type == 'suggestion' and input_line.value_suggested.id == label.id and (not current_filters or input_line.user_input_id.id in current_filters):
result_summary = result_summary.values() count = count + 1
label_summary = {'text': label.value, 'count': count, 'answer_id': label.id}
result_summary = result_summary + [label_summary]
#Calculate and return statistics for matrix #Calculate and return statistics for matrix
if question.type == 'matrix': if question.type == 'matrix':
rows, answers, res = {}, {}, {} rows = OrderedDict()
answers = OrderedDict()
res = dict()
[rows.update({label.id: label.value}) for label in question.labels_ids_2] [rows.update({label.id: label.value}) for label in question.labels_ids_2]
[answers.update({label.id: label.value}) for label in question.labels_ids] [answers.update({label.id: label.value}) for label in question.labels_ids]
for cell in product(rows.keys(), answers.keys()): for cell in product(rows.keys(), answers.keys()):
@ -386,10 +391,10 @@ class survey_survey(osv.Model):
'most_comman': Counter(all_inputs).most_common(5)}) 'most_comman': Counter(all_inputs).most_common(5)})
return result_summary return result_summary
def get_input_summary(self, cr, uid, question, current_filters=[], context=None): def get_input_summary(self, cr, uid, question, current_filters=None, context=None):
''' Returns overall summary of question e.g. answered, skipped, total_inputs on basis of filter ''' ''' Returns overall summary of question e.g. answered, skipped, total_inputs on basis of filter '''
if context is None: current_filters = current_filters if current_filters else []
context = {} context = context if context else {}
result = {} result = {}
if question.survey_id.user_input_ids: if question.survey_id.user_input_ids:
total_input_ids = current_filters or [input_id.id for input_id in question.survey_id.user_input_ids if input_id.state != 'new'] total_input_ids = current_filters or [input_id.id for input_id in question.survey_id.user_input_ids if input_id.state != 'new']