2013-11-05 10:36:54 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
##############################################################################
|
|
|
|
#
|
|
|
|
# OpenERP, Open Source Management Solution
|
|
|
|
# Copyright (C) 2013-Today OpenERP SA (<http://www.openerp.com>).
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as
|
|
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
|
|
# License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Affero General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
#
|
|
|
|
##############################################################################
|
|
|
|
|
2013-11-15 15:14:35 +00:00
|
|
|
import json
|
2013-11-08 07:53:47 +00:00
|
|
|
import logging
|
2014-03-03 13:05:44 +00:00
|
|
|
import werkzeug
|
|
|
|
from datetime import datetime
|
|
|
|
from math import ceil
|
|
|
|
|
|
|
|
from openerp import SUPERUSER_ID
|
2014-01-21 07:27:42 +00:00
|
|
|
from openerp.addons.web import http
|
|
|
|
from openerp.addons.web.http import request
|
2014-03-03 13:05:44 +00:00
|
|
|
from openerp.tools.misc import DEFAULT_SERVER_DATETIME_FORMAT as DTF
|
|
|
|
from openerp.tools.safe_eval import safe_eval
|
2013-11-08 07:53:47 +00:00
|
|
|
|
2013-12-12 11:04:35 +00:00
|
|
|
|
2013-11-08 07:53:47 +00:00
|
|
|
_logger = logging.getLogger(__name__)
|
2013-11-05 10:36:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
class WebsiteSurvey(http.Controller):
|
|
|
|
|
2013-12-12 11:04:35 +00:00
|
|
|
## HELPER METHODS ##
|
|
|
|
|
|
|
|
def _check_bad_cases(self, cr, uid, request, survey_obj, survey, user_input_obj, context=None):
|
|
|
|
# In case of bad survey, redirect to surveys list
|
|
|
|
if survey_obj.exists(cr, SUPERUSER_ID, survey.id, context=context) == []:
|
|
|
|
return werkzeug.utils.redirect("/survey/")
|
|
|
|
|
|
|
|
# In case of auth required, block public user
|
|
|
|
if survey.auth_required and uid == request.registry['website'].get_public_user(cr, uid, context):
|
2014-04-09 08:52:16 +00:00
|
|
|
return request.website.render("website.403")
|
2013-12-12 11:04:35 +00:00
|
|
|
|
|
|
|
# In case of non open surveys
|
2014-04-09 08:52:16 +00:00
|
|
|
if survey.stage_id.closed:
|
2013-12-12 11:04:35 +00:00
|
|
|
return request.website.render("survey.notopen")
|
|
|
|
|
2014-04-10 14:30:18 +00:00
|
|
|
# If there is no pages
|
|
|
|
if not survey.page_ids:
|
|
|
|
return request.website.render("survey.nopages")
|
|
|
|
|
2013-12-12 11:04:35 +00:00
|
|
|
# Everything seems to be ok
|
|
|
|
return None
|
|
|
|
|
|
|
|
def _check_deadline(self, cr, uid, user_input, context=None):
|
|
|
|
'''Prevent opening of the survey if the deadline has turned out
|
|
|
|
|
|
|
|
! This will NOT disallow access to users who have already partially filled the survey !'''
|
|
|
|
if user_input.deadline:
|
|
|
|
dt_deadline = datetime.strptime(user_input.deadline, DTF)
|
|
|
|
dt_now = datetime.now()
|
|
|
|
if dt_now > dt_deadline: # survey is not open anymore
|
|
|
|
return request.website.render("survey.notopen")
|
|
|
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
## ROUTES HANDLERS ##
|
|
|
|
|
2013-11-26 15:17:26 +00:00
|
|
|
# Survey start
|
2014-01-21 07:27:42 +00:00
|
|
|
@http.route(['/survey/start/<model("survey.survey"):survey>',
|
|
|
|
'/survey/start/<model("survey.survey"):survey>/<string:token>'],
|
|
|
|
type='http', auth='public', multilang=True, website=True)
|
2013-11-26 15:17:26 +00:00
|
|
|
def start_survey(self, survey, token=None, **post):
|
|
|
|
cr, uid, context = request.cr, request.uid, request.context
|
|
|
|
survey_obj = request.registry['survey.survey']
|
|
|
|
user_input_obj = request.registry['survey.user_input']
|
|
|
|
|
2014-01-28 14:51:38 +00:00
|
|
|
# Test mode
|
|
|
|
if token and token == "phantom":
|
2014-04-09 10:03:13 +00:00
|
|
|
_logger.info("[survey] Phantom mode")
|
2014-01-28 14:51:38 +00:00
|
|
|
user_input_id = user_input_obj.create(cr, uid, {'survey_id': survey.id, 'test_entry': True}, context=context)
|
|
|
|
user_input = user_input_obj.browse(cr, uid, [user_input_id], context=context)[0]
|
|
|
|
data = {'survey': survey, 'page': None, 'token': user_input.token}
|
|
|
|
return request.website.render('survey.survey_init', data)
|
|
|
|
# END Test mode
|
|
|
|
|
2013-12-12 11:04:35 +00:00
|
|
|
# Controls if the survey can be displayed
|
|
|
|
errpage = self._check_bad_cases(cr, uid, request, survey_obj, survey, user_input_obj, context=context)
|
|
|
|
if errpage:
|
|
|
|
return errpage
|
2013-11-26 15:17:26 +00:00
|
|
|
|
|
|
|
# Manual surveying
|
|
|
|
if not token:
|
2014-04-09 08:52:16 +00:00
|
|
|
user_input_id = user_input_obj.create(cr, uid, {'survey_id': survey.id}, context=context)
|
|
|
|
user_input = user_input_obj.browse(cr, uid, [user_input_id], context=context)[0]
|
2013-11-26 15:17:26 +00:00
|
|
|
else:
|
|
|
|
try:
|
2014-01-28 14:52:17 +00:00
|
|
|
user_input_id = user_input_obj.search(cr, uid, [('token', '=', token)], context=context)[0]
|
2013-11-26 15:17:26 +00:00
|
|
|
except IndexError: # Invalid token
|
|
|
|
return request.website.render("website.403")
|
|
|
|
else:
|
|
|
|
user_input = user_input_obj.browse(cr, uid, [user_input_id], context=context)[0]
|
|
|
|
|
2013-12-12 11:04:35 +00:00
|
|
|
# Do not open expired survey
|
|
|
|
errpage = self._check_deadline(cr, uid, user_input, context=context)
|
|
|
|
if errpage:
|
|
|
|
return errpage
|
2013-11-26 15:17:26 +00:00
|
|
|
|
|
|
|
# Select the right page
|
|
|
|
if user_input.state == 'new': # Intro page
|
|
|
|
data = {'survey': survey, 'page': None, 'token': user_input.token}
|
|
|
|
return request.website.render('survey.survey_init', data)
|
|
|
|
else:
|
|
|
|
return request.redirect('/survey/fill/%s/%s' % (survey.id, user_input.token))
|
|
|
|
|
2013-11-20 15:13:01 +00:00
|
|
|
# Survey displaying
|
2014-01-21 07:27:42 +00:00
|
|
|
@http.route(['/survey/fill/<model("survey.survey"):survey>/<string:token>',
|
|
|
|
'/survey/fill/<model("survey.survey"):survey>/<string:token>/<string:prev>'],
|
|
|
|
type='http', auth='public', multilang=True, website=True)
|
2013-12-12 11:04:35 +00:00
|
|
|
def fill_survey(self, survey, token, prev=None, **post):
|
2013-11-08 07:53:47 +00:00
|
|
|
'''Display and validates a survey'''
|
2013-11-05 15:28:21 +00:00
|
|
|
cr, uid, context = request.cr, request.uid, request.context
|
|
|
|
survey_obj = request.registry['survey.survey']
|
2013-11-20 15:13:01 +00:00
|
|
|
user_input_obj = request.registry['survey.user_input']
|
2013-11-06 07:33:53 +00:00
|
|
|
|
2013-12-12 11:04:35 +00:00
|
|
|
# Controls if the survey can be displayed
|
|
|
|
errpage = self._check_bad_cases(cr, uid, request, survey_obj, survey, user_input_obj, context=context)
|
|
|
|
if errpage:
|
|
|
|
return errpage
|
2013-11-20 15:13:01 +00:00
|
|
|
|
2013-12-12 11:04:35 +00:00
|
|
|
# Load the user_input
|
|
|
|
try:
|
|
|
|
user_input_id = user_input_obj.search(cr, uid, [('token', '=', token)])[0]
|
|
|
|
except IndexError: # Invalid token
|
|
|
|
return request.website.render("website.403")
|
2013-11-20 16:15:04 +00:00
|
|
|
else:
|
2013-12-12 11:04:35 +00:00
|
|
|
user_input = user_input_obj.browse(cr, uid, [user_input_id], context=context)[0]
|
2013-11-20 15:13:01 +00:00
|
|
|
|
2013-12-12 11:04:35 +00:00
|
|
|
# Do not display expired survey (even if some pages have already been
|
|
|
|
# displayed -- There's a time for everything!)
|
|
|
|
errpage = self._check_deadline(cr, uid, user_input, context=context)
|
|
|
|
if errpage:
|
|
|
|
return errpage
|
2013-11-08 07:53:47 +00:00
|
|
|
|
2013-11-25 08:04:46 +00:00
|
|
|
# Select the right page
|
|
|
|
if user_input.state == 'new': # First page
|
2013-12-12 11:04:35 +00:00
|
|
|
page, page_nr, last = survey_obj.next_page(cr, uid, user_input, 0, go_back=False, context=context)
|
2013-11-25 08:04:46 +00:00
|
|
|
data = {'survey': survey, 'page': page, 'page_nr': page_nr, 'token': user_input.token}
|
|
|
|
if last:
|
|
|
|
data.update({'last': True})
|
|
|
|
return request.website.render('survey.survey', data)
|
|
|
|
elif user_input.state == 'done': # Display success message
|
2013-12-24 15:59:40 +00:00
|
|
|
return request.website.render('survey.sfinished', {'survey': survey,
|
2014-02-05 08:16:26 +00:00
|
|
|
'token': token,
|
|
|
|
'user_input': user_input})
|
2013-11-25 08:04:46 +00:00
|
|
|
elif user_input.state == 'skip':
|
2013-12-12 11:04:35 +00:00
|
|
|
flag = (True if prev and prev == 'prev' else False)
|
|
|
|
page, page_nr, last = survey_obj.next_page(cr, uid, user_input, user_input.last_displayed_page_id.id, go_back=flag, context=context)
|
2013-11-28 07:44:18 +00:00
|
|
|
data = {'survey': survey, 'page': page, 'page_nr': page_nr, 'token': user_input.token}
|
2013-11-25 08:04:46 +00:00
|
|
|
if last:
|
|
|
|
data.update({'last': True})
|
2013-12-12 11:04:35 +00:00
|
|
|
return request.website.render('survey.survey', data)
|
2013-11-25 08:04:46 +00:00
|
|
|
else:
|
|
|
|
return request.website.render("website.403")
|
2013-11-06 16:11:17 +00:00
|
|
|
|
2013-12-12 11:04:35 +00:00
|
|
|
# AJAX prefilling of a survey
|
2014-01-21 07:27:42 +00:00
|
|
|
@http.route(['/survey/prefill/<model("survey.survey"):survey>/<string:token>',
|
|
|
|
'/survey/prefill/<model("survey.survey"):survey>/<string:token>/<model("survey.page"):page>'],
|
|
|
|
type='http', auth='public', multilang=True, website=True)
|
2013-12-16 15:32:44 +00:00
|
|
|
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
|
2014-04-11 12:15:41 +00:00
|
|
|
elif answer.answer_type == 'text' and answer.question_id.type == 'textbox':
|
|
|
|
answer_value = answer.value_text
|
|
|
|
elif answer.answer_type == 'text' and answer.question_id.type != 'textbox':
|
|
|
|
# here come comment answers for matrices, simple choice and multiple choice
|
|
|
|
answer_tag = "%s_%s" % (answer_tag, 'comment')
|
2013-12-16 15:32:44 +00:00
|
|
|
answer_value = answer.value_text
|
|
|
|
elif answer.answer_type == 'number':
|
2013-12-16 16:09:08 +00:00
|
|
|
answer_value = answer.value_number.__str__()
|
2013-12-16 15:32:44 +00:00
|
|
|
elif answer.answer_type == 'date':
|
|
|
|
answer_value = answer.value_date
|
2013-12-20 09:58:45 +00:00
|
|
|
elif answer.answer_type == 'suggestion' and not answer.value_suggested_row:
|
2013-12-18 07:20:04 +00:00
|
|
|
answer_value = answer.value_suggested.id
|
2013-12-20 09:58:45 +00:00
|
|
|
elif answer.answer_type == 'suggestion' and answer.value_suggested_row:
|
2013-12-24 13:23:09 +00:00
|
|
|
answer_tag = "%s_%s" % (answer_tag, answer.value_suggested_row.id)
|
2013-12-20 09:58:45 +00:00
|
|
|
answer_value = answer.value_suggested.id
|
2013-12-16 15:32:44 +00:00
|
|
|
if answer_value:
|
2013-12-24 13:23:09 +00:00
|
|
|
dict_soft_update(ret, answer_tag, answer_value)
|
2013-12-16 15:32:44 +00:00
|
|
|
else:
|
2013-12-24 13:23:09 +00:00
|
|
|
_logger.warning("[survey] No answer has been found for question %s marked as non skipped" % answer_tag)
|
2013-12-16 15:32:44 +00:00
|
|
|
return json.dumps(ret)
|
|
|
|
|
2014-04-16 15:13:20 +00:00
|
|
|
# AJAX scores loading for quiz correction mode
|
|
|
|
@http.route(['/survey/scores/<model("survey.survey"):survey>/<string:token>'],
|
|
|
|
type='http', auth='public', multilang=True, website=True)
|
|
|
|
def get_scores(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 answers
|
|
|
|
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)
|
|
|
|
|
|
|
|
# Compute score for each question
|
|
|
|
for answer in previous_answers:
|
|
|
|
tmp_score = ret.get(answer.question_id.id, 0.0)
|
|
|
|
ret.update({answer.question_id.id: tmp_score + answer.quizz_mark})
|
|
|
|
return json.dumps(ret)
|
|
|
|
|
2013-12-12 11:04:35 +00:00
|
|
|
# AJAX submission of a page
|
2014-01-21 07:27:42 +00:00
|
|
|
@http.route(['/survey/submit/<model("survey.survey"):survey>'],
|
|
|
|
type='http', auth='public', multilang=True, website=True)
|
2013-11-15 15:14:35 +00:00
|
|
|
def submit(self, survey, **post):
|
2013-11-20 15:13:01 +00:00
|
|
|
_logger.debug('Incoming data: %s', post)
|
2013-11-25 08:04:46 +00:00
|
|
|
page_id = int(post['page_id'])
|
|
|
|
cr, uid, context = request.cr, request.uid, request.context
|
2013-12-12 11:04:35 +00:00
|
|
|
survey_obj = request.registry['survey.survey']
|
2013-11-25 08:04:46 +00:00
|
|
|
questions_obj = request.registry['survey.question']
|
2013-11-28 07:44:18 +00:00
|
|
|
questions_ids = questions_obj.search(cr, uid, [('page_id', '=', page_id)], context=context)
|
2013-11-25 08:04:46 +00:00
|
|
|
questions = questions_obj.browse(cr, uid, questions_ids, context=context)
|
2013-11-14 07:45:55 +00:00
|
|
|
|
2013-11-28 07:44:18 +00:00
|
|
|
# Answer validation
|
2013-11-15 15:14:35 +00:00
|
|
|
errors = {}
|
|
|
|
for question in questions:
|
2013-11-25 08:04:46 +00:00
|
|
|
answer_tag = "%s_%s_%s" % (survey.id, page_id, question.id)
|
2013-11-29 09:27:32 +00:00
|
|
|
errors.update(questions_obj.validate_question(cr, uid, question, post, answer_tag, context=context))
|
2013-11-14 07:45:55 +00:00
|
|
|
|
2013-11-15 15:14:35 +00:00
|
|
|
ret = {}
|
|
|
|
if (len(errors) != 0):
|
2013-11-28 07:44:18 +00:00
|
|
|
# Return errors messages to webpage
|
2013-11-15 15:14:35 +00:00
|
|
|
ret['errors'] = errors
|
|
|
|
else:
|
2013-11-28 07:44:18 +00:00
|
|
|
# Store answers into database
|
2013-11-25 08:04:46 +00:00
|
|
|
user_input_obj = request.registry['survey.user_input']
|
2013-12-12 11:04:35 +00:00
|
|
|
|
2013-11-29 16:10:32 +00:00
|
|
|
user_input_line_obj = request.registry['survey.user_input_line']
|
2013-11-25 08:04:46 +00:00
|
|
|
try:
|
2013-11-28 07:44:18 +00:00
|
|
|
user_input_id = user_input_obj.search(cr, uid, [('token', '=', post['token'])], context=context)[0]
|
2013-11-26 15:17:26 +00:00
|
|
|
except KeyError: # Invalid token
|
2013-11-25 08:04:46 +00:00
|
|
|
return request.website.render("website.403")
|
2013-11-28 07:44:18 +00:00
|
|
|
for question in questions:
|
|
|
|
answer_tag = "%s_%s_%s" % (survey.id, page_id, question.id)
|
2013-11-29 16:10:32 +00:00
|
|
|
user_input_line_obj.save_lines(cr, uid, user_input_id, question, post, answer_tag, context=context)
|
|
|
|
|
2013-12-12 11:04:35 +00:00
|
|
|
user_input = user_input_obj.browse(cr, uid, user_input_id, context=context)
|
|
|
|
go_back = post['button_submit'] == 'previous'
|
|
|
|
next_page, _, last = survey_obj.next_page(cr, uid, user_input, page_id, go_back=go_back, context=context)
|
|
|
|
vals = {'last_displayed_page_id': page_id}
|
|
|
|
if next_page is None and not go_back:
|
|
|
|
vals.update({'state': 'done'})
|
|
|
|
else:
|
|
|
|
vals.update({'state': 'skip'})
|
|
|
|
user_input_obj.write(cr, uid, user_input_id, vals, context=context)
|
2013-11-26 15:17:26 +00:00
|
|
|
ret['redirect'] = '/survey/fill/%s/%s' % (survey.id, post['token'])
|
2013-12-12 11:04:35 +00:00
|
|
|
if go_back:
|
|
|
|
ret['redirect'] += '/prev'
|
2013-11-15 15:14:35 +00:00
|
|
|
return json.dumps(ret)
|
2013-11-15 07:51:50 +00:00
|
|
|
|
|
|
|
# Printing routes
|
2014-03-19 14:29:52 +00:00
|
|
|
@http.route(['/survey/print/<model("survey.survey"):survey>',
|
|
|
|
'/survey/print/<model("survey.survey"):survey>/<string:token>'],
|
2014-04-17 14:26:48 +00:00
|
|
|
type='http', auth='public', multilang=True, website=True)
|
2013-12-20 09:58:45 +00:00
|
|
|
def print_survey(self, survey, token=None, **post):
|
|
|
|
'''Display an survey in printable view; if <token> is set, it will
|
|
|
|
grab the answers of the user_input_id that has <token>.'''
|
2013-11-06 16:11:17 +00:00
|
|
|
return request.website.render('survey.survey_print',
|
2013-12-24 15:59:40 +00:00
|
|
|
{'survey': survey,
|
|
|
|
'token': token,
|
2014-04-16 10:11:29 +00:00
|
|
|
'page_nr': 0,
|
|
|
|
'quizz_correction': True if survey.quizz_mode and token else False})
|
2013-11-15 15:14:35 +00:00
|
|
|
|
2014-03-03 13:05:44 +00:00
|
|
|
@http.route(['/survey/results/<model("survey.survey"):survey>'],
|
|
|
|
type='http', auth='user', multilang=True, website=True)
|
2014-01-23 11:21:07 +00:00
|
|
|
def survey_reporting(self, survey, token=None, **post):
|
2014-03-03 13:05:44 +00:00
|
|
|
'''Display survey Results & Statistics for given survey.'''
|
2014-03-27 06:43:05 +00:00
|
|
|
result_template, current_filters, filter_display_data, filter_finish = 'survey.result', [], [], False
|
2014-03-28 13:37:06 +00:00
|
|
|
survey_obj = request.registry['survey.survey']
|
2014-02-04 09:51:57 +00:00
|
|
|
if not survey.user_input_ids or not [input_id.id for input_id in survey.user_input_ids if input_id.state != 'new']:
|
2014-03-03 13:05:44 +00:00
|
|
|
result_template = 'survey.no_result'
|
2014-03-28 13:37:06 +00:00
|
|
|
if 'finished' in post:
|
2014-03-27 06:43:05 +00:00
|
|
|
post.pop('finished')
|
|
|
|
filter_finish = True
|
2014-03-28 13:37:06 +00:00
|
|
|
if post or filter_finish:
|
|
|
|
filter_data = self.get_filter_data(post)
|
|
|
|
current_filters = survey_obj.filter_input_ids(request.cr, request.uid, filter_data, filter_finish, context=request.context)
|
|
|
|
filter_display_data = survey_obj.get_filter_display_data(request.cr, request.uid, filter_data, context=request.context)
|
2014-03-03 13:05:44 +00:00
|
|
|
return request.website.render(result_template,
|
2014-03-25 13:08:48 +00:00
|
|
|
{'survey_dict': self.prepare_result_dict(survey, current_filters),
|
2014-03-03 13:05:44 +00:00
|
|
|
'page_range': self.page_range,
|
|
|
|
'current_filters': current_filters,
|
2014-03-27 06:43:05 +00:00
|
|
|
'filter_display_data': filter_display_data,
|
|
|
|
'filter_finish': filter_finish
|
2014-01-23 11:21:07 +00:00
|
|
|
})
|
2014-03-03 13:05:44 +00:00
|
|
|
|
2014-03-25 13:08:48 +00:00
|
|
|
def prepare_result_dict(self,survey, current_filters=[]):
|
2014-04-03 10:46:37 +00:00
|
|
|
"""Returns dictionary having values for rendering template"""
|
|
|
|
survey_obj = request.registry['survey.survey']
|
2014-03-25 13:08:48 +00:00
|
|
|
result = {'survey':survey, 'page_ids': []}
|
|
|
|
for page in survey.page_ids:
|
|
|
|
page_dict = {'page': page, 'question_ids': []}
|
|
|
|
for question in page.question_ids:
|
2014-04-04 05:19:13 +00:00
|
|
|
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), 'graph_data': self.get_graph_data(question, current_filters)}
|
2014-03-25 13:08:48 +00:00
|
|
|
page_dict['question_ids'].append(question_dict)
|
|
|
|
result['page_ids'].append(page_dict)
|
|
|
|
return result
|
2014-03-03 13:05:44 +00:00
|
|
|
|
2014-03-28 13:37:06 +00:00
|
|
|
def get_filter_data(self, post):
|
2014-04-03 10:46:37 +00:00
|
|
|
"""Returns data used for filtering the result"""
|
2014-03-28 13:37:06 +00:00
|
|
|
filters = []
|
2014-04-03 11:54:23 +00:00
|
|
|
for ids in post:
|
2014-04-08 13:59:08 +00:00
|
|
|
#if user add some random data in query URI, ignore it
|
2014-04-03 11:54:23 +00:00
|
|
|
try:
|
2014-03-03 13:05:44 +00:00
|
|
|
row_id, answer_id = ids.split(',')
|
2014-03-28 13:37:06 +00:00
|
|
|
filters.append({'row_id': int(row_id), 'answer_id': int(answer_id)})
|
2014-04-03 11:54:23 +00:00
|
|
|
except:
|
|
|
|
return filters
|
2014-03-28 13:37:06 +00:00
|
|
|
return filters
|
2014-01-23 11:21:07 +00:00
|
|
|
|
2014-02-04 09:51:57 +00:00
|
|
|
def page_range(self, total_record, limit):
|
2014-01-29 12:48:12 +00:00
|
|
|
'''Returns number of pages required for pagination'''
|
2014-03-03 13:05:44 +00:00
|
|
|
total = ceil(total_record / float(limit))
|
|
|
|
return range(1, int(total + 1))
|
2014-01-23 11:21:07 +00:00
|
|
|
|
2014-04-04 05:19:13 +00:00
|
|
|
def get_graph_data(self, question, current_filters=[]):
|
2014-04-08 13:59:08 +00:00
|
|
|
'''Returns formatted data required by graph library on basis of filter'''
|
2014-04-03 10:46:37 +00:00
|
|
|
survey_obj = request.registry['survey.survey']
|
2014-01-23 11:21:07 +00:00
|
|
|
result = []
|
2014-03-26 05:16:38 +00:00
|
|
|
if question.type == 'multiple_choice':
|
2014-03-03 13:05:44 +00:00
|
|
|
result.append({'key': str(question.question),
|
2014-04-03 10:46:37 +00:00
|
|
|
'values': survey_obj.prepare_result(request.cr, request.uid, question, current_filters, context=request.context)})
|
2014-03-26 05:16:38 +00:00
|
|
|
if question.type == 'simple_choice':
|
2014-04-03 10:46:37 +00:00
|
|
|
result = survey_obj.prepare_result(request.cr, request.uid, question, current_filters, context=request.context)
|
2014-01-23 11:21:07 +00:00
|
|
|
if question.type == 'matrix':
|
2014-04-03 10:46:37 +00:00
|
|
|
data = survey_obj.prepare_result(request.cr, request.uid, question, current_filters, context=request.context)
|
2014-01-23 11:21:07 +00:00
|
|
|
for answer in data['answers']:
|
|
|
|
values = []
|
|
|
|
for res in data['result']:
|
|
|
|
if res[1] == answer:
|
|
|
|
values.append({'text': data['rows'][res[0]], 'count': data['result'][res]})
|
2014-03-03 13:05:44 +00:00
|
|
|
result.append({'key': data['answers'].get(answer), 'values': values})
|
2014-01-23 11:21:07 +00:00
|
|
|
return json.dumps(result)
|
2014-03-03 13:05:44 +00:00
|
|
|
|
2013-12-20 09:58:45 +00:00
|
|
|
def dict_soft_update(dictionary, key, value):
|
2013-12-24 13:42:15 +00:00
|
|
|
''' Insert the pair <key>: <value> into the <dictionary>. If <key> is
|
|
|
|
already present, this function will append <value> to the list of
|
|
|
|
existing data (instead of erasing it) '''
|
2013-12-20 09:58:45 +00:00
|
|
|
if key in dictionary:
|
|
|
|
dictionary[key].append(value)
|
|
|
|
else:
|
|
|
|
dictionary.update({key: [value]})
|