Kentico CMS 6.0 Developer's Guide

Managing user badges

Managing user badges

Previous topic Next topic Mail us feedback on this topic!  

Managing user badges

Previous topic Next topic JavaScript is required for the print function Mail us feedback on this topic!  

Arrow


API examples for newer versions


Please visit the latest API Examples documentation to view API examples for newer versions of Kentico.



The following example assigns a badge to a specific user.

 

private bool AddBadgeToUser()
{
    // Get user object
    UserInfo user = UserInfoProvider.GetUserInfo(CMSContext.CurrentUser.UserName);
 
    // Get badge object
    BadgeInfo myBadge = BadgeInfoProvider.GetBadgeInfo("MyNewBadge");
 
    if ((user != null) && (myBadge != null))
    {
        // Add badge to user settings
        user.UserSettings.UserBadgeID = myBadge.BadgeID;
 
        // Update user object in database
        UserInfoProvider.SetUserInfo(user);
 
        return true;
    }
 
    return false;
}

 

The following example updates the activity points of a specific user.

 

private bool UpdateActivityPoints()
{
    // Get user
    UserInfo user = UserInfoProvider.GetUserInfo(CMSContext.CurrentUser.UserName);
 
    // If user exists
    if (user != null)
    {

      // Update activity points of the user
      BadgeInfoProvider.UpdateActivityPointsToUser(ActivityPointsEnum.BlogCommentPost, user.UserID, CMSContext.CurrentSiteName, true);

 
        return true;
    }
 
    return false;
}

 

The following example removes a user's badge.

 

private bool RemoveBadgeFromUser()
{

  // Get user
  UserInfo user = UserInfoProvider.GetUserInfo(CMSContext.CurrentUser.UserName);

 
    // If user exists
    if (user != null)
    {
        user.UserSettings.UserBadgeID = 0;
 
        // Save updates
        UserInfoProvider.SetUserInfo(user);
 
        return true;
    }
 
    return false;
}