Skip to content

Commit

Permalink
[IMP] Gamification: New report templated
Browse files Browse the repository at this point in the history
  • Loading branch information
photoscene authored and rrahir committed May 22, 2018
1 parent 4699f3c commit 6952334
Show file tree
Hide file tree
Showing 8 changed files with 318 additions and 227 deletions.
2 changes: 1 addition & 1 deletion addons/gamification/__manifest__.py
Expand Up @@ -5,7 +5,7 @@
'version': '1.0',
'sequence': 160,
'category': 'Human Resources',
'website' : 'https://www.odoo.com/page/gamification',
'website': 'https://www.odoo.com/page/gamification',
'depends': ['mail', 'web_kanban_gauge'],
'description': """
Gamification process
Expand Down
475 changes: 284 additions & 191 deletions addons/gamification/data/goal_base.xml

Large diffs are not rendered by default.

64 changes: 33 additions & 31 deletions addons/gamification/models/challenge.py
Expand Up @@ -105,7 +105,7 @@ class Challenge(models.Model):
visibility_mode = fields.Selection([
('personal', "Individual Goals"),
('ranking', "Leader Board (Group Ranking)"),
], default='personal',
], default='ranking',
string="Display Mode", required=True)

report_message_frequency = fields.Selection([
Expand All @@ -118,7 +118,6 @@ class Challenge(models.Model):
], default='never',
string="Report Frequency", required=True)
report_message_group_id = fields.Many2one('mail.channel', string="Send a copy to", help="Group that will receive a copy of the report in addition to the user")
report_template_id = fields.Many2one('mail.template', default=lambda self: self._get_report_template(), string="Report Template", required=True)
remind_update_delay = fields.Integer("Non-updated manual goals will be reminded after", help="Never reminded if no value or zero is specified.")
last_report_date = fields.Date("Last Report Date", default=fields.Date.today)
next_report_date = fields.Date("Next Report Date", compute='_get_next_report_date', store=True)
Expand All @@ -135,6 +134,7 @@ class Challenge(models.Model):
'monthly': relativedelta(months=1),
'yearly': relativedelta(years=1),
}

@api.depends('last_report_date', 'report_message_frequency')
def _get_next_report_date(self):
""" Return the next report date based on the last report date and
Expand All @@ -149,11 +149,6 @@ def _get_next_report_date(self):
else:
challenge.next_report_date = False

def _get_report_template(self):
template = self.env.ref('gamification.simple_report_template', raise_if_not_found=False)

return template.id if template else False

@api.model
def create(self, vals):
"""Overwrite the create method to add the user of groups"""
Expand Down Expand Up @@ -349,7 +344,7 @@ def _generate_goals_from_challenge(self):
if end_date:
date_clause += "AND g.end_date = %s"
query_params.append(end_date)

query = """SELECT u.id AS user_id
FROM res_users u
LEFT JOIN gamification_goal g
Expand All @@ -363,7 +358,7 @@ def _generate_goals_from_challenge(self):
participant_user_ids = set(challenge.user_ids.ids)
user_squating_challenge_ids = user_with_goal_ids - participant_user_ids
if user_squating_challenge_ids:
# users that used to match the challenge
# users that used to match the challenge
Goals.search([
('challenge_id', '=', challenge.id),
('user_id', 'in', list(user_squating_challenge_ids))
Expand Down Expand Up @@ -451,7 +446,7 @@ def _get_serialized_challenge_lines(self, user=(), restrict_goals=(), restrict_t
'action': <{True,False}>,
'display_mode': <{progress,boolean}>,
'target': <challenge line target>,
'state': <gamification.goal state {draft,inprogress,reached,failed,canceled}>,
'state': <gamification.goal state {draft,inprogress,reached,failed,canceled}>,
'completeness': <percentage>,
'current': <current value>,
}
Expand All @@ -468,6 +463,7 @@ def _get_serialized_challenge_lines(self, user=(), restrict_goals=(), restrict_t
'computation_mode': line.definition_id.computation_mode,
'monetary': line.definition_id.monetary,
'suffix': line.definition_id.suffix,
'full_suffix': line.definition_id.full_suffix,
'action': True if line.definition_id.action_id else False,
'display_mode': line.definition_id.display_mode,
'target': line.target_goal,
Expand All @@ -494,10 +490,10 @@ def _get_serialized_challenge_lines(self, user=(), restrict_goals=(), restrict_t
goal = Goals.search(domain, limit=1)
if not goal:
continue

if goal.state != 'reached':
return []
line_data.update(goal.read(['id', 'current', 'completeness', 'state'])[0])
line_data['user_id'] = goal.user_id
res_lines.append(line_data)
continue

Expand All @@ -516,8 +512,7 @@ def _get_serialized_challenge_lines(self, user=(), restrict_goals=(), restrict_t

line_data['goals'].append({
'id': goal.id,
'user_id': goal.user_id.id,
'name': goal.user_id.name,
'user_id': goal.user_id,
'rank': ranking,
'current': goal.current,
'completeness': goal.completeness,
Expand All @@ -543,42 +538,49 @@ def report_progress(self, users=(), subset_goals=False):

MailTemplates = self.env['mail.template']
if challenge.visibility_mode == 'ranking':
template_id = self.env.ref('gamification.simple_report_template_ranking', raise_if_not_found=False)
lines_boards = challenge._get_serialized_challenge_lines(restrict_goals=subset_goals)
ctx = {
'challenge_lines': lines_boards,
'default_use_template': bool(template_id),
'default_template_id': template_id,
'custom_layout': "gamification.challenge_mail_template",
}
body_html = MailTemplates.with_context(ctx).render_template(template_id.body_html, 'gamification.challenge', challenge.id)

body_html = MailTemplates.with_context(challenge_lines=lines_boards).render_template(challenge.report_template_id.body_html, 'gamification.challenge', challenge.id)
challenge.with_context(ctx).message_post_with_template(
template_id.id,
res_id=challenge.id,
model='gamification.challenge',
partner_ids=challenge.mapped('user_ids.partner_id.id')
)

# send to every follower and participant of the challenge
challenge.message_post(
body=body_html,
partner_ids=challenge.mapped('user_ids.partner_id.id'),
subtype='mail.mt_comment')
if challenge.report_message_group_id:
challenge.report_message_group_id.message_post(
body=body_html,
subtype='mail.mt_comment')

else:
# generate individual reports
template_id = self.env.ref('gamification.simple_report_template_personal', raise_if_not_found=False)
ctx = {
'default_use_template': bool(template_id),
'default_template_id': template_id,
}
for user in (users or challenge.user_ids):
lines = challenge._get_serialized_challenge_lines(user, restrict_goals=subset_goals)
if not lines:
continue

body_html = MailTemplates.sudo(user).with_context(challenge_lines=lines).render_template(
challenge.report_template_id.body_html,
'gamification.challenge',
challenge.id)
ctx.update(challenge_lines=lines)

# send message only to users, not on the challenge
self.env['gamification.challenge'].message_post(
body=body_html,
partner_ids=[(4, user.partner_id.id)],
subtype='mail.mt_comment'
)
template_id.with_context(ctx).send_mail(challenge.id, email_values={'recipient_ids': [(4, user.partner_id.id)]})

if challenge.report_message_group_id:
challenge.report_message_group_id.message_post(
body=body_html,
subtype='mail.mt_comment')
body=body_html,
subtype='mail.mt_comment')
return challenge.write({'last_report_date': fields.Date.today()})

##### Challenges #####
Expand Down Expand Up @@ -721,7 +723,7 @@ def _get_topN_users(self, n):
for goal in goal_ids:
if goal.state != 'reached':
all_reached = False
if goal.definition_condition == 'higher':
if goal.definition_condition == 'higher' and goal.target_goal > 0:
# can be over 100
total_completeness += 100.0 * goal.current / goal.target_goal
elif goal.state == 'reached':
Expand Down
3 changes: 0 additions & 3 deletions addons/gamification/models/goal.py
Expand Up @@ -193,9 +193,6 @@ def _get_completion(self):
"""Return the percentage of completeness of the goal, between 0 and 100"""
for goal in self:
if goal.definition_condition == 'higher':
if goal.current >= goal.target_goal:
goal.completeness = 100.0
else:
goal.completeness = round(100.0 * goal.current / goal.target_goal, 2)
elif goal.current < goal.target_goal:
# a goal 'lower than' has only two values possible: 0 or 100%
Expand Down
Binary file added addons/gamification/static/img/arrow_down.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added addons/gamification/static/img/arrow_up.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 0 additions & 1 deletion addons/gamification/views/challenge.xml
Expand Up @@ -112,7 +112,6 @@
</div>
<group colspan="4">
<field name="report_message_frequency"/>
<field name="report_template_id" attrs="{'invisible': [('report_message_frequency','=','never')]}" />
<field name="report_message_group_id" attrs="{'invisible': [('report_message_frequency','=','never')]}" />
</group>
</group>
Expand Down

0 comments on commit 6952334

Please sign in to comment.