General Actions:
Log-in
Wiki:
Courses
▼
:
Document Index
»
Space:
XWiki
▼
:
Document Index
»
Page:
UpdateRatings
Search
Page Actions:
Export
▼
:
Export as PDF
Export as RTF
Export as HTML
More actions
▼
:
Print preview
View Source
UpdateRatings
Wiki source code of
UpdateRatings
Last modified by
Holger Dick
on 2010/08/20 15:48
Content
·
Comments
(0)
·
Attachments
(0)
·
History
·
Information
Show line numbers
<% 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) { upAndDown[0]++ }else{ upAndDown[1]++ } pageRatings.put(page, upAndDown) }else{ int[] upAndDown = 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 = 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', upAndDowns[0].toInteger()) perPageObject.set('downratings', upAndDowns[1].toInteger()) }else{ perPageObject = rat2doc.newObject('XWiki.PerPageRatingsClass') perPageObject.set('pagename', page) perPageObject.set('upratings', upAndDowns[0].toInteger()) perPageObject.set('downratings', upAndDowns[1].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()) } */ %>
Welcome
Welcome to this XWiki!
Quick Links
DSSF 2008
DCNM 2009
HCCF 2010
Document Index
Sandbox
My Recent Modifications
ASSIGNMENTNAME