[MERGE]: survey: fixed a bug in warning message when users exceed maximal number...
[odoo/odoo.git] / addons / survey / wizard / survey_selection.py
1 # -*- encoding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-TODAY OpenERP S.A. <http://www.openerp.com>
6 #
7 #    This program is free software: you can redistribute it and/or modify
8 #    it under the terms of the GNU Affero General Public License as
9 #    published by the Free Software Foundation, either version 3 of the
10 #    License, or (at your option) any later version.
11 #
12 #    This program is distributed in the hope that it will be useful,
13 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
14 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 #    GNU Affero General Public License for more details.
16 #
17 #    You should have received a copy of the GNU Affero General Public License
18 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 #
20 ##############################################################################
21
22 from osv import osv
23 from osv import fields
24 from tools.translate import _
25
26 class survey_name_wiz(osv.osv_memory):
27     _name = 'survey.name.wiz'
28
29     _columns = {
30         'survey_id': fields.many2one('survey', 'Survey', required=True, ondelete='cascade'),
31         'page_no': fields.integer('Page Number'),
32         'note': fields.text("Description"),
33         'page': fields.char('Page Position',size = 12),
34         'transfer': fields.boolean('Page Transfer'),
35         'store_ans': fields.text('Store Answer'),
36         'response': fields.char('Answer',size=16)
37     }
38     _defaults = {
39         'page_no': -1,
40         'page': 'next',
41         'transfer': 1,
42         'response': 0,
43         'survey_id': lambda self,cr,uid,context:context.get('survey_id',False),
44         'store_ans': '{}' #Setting the default pattern as '{}' as the field is of type text. The field always gets the value in dict format
45     }
46
47     def action_next(self, cr, uid, ids, context=None):
48         """
49         Start the survey, Increment in started survey field but if set the max_response_limit of
50         survey then check the current user how many times start this survey. if current user max_response_limit
51         is reach then this user can not start this survey(Raise Exception).
52         """
53         survey_obj = self.pool.get('survey')
54         search_obj = self.pool.get('ir.ui.view')
55         if context is None: context = {}
56
57         this = self.browse(cr, uid, ids, context=context)[0]
58         survey_id = this.survey_id.id
59         context.update({'survey_id': survey_id, 'sur_name_id': this.id})
60         cr.execute('select count(id) from survey_history where user_id=%s\
61                     and survey_id=%s' % (uid,survey_id))
62
63         res = cr.fetchone()[0]
64         sur_rec = survey_obj.browse(cr,uid,survey_id,context=context)
65         if sur_rec.response_user and res >= sur_rec.response_user:
66             raise osv.except_osv(_('Warning !'),_("You can not give response for this survey more than %s times") % (sur_rec.response_user))
67
68         if sur_rec.max_response_limit and sur_rec.max_response_limit <= sur_rec.tot_start_survey:
69             raise osv.except_osv(_('Warning !'),_("You can not give more response. Please contact the author of this survey for further assistance."))
70
71         search_id = search_obj.search(cr,uid,[('model','=','survey.question.wiz'),('name','=','Survey Search')])
72         return {
73             'view_type': 'form',
74             "view_mode": 'form',
75             'res_model': 'survey.question.wiz',
76             'type': 'ir.actions.act_window',
77             'target': 'new',
78             'search_view_id': search_id[0],
79             'context': context
80         }
81
82     def on_change_survey(self, cr, uid, ids, survey_id, context=None):
83         """
84             on change event of survey_id field, if note is available in selected survey then display this note in note fields.
85         """
86         if not survey_id:
87             return {}
88         notes = self.pool.get('survey').read(cr, uid, survey_id, ['note'])['note']
89         return {'value': {'note': notes}}
90
91 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: