2013-02-22 11:44:21 +00:00
|
|
|
from openerp.osv import osv
|
|
|
|
|
2013-03-11 11:04:23 +00:00
|
|
|
|
2013-02-22 11:44:21 +00:00
|
|
|
class res_users_gamification_group(osv.Model):
|
|
|
|
""" Update of res.users class
|
2013-03-21 11:01:52 +00:00
|
|
|
- if adding groups to an user, check gamification.goal.plan linked to
|
2013-02-22 11:44:21 +00:00
|
|
|
this group, and the user. This is done by overriding the write method.
|
|
|
|
"""
|
|
|
|
_name = 'res.users'
|
|
|
|
_inherit = ['res.users']
|
|
|
|
|
|
|
|
def write(self, cr, uid, ids, vals, context=None):
|
|
|
|
write_res = super(res_users_gamification_group, self).write(cr, uid, ids, vals, context=context)
|
|
|
|
if vals.get('groups_id'):
|
|
|
|
# form: {'group_ids': [(3, 10), (3, 3), (4, 10), (4, 3)]} or {'group_ids': [(6, 0, [ids]}
|
|
|
|
user_group_ids = [command[1] for command in vals['groups_id'] if command[0] == 4]
|
|
|
|
user_group_ids += [id for command in vals['groups_id'] if command[0] == 6 for id in command[2]]
|
|
|
|
|
|
|
|
goal_plan_obj = self.pool.get('gamification.goal.plan')
|
|
|
|
plan_ids = goal_plan_obj.search(cr, uid, [('autojoin_group_id', 'in', user_group_ids)], context=context)
|
|
|
|
|
|
|
|
goal_plan_obj.plan_subscribe_users(cr, uid, plan_ids, ids, context=context)
|
|
|
|
return write_res
|
|
|
|
|
2013-03-21 11:01:52 +00:00
|
|
|
def get_goals_todo_info(self, cr, uid, context=None):
|
|
|
|
"""Return the list of goals assigned to the user, grouped by plan"""
|
|
|
|
goals_info = []
|
|
|
|
goal_obj = self.pool.get('gamification.goal')
|
|
|
|
plan_obj = self.pool.get('gamification.goal.plan')
|
|
|
|
plan_ids = plan_obj.search(cr, uid, [('user_ids', 'in', uid)], context=context)
|
|
|
|
for plan in plan_obj.browse(cr, uid, plan_ids, context=context):
|
|
|
|
vals = {'name': plan.name, 'goals': []}
|
|
|
|
|
|
|
|
goal_ids = goal_obj.search(cr, uid, [('user_id', '=', uid), ('plan_id', '=', plan.id)], context=context)
|
2013-03-22 08:51:51 +00:00
|
|
|
all_done = True
|
2013-03-21 11:01:52 +00:00
|
|
|
for goal in goal_obj.browse(cr, uid, goal_ids, context=context):
|
2013-03-22 08:51:51 +00:00
|
|
|
if goal.last_update and goal.end_date and goal.last_update > goal.end_date:
|
|
|
|
# do not include goals of previous plan run
|
|
|
|
continue
|
|
|
|
|
|
|
|
if goal.state == 'inprogress' or goal.state == 'inprogress_update':
|
|
|
|
all_done = False
|
|
|
|
|
2013-03-21 11:01:52 +00:00
|
|
|
vals['goals'].append({
|
|
|
|
'id': goal.id,
|
|
|
|
'type_name': goal.type_id.name,
|
2013-03-22 11:13:44 +00:00
|
|
|
'type_condition': goal.type_id.condition,
|
2013-03-21 11:01:52 +00:00
|
|
|
'type_description': goal.type_description,
|
|
|
|
'state': goal.state,
|
|
|
|
'completeness': goal.completeness,
|
|
|
|
'computation_mode': goal.computation_mode,
|
2013-03-22 11:13:44 +00:00
|
|
|
'current': goal.current,
|
|
|
|
'target_goal': goal.target_goal,
|
2013-03-21 11:01:52 +00:00
|
|
|
})
|
2013-03-22 08:51:51 +00:00
|
|
|
# skip plans where all goal are done or failed
|
|
|
|
if not all_done:
|
|
|
|
goals_info.append(vals)
|
2013-03-21 11:01:52 +00:00
|
|
|
return goals_info
|
|
|
|
|
|
|
|
|
2013-02-22 11:44:21 +00:00
|
|
|
class res_groups_gamification_group(osv.Model):
|
|
|
|
""" Update of res.groups class
|
2013-03-21 11:01:52 +00:00
|
|
|
- if adding users from a group, check gamification.goal.plan linked to
|
2013-02-22 11:44:21 +00:00
|
|
|
this group, and the user. This is done by overriding the write method.
|
|
|
|
"""
|
|
|
|
_name = 'res.groups'
|
|
|
|
_inherit = 'res.groups'
|
|
|
|
|
|
|
|
def write(self, cr, uid, ids, vals, context=None):
|
|
|
|
write_res = super(res_groups_gamification_group, self).write(cr, uid, ids, vals, context=context)
|
|
|
|
if vals.get('users'):
|
|
|
|
# form: {'group_ids': [(3, 10), (3, 3), (4, 10), (4, 3)]} or {'group_ids': [(6, 0, [ids]}
|
|
|
|
user_ids = [command[1] for command in vals['users'] if command[0] == 4]
|
|
|
|
user_ids += [id for command in vals['users'] if command[0] == 6 for id in command[2]]
|
|
|
|
|
|
|
|
goal_plan_obj = self.pool.get('gamification.goal.plan')
|
|
|
|
plan_ids = goal_plan_obj.search(cr, uid, [('autojoin_group_id', 'in', ids)], context=context)
|
|
|
|
|
|
|
|
goal_plan_obj.plan_subscribe_users(cr, uid, plan_ids, user_ids, context=context)
|
|
|
|
return write_res
|