1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2013 OpenERP SA (<http://www.openerp.com>)
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.
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.
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/>
20 ##############################################################################
22 from openerp import SUPERUSER_ID
23 from openerp.osv import osv
24 from challenge import MAX_VISIBILITY_RANKING
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.
32 _inherit = ['res.users']
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]]
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)
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)
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]]
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)
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)
64 # def get_goals_todo_info(self, cr, uid, context=None):
66 def get_serialised_gamification_summary(self, cr, uid, context=None):
67 return self._serialised_goals_summary(cr, uid, user_id=uid, context=context)
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
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)]
83 challenge_obj = self.pool.get('gamification.challenge')
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)
91 all_goals_info.append({
93 'name': challenge.name,
94 'visibility_mode': challenge.visibility_mode,
95 'currency': user.company_id.currency_id.id,
101 def get_challenge_suggestions(self, cr, uid, context=None):
102 """Return the list of challenges suggested to the user"""
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):
109 'name': challenge.name,
110 'description': challenge.description,
112 challenge_info.append(values)
113 return challenge_info
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.
122 _inherit = 'res.groups'
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]]
133 challenge_obj = self.pool.get('gamification.challenge')
134 challenge_ids = challenge_obj.search(cr, SUPERUSER_ID, [('autojoin_group_id', 'in', ids)], context=context)
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)