UpdateRatings

UpdateRatings

Last modified by Holger Dick on 2009/07/14 12:34
Topic XWiki.Rater does not exist

ratdoc = xwiki.getDocument("XWiki.RatStats") rat2doc = xwiki.getDocument("XWiki.RatingStats") userList = xwiki.getArrayList() topUsers = xwiki.getHashMap() pageList = xwiki.getArrayList() pageRatings = xwiki.getHashMap() existingUsersIndex = xwiki.getHashMap() existingPagesIndex = 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) { upAndDown0?++ }else{ upAndDown1?++ } pageRatings.put(page, upAndDown) }else{ int] up And Down = new int[2? if(stars > 3) { upAndDown0? = 1 upAndDown1? = 0 }else{ upAndDown0? = 0 upAndDown1? = 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 = rat2doc.display("username", user) 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 gets created. */ for(user in userList) { count = topUsers.get(user).toInteger() if(existingUsersIndex.containsKey(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) } }

/* Now, let's save the statistics per page - similar to per user*/ i = 0 for(page in allPerPageRatings) { pagename = rat2doc.display("pagename", page) existingPagesIndex.put(pagename, i) i++ } for(page in pageList) { upAndDowns = pageRatings.get(page) if(existingPagesIndex.containsKey(page)) { currentIndex = existingPagesIndex.get(page) perPageObject = rat2doc.getObject('XWiki.PerPageRatingsClass', currentIndex) perPageObject.set('upratings', upAndDowns0?.toInteger()) perPageObject.set('downratings', upAndDowns1?.toInteger()) }else{ perPageObject = rat2doc.newObject('XWiki.PerPageRatingsClass') perPageObject.set('pagename', page) perPageObject.set('upratings', upAndDowns0?.toInteger()) perPageObject.set('downratings', upAndDowns1?.toInteger()) } }

/* Finally, we save the changes to the objects */ rat2doc.save('Updated Stats', true)

/* 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()) } */

Tags:
Created by Holger Dick on 2009/07/06 10:31

This wiki is licensed under a Creative Commons 2.0 license
XWiki Enterprise 2.7.1.${buildNumber} - Documentation