Beispiel #1
0
        /// <summary>
        /// Processes the user for the specified badge type
        /// </summary>
        /// <param name="badgeType"></param>
        /// <param name="user"></param>
        /// <returns>True if badge was awarded</returns>
        public bool ProcessBadge(BadgeType badgeType, MembershipUser user)
        {
            var databaseUpdateNeeded = false;

            var e = new BadgeEventArgs {
                User = user, BadgeType = badgeType
            };

            EventManager.Instance.FireBeforeBadgeAwarded(this, e);

            if (!e.Cancel)
            {
                if (_badges.ContainsKey(badgeType))
                {
                    if (!RecentlyProcessed(badgeType, user))
                    {
                        databaseUpdateNeeded = true;

                        var badgeSet = _badges[badgeType];

                        foreach (var badgeMapping in badgeSet)
                        {
                            if (!BadgeCanBeAwarded(user, badgeMapping))
                            {
                                continue;
                            }

                            // Instantiate the badge and execute the rule
                            var badge = GetInstance <IBadge>(badgeMapping);

                            if (badge != null)
                            {
                                var dbBadge = Get(badgeMapping.DbBadge.Id);

                                // Award badge?
                                if (badge.Rule(user))
                                {
                                    // Re-fetch the badge otherwise system will try and create new badges!
                                    if (dbBadge.AwardsPoints != null && dbBadge.AwardsPoints > 0)
                                    {
                                        var points = new MembershipUserPoints
                                        {
                                            Points      = (int)dbBadge.AwardsPoints,
                                            PointsFor   = PointsFor.Badge,
                                            PointsForId = dbBadge.Id,
                                            User        = user
                                        };
                                        _membershipUserPointsService.Add(points);
                                    }
                                    user.Badges.Add(dbBadge);
                                    //_activityService.BadgeAwarded(badgeMapping.DbBadge, user, DateTime.UtcNow);
                                    var badgeActivity = BadgeActivity.GenerateMappedRecord(badgeMapping.DbBadge, user, DateTime.UtcNow);
                                    _context.Activity.Add(badgeActivity);
                                    EventManager.Instance.FireAfterBadgeAwarded(this,
                                                                                new BadgeEventArgs
                                    {
                                        User      = user,
                                        BadgeType = badgeType
                                    });
                                }
                                //else
                                //{
                                //    // If we get here the user should not have the badge
                                //    // Remove the badge if the user no longer has the criteria to be awarded it
                                //    // and also remove any points associated with it.
                                //    user.Badges.Remove(dbBadge);
                                //    _membershipUserPointsService.Delete(user, PointsFor.Badge, dbBadge.Id);
                                //}
                            }
                        }
                    }
                }
            }
            return(databaseUpdateNeeded);
        }
Beispiel #2
0
        /// <summary>
        ///     New badge has been awarded
        /// </summary>
        /// <param name="badge"></param>
        /// <param name="user"> </param>
        /// <param name="timestamp"> </param>
        public void BadgeAwarded(Badge badge, MembershipUser user, DateTime timestamp)
        {
            var badgeActivity = BadgeActivity.GenerateMappedRecord(badge, user, timestamp);

            Add(badgeActivity);
        }
Beispiel #3
0
        /// <summary>
        ///     Processes the user for the specified badge type
        /// </summary>
        /// <param name="badgeType"></param>
        /// <param name="user"></param>
        /// <returns>True if badge was awarded</returns>
        public async Task <bool> ProcessBadge(BadgeType badgeType, MembershipUser user)
        {
            var databaseUpdateNeeded = false;

            var e = new BadgeEventArgs {
                User = user, BadgeType = badgeType
            };

            EventManager.Instance.FireBeforeBadgeAwarded(this, e);

            if (!e.Cancel)
            {
                try
                {
                    if (_badges.ContainsKey(badgeType))
                    {
                        if (!RecentlyProcessed(badgeType, user))
                        {
                            databaseUpdateNeeded = true;

                            var badgeSet = _badges[badgeType];

                            foreach (var badgeMapping in badgeSet)
                            {
                                if (!BadgeCanBeAwarded(user, badgeMapping))
                                {
                                    continue;
                                }

                                // Instantiate the badge and execute the rule
                                IBadge badge;
                                if (badgeMapping.BadgeClassInstance != null)
                                {
                                    badge = badgeMapping.BadgeClassInstance;
                                }
                                else
                                {
                                    badgeMapping.BadgeClassInstance = UnityHelper.Container.Resolve(badgeMapping.BadgeClass) as IBadge;
                                    badge = badgeMapping.BadgeClassInstance;
                                }

                                if (badge != null)
                                {
                                    var dbBadge = Get(badgeMapping.DbBadge.Id);

                                    // Award badge?
                                    if (badge.Rule(user))
                                    {
                                        // Re-fetch the badge otherwise system will try and create new badges!
                                        if (dbBadge.AwardsPoints != null && dbBadge.AwardsPoints > 0)
                                        {
                                            var points = new MembershipUserPoints
                                            {
                                                Points      = (int)dbBadge.AwardsPoints,
                                                PointsFor   = PointsFor.Badge,
                                                PointsForId = dbBadge.Id,
                                                User        = user
                                            };
                                            var pointsAddResult = await _membershipUserPointsService.Add(points);

                                            if (!pointsAddResult.Successful)
                                            {
                                                _loggingService.Error(pointsAddResult.ProcessLog.FirstOrDefault());
                                                return(false);
                                            }
                                        }
                                        user.Badges.Add(dbBadge);
                                        //_activityService.BadgeAwarded(badgeMapping.DbBadge, user, DateTime.UtcNow);
                                        var badgeActivity =
                                            BadgeActivity.GenerateMappedRecord(badgeMapping.DbBadge, user, DateTime.UtcNow);
                                        _context.Activity.Add(badgeActivity);
                                        EventManager.Instance.FireAfterBadgeAwarded(this,
                                                                                    new BadgeEventArgs
                                        {
                                            User      = user,
                                            BadgeType = badgeType
                                        });
                                    }
                                    //else
                                    //{
                                    //    // If we get here the user should not have the badge
                                    //    // Remove the badge if the user no longer has the criteria to be awarded it
                                    //    // and also remove any points associated with it.
                                    //    user.Badges.Remove(dbBadge);
                                    //    _membershipUserPointsService.Delete(user, PointsFor.Badge, dbBadge.Id);
                                    //}
                                }
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    _loggingService.Error(ex);
                }
            }
            return(databaseUpdateNeeded);
        }