diff --git a/addons/survey/controllers/main.py b/addons/survey/controllers/main.py index f1b4e3ebc91..d5a66629c55 100644 --- a/addons/survey/controllers/main.py +++ b/addons/survey/controllers/main.py @@ -22,9 +22,7 @@ import json import logging import werkzeug -from collections import Counter from datetime import datetime -from itertools import product from math import ceil from openerp import SUPERUSER_ID @@ -305,16 +303,19 @@ class WebsiteSurvey(http.Controller): }) def prepare_result_dict(self,survey, current_filters=[]): + """Returns dictionary having values for rendering template""" + survey_obj = request.registry['survey.survey'] result = {'survey':survey, 'page_ids': []} for page in survey.page_ids: page_dict = {'page': page, 'question_ids': []} for question in page.question_ids: - question_dict = { 'question':question, 'input_summary':self.get_input_summary(question, current_filters),'prepare_result':self.prepare_result(question, current_filters)} + question_dict = { 'question':question, 'input_summary':survey_obj.get_input_summary(request.cr, request.uid, question, current_filters, context=request.context),'prepare_result':survey_obj.prepare_result(request.cr, request.uid, question, current_filters, context=request.context)} page_dict['question_ids'].append(question_dict) result['page_ids'].append(page_dict) return result def get_filter_data(self, post): + """Returns data used for filtering the result""" filters = [] #if user add some random data in query URI try: @@ -331,66 +332,21 @@ class WebsiteSurvey(http.Controller): total = ceil(total_record / float(limit)) return range(1, int(total + 1)) - def prepare_result(self, question, current_filters=[]): - '''Prepare statistical data for questions by counting number of vote per choice on basis of filter''' - - #Calculate and return statistics for choice - if question.type in ['simple_choice', 'multiple_choice']: - result_summary = {} - [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(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() - - #Calculate and return statistics for matrix - if question.type == 'matrix': - rows, answers, res = {}, {}, {} - [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] - for cell in product(rows.keys(), answers.keys()): - res[cell] = 0 - for input_line in question.user_input_line_ids: - 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} - - #Calculate and return statistics for free_text, textbox, datetime - if question.type in ['free_text', 'textbox', 'datetime']: - result_summary = [] - for input_line in question.user_input_line_ids: - if not(current_filters) or input_line.user_input_id.id in current_filters: - result_summary.append(input_line) - - #Calculate and return statistics for numerical_box - if question.type == 'numerical_box': - result_summary = {'input_lines': []} - all_inputs = [] - for input_line in question.user_input_line_ids: - if not(current_filters) or input_line.user_input_id.id in current_filters: - all_inputs.append(input_line.value_number) - result_summary['input_lines'].append(input_line) - result_summary.update({'average': round(sum(all_inputs) / len(all_inputs), 2), - 'max': round(max(all_inputs), 2), - 'min': round(min(all_inputs), 2), - 'most_comman': Counter(all_inputs).most_common(5)}) - - return result_summary - @http.route(['/survey/results/graph/'], type='http', auth='user', multilang=True, website=True) def get_graph_data(self, question, **post): '''Returns appropriate formated data required by graph library on basis of filter''' question = request.registry['survey.question'].browse(request.cr, request.uid, question) + survey_obj = request.registry['survey.survey'] current_filters = safe_eval(post.get('current_filters', '[]')) result = [] if question.type == 'multiple_choice': result.append({'key': str(question.question), - 'values': self.prepare_result(question, current_filters)}) + 'values': survey_obj.prepare_result(request.cr, request.uid, question, current_filters, context=request.context)}) if question.type == 'simple_choice': - result = self.prepare_result(question, current_filters) + result = survey_obj.prepare_result(request.cr, request.uid, question, current_filters, context=request.context) if question.type == 'matrix': - data = self.prepare_result(question, current_filters) + data = survey_obj.prepare_result(request.cr, request.uid, question, current_filters, context=request.context) for answer in data['answers']: values = [] for res in data['result']: @@ -399,21 +355,6 @@ class WebsiteSurvey(http.Controller): result.append({'key': data['answers'].get(answer), 'values': values}) return json.dumps(result) - def get_input_summary(self, question, current_filters=[]): - '''Returns overall summary of question e.g. answered, skipped, total_inputs on basis of filter''' - result = {} - 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'] - result['total_inputs'] = len(total_input_ids) - question_input_ids = [] - for user_input in question.user_input_line_ids: - if not user_input.skipped: - question_input_ids.append(user_input.user_input_id.id) - result['answered'] = len(set(question_input_ids) & set(total_input_ids)) - result['skipped'] = result['total_inputs'] - result['answered'] - return result - - def dict_soft_update(dictionary, key, value): ''' Insert the pair : into the . If is already present, this function will append to the list of diff --git a/addons/survey/survey.py b/addons/survey/survey.py index 2030c421900..57895e0c004 100644 --- a/addons/survey/survey.py +++ b/addons/survey/survey.py @@ -24,6 +24,8 @@ from openerp.tools.translate import _ from openerp.tools import DEFAULT_SERVER_DATETIME_FORMAT as DF from openerp.addons.website.models.website import slug from urlparse import urljoin +from itertools import product +from collections import Counter import datetime import logging @@ -185,6 +187,74 @@ class survey_survey(osv.Model): labels = label_obj.browse(cr, uid, [row_id, answer_id], context=context) filter_display_data.append({'question_text': question.question, 'labels': [label.value for label in labels]}) return filter_display_data + + def prepare_result(self, cr, uid, question, current_filters=[], context=None): + '''Prepare statistical data for questions by counting number of vote per choice on basis of filter + :param question: browsed record of survey.question + ''' + if context is None: + context = {} + #Calculate and return statistics for choice + if question.type in ['simple_choice', 'multiple_choice']: + result_summary = {} + [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(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() + + #Calculate and return statistics for matrix + if question.type == 'matrix': + rows, answers, res = {}, {}, {} + [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] + for cell in product(rows.keys(), answers.keys()): + res[cell] = 0 + for input_line in question.user_input_line_ids: + 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} + + #Calculate and return statistics for free_text, textbox, datetime + if question.type in ['free_text', 'textbox', 'datetime']: + result_summary = [] + for input_line in question.user_input_line_ids: + if not(current_filters) or input_line.user_input_id.id in current_filters: + result_summary.append(input_line) + + #Calculate and return statistics for numerical_box + if question.type == 'numerical_box': + result_summary = {'input_lines': []} + all_inputs = [] + for input_line in question.user_input_line_ids: + if not(current_filters) or input_line.user_input_id.id in current_filters: + all_inputs.append(input_line.value_number) + result_summary['input_lines'].append(input_line) + if all_inputs: + result_summary.update({'average': round(sum(all_inputs) / len(all_inputs), 2), + 'max': round(max(all_inputs), 2), + 'min': round(min(all_inputs), 2), + 'most_comman': Counter(all_inputs).most_common(5)}) + return result_summary + + def get_input_summary(self, cr, uid, question, current_filters=[], context=None): + '''Returns overall summary of question e.g. answered, skipped, total_inputs on basis of filter + :param question: browsed record of survey.question + ''' + if context is None: + context = {} + result = {} + 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'] + result['total_inputs'] = len(total_input_ids) + question_input_ids = [] + for user_input in question.user_input_line_ids: + if not user_input.skipped: + question_input_ids.append(user_input.user_input_id.id) + result['answered'] = len(set(question_input_ids) & set(total_input_ids)) + result['skipped'] = result['total_inputs'] - result['answered'] + return result + # Model fields # _columns = { diff --git a/addons/survey/views/survey_result.xml b/addons/survey/views/survey_result.xml index f021723043f..e262b1dc859 100644 --- a/addons/survey/views/survey_result.xml +++ b/addons/survey/views/survey_result.xml @@ -29,12 +29,12 @@ - - All surveys - - - Finished surveys - + + All surveys + + + Finished surveys + Clear All Filters @@ -62,20 +62,25 @@ Skipped - -
+ + +
+
+ + + + + + + + + + + +
- - - - - - - - - - - + +

Sorry, No one answered this question.

@@ -86,41 +91,36 @@