[FIX] gamification, website_profile : re-fix division by zero if user has no karma

If a new user try to access the elearning platform or his porfile, if he has
no karma, has it is the case for new users, he won't get any rank. So that
the next rank karma minus current rank karma equals zero.

To avoid this, next_rank will always be first one existing if the user has no
karma. Also, the current rank will not be displayed if the user has no karma.
finally, next rank will not be displayed if the user reached the last existing
rank.

Task ID : 1941250
PR #31512
This commit is contained in:
David Beguin
2019-03-06 14:08:24 +00:00
committed by Thibault Delavallée
parent c7cceaef79
commit 1949c796b5
5 changed files with 35 additions and 18 deletions
@@ -13,6 +13,12 @@
<record id="rank_newbie" model="gamification.karma.rank">
<field name="name">Newbie</field>
<field name="description" type="html"><p>You just began the adventure! Welcome!</p></field>
<field name="description_motivational" type="html">
<div class="media align-items-center">
<div class="media-body">Reach the next rank to start conquering the world!</div>
<img class="ml-3 img img-fluid" style="max-height: 72px;" src="/gamification/static/img/rank_misc_world.png"/>
</div>
</field>
<field name="karma_min">1</field>
<field name="image" type="base64" file="gamification/static/img/rank_newbie_badge.svg"/>
</record>
+12 -8
View File
@@ -47,9 +47,10 @@ class Users(models.Model):
@api.multi
def write(self, vals):
result = super(Users, self).write(vals)
if 'karma' in vals:
self._recompute_rank()
return super(Users, self).write(vals)
return result
@api.multi
def add_karma(self, karma):
@@ -72,13 +73,16 @@ class Users(models.Model):
self.env['gamification.karma.rank'].search([], order="karma_min DESC")]
for user in self:
old_rank = user.rank_id
for i in range(0, len(ranks)):
if user.karma >= ranks[i]['karma_min']:
user.write({
'rank_id': ranks[i]['rank'].id,
'next_rank_id': ranks[i - 1]['rank'].id if i > 0 else False
})
break
if user.karma == 0 and ranks:
user.write({'next_rank_id': ranks[-1]['rank'].id})
else:
for i in range(0, len(ranks)):
if user.karma >= ranks[i]['karma_min']:
user.write({
'rank_id': ranks[i]['rank'].id,
'next_rank_id': ranks[i - 1]['rank'].id if 0 < i else False
})
break
if old_rank != user.rank_id:
user._rank_changed()
Binary file not shown.

After

Width:  |  Height:  |  Size: 52 KiB

@@ -178,8 +178,8 @@
</div>
<button class="btn btn-sm d-md-none bg-white border" type="button" data-toggle="collapse" data-target="#o_wprofile_sidebar_collapse" aria-expanded="false" aria-controls="o_wprofile_sidebar_collapse">More info</button>
</div>
<div t-if="user.rank_id" class="collapse d-md-block" id="o_wprofile_sidebar_collapse">
<div class="mb-3">
<div class="collapse d-md-block" id="o_wprofile_sidebar_collapse">
<div t-if="user.next_rank_id" class="mb-3">
<small class="font-weight-bold mt-1">Next rank:</small>
<t t-call="website_profile.profile_next_rank_card">
<t t-set="img_max_width">40%</t>
@@ -228,9 +228,14 @@
</template>
<template id="profile_next_rank_card" name="Profile Next Rank Card">
<div class="o_wprofile_progress_circle">
<div t-if="user.next_rank_id" class="o_wprofile_progress_circle">
<svg viewBox="0 0 36 36" class="o_pc_circular_chart">
<t t-set="user_points" t-value="int(100*(user.karma - user.rank_id.karma_min)/(user.next_rank_id.karma_min - user.rank_id.karma_min))"/>
<t t-if="(user.next_rank_id.karma_min - user.rank_id.karma_min) > 0">
<t t-set="user_points" t-value="int(100*(user.karma - user.rank_id.karma_min)/(user.next_rank_id.karma_min - user.rank_id.karma_min))"/>
</t>
<t t-else="">
<t t-set="user_points" t-value="0"/>
</t>
<path class="o_pc_circle_bg" d="M18 2.0845 a 15.9155 15.9155 0 0 1 0 31.831 a 15.9155 15.9155 0 0 1 0 -31.831" />
<path class="o_pc_circle" t-attf-stroke-dasharray="#{user_points}, 100" d="M18 2.0845 a 15.9155 15.9155 0 0 1 0 31.831 a 15.9155 15.9155 0 0 1 0 -31.831" stroke="url(#gradient)" />
<linearGradient id="gradient">
@@ -342,17 +342,19 @@
<template id='slides_home_user_profile_small' name="User Profile">
<div class="o_wslides_home_aside">
<div class="d-flex align-items-center">
<div t-if="user.rank_id" class="d-flex align-items-center">
<span class="font-weight-bold text-muted mr-2">Current rank:</span>
<img t-attf-src="/web/image/gamification.karma.rank/#{user.rank_id.id}/image_small" height="16" alt="" class="mr-1"/>
<a href="/profile/ranks" t-field="user.rank_id"/>
</div>
<div class="font-weight-bold text-muted mt-1">Next rank:</div>
<t t-call="website_profile.profile_next_rank_card">
<t t-set="bg_class">bg-200</t>
<t t-set="img_max_width">50%</t>
<t t-if="user.next_rank_id">
<div class="font-weight-bold text-muted mt-1">Next rank:</div>
<t t-call="website_profile.profile_next_rank_card">
<t t-set="bg_class">bg-200</t>
<t t-set="img_max_width">50%</t>
</t>
<div t-field="user.next_rank_id.description_motivational"/>
</t>
<div t-field="user.next_rank_id.description_motivational"/>
</div>
</template>