~openerp-commiter/openobject-addons/trunk-extra-addons

« back to all changes in this revision

Viewing changes to survey/survey.py

  • Committer: ysa(Open ERP)
  • Date: 2009-10-13 11:01:02 UTC
  • mto: This revision was merged to the branch mainline in revision 3907.
  • Revision ID: ysa@tinyerp.co.in-20091013110102-7v69yb1ssfqt5n03
[ADD] survey module

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- encoding: utf-8 -*-
 
2
##############################################################################
 
3
#
 
4
#    OpenERP, Open Source Management Solution    
 
5
#    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>). All Rights Reserved
 
6
#    $Id$
 
7
#
 
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.
 
12
#
 
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.
 
17
#
 
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/>.
 
20
#
 
21
##############################################################################
 
22
 
 
23
from osv import fields, osv
 
24
import datetime
 
25
 
 
26
question_no = 0
 
27
 
 
28
class survey(osv.osv):
 
29
    _name = 'survey'
 
30
    _description = 'Survey'
 
31
    _rec_name = 'title'
 
32
    _columns = {  
 
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'),
 
41
    }
 
42
    _defaults = {
 
43
        'state' : lambda *a: "draft"
 
44
    }
 
45
    
 
46
    def survey_draft(self, cr, uid, ids, arg):
 
47
        self.write(cr, uid, ids, { 'state' : 'draft' })
 
48
        return True
 
49
     
 
50
    def survey_open(self, cr, uid, ids, arg):
 
51
        self.write(cr, uid, ids, { 'state' : 'open' })
 
52
        return True
 
53
     
 
54
    def survey_close(self, cr, uid, ids, arg):
 
55
        self.write(cr, uid, ids, { 'state' : 'close' })
 
56
        return True
 
57
     
 
58
    def survey_cancel(self, cr, uid, ids, arg):
 
59
        self.write(cr, uid, ids, { 'state' : 'cancel' })
 
60
        return True
 
61
    
 
62
survey()
 
63
 
 
64
class survey_page(osv.osv):
 
65
    _name = 'survey.page'
 
66
    _description = 'Survey Pages'
 
67
    _rec_name =  'title'
 
68
    _order = 'sequence'
 
69
    _columns = {
 
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'),
 
75
    }
 
76
 
 
77
survey_page()
 
78
 
 
79
class survey_question(osv.osv):
 
80
    _name = 'survey.question'
 
81
    _description = 'Survey Question'
 
82
    _rec_name = 'question'
 
83
    _order = 'sequence'
 
84
    _columns = {
 
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')
 
92
    }
 
93
    
 
94
survey_question()
 
95
 
 
96
class survey_answer(osv.osv):
 
97
    _name = 'survey.answer'
 
98
    _description = 'Survey Answer'
 
99
    _rec_name = 'answer'
 
100
    _order = 'sequence'
 
101
    _columns = {
 
102
                'question_id' : fields.many2one('survey.question', 'Question'),
 
103
                'answer' : fields.char('Answer', size = 128,required=1),
 
104
                'sequence' : fields.integer('Sequence')
 
105
    }
 
106
survey_answer()
 
107
 
 
108
class survey_response(osv.osv):
 
109
    _name = 'survey.response'
 
110
    _description = 'Survey Response'
 
111
    _rec_name = 'date_start'
 
112
    _columns = {
 
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'),
 
120
    }
 
121
    _defaults = {
 
122
        'state' : lambda *a: "draft"
 
123
    }
 
124
    
 
125
    def response_draft(self, cr, uid, ids, arg):
 
126
        self.write(cr, uid, ids, { 'state' : 'draft' })
 
127
        return True
 
128
     
 
129
    def response_done(self, cr, uid, ids, arg):
 
130
        self.write(cr, uid, ids, { 'state' : 'done' })
 
131
        return True
 
132
     
 
133
    def response_skip(self, cr, uid, ids, arg):
 
134
        self.write(cr, uid, ids, { 'state' : 'skip' })
 
135
        return True
 
136
 
 
137
survey_response()
 
138
 
 
139
class survey_response_answer(osv.osv):
 
140
    _name = 'survey.response.answer'
 
141
    _description = 'Survey Response Answer'
 
142
    _rec_name = 'response_id'
 
143
    _columns = {
 
144
                'response_id' : fields.many2one('survey.response','Response'),
 
145
                'answer_id' : fields.many2one('survey.answer','Answer',required=1),
 
146
                'comment' : fields.text('Notes'),                
 
147
    }
 
148
 
 
149
survey_response_answer()
 
150
 
 
151
 
 
152
class survey_name_wiz(osv.osv_memory):
 
153
    _name = 'survey.name.wiz'
 
154
    _columns = {
 
155
        'survey_id': fields.many2one('survey',"Survey",required = "1"),
 
156
    }
 
157
    
 
158
    def action_next(self, cr, uid, ids, context=None):
 
159
        global question_no
 
160
        question_no = 0
 
161
        return {
 
162
                'view_type': 'form',
 
163
                "view_mode": 'form',
 
164
                'res_model': 'survey.question.wiz',
 
165
                'type': 'ir.actions.act_window',
 
166
                'target': 'new',
 
167
         }
 
168
survey_name_wiz()
 
169
 
 
170
class survey_question_wiz(osv.osv_memory):
 
171
    _name = 'survey.question.wiz'
 
172
    _columns = {
 
173
        'name': fields.text('statistics'),
 
174
    }
 
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)
 
177
        global question_no
 
178
        question_no += 1
 
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)])
 
189
        if page_id:
 
190
            xml = '''<?xml version="1.0" encoding="utf-8"?> <form string="Select a survey Name">'''
 
191
            fields = {}
 
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']
 
196
            for que in que_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'],[])
 
200
                for ans in ans_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':{}}
 
206
 
 
207
            xml += '''
 
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"/>
 
212
            </group>
 
213
            </form>'''
 
214
            result['arch'] = xml
 
215
            result['fields']=fields
 
216
        else:
 
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" />
 
221
                                <newline/>
 
222
                                <button colspan="2" icon="gtk-go-forward" readonly="0" special="cancel" string="OK"/>
 
223
                         </form>'''
 
224
            question_no = 0
 
225
            result['arch'] = xml_form
 
226
            result['fields']={}
 
227
            return result
 
228
        return result
 
229
    
 
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')
 
234
        que_li = []
 
235
        for key,val in vals.items():
 
236
            que_id = key.split('_')[0]
 
237
            if que_id not in que_li:
 
238
                ans = False
 
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})
 
245
                        ans = True
 
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]})
 
249
                        ans = True
 
250
                if que_rec[0]['is_require_answer'] and not ans:
 
251
                    raise osv.except_osv(_('Error !'),_('This question requires an answer.'))
 
252
        return True
 
253
    def action_next(self, cr, uid, ids, context=None):
 
254
        return {
 
255
                'view_type': 'form',
 
256
                "view_mode": 'form',
 
257
                'res_model': 'survey.question.wiz',
 
258
                'type': 'ir.actions.act_window',
 
259
                'target': 'new',
 
260
                }
 
261
            
 
262
survey_question_wiz()
 
263
 
 
264
class report_survey_question(osv.osv):
 
265
    _name = "report.survey.question"
 
266
    _description = "Survey Question Report"
 
267
    _auto = False
 
268
    def _calc_response_avg(self,cr,uid,ids,field_name,arg,context):
 
269
        val = {}
 
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
 
273
            else:
 
274
                val[rec.id] = 0  
 
275
        return val
 
276
    _columns = {
 
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(%)")
 
282
    }
 
283
    def init(self, cr):
 
284
        cr.execute("""
 
285
                create or replace view report_survey_question as (
 
286
                select 
 
287
                    sa.id as id,
 
288
                    sq.id as que_id, 
 
289
                    sa.id as ans_id, 
 
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
 
292
                from 
 
293
                    survey_question sq, survey_answer sa 
 
294
                where sq.id = sa.question_id )
 
295
                """)
 
296
 
 
297
report_survey_question()
 
298
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
 
 
b'\\ No newline at end of file'