ratdoc = xwiki.getDocument("XWiki.RatStats")
rat2doc = xwiki.getDocument("XWiki.RatingStats")
userList = xwiki.getArrayList()
topUsers = xwiki.getHashMap()
pageList = xwiki.getArrayList()
pageRatings = xwiki.getHashMap()
existingUsers = xwiki.getArrayList()
existingUsersIndex = xwiki.getHashMap()
Comparator comparator = Collections.reverseOrder();
allRatings = ratdoc.getObjects("XWiki.RatingClass")
allPerUserRatings = rat2doc.getObjects('XWiki.PerUserRatingsClass')
allPerPageRatings = rat2doc.getObjects('XWiki.PerPageRatingsClass')
/* Create a hashtable with all users and the count of their ratings */
for(rating in allRatings)
{
/*
- first the top-rater statistics - */
user = ratdoc.display("user", rating)
page = ratdoc.display("page", rating)
stars = ratdoc.display("rating", rating).toInteger()
if (userList.contains(user))
{
currentCount = topUsers.get(user, rating)
currentCount ++
topUsers.put(user, currentCount)
}
else
{
userList.add(user)
topUsers.put(user, 1)
}
/*
- And now the ratings per page statistics - */
if (pageList.contains(page))
{
upAndDown = pageRatings.get(page)
if(stars > 3)
{
upAndDown
0?++
}else{
upAndDown
1?++
}
pageRatings.put(page, upAndDown)
}else{
int
] up And Down = new int[2?
if(stars > 3)
{
upAndDown
0? = 1
upAndDown
1? = 0
}else{
upAndDown
0? = 0
upAndDown
1? = 1
}
pageRatings.put(page, upAndDown)
pageList.add(page)
}
}
/* now we want to save the results as objects in another page so that they can
be easily retrieved. First, we check what users already have entries. */
int i = 0
for(user in allPerUserRatings)
{
username = ratdoc.display("username", user)
existingUsers.add(username)
existingUsersIndex.put(username, i)
i++
}
/* save the count of ratings given for each user as an object of the class
'XWiki.PerUserRatingsClass'. If the user already has an object, we will change
this. Otherwise, a new one get created. */
for(user in userList)
{
count = topUsers.get(user).toInteger()
if(existingUsers.contains(user))
{
currentIndex = existingUsersIndex.get(user)
perUserObject = rat2doc.getObject('XWiki.PerUserRatingsClass', currentIndex)
perUserObject.set('numberOfRatings', count)
}else{
perUserObject = rat2doc.newObject('XWiki.PerUserRatingsClass')
perUserObject.set('username', user)
perUserObject.set('numberOfRatings', count)
}
}
/* Finally, we save the changes to the objects */
rat2doc.save()
/* Print the (up to) 5 users with the most rating
!< currently it sorts by users names, not their contributions
sortedList = userList.sort(comparator)
if(sortedList.size() > 5)
{
sortedList.removeRange(5, sortedList.size())
}*/
for(item in userList)
{
user = topUsers.get(item)
println "* $item : $user "
}
/* Print the pages and the respective Ups and Downs
for(pages in pageList)
{
page = pageRatings.get(pages)
println "** $pages : $page"
}
*/