diff --git a/addons/gamification/badge.py b/addons/gamification/badge.py index 6c8c7bc90b2..7c505cbf094 100644 --- a/addons/gamification/badge.py +++ b/addons/gamification/badge.py @@ -23,6 +23,7 @@ from openerp.osv import fields, osv from openerp import tools from templates import TemplateHelper +from datetime import date class gamification_badge_user(osv.Model): @@ -34,7 +35,7 @@ class gamification_badge_user(osv.Model): _columns = { 'employee_id': fields.many2one("hr.employee", string='Employee', required=True), 'user_id': fields.related('employee_id', 'user_id', string="User"), - 'badge_ids': fields.many2many('gamification.badge', 'rel_badge_users', string='Badge'), # or many2one ?? + 'badge_id': fields.many2one('gamification.badge', string='Badge'), # or many2one ?? } @@ -50,15 +51,63 @@ class gamification_badge(osv.Model): result[obj.id] = tools.image_get_resized_images(obj.image) return result + def _get_global_count(self, cr, uid, ids, name, args, context=None): + """Return the number of time this badge has been granted""" + result = dict.fromkeys(ids, False) + for obj in self.browse(cr, uid, ids, context=context): + result[obj.id] = len(self.pool.get('gamification.badge.user').search( + cr, uid, [('badge_id', '=', obj.id)], context=context)) + return result + + def _get_unique_global_count(self, cr, uid, ids, name, args, context=None): + """Return the number of time this badge has been granted to individual users""" + result = dict.fromkeys(ids, False) + for obj in self.browse(cr, uid, ids, context=context): + res = self.pool.get('gamification.badge.user').read_group( + cr, uid, domain=[('badge_id', '=', obj.id)], + fields=['badge_id', 'employee_id'], + groupby=['employee_id'], context=context) + result[obj.id] = len(res) + return result + + def _get_month_count(self, cr, uid, ids, name, args, context=None): + """Return the number of time this badge has been granted this month""" + result = dict.fromkeys(ids, False) + first_month_day = date.today().replace(day=1).isoformat() + for obj in self.browse(cr, uid, ids, context=context): + result[obj.id] = len(self.pool.get('gamification.badge.user').search( + cr, uid, [('badge_id', '=', obj.id), + ('create_date', '>=', first_month_day)], context=context)) + return result + + def _get_global_my_count(self, cr, uid, ids, name, args, context=None): + """Return the number of time this badge has been granted to the current user""" + result = dict.fromkeys(ids, False) + for obj in self.browse(cr, uid, ids, context=context): + result[obj.id] = len(self.pool.get('gamification.badge.user').search( + cr, uid, [('badge_id', '=', obj.id), ('user_id', '=', uid)], + context=context)) + return result + + def _get_month_my_count(self, cr, uid, ids, name, args, context=None): + """Return the number of time this badge has been granted to the current user this month""" + result = dict.fromkeys(ids, False) + first_month_day = date.today().replace(day=1).isoformat() + for obj in self.browse(cr, uid, ids, context=context): + result[obj.id] = len(self.pool.get('gamification.badge.user').search( + cr, uid, [('badge_id', '=', obj.id), ('user_id', '=', uid) + ('create_date', '>=', first_month_day)], context=context)) + return result + _columns = { 'name': fields.char('Badge', required=True, translate=True), 'description': fields.text('Description'), - 'image' : fields.binary("Image", + 'image': fields.binary("Image", help="This field holds the image used for the badge, limited to 256x256"), # image_select: selection with a on_change to fill image with predefined picts 'rule_auth': fields.selection([ ('everyone', 'Everyone'), - ('list', 'A selected list of users'), + ('users', 'A selected list of users'), ('having', 'People having some badges'), ('computed', 'Nobody, Computed'), ], @@ -76,24 +125,31 @@ class gamification_badge(osv.Model): help="This badge can not be send indefinitely"), 'rule_max_number': fields.integer('Limitation Number', help="The maximum number of time this badge can be sent per month."), + 'compute_code': fields.text('Compute Code', + help="The python code that will be executed to verify if a user can receive this badge."), 'goal_type_ids': fields.many2many('gamification.goal.type', string='Goals Linked', help="The users that have succeeded theses goals will receive automatically the badge."), + 'public': fields.boolean('Public', help="A message will be posted on the user profile or just sent to him"), 'owner_ids': fields.many2many('gamification.badge.user', 'rel_badge_users', string='Owners', help='The list of users having receive this badge'), - 'stat_count': fields.integer('Total Count', + + 'stat_count': fields.function(_get_global_count, string='Total', help="The number of time this badge has been received."), - 'stat_count_distinct': fields.integer('Uniaue Count', - help="The number of time this badge has been received by individual users."), - 'stat_this_month': fields.integer('Monthly Count', + 'stat_count_distinct': fields.function(_get_unique_global_count, + string='Unique Count', + help="The number of time this badge has been received by individual employees."), + 'stat_this_month': fields.function(_get_month_count, + string='Monthly Count', help="The number of time this badge has been received this month."), - # stat_my - # stat_my_this_month - 'compute_code': fields.text('Compute Code', - help="The python code that will be executed to verify if a user can receive this badge.") + 'stat_my': fields.function(_get_global_my_count, string='My Total', + help="The number of time the current user has received this badge."), + 'stat_my_this_month': fields.function(_get_month_my_count, + string='My Monthly Total', + help="The number of time the current user has received this badge this month."), } _default = { diff --git a/addons/gamification/badge_view.xml b/addons/gamification/badge_view.xml index 0030709736f..3d88dbcd68a 100644 --- a/addons/gamification/badge_view.xml +++ b/addons/gamification/badge_view.xml @@ -25,6 +25,7 @@ + @@ -49,11 +50,11 @@ - - + + + - - + diff --git a/addons/gamification/goal.py b/addons/gamification/goal.py index 04030037796..50927fa1b47 100644 --- a/addons/gamification/goal.py +++ b/addons/gamification/goal.py @@ -40,11 +40,12 @@ class gamification_goal_type(osv.Model): _columns = { 'name': fields.char('Type Name', required=True, translate=True), 'description': fields.text('Description'), - 'unit': fields.char('Unit', help="The unit of the target and current values", translate=True), + 'unit': fields.char('Unit', + help="The unit of the target and current values", translate=True), 'computation_mode': fields.selection([ - ('sum','Sum'), - ('count','Count'), - ('manually','Manually') + ('sum', 'Sum'), + ('count', 'Count'), + ('manually', 'Manually') ], string="Mode of Computation", help="""How is computed the goal value :\n @@ -54,24 +55,24 @@ class gamification_goal_type(osv.Model): required=True), 'model_id': fields.many2one('ir.model', string='Model', - help='The model object for the field to evaluate' ), + help='The model object for the field to evaluate'), 'field_id': fields.many2one('ir.model.fields', string='Evaluated Field', - help='The field containing the value to evaluate' ), + help='The field containing the value to evaluate'), 'field_date_id': fields.many2one('ir.model.fields', string='Evaluated Date Field', help='The date to use for the time period evaluated'), 'domain': fields.char("Domain", help="Technical filters rules to apply", required=True), # how to apply it ? - 'condition' : fields.selection([ - ('lower','<='), - ('higher','>=') + 'condition': fields.selection([ + ('lower', '<='), + ('higher', '>=') ], string='Validation Condition', help='A goal is considered as completed when the current value is compared to the value to reach', required=True), - 'sequence' : fields.integer('Sequence', + 'sequence': fields.integer('Sequence', help='Sequence number for ordering', required=True), } @@ -144,7 +145,7 @@ class gamification_goal(osv.Model): ], string='State', required=True, - track_visibility = 'always'), + track_visibility='always'), 'computation_mode': fields.related('type_id','computation_mode', type='char', @@ -153,7 +154,7 @@ class gamification_goal(osv.Model): help="The number of days after which the user assigned to a manual goal will be reminded. Never reminded if no value is specified."), 'last_update' : fields.date('Last Update', help="In case of manual goal, reminders are sent if the goal as not been updated for a while (defined in goal plan). Ignored in case of non-manual goal or goal not linked to a plan."), - + 'type_description': fields.related('type_id','description', type='char', string='Type Description'), @@ -207,12 +208,12 @@ class gamification_goal(osv.Model): partner_ids=[goal.user_id.partner_id.id], context=context, subtype='mail.mt_comment') - - else: # count or sum + + else: # count or sum obj = self.pool.get(goal.type_id.model_id.model) field_date_name = goal.type_id.field_date_id.name - - domain = safe_eval(goal.type_id.domain, + + domain = safe_eval(goal.type_id.domain, {'user_id': goal.user_id.id}) if goal.start_date: domain.append((field_date_name, '>=', goal.start_date)) @@ -224,11 +225,11 @@ class gamification_goal(osv.Model): res = obj.read_group(cr, uid, domain, [field_name], [''], context=context) towrite = {'current': res[0][field_name]} - - else: # computation mode = count + + else: # computation mode = count res = obj.search(cr, uid, domain, context=context) towrite = {'current': len(res)} - + # check goal target reached if (goal.type_id.condition == 'higher' \ and towrite['current'] >= goal.target_goal) \ diff --git a/addons/gamification/res_users.py b/addons/gamification/res_users.py index 38d4950b185..22aaf89ce57 100644 --- a/addons/gamification/res_users.py +++ b/addons/gamification/res_users.py @@ -1,5 +1,6 @@ from openerp.osv import osv + class res_users_gamification_group(osv.Model): """ Update of res.users class - if adding groups to an user, check gamification.goal.plan linked to