by Keyvan Nayyeri via Keyvan Nayyeri on 11/17/2006 5:57:52 AM
Poll is a very nice feature in Community Server and I love it. Having a powerful voting system in forums (which is available through APIs to other applications) is very helpful.
Working with polls via APIs can be a real world scenario because some people ask for poll functionality in blogs on Community Server forums. Actually I haven't implemented a voting system for blog with Community Server APIs to be aware of any possible error or issue for working with them but provided methods and properties should work for all applications. Although appropriate user interface isn't provided for all applications to let you create your polls and this is left to you. I hope I can find time to work on creating a custom control to add polls to blogs as a useful feature and good practice on these APIs but if you're interested to write someone for Community Server this is a good point.
Before start talking in details it's better to give a short introduction about some words that I frequently use in this post:
In Community Server each post may have no or one poll. Each poll may have one or more answers. Each user can send a vote. A vote assigns a user to an answer. Later you can use these information to present a summary of polls to end users.
As you probably know, Post class has three properties to let you define the title, description and expiration date for a poll (PollTitle, PollDescription, PollExpirationDate) as well as a Boolean HasPoll property to specify if a post has a poll. Using these properties you can define a poll but what about poll items and answers?
Four classes in CommunityServer.Components namespace help you to work with poll items and their summary:
Let me talk about each of above classes in some details:
Each PollItem is an answer to a poll. It has some properties as follows:
You can create an instance of PollItem by passing required properties to its constructor as well.
It's a user's vote to a poll. It assigns an answer to a user. It has some ReadOnly properties to let you access to some common information easier. You can create an instance of PollVote object by passing a PollSummary object as well as UserID and AnswerID to its constructor. PollSummary will be described in next section.
This class is very helpful because lets you to have all information about your poll in one place. You pass a Post object to its constructor and get a summary of available poll for that post and its answers and votes. Like PollVote, all of its properties are ReadOnly.
There are two important properties for this object: Answers is an ArrayList of PollItem objects which contains all answers to poll and Voters is a HashTable of voters (users and their votes).
In addition to these properties, PollSummary provides some methods which are actually some handy shortcuts to help you retrieve some common information about poll:
This object is very simple. It keeps a list of PollVote objects for a poll. Its PollVotes property returns an ArrayList of PollVote objects, its HasResults Boolean property checks for any available PollVote in list and its TotalRecords integer property returns the number of PollVote objects in the list.
So far we've talked about objects in theory but how we can add a vote to a poll? Answer is simple and doesn't need much information about above theories!
CommunityServer.Polls namespace has two static methods which let you to get a PollSummary for a post and add a vote to a poll in a post:
Below is an example. AddVote() method gets a string value of an answer which user has chosen and creates a vote for that user and its chosen answer.
void AddVote(String answerID)
{
CSContext context = CSContext.Current;
Polls.Vote(context.Post.PostID, context.User.UserID, answerID);
}
In following code, CheckForVote() Boolean function checks to make sure if current user (who is viewing the site) has voted to the poll of current post that is being viewed or not.
bool CheckForVote()
PollSummary summary = Polls.GetPoll(context.Post);
return summary.HasVoted(context.User.UserID);
Below ReturnUserAnswer() is a string function which checks to make sure if current user has voted then returns its answer text.
String ReturnUserAnswer()
if (summary.HasVoted(context.User.UserID))
PollItem item = summary.GetUserVote(context.User.UserID);
return item.Answer;
return null;
And as the last example: GetFirstVoterName in following code returns the DisplayName of first user who has voted to poll of current post.
String GetFirstVoterName()
PollVoteSet votes = summary.GetUserVotes(0, 5);
if (votes.HasResults)
PollVote vote = votes.PollVotes[0] as PollVote;
return vote.UserDisplayName;
Now playing: Yanni - Nightingale
Original Post: CS Dev Guide: Polls
The content of the postings is owned by the respective author. CSharpFeeds is not responsible for the contents of the postings. This site is automatically generated and cannot be reviewed for abusive content. If you find abusive content on CSharpFeeds, please contact us. Designated trademarks and brands are the property of their respective owners. All rights reserved.