Badges
List of examples:
- Creating a badge
- Updating a badge
- Updating multiple badges
- Assigning a badge to a user
- Updating activity points for users
- Removing a badge from a user
- Deleting a badge
Creating a badge
// Creates a new badge object
BadgeInfo newBadge = new BadgeInfo();
// Sets the properties for the new badge
newBadge.BadgeDisplayName = "New badge";
newBadge.BadgeName = "NewBadge";
newBadge.BadgeTopLimit = 50;
newBadge.BadgeImageURL = "Objects/CMS_Badge/Default/siteadmin.gif";
newBadge.BadgeIsAutomatic = true;
// Saves the badge to the database
BadgeInfoProvider.SetBadgeInfo(newBadge);
Updating a badge
// Gets the badge
BadgeInfo updateBadge = BadgeInfoProvider.GetBadgeInfo("NewBadge");
if (updateBadge != null)
{
// Updates the badge properties
updateBadge.BadgeDisplayName = updateBadge.BadgeDisplayName.ToLowerCSafe();
// Saves the badge changes to the database
BadgeInfoProvider.SetBadgeInfo(updateBadge);
}
Updating multiple badges
// Gets all badges whose code name starts with 'NewBadge'
var badges = BadgeInfoProvider.GetBadges().WhereStartsWith("BadgeName", "NewBadge");
// Loops through individual badges
foreach (BadgeInfo modifyBadge in badges)
{
// Updates the badge properties
modifyBadge.BadgeDisplayName = modifyBadge.BadgeDisplayName.ToUpper();
// Saves the changed badge to the database
BadgeInfoProvider.SetBadgeInfo(modifyBadge);
}
Assigning a badge to a user
// Gets the user
UserInfo user = UserInfoProvider.GetUserInfo("Andy");
// Gets the badge
BadgeInfo badge = BadgeInfoProvider.GetBadgeInfo("NewBadge");
if ((user != null) && (badge != null))
{
// Assigns the badge to the user
user.UserSettings.UserBadgeID = badge.BadgeID;
// Saves the updated user object to the database
UserInfoProvider.SetUserInfo(user);
}
Updating activity points for users
// Gets the user
UserInfo user = UserInfoProvider.GetUserInfo("Andy");
if (user != null)
{
// Adds activity points for the user
// The number of points depends on the type of the activity (blog comment post in this case) and the number of points assigned to the activity in the settings
BadgeInfoProvider.UpdateActivityPointsToUser(ActivityPointsEnum.BlogCommentPost, user.UserID, SiteContext.CurrentSiteName, true);
}
Removing a badge from a user
// Gets the user
UserInfo user = UserInfoProvider.GetUserInfo("Andy");
if (user != null)
{
//Removes the user's current badge
user.UserSettings.UserBadgeID = 0;
// Saves the updated user to the database
UserInfoProvider.SetUserInfo(user);
}
Deleting a badge
// Gets the badge
BadgeInfo deleteBadge = BadgeInfoProvider.GetBadgeInfo("NewBadge");
if (deleteBadge != null)
{
// Deletes the badge
BadgeInfoProvider.DeleteBadgeInfo(deleteBadge);
}