[IMP] Mandatory questions
bzr revid: rim@openerp.com-20131113135532-igwkncnhsvr74msj
This commit is contained in:
parent
29903b2dac
commit
12ff7758a0
|
@ -60,39 +60,18 @@ class WebsiteSurvey(http.Controller):
|
|||
|
||||
_logger.debug('Post request data: %s', post)
|
||||
|
||||
# Answer validation and storage
|
||||
problems = []
|
||||
if post and int(post['current']) != -1:
|
||||
for question in survey.page_ids[int(post['current'])].question_ids:
|
||||
|
||||
# validation methods are dynamically loaded, if defined for the
|
||||
# question type
|
||||
#
|
||||
# eg: if question type is 'new_question_type', this will try to
|
||||
# load validate_new_question_type
|
||||
|
||||
try:
|
||||
checker = getattr(self, 'validate_' + question.type)
|
||||
except AttributeError:
|
||||
_logger.warning(question.type + ": This type of question has no validation method")
|
||||
else:
|
||||
problems = problems + checker(survey.id, post['current'], question, post)
|
||||
|
||||
# Return the same survey page if problems occur
|
||||
if problems:
|
||||
_logger.debug('Problems in the survey: %s', problems)
|
||||
pagination = {'current': int(post['current']), 'next': int(post['next'])}
|
||||
return request.website.render('survey.survey',
|
||||
{'survey': survey,
|
||||
'pagination': pagination,
|
||||
'problems': problems})
|
||||
# Store answer data
|
||||
# (/!\ assumes JavaScript validation of answers has succeeded!)
|
||||
# TODO
|
||||
|
||||
# Display success message if totally succeeded
|
||||
if post and post['next'] == "finished":
|
||||
return request.website.render('survey.finished')
|
||||
|
||||
# Page selection
|
||||
pagination = {'current': -1, 'next': 0} # Default pagination if first opening
|
||||
pagination = {'current': -1, 'next': 0}
|
||||
# Default pagination if first opening
|
||||
|
||||
if 'current' in post and 'next' in post and post['next'] != "finished":
|
||||
oldnext = int(post['next'])
|
||||
if oldnext not in range(0, len(survey.page_ids)):
|
||||
|
@ -106,8 +85,7 @@ class WebsiteSurvey(http.Controller):
|
|||
|
||||
return request.website.render('survey.survey',
|
||||
{'survey': survey,
|
||||
'pagination': pagination,
|
||||
'problems': None})
|
||||
'pagination': pagination})
|
||||
|
||||
@website.route(['/survey/print/<model("survey.survey"):survey>/'],
|
||||
type='http', auth='public', multilang=True)
|
||||
|
@ -117,95 +95,3 @@ class WebsiteSurvey(http.Controller):
|
|||
return request.website.render('survey.survey_print',
|
||||
{'survey': survey,
|
||||
'pagination': pagination})
|
||||
|
||||
def validate_free_text(self, survey_id, page_nr, question, post):
|
||||
answer_tag = survey_id.__str__() + '--' + page_nr + '--' + question.id.__str__()
|
||||
problems = []
|
||||
if question.constr_mandatory:
|
||||
problems = problems + self.__has_empty_input(question, post, answer_tag)
|
||||
return problems
|
||||
|
||||
def validate_textbox(self, survey_id, page_nr, question, post):
|
||||
answer_tag = survey_id.__str__() + '--' + page_nr + '--' + question.id.__str__()
|
||||
problems = []
|
||||
if question.constr_mandatory:
|
||||
problems = problems + self.__has_empty_input(question, post, answer_tag)
|
||||
return problems
|
||||
|
||||
def validate_numerical_box(self, survey_id, page_nr, question, post):
|
||||
answer_tag = survey_id.__str__() + '--' + page_nr + '--' + question.id.__str__()
|
||||
# Check for an empty input
|
||||
problems = []
|
||||
if question.constr_mandatory:
|
||||
problems = problems + self.__has_empty_input(question, post, answer_tag)
|
||||
# Check for a non number input
|
||||
if answer_tag in post and post[answer_tag].strip() != "":
|
||||
try:
|
||||
float(post[answer_tag].strip())
|
||||
except ValueError:
|
||||
problems = problems + [{'qlabel': question.question,
|
||||
'errmsg': "This is not a number"}]
|
||||
|
||||
return problems
|
||||
|
||||
def validate_datetime(self, survey_id, page_nr, question, post):
|
||||
answer_tag = survey_id.__str__() + '--' + page_nr + '--' + question.id.__str__()
|
||||
problems = []
|
||||
if question.constr_mandatory:
|
||||
problems = problems + self.__has_empty_input(question, post, answer_tag)
|
||||
|
||||
# TODO CHECK, check if this is a date
|
||||
|
||||
return problems
|
||||
|
||||
def validate_simple_choice_scale(self, survey_id, page_nr, question, post):
|
||||
answer_tag = survey_id.__str__() + '--' + page_nr + '--' + question.id.__str__()
|
||||
problems = []
|
||||
if question.constr_mandatory:
|
||||
problems = problems + self.__has_empty_input(question, post, answer_tag)
|
||||
return problems
|
||||
|
||||
def validate_simple_choice_dropdown(self, survey_id, page_nr, question, post):
|
||||
answer_tag = survey_id.__str__() + '--' + page_nr + '--' + question.id.__str__()
|
||||
problems = []
|
||||
if question.constr_mandatory:
|
||||
problems = problems + self.__has_empty_input(question, post, answer_tag)
|
||||
return problems
|
||||
|
||||
def validate_multiple_choice(self, survey_id, page_nr, question, post):
|
||||
answer_tag = survey_id.__str__() + '--' + page_nr + '--' + question.id.__str__()
|
||||
problems = []
|
||||
if question.constr_mandatory:
|
||||
answers = dict_keys_startswith(post, answer_tag)
|
||||
print(answers)
|
||||
if not answers:
|
||||
return [{'qlabel': question.question,
|
||||
'errmsg': "This question is mandatory"}]
|
||||
return problems
|
||||
|
||||
# def validate_vector(self, survey_id, page_nr, question, post):
|
||||
# answer_tag = survey_id.__str__() + '--' + page_nr + '--' + question.id.__str__()
|
||||
# problems = []
|
||||
# return problems
|
||||
|
||||
# def validate_matrix(self, survey_id, page_nr, question, post):
|
||||
# answer_tag = survey_id.__str__() + '--' + page_nr + '--' + question.id.__str__()
|
||||
# problems = []
|
||||
# return problems
|
||||
|
||||
def __has_empty_input(self, question, post, answer_tag):
|
||||
''' Check for an empty input '''
|
||||
if answer_tag not in post or post[answer_tag].strip() == "":
|
||||
return [{'qlabel': question.question,
|
||||
'errmsg': "This question is mandatory"}]
|
||||
else:
|
||||
return []
|
||||
|
||||
|
||||
def dict_keys_startswith(dictionary, string):
|
||||
'''Returns a dictionary containing the elements of <dict> whose keys start
|
||||
with <string>.
|
||||
|
||||
.. note::
|
||||
This function uses dictionary comprehensions (Python >= 2.7)'''
|
||||
return {k: dictionary[k] for k in filter(lambda key: key.startswith(string), dictionary.keys())}
|
||||
|
|
|
@ -1,9 +1,120 @@
|
|||
$(document).ready(function () {
|
||||
console.debug("[survey] Custom JS for survey loaded");
|
||||
$('.survey-btn').click(
|
||||
function(){
|
||||
console.debug("[survey] Page button clicked");
|
||||
|
||||
// startsWith compatibility patch with old browsers
|
||||
if (typeof String.prototype.startsWith != 'function') {
|
||||
String.prototype.startsWith = function (str){
|
||||
return this.slice(0, str.length) == str;
|
||||
};
|
||||
}
|
||||
);
|
||||
|
||||
// function that display error messages
|
||||
var display_form_error = function (question_id, err_msg){
|
||||
$('#survey_' + question_id + '>.js_errzone').append('<p>' + err_msg + '</p>').show();
|
||||
};
|
||||
|
||||
//$('.js_errzone').hide();
|
||||
|
||||
console.debug("[survey] Custom JS for survey loaded");
|
||||
|
||||
// for each question
|
||||
// check if answer exists and is not empty and is mandatory
|
||||
// check if answer type is ok
|
||||
// check if number of answers is ok
|
||||
// check if answer has good qualities
|
||||
|
||||
|
||||
$('form').submit(
|
||||
function(){
|
||||
console.debug("[survey] Submit button clicked");
|
||||
var answers = $('form').serializeArray();
|
||||
var valid = true;
|
||||
|
||||
$('.js_errzone').html("").hide();
|
||||
|
||||
$('.question-wrapper').each(
|
||||
function(){
|
||||
var qparams = jQuery.parseJSON($(this).attr('data-oe-survey-qparams'));
|
||||
var question_id = $(this).attr('id').slice(7); //all the ids of question containers start with "survey_"
|
||||
var candidate_answers = _.filter(answers, function(item){return item.name.startsWith(question_id);});
|
||||
var answer_value;
|
||||
|
||||
if(qparams.type === 'free_text'){
|
||||
answer_value = candidate_answers[0].value.trim();
|
||||
if(qparams.constr_mandatory === true && answer_value === ''){
|
||||
valid = false;
|
||||
display_form_error(question_id, "This question is mandatory");
|
||||
}
|
||||
|
||||
|
||||
} else if (qparams.type === 'textbox'){
|
||||
answer_value = candidate_answers[0].value.trim();
|
||||
if(qparams.constr_mandatory === true && answer_value === ''){
|
||||
valid = false;
|
||||
display_form_error(question_id, "This question is mandatory");
|
||||
}
|
||||
|
||||
|
||||
} else if (qparams.type === 'numerical_box'){
|
||||
answer_value = candidate_answers[0].value.trim();
|
||||
if(qparams.constr_mandatory === true && answer_value === ''){
|
||||
valid = false;
|
||||
display_form_error(question_id, "This question is mandatory");
|
||||
|
||||
}
|
||||
if(answer_value !== ''){
|
||||
if(_.isNaN(parseFloat(answer_value))){
|
||||
valid = false;
|
||||
display_form_error(question_id, "You must answer with a number");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
} else if (qparams.type === 'datetime'){
|
||||
answer_value = candidate_answers[0].value.trim();
|
||||
if(qparams.constr_mandatory === true && answer_value === ''){
|
||||
valid = false;
|
||||
display_form_error(question_id, "This question is mandatory");
|
||||
}
|
||||
if(answer_value !== ''){
|
||||
console.warning("Date format validation is not yet implemented");
|
||||
}
|
||||
|
||||
} else if (qparams.type === 'simple_choice_scale' || qparams.type === 'simple_choice_dropdown'){
|
||||
if(qparams.constr_mandatory === true && _.size(candidate_answers) === 0){
|
||||
valid = false;
|
||||
display_form_error(question_id, "This question is mandatory");
|
||||
}
|
||||
// if answers:
|
||||
// answer_value = candidate_answers[0].value;
|
||||
|
||||
|
||||
|
||||
} else if (qparams.type === 'multiple_choice'){
|
||||
var answ_nbr = _.size(candidate_answers);
|
||||
if(qparams.constr_mandatory === true && answ_nbr <= 0){
|
||||
valid = false;
|
||||
display_form_error(question_id, "This question is mandatory");
|
||||
}
|
||||
|
||||
|
||||
// } else if (qparams.type === 'vector'){
|
||||
|
||||
|
||||
|
||||
// } else if (qparams.type === 'matrix'){
|
||||
|
||||
|
||||
|
||||
} else {
|
||||
console.warning('Not supported question type!');
|
||||
}
|
||||
|
||||
|
||||
});
|
||||
|
||||
return valid;
|
||||
|
||||
// _.filter(answers, function(item){return item.name.startsWith(question_id);})
|
||||
});
|
||||
|
||||
});
|
|
@ -23,6 +23,7 @@ from urlparse import urljoin
|
|||
from openerp.osv import fields, osv
|
||||
from openerp.tools.translate import _
|
||||
import uuid
|
||||
import json
|
||||
|
||||
|
||||
class survey_survey(osv.osv):
|
||||
|
@ -376,8 +377,8 @@ class survey_question(osv.osv):
|
|||
'validation_max_float_value': fields.float('Maximum value'),
|
||||
'validation_min_int_value': fields.integer('Minimum value'),
|
||||
'validation_max_int_value': fields.integer('Maximum value'),
|
||||
#'validation_min_date': fields.date('Start date range'),
|
||||
#'validation_max_date': fields.date('End date range'),
|
||||
'validation_min_date': fields.date('Start date range'),
|
||||
'validation_max_date': fields.date('End date range'),
|
||||
'validation_error_msg': fields.char("Error message if validation \
|
||||
fails", oldname='validation_valid_err_msg'),
|
||||
|
||||
|
|
|
@ -142,6 +142,8 @@
|
|||
<field name="visible_to_user"/>
|
||||
<field name="public_url" widget="url"/>
|
||||
<field name="auth_required" />
|
||||
<field name="date_open" />
|
||||
<field name="date_close" />
|
||||
</group>
|
||||
</group>
|
||||
<field name="page_ids" colspan="4" mode="tree" attrs="{'readonly':[('state','=','close')]}" context="{'default_survey_id': active_id}">
|
||||
|
|
|
@ -31,7 +31,8 @@
|
|||
<div class="wrap">
|
||||
<div class="container">
|
||||
<div class="jumbotron">
|
||||
<h1>Thank you!<br/><small>Your answers have been recorded.</small></h1>
|
||||
<h1>Thank you!</h1>
|
||||
<p>Your answers have been recorded.</p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
@ -51,37 +52,19 @@
|
|||
<div class="col-md-12">
|
||||
<t t-if="pagination['current'] == -1">
|
||||
<div class='jumbotron'>
|
||||
<h1>
|
||||
<span t-field='survey.title'/>
|
||||
<t t-if="survey.description is not False">
|
||||
<br/>
|
||||
<small><span t-field='survey.description'/></small>
|
||||
</t>
|
||||
</h1>
|
||||
</div>
|
||||
<div class="text-center">
|
||||
<h1><span t-field='survey.title'/></h1>
|
||||
<t t-if="survey.description"><p><span t-field='survey.description'/></p></t>
|
||||
<form role="form" method="post" t-att-name="survey.id.__str__() + '---1'" t-att-action="'/survey/fill/' + survey.id.__str__()" target="">
|
||||
<input type="hidden" name="current" t-att-value="pagination['current'].__str__()" />
|
||||
<input type="hidden" name="next" t-att-value="pagination['next'].__str__()" />
|
||||
<input type="submit" class="btn btn-primary active" value="Take survey"/>
|
||||
<input type="submit" class="btn btn-primary btn-lg active" value="Take survey"/>
|
||||
</form>
|
||||
</div>
|
||||
</t>
|
||||
<t t-if="pagination['current'] != -1">
|
||||
<div class='page-header'>
|
||||
<h1>
|
||||
<span t-field='survey.title'/>
|
||||
<t t-if="survey.description is not False">
|
||||
<br/>
|
||||
<small><span t-field='survey.description'/></small>
|
||||
</t>
|
||||
</h1>
|
||||
</div>
|
||||
<t t-set='page' t-value="survey.page_ids[pagination['current']]" />
|
||||
<t t-call="survey.page" />
|
||||
</t>
|
||||
|
||||
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
@ -101,15 +84,13 @@
|
|||
|
||||
<!-- A page -->
|
||||
<template id="page" name="Page">
|
||||
<div>
|
||||
<div class="page-header">
|
||||
<h2><span t-field='page.title' /></h2>
|
||||
<p><t t-if="page.description"><span t-field='page.description'/></t></p>
|
||||
<p class="text-info text-right">Questions marked with <span class="glyphicon glyphicon-exclamation-sign"></span> are mandatory.</p>
|
||||
</div>
|
||||
<hr/>
|
||||
|
||||
<!-- Panel to display input errors -->
|
||||
<t t-if="problems">
|
||||
<!-- <t t-if="problems">
|
||||
<div class="panel panel-danger">
|
||||
<div class="panel-heading">
|
||||
<h3 class="panel-title">Errors on this page</h3>
|
||||
|
@ -122,7 +103,7 @@
|
|||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
</t>
|
||||
</t> -->
|
||||
|
||||
<form role="form" method="post" t-att-name="survey.id.__str__() + '--' + pagination['current'].__str__()" t-att-action="'/survey/fill/' + survey.id.__str__()" target="">
|
||||
<input type="hidden" name="current" t-att-value="pagination['current'].__str__()" />
|
||||
|
@ -143,13 +124,18 @@
|
|||
<t t-if="question.parent_id and question.children_ids"><t t-set="is_matrix_vector" t-value="True" /></t>
|
||||
<t t-if="question.parent_id"><t t-set="is_vector_element" t-value="True" /></t>
|
||||
|
||||
<div class="question-wrapper" t-att-id="'survey_' + survey.id.__str__() + '--' + pagination['current'].__str__() + '--' + question.id.__str__()" t-att-data-oe-survey-qparams='json.dumps({
|
||||
"type": question.type,
|
||||
"constr_mandatory": question.constr_mandatory,
|
||||
"constr_type": question.constr_type,
|
||||
"constr_maximum_req_ans": question.constr_maximum_req_ans,
|
||||
"constr_minimum_req_ans": question.constr_minimum_req_ans,
|
||||
"constr_error_msg": question.constr_error_msg
|
||||
})'>
|
||||
|
||||
<t t-if="not is_vector_element"> <!-- if -->
|
||||
<h3>
|
||||
<t t-if="question.constr_mandatory"><span class="glyphicon glyphicon-exclamation-sign"></span></t>
|
||||
<span t-field='question.question' t-att-id="'survey_' + survey.id.__str__() + '--' + pagination['current'].__str__() + '--' + question.id.__str__()"/><br/>
|
||||
<small><t t-if="question.description"><span t-field='question.description'/></t></small>
|
||||
</h3>
|
||||
<h3><span t-field='question.question' /> <t t-if="question.constr_mandatory"><abbr title="This question is mandatory." class="text-danger">*</abbr></t></h3>
|
||||
<p class="text-muted"><t t-if="question.description"><span t-field='question.description'/></t></p>
|
||||
</t>
|
||||
<t t-if="is_vector_element"> <!-- else -->
|
||||
<t t-if="question.type == 'simple_choice_scale'">
|
||||
|
@ -167,17 +153,17 @@
|
|||
</t>
|
||||
|
||||
<t t-if="question.type in ['numerical_box']">
|
||||
<input type="number" class="form-control" t-att-name="survey.id.__str__() + '--' + pagination['current'].__str__() + '--' + question.id.__str__()"/>
|
||||
<input type="number" step=".1" class="form-control" t-att-name="survey.id.__str__() + '--' + pagination['current'].__str__() + '--' + question.id.__str__()"/>
|
||||
</t>
|
||||
|
||||
<t t-if="question.type in ['datetime']" >
|
||||
<input type="datetime-local" class="form-control" t-att-name="survey.id.__str__() + '--' + pagination['current'].__str__() + '--' + question.id.__str__()"/>
|
||||
<input type="datetime-local" class="form-control" t-att-name="survey.id.__str__() + '--' + pagination['current'].__str__() + '--' + question.id.__str__()" placeholder="jj-mm-aaaaThh:mm" />
|
||||
TODO: replace html5 datetime widget by OpenERP-CMS datetime widget
|
||||
</t>
|
||||
|
||||
<t t-if="question.type in ['simple_choice_scale']">
|
||||
<t t-foreach='question.suggested_answers_ids' t-as='suggestion'>
|
||||
<div class="radio-inline">
|
||||
<div class="radio">
|
||||
<label>
|
||||
<input type="radio" t-att-name="survey.id.__str__() + '--' + pagination['current'].__str__() + '--' + question.id.__str__()" t-att-value='suggestion.value' />
|
||||
<span t-field='suggestion.value'/>
|
||||
|
@ -245,6 +231,11 @@
|
|||
Other: <input type="text" class="form-control" t-att-name="survey.id.__str__() + '--' + pagination['current'].__str__() + '--' + question."/>
|
||||
</t>
|
||||
|
||||
|
||||
<div class="js_errzone alert alert-danger" style="display:none;"></div>
|
||||
</div>
|
||||
|
||||
|
||||
</template>
|
||||
|
||||
|
||||
|
@ -254,26 +245,15 @@
|
|||
<div class="wrap">
|
||||
<div class="container">
|
||||
<div class="row">
|
||||
<div class="col-md-1"> </div>
|
||||
<div class="col-md-1"> </div>
|
||||
<div class="col-md-8">
|
||||
<div class='jumbotron'>
|
||||
<h1>
|
||||
<span t-field='survey.title'/>
|
||||
<t t-if="survey.description is not False">
|
||||
<br/>
|
||||
<small><span t-field='survey.description'/></small>
|
||||
</t>
|
||||
</h1>
|
||||
<h1><span t-field='survey.title'/></h1>
|
||||
<t t-if="survey.description is not False"><p><span t-field='survey.description'/></p></t>
|
||||
</div>
|
||||
<t t-foreach="survey.page_ids" t-as="page">
|
||||
<t t-call="survey.page" />
|
||||
<hr/>
|
||||
</t>
|
||||
</div>
|
||||
<div class="col-md-1"> </div>
|
||||
<div class="col-md-1"> </div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</t>
|
||||
|
|
Loading…
Reference in New Issue