/// <summary> /// btnAddActivity_OnClick handler. /// </summary> protected void btnAddActivity_OnClick(object sender, EventArgs e) { try // try to handle btnAddActivity_OnClick { if (!Page.IsValid) { return; } int id = -1; int activityPoints = 0; if (!Int32.TryParse(ddlActivities.SelectedValue, out id)) { throw new Exception(String.Format("{0} value can't be parsed", "cbActivities")); } if (!Int32.TryParse(tbPoints.Text, out activityPoints)) { throw new Exception(String.Format("{0} value can't be parsed", "tbPoints")); } var rows = (Session[ROWS_OBJECT] as List <BadgeActivityRow>); { if (rows.Any(r => r.DataItem.ActivityId == id && r.State != RowState.Deleted)) { return; // return if reference is already added } } Activity activity = null; if ((activity = UnitOfWork.Activities.GetBy(id)) == null) { throw new NullReferenceException(String.Format("Activity with ID: {0} is not found", id)); } var item = new BadgeActivity(-1, activityPoints) { ActivityId = activity.ActivityId, // define activity ID DesktopModuleId = activity.DesktopModuleId, // define desktop module ID DesktopModuleName = activity.DesktopModuleName, // define desktop module name Name = activity.Name // define name }; rows.Add(new BadgeActivityRow(item, RowState.New)); { rptBadgeActivities.DataBind(); } } catch (Exception ex) // catch exceptions { Exceptions.ProcessModuleLoadException(this, ex); } }
/// <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); }
/// <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); }
/// <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); }
/// <summary> /// /// </summary> public BadgeActivityRow(BadgeActivity data, RowState state) : this() { DataItem = data; State = state; }