1
# -*- encoding: utf-8 -*-
2
##############################################################################
4
# OpenERP, Open Source Management Solution
5
# Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>). All Rights Reserved
8
# This program is free software: you can redistribute it and/or modify
9
# it under the terms of the GNU General Public License as published by
10
# the Free Software Foundation, either version 3 of the License, or
11
# (at your option) any later version.
13
# This program is distributed in the hope that it will be useful,
14
# but WITHOUT ANY WARRANTY; without even the implied warranty of
15
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
# GNU General Public License for more details.
18
# You should have received a copy of the GNU General Public License
19
# along with this program. If not, see <http://www.gnu.org/licenses/>.
21
##############################################################################
23
from osv import fields, osv
28
class survey(osv.osv):
30
_description = 'Survey'
33
'title' : fields.char('Survey Title', size=128, required=1),
34
'page_ids' : fields.one2many('survey.page','survey_id','Page'),
35
'date_open' : fields.datetime('Survey Open Date'),
36
'date_close' : fields.datetime('Survey Close Date'),
37
'survey_link' : fields.char('Survey Link', size = 255),
38
'max_response_limit' : fields.integer('Maximum Response Limit'),
39
'state' : fields.selection([('draft','Draft'),('open','Open'),('close','Close'),('cancel','Cancel')],'Status',readonly = True),
40
'responsible_id' : fields.many2one('res.users','Responsible'),
43
'state' : lambda *a: "draft"
46
def survey_draft(self, cr, uid, ids, arg):
47
self.write(cr, uid, ids, { 'state' : 'draft' })
50
def survey_open(self, cr, uid, ids, arg):
51
self.write(cr, uid, ids, { 'state' : 'open' })
54
def survey_close(self, cr, uid, ids, arg):
55
self.write(cr, uid, ids, { 'state' : 'close' })
58
def survey_cancel(self, cr, uid, ids, arg):
59
self.write(cr, uid, ids, { 'state' : 'cancel' })
64
class survey_page(osv.osv):
66
_description = 'Survey Pages'
70
'title' : fields.char('Page Title', size = 128,required=1),
71
'survey_id' : fields.many2one('survey', 'Survey'),
72
'question_ids' : fields.one2many('survey.question','page_id','Question'),
73
'sequence' : fields.integer('Sequence'),
74
'note' : fields.text('Description'),
79
class survey_question(osv.osv):
80
_name = 'survey.question'
81
_description = 'Survey Question'
82
_rec_name = 'question'
85
'page_id' : fields.many2one('survey.page','Survey Page'),
86
'question' : fields.char('Question', size = 128,required=1),
87
'answer_choice_ids' : fields.one2many('survey.answer','question_id','Answer'),
88
'response_ids' : fields.one2many('survey.response','question_id','Response'),
89
'is_require_answer' : fields.boolean('Required Answer'),
90
'allow_comment' : fields.boolean('Allow Comment Field'),
91
'sequence' : fields.integer('Sequence')
96
class survey_answer(osv.osv):
97
_name = 'survey.answer'
98
_description = 'Survey Answer'
102
'question_id' : fields.many2one('survey.question', 'Question'),
103
'answer' : fields.char('Answer', size = 128,required=1),
104
'sequence' : fields.integer('Sequence')
108
class survey_response(osv.osv):
109
_name = 'survey.response'
110
_description = 'Survey Response'
111
_rec_name = 'date_start'
113
'date_create' : fields.datetime('Create Date',required=1),
114
'date_modify' : fields.datetime('Modify Date'),
115
'state' : fields.selection([('draft', 'Draft'),('done', 'Done'), ('skip',' Skip')], 'Status', readonly = True),
116
'response_id' : fields.many2one('res.users', 'Responsibal User'),
117
'question_id' : fields.many2one('survey.question', 'Question'),
118
'response_type' : fields.selection([('normal','Normal'),('manual','Manually'),('link','Link'),('mail','Mail')],'Response Type'),
119
'response_answer_ids' : fields.one2many('survey.response.answer', 'response_id', 'Response Answer'),
122
'state' : lambda *a: "draft"
125
def response_draft(self, cr, uid, ids, arg):
126
self.write(cr, uid, ids, { 'state' : 'draft' })
129
def response_done(self, cr, uid, ids, arg):
130
self.write(cr, uid, ids, { 'state' : 'done' })
133
def response_skip(self, cr, uid, ids, arg):
134
self.write(cr, uid, ids, { 'state' : 'skip' })
139
class survey_response_answer(osv.osv):
140
_name = 'survey.response.answer'
141
_description = 'Survey Response Answer'
142
_rec_name = 'response_id'
144
'response_id' : fields.many2one('survey.response','Response'),
145
'answer_id' : fields.many2one('survey.answer','Answer',required=1),
146
'comment' : fields.text('Notes'),
149
survey_response_answer()
152
class survey_name_wiz(osv.osv_memory):
153
_name = 'survey.name.wiz'
155
'survey_id': fields.many2one('survey',"Survey",required = "1"),
158
def action_next(self, cr, uid, ids, context=None):
164
'res_model': 'survey.question.wiz',
165
'type': 'ir.actions.act_window',
170
class survey_question_wiz(osv.osv_memory):
171
_name = 'survey.question.wiz'
173
'name': fields.text('statistics'),
175
def fields_view_get(self, cr, uid, view_id=None, view_type='form', context=None, toolbar=False):
176
result = super(survey_question_wiz, self).fields_view_get(cr, uid, view_id, view_type, context, toolbar)
179
survey_name_obj = self.pool.get('survey.name.wiz')
180
surv_id = survey_name_obj.search(cr,uid,[])
181
surv_id = surv_id[int(len(surv_id) -1)]
182
survey_id = survey_name_obj.read(cr,uid,surv_id,[])[0]['survey_id']
183
sur_rec = self.pool.get('survey').read(cr,uid,survey_id,[])
184
page_ids = sur_rec['page_ids']
185
page_obj = self.pool.get('survey.page')
186
que_obj = self.pool.get('survey.question')
187
ans_obj = self.pool.get('survey.answer')
188
page_id = page_obj.search(cr,uid,[('survey_id','=',survey_id),('sequence','=',question_no)])
190
xml = '''<?xml version="1.0" encoding="utf-8"?> <form string="Select a survey Name">'''
192
pag_rec = page_obj.read(cr,uid,page_id[0])
193
xml += '''<separator string="'''+ str(pag_rec['title']) + '''" colspan="4"/>'''
194
xml += '''<label string="'''+ str(pag_rec['note']) + '''"/> <newline/> <newline/><newline/>'''
195
que_ids = pag_rec['question_ids']
197
que_rec = que_obj.read(cr,uid,que)
198
xml += '''<separator string="'''+ str(que_rec['question']) + '''" colspan="4"/> <newline/> '''
199
ans_ids = ans_obj.read(cr,uid,que_rec['answer_choice_ids'],[])
201
xml += '''<field name="'''+ str(que) + "_" + ans['answer'] +"_" + str(ans['id']) + '''"/> '''
202
fields[str(que) + "_" + ans['answer'] +"_" + str(ans['id']) ] = {'type':'boolean','string':ans['answer'],'views':{}}
203
# if que_rec['allow_comment']:
204
# xml += '''<newline/> <field name="'''+ str(que) + "_other" '''"/> '''
205
# fields[str(que) + "_other"] = {'type':'char','string':"Other",'size':64,'views':{}}
208
<separator colspan="4" />
209
<group col="4" colspan="4">
210
<button colspan="2" icon="gtk-cancel" readonly="0" special="cancel" string="Cancel"/>
211
<button icon="gtk-go-forward" name="action_next" string="Next" type="object"/>
215
result['fields']=fields
217
xml_form ='''<?xml version="1.0"?>
218
<form string="Complete Survey Response">
219
<separator string="Complete Survey" colspan="4"/>
220
<label string = "Thanks for your response" />
222
<button colspan="2" icon="gtk-go-forward" readonly="0" special="cancel" string="OK"/>
225
result['arch'] = xml_form
230
def create(self, cr, uid, vals, context=None):
231
resp_obj = self.pool.get('survey.response')
232
res_ans_obj = self.pool.get('survey.response.answer')
233
que_obj = self.pool.get('survey.question')
235
for key,val in vals.items():
236
que_id = key.split('_')[0]
237
if que_id not in que_li:
239
que_li.append(que_id)
240
que_rec = que_obj.read(cr,uid,que_id,['is_require_answer'])
241
resp_id = resp_obj.create(cr,uid,{'response_id':uid,'question_id':que_id,'date_create':datetime.datetime.now(),'response_type':'normal'})
242
for key1,val1 in vals.items():
243
if val1 and key1.split('_')[1] =="other":
244
resp_obj.write(cr,uid,resp_id,{'other':val1})
246
elif val1 and que_id == key1.split('_')[0]:
247
ans_id_len = key1.split('_')
248
res_ans_obj.create(cr,uid,{'response_id':resp_id,'answer_id':ans_id_len[len(ans_id_len) -1]})
250
if que_rec[0]['is_require_answer'] and not ans:
251
raise osv.except_osv(_('Error !'),_('This question requires an answer.'))
253
def action_next(self, cr, uid, ids, context=None):
257
'res_model': 'survey.question.wiz',
258
'type': 'ir.actions.act_window',
262
survey_question_wiz()
264
class report_survey_question(osv.osv):
265
_name = "report.survey.question"
266
_description = "Survey Question Report"
268
def _calc_response_avg(self,cr,uid,ids,field_name,arg,context):
270
for rec in self.browse(cr,uid,ids):
271
if rec.res_que_count:
272
val[rec.id] = rec.res_ans_count *100 / rec.res_que_count
277
'que_id': fields.many2one('survey.question','Question'),
278
'ans_id': fields.many2one('survey.answer','Answer'),
279
'res_ans_count': fields.integer('Response Answer Count'),
280
'res_que_count': fields.integer('Response Question Count'),
281
'res_avg' : fields.function(_calc_response_avg,method =True, string ="Response Average(%)")
285
create or replace view report_survey_question as (
290
(select count(answer_id) from survey_response_answer sra where sa.id = sra.answer_id) as res_ans_count ,
291
(select count(question_id) from survey_response where question_id = sa.question_id group by question_id) as res_que_count
293
survey_question sq, survey_answer sa
294
where sq.id = sa.question_id )
297
report_survey_question()
298
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
b'\\ No newline at end of file'