Skip to content

Commit

Permalink
feat: Allows to choose a date by displaying the profile achievement d…
Browse files Browse the repository at this point in the history
…rawer - MEED-7482 - Meeds-io/MIPs#154 - Meeds-io/MIPs#150 (#1758)

This PR will allow to customize date by displaying the profile
achievement drawer.
  • Loading branch information
boubaker committed Oct 23, 2024
1 parent 98dd956 commit b44e97b
Show file tree
Hide file tree
Showing 21 changed files with 342 additions and 243 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,7 @@ gamification.profileStats.MONTH=Current Month Statistics
gamification.profileStats.QUARTER=Current Quarter Statistics
gamification.profileStats.ALL=All time statistics
gamification.profileStats.achievements=Contributions
gamification.profileStats.to=to

gamification.overview.rewardsTitle=Rewards
gamification.overview.rewards.earningsTitle=Estimated
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@
:identity-id="user.identityId"
:score="user.score"
:period="period"
:program-id="programId"
central-points
@open="$refs.detailsDrawer.open(user, period)" />
<users-leaderboard-profile-achievements-drawer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
:programs="programs"
:period="period"
:program-id="programId"
:date-in-seconds="dateInSeconds"
@select="programId = $event" />
</template>

Expand All @@ -64,6 +65,8 @@
:identity-id="user.identityId"
:program-id="programId"
:page-size="pageSize"
:from-date-in-second="fromDateInSecond"
:to-date-in-second="toDateInSecond"
@loading="loading = $event"
@has-more="hasMore = $event" />
</div>
Expand Down Expand Up @@ -105,6 +108,14 @@ export default {
type: Boolean,
default: false,
},
fromDateInSecond: {
type: Number,
default: 0,
},
toDateInSecond: {
type: Number,
default: 0,
},
},
data: () => ({
drawer: false,
Expand All @@ -115,6 +126,12 @@ export default {
programs: null,
programId: null,
pageSize: Math.max(10, parseInt((window.innerHeight - 560) / 62)),
lang: eXo.env.portal.language,
dateFormat: {
year: 'numeric',
month: 'short',
day: 'numeric',
},
}),
computed: {
header() {
Expand All @@ -124,8 +141,19 @@ export default {
})
: this.$t('gamification.overview.userAchievementsList.drawer.title');
},
rangeDateTimeTitle() {
return `${this.fromDateFormat} ${this.$t('gamification.profileStats.to')} ${this.toDateFormat}`;
},
fromDateFormat() {
return new window.Intl.DateTimeFormat(this.lang, this.dateFormat).format(new Date(this.fromDateInSecond * 1000 - new Date().getTimezoneOffset() * 60 * 1000));
},
toDateFormat() {
return new window.Intl.DateTimeFormat(this.lang, this.dateFormat)
.format(new Date(this.toDateInSecond * 1000 - 86400 * 1000 - new Date().getTimezoneOffset() * 60 * 1000));
},
title() {
return this.$t(`gamification.profileStats.${this.period.toUpperCase()}`);
return this.dateInSeconds ? this.rangeDateTimeTitle : this.$t(`gamification.profileStats.${this.period.toUpperCase()}`);
},
program() {
return this.programId && this.programs?.find?.(p => p.id === this.programId);
Expand All @@ -139,6 +167,9 @@ export default {
1: '</a>',
});
},
dateInSeconds() {
return (this.fromDateInSecond + this.toDateInSecond) / 2;
}
},
created() {
window.addEventListener('rules-list-drawer-open', this.openActionsList);
Expand All @@ -156,6 +187,8 @@ export default {
this.loading = true;
this.$leaderboardService.getLeaderboard({
identityId,
period: this.period,
dateInSeconds: this.dateInSeconds,
limit: 0,
})
.then(data => this.user = data.find(u => Number(u.identityId) === Number(identityId)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ export default {
type: String,
default: () => 'WEEK',
},
dateInSeconds: {
type: Number,
default: () => 0,
},
chartType: {
type: String,
default: () => 'pie',
Expand Down Expand Up @@ -178,7 +182,7 @@ export default {
}
return new Promise((resolve, reject) => {
this.$leaderboardService.getStats(this.identityId, this.period)
this.$leaderboardService.getStats(this.identityId, this.period, this.dateInSeconds)
.then(stats => {
let id = 0;
this.chartData = stats.map(s => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,9 @@ export default {
},
actionValueExtension() {
return this.actionEventName
&& Object.values(this.$root.actionValueExtensions)
.sort((ext1, ext2) => (ext1.rank || 0) - (ext2.rank || 0))
.find(extension => extension?.match?.(this.actionEventName))
&& this.$root.actionValueExtensions && Object.values(this.$root.actionValueExtensions)
.sort((ext1, ext2) => (ext1.rank || 0) - (ext2.rank || 0))
.find(extension => extension?.match?.(this.actionEventName))
|| null;
},
linkExtensionMethod() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,22 @@ export default {
type: Number,
default: () => 10,
},
fromDateInSecond: {
type: Number,
default: () => 0,
},
toDateInSecond: {
type: Number,
default: () => 0,
},
},
data: () => ({
loading: false,
totalSize: 0,
sortBy: 'date',
sortDescending: true,
realizations: [],
lang: eXo.env.portal.language,
}),
computed: {
hasMore() {
Expand All @@ -61,6 +70,8 @@ export default {
programIds: this.programId && [this.programId],
allPrograms: true,
sortBy: this.sortBy,
fromDate: this.fromDateInSecond ? new Date(this.fromDateInSecond * 1000).toISOString() : null,
toDate: this.toDateInSecond ? new Date(this.toDateInSecond * 1000).toISOString() : null,
sortDescending: this.sortDescending,
};
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
:period="period"
:program-id="programId"
:central-points="centralPoints"
:date-in-seconds="dateInSeconds"
@select="$emit('select', $event)"
@open="$emit('open')" />
</template>
Expand All @@ -50,6 +51,10 @@ export default {
type: String,
default: () => 'WEEK',
},
dateInSeconds: {
type: Number,
default: () => 0,
},
centralPoints: {
type: Boolean,
default: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ export function getLeaderboard(filter) {
if (filter.identityId) {
formData.append('identityId', filter.identityId);
}
if (filter.dateInSeconds) {
formData.append('dateInSeconds', filter.dateInSeconds);
}
if (filter.offset) {
formData.append('offset', filter.offset);
}
Expand All @@ -89,8 +92,8 @@ export function getLeaderboard(filter) {
});
}

export function getStats(identityId, period) {
return fetch(`${eXo.env.portal.context}/${eXo.env.portal.rest}/gamification/leaderboard/stats/${identityId}?period=${period || 'WEEK'}`, {
export function getStats(identityId, period, dateInSeconds) {
return fetch(`${eXo.env.portal.context}/${eXo.env.portal.rest}/gamification/leaderboard/stats/${identityId}?period=${period || 'WEEK'}&dateInSeconds=${dateInSeconds || '0'}`, {
method: 'GET',
credentials: 'include',
}).then((resp) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,10 @@ public int getLeaderboardRank(IdentityType earnerType, String earnerIdentityId)
}
}

public int getLeaderboardRankByDateAndProgramId(IdentityType earnerType, String earnerIdentityId, Date date, long domainId) {
public int getLeaderboardRankByDatesAndProgramId(IdentityType earnerType, String earnerIdentityId, Date fromDate, Date toDate, long domainId) {
Query query = getEntityManager().createNamedQuery("RealizationEntity.getLeaderboardRankByDateAndProgramId");
query.setParameter(DATE_PARAM_NAME, date, TemporalType.DATE)
query.setParameter(FROM_DATE_PARAM_NAME, fromDate, TemporalType.DATE)
.setParameter(TO_DATE_PARAM_NAME, toDate, TemporalType.DATE)
.setParameter(PROGRAM_ID_PARAM_NAME, domainId)
.setParameter(EARNER_TYPE_PARAM_NAME, earnerType.ordinal())
.setParameter(EARNER_ID_PARAM_NAME, Long.parseLong(earnerIdentityId))
Expand All @@ -119,9 +120,10 @@ public int getLeaderboardRankByProgramId(IdentityType earnerType, String earnerI
}
}

public int getLeaderboardRankByDate(IdentityType earnerType, String earnerIdentityId, Date fromDate) {
Query query = getEntityManager().createNamedQuery("RealizationEntity.getLeaderboardRankByDate");
query.setParameter(DATE_PARAM_NAME, fromDate, TemporalType.DATE)
public int getLeaderboardRankByDates(IdentityType earnerType, String earnerIdentityId, Date fromDate, Date toDate) {
Query query = getEntityManager().createNamedQuery("RealizationEntity.getLeaderboardRankByDates");
query.setParameter(FROM_DATE_PARAM_NAME, fromDate, TemporalType.DATE)
.setParameter(TO_DATE_PARAM_NAME, toDate, TemporalType.DATE)
.setParameter(EARNER_TYPE_PARAM_NAME, earnerType.ordinal())
.setParameter(EARNER_ID_PARAM_NAME, Long.parseLong(earnerIdentityId))
.setParameter(STATUS_PARAM_NAME, RealizationStatus.ACCEPTED.ordinal());
Expand Down Expand Up @@ -153,26 +155,28 @@ public List<StandardLeaderboard> getLeaderboardByProgramId(long domainId, Identi
return query.getResultList();
}

public List<StandardLeaderboard> getLeaderboardByDate(Date fromDate, IdentityType earnerType, int offset, int limit) {
public List<StandardLeaderboard> getLeaderboardByDates(Date fromDate, Date toDate, IdentityType earnerType, int offset, int limit) {
TypedQuery<StandardLeaderboard> query = getEntityManager().createNamedQuery("RealizationEntity.getLeaderboardByDate",
StandardLeaderboard.class);
query.setParameter(DATE_PARAM_NAME, fromDate)
query.setParameter(FROM_DATE_PARAM_NAME, fromDate)
.setParameter(TO_DATE_PARAM_NAME, toDate)
.setParameter(EARNER_TYPE_PARAM_NAME, earnerType)
.setParameter(STATUS_PARAM_NAME, RealizationStatus.ACCEPTED);
query.setFirstResult(offset);
query.setMaxResults(limit);
return query.getResultList();
}

public List<StandardLeaderboard> getLeaderboardByDateAndProgramId(Date fromDate,
IdentityType earnerType,
long domainId,
int offset,
int limit) {
public List<StandardLeaderboard> getLeaderboardByDatesAndProgramId(Date fromDate,
Date toDate, IdentityType earnerType,
long domainId,
int offset,
int limit) {
TypedQuery<StandardLeaderboard> query =
getEntityManager().createNamedQuery("RealizationEntity.getLeaderboardByDateAndProgramId",
StandardLeaderboard.class);
query.setParameter(DATE_PARAM_NAME, fromDate)
query.setParameter(FROM_DATE_PARAM_NAME, fromDate)
.setParameter(TO_DATE_PARAM_NAME, toDate)
.setParameter(EARNER_TYPE_PARAM_NAME, earnerType)
.setParameter(PROGRAM_ID_PARAM_NAME, domainId)
.setParameter(STATUS_PARAM_NAME, RealizationStatus.ACCEPTED);
Expand All @@ -192,19 +196,21 @@ public long getScoreByIdentityId(String earnerIdentityId) {
}
}

public List<PiechartLeaderboard> getLeaderboardStatsByIdentityId(String earnerId, Date fromDate, Date toDate) {
public List<PiechartLeaderboard> getLeaderboardStatsByIdentityIdAndDates(String earnerId, Date fromDate, Date toDate) {
TypedQuery<PiechartLeaderboard> query;
if (fromDate != null && toDate != null) {
query = getEntityManager().createNamedQuery("RealizationEntity.getLeaderboardStatsByIdentityIdAndDates",
PiechartLeaderboard.class);
query.setParameter(EARNER_ID_PARAM_NAME, earnerId)
.setParameter(FROM_DATE_PARAM_NAME, fromDate)
.setParameter(TO_DATE_PARAM_NAME, toDate)
.setParameter(STATUS_PARAM_NAME, RealizationStatus.ACCEPTED);
} else {
query = getEntityManager().createNamedQuery("RealizationEntity.getLeaderboardStatsByIdentityId", PiechartLeaderboard.class);
query.setParameter(EARNER_ID_PARAM_NAME, earnerId).setParameter(STATUS_PARAM_NAME, RealizationStatus.ACCEPTED);
}
query = getEntityManager().createNamedQuery("RealizationEntity.getLeaderboardStatsByIdentityIdAndDates",
PiechartLeaderboard.class);
query.setParameter(EARNER_ID_PARAM_NAME, earnerId)
.setParameter(FROM_DATE_PARAM_NAME, fromDate)
.setParameter(TO_DATE_PARAM_NAME, toDate)
.setParameter(STATUS_PARAM_NAME, RealizationStatus.ACCEPTED);
return query.getResultList();
}

public List<PiechartLeaderboard> getLeaderboardStatsByIdentityId(String earnerId) {
TypedQuery<PiechartLeaderboard> query;
query = getEntityManager().createNamedQuery("RealizationEntity.getLeaderboardStatsByIdentityId", PiechartLeaderboard.class);
query.setParameter(EARNER_ID_PARAM_NAME, earnerId).setParameter(STATUS_PARAM_NAME, RealizationStatus.ACCEPTED);
return query.getResultList();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@
+ " SELECT earnerId, ROW_NUMBER() OVER() rankIndex FROM "
+ " (SELECT g.EARNER_ID earnerId, SUM(g.ACTION_SCORE) total"
+ " FROM GAMIFICATION_ACTIONS_HISTORY g"
+ " WHERE g.CREATED_DATE >= :date"
+ " WHERE g.CREATED_DATE >= :fromDate"
+ " AND g.CREATED_DATE < :toDate"
+ " AND g.DOMAIN_ID = :domainId"
+ " AND g.EARNER_TYPE = :earnerType"
+ " AND g.STATUS = :status "
Expand All @@ -81,12 +82,13 @@
+ ") LEADERBOARD WHERE LEADERBOARD.earnerId = :earnerId"
)
@NamedNativeQuery(
name = "RealizationEntity.getLeaderboardRankByDate",
name = "RealizationEntity.getLeaderboardRankByDates",
query = "SELECT LEADERBOARD.rankIndex FROM ("
+ " SELECT earnerId, ROW_NUMBER() OVER() rankIndex FROM "
+ " (SELECT g.EARNER_ID earnerId, SUM(g.ACTION_SCORE) total"
+ " FROM GAMIFICATION_ACTIONS_HISTORY g"
+ " WHERE g.CREATED_DATE >= :date"
+ " WHERE g.CREATED_DATE >= :fromDate"
+ " AND g.CREATED_DATE < :toDate"
+ " AND g.EARNER_TYPE = :earnerType"
+ " AND g.STATUS = :status "
+ " GROUP BY g.EARNER_ID"
Expand All @@ -107,7 +109,8 @@
name = "RealizationEntity.getLeaderboardByDateAndProgramId",
query = "SELECT new io.meeds.gamification.model.StandardLeaderboard(g.earnerId as earnerId, SUM(g.actionScore) as total)"
+ " FROM RealizationEntity g"
+ " WHERE g.createdDate >= :date"
+ " WHERE g.createdDate >= :fromDate"
+ " AND g.createdDate < :toDate"
+ " AND g.domainEntity.id = :domainId"
+ " AND g.earnerType = :earnerType"
+ " AND g.status = :status"
Expand All @@ -128,7 +131,8 @@
name = "RealizationEntity.getLeaderboardByDate",
query = "SELECT new io.meeds.gamification.model.StandardLeaderboard(g.earnerId as earnerId, SUM(g.actionScore) as total)"
+ " FROM RealizationEntity g"
+ " WHERE g.createdDate >= :date"
+ " WHERE g.createdDate >= :fromDate"
+ " AND g.createdDate < :toDate"
+ " AND g.earnerType = :earnerType"
+ " AND g.status = :status"
+ " GROUP BY g.earnerId"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ public class LeaderboardFilter {

private Period period = Period.WEEK;

private long medianDateInSeconds;

private int offset = 0;

private int limit = 10;
Expand Down
Loading

0 comments on commit b44e97b

Please sign in to comment.