Ranking Part III

Posted by PointsToShare on Geeks with Blogs See other posts from Geeks with Blogs or by PointsToShare
Published on Wed, 23 Nov 2011 18:13:47 GMT Indexed on 2011/11/24 1:53 UTC
Read the original article Hit count: 496

Filed under:

© 2011 By: Dov Trietsch. All rights reserved

 

Ranking Part III

In a previous blogs “Ranking an Introduction” and  Ranking Part II” , you have already praised me in “Rank the Author” and learned how to create a new element on a page and how to place it where you need it. For this installment, I just added code to keep the number of votes (you vote by clicking one of the stars) and the total vote. Using these two, we can compute the average rating.

It’s a small step, but its purpose is to show that we do not need a detailed history in order to compute the average. A running total is sufficient. Please note that once you close the game, you will lose your previous total. In real life, we persist the totals in the list itself. We also keep a list of actual votes, but its purpose is to prevent double votes. If a person has already voted, his user id is already on the list and our program will check for it and bar the person from voting again. This is coded in an event receiver, which is a SharePoint server piece of code. I will show you how to do this part in a subsequent blog.

Again, go to the page and look at the code. The gist of it is here. avg, votes, and stars are global variables that I defined before.

function sendRate(sel)
{
//I hate long line so I created pieces of the message in their own vars
            var s1 = "Your Rating Was: ";
            var s2 = ".. ";
            var s3 = "\nVotes = ";
            var s4 = "\nTotal Stars = ";
            var s5 = "\nAverage = ";
            var s;
            s = parseInt(sel.id.replace("_", '')); // Get the selected star number
            votes = parseInt(votes) + 1;
            stars = parseInt(stars) + s;
            avg = parseFloat(stars) / parseFloat(votes);
            alert(s1 + sel.id + s2 +sel.title + s3 + votes + s4 + stars + s5 + avg);
}

Click on the link to play and examine “Ranking with Stats

That’s all folks!

 

© Geeks with Blogs or respective owner