[FIX] gamification: do not set a start_date by default as the cron starts past challe...
[odoo/odoo.git] / addons / gamification / models / res_users.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2013 OpenERP SA (<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 General Public License as published by
9 #    the Free Software Foundation, either version 3 of the License, or
10 #    (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 General Public License for more details.
16 #
17 #    You should have received a copy of the GNU General Public License
18 #    along with this program.  If not, see <http://www.gnu.org/licenses/>
19 #
20 ##############################################################################
21
22 from openerp import SUPERUSER_ID
23 from openerp.osv import osv
24 from challenge import MAX_VISIBILITY_RANKING
25
26 class res_users_gamification_group(osv.Model):
27     """ Update of res.users class
28         - if adding groups to an user, check gamification.challenge linked to
29         this group, and the user. This is done by overriding the write method.
30     """
31     _name = 'res.users'
32     _inherit = ['res.users']
33
34     def write(self, cr, uid, ids, vals, context=None):
35         """Overwrite to autosubscribe users if added to a group marked as autojoin, user will be added to challenge"""
36         write_res = super(res_users_gamification_group, self).write(cr, uid, ids, vals, context=context)
37         if vals.get('groups_id'):
38             # form: {'group_ids': [(3, 10), (3, 3), (4, 10), (4, 3)]} or {'group_ids': [(6, 0, [ids]}
39             user_group_ids = [command[1] for command in vals['groups_id'] if command[0] == 4]
40             user_group_ids += [id for command in vals['groups_id'] if command[0] == 6 for id in command[2]]
41
42             challenge_obj = self.pool.get('gamification.challenge')
43             challenge_ids = challenge_obj.search(cr, SUPERUSER_ID, [('autojoin_group_id', 'in', user_group_ids)], context=context)
44             if challenge_ids:
45                 challenge_obj.write(cr, SUPERUSER_ID, challenge_ids, {'user_ids': [(4, user_id) for user_id in ids]}, context=context)
46                 challenge_obj.generate_goals_from_challenge(cr, SUPERUSER_ID, challenge_ids, context=context)
47         return write_res
48
49     def create(self, cr, uid, vals, context=None):
50         """Overwrite to autosubscribe users if added to a group marked as autojoin, user will be added to challenge"""
51         write_res = super(res_users_gamification_group, self).create(cr, uid, vals, context=context)
52         if vals.get('groups_id'):
53             # form: {'group_ids': [(3, 10), (3, 3), (4, 10), (4, 3)]} or {'group_ids': [(6, 0, [ids]}
54             user_group_ids = [command[1] for command in vals['groups_id'] if command[0] == 4]
55             user_group_ids += [id for command in vals['groups_id'] if command[0] == 6 for id in command[2]]
56
57             challenge_obj = self.pool.get('gamification.challenge')
58             challenge_ids = challenge_obj.search(cr, SUPERUSER_ID, [('autojoin_group_id', 'in', user_group_ids)], context=context)
59             if challenge_ids:
60                 challenge_obj.write(cr, SUPERUSER_ID, challenge_ids, {'user_ids': [(4, write_res)]}, context=context)
61                 challenge_obj.generate_goals_from_challenge(cr, SUPERUSER_ID, challenge_ids, context=context)
62         return write_res
63
64     # def get_goals_todo_info(self, cr, uid, context=None):
65
66     def get_serialised_gamification_summary(self, cr, uid, context=None):
67         return self._serialised_goals_summary(cr, uid, user_id=uid, context=context)
68
69     def _serialised_goals_summary(self, cr, uid, user_id, context=None):
70         """Return a serialised list of goals assigned to the user, grouped by challenge
71
72         [
73             {
74                 'id': <gamification.challenge id>,
75                 'name': <gamification.challenge name>,
76                 'visibility_mode': <visibility {ranking,personal}>,
77                 'currency': <res.currency id>,
78                 'lines': [(see gamification_challenge._get_serialized_challenge_lines() format)]
79             },
80         ]
81         """
82         all_goals_info = []
83         challenge_obj = self.pool.get('gamification.challenge')
84
85         user = self.browse(cr, uid, uid, context=context)
86         challenge_ids = challenge_obj.search(cr, uid, [('user_ids', 'in', uid), ('state', '=', 'inprogress')], context=context)
87         for challenge in challenge_obj.browse(cr, uid, challenge_ids, context=context):
88             # serialize goals info to be able to use it in javascript
89             lines = challenge_obj._get_serialized_challenge_lines(cr, uid, challenge, user_id, restrict_top=MAX_VISIBILITY_RANKING, context=context)
90             if lines:
91                 all_goals_info.append({
92                     'id': challenge.id,
93                     'name': challenge.name,
94                     'visibility_mode': challenge.visibility_mode,
95                     'currency': user.company_id.currency_id.id,
96                     'lines': lines,
97                 })
98
99         return all_goals_info
100
101     def get_challenge_suggestions(self, cr, uid, context=None):
102         """Return the list of challenges suggested to the user"""
103         challenge_info = []
104         challenge_obj = self.pool.get('gamification.challenge')
105         challenge_ids = challenge_obj.search(cr, uid, [('invited_user_ids', 'in', uid), ('state', '=', 'inprogress')], context=context)
106         for challenge in challenge_obj.browse(cr, uid, challenge_ids, context=context):
107             values = {
108                 'id': challenge.id,
109                 'name': challenge.name,
110                 'description': challenge.description,
111             }
112             challenge_info.append(values)
113         return challenge_info
114
115
116 class res_groups_gamification_group(osv.Model):
117     """ Update of res.groups class
118         - if adding users from a group, check gamification.challenge linked to
119         this group, and the user. This is done by overriding the write method.
120     """
121     _name = 'res.groups'
122     _inherit = 'res.groups'
123
124     # No need to overwrite create as very unlikely to be the value in the autojoin_group_id field
125     def write(self, cr, uid, ids, vals, context=None):
126         """Overwrite to autosubscribe users if add users to a group marked as autojoin, these will be added to the challenge"""
127         write_res = super(res_groups_gamification_group, self).write(cr, uid, ids, vals, context=context)
128         if vals.get('users'):
129             # form: {'group_ids': [(3, 10), (3, 3), (4, 10), (4, 3)]} or {'group_ids': [(6, 0, [ids]}
130             user_ids = [command[1] for command in vals['users'] if command[0] == 4]
131             user_ids += [id for command in vals['users'] if command[0] == 6 for id in command[2]]
132
133             challenge_obj = self.pool.get('gamification.challenge')
134             challenge_ids = challenge_obj.search(cr, SUPERUSER_ID, [('autojoin_group_id', 'in', ids)], context=context)
135             if challenge_ids:
136                 challenge_obj.write(cr, SUPERUSER_ID, challenge_ids, {'user_ids': [(4, user_id) for user_id in user_ids]}, context=context)
137                 challenge_obj.generate_goals_from_challenge(cr, SUPERUSER_ID, challenge_ids, context=context)
138         return write_res