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
Hide line numbers
1: <% 2: ratdoc = xwiki.getDocument("XWiki.RatStats") 3: rat2doc = xwiki.getDocument("XWiki.RatingStats") 4: userList = xwiki.getArrayList() 5: topUsers = xwiki.getHashMap() 6: pageList = xwiki.getArrayList() 7: pageRatings = xwiki.getHashMap() 8: existingUsersIndex = xwiki.getHashMap() 9: existingPagesIndex = xwiki.getHashMap() 10: Comparator comparator = Collections.reverseOrder(); 11: allRatings = ratdoc.getObjects("XWiki.RatingClass") 12: allPerUserRatings = rat2doc.getObjects('XWiki.PerUserRatingsClass') 13: allPerPageRatings = rat2doc.getObjects('XWiki.PerPageRatingsClass') 14: 15: /* Create a hashtable with all users and the count of their ratings */ 16: for(rating in allRatings) 17: { 18: /* --- first the top-rater statistics --- */ 19: user = ratdoc.display("user", rating) 20: page = ratdoc.display("page", rating) 21: stars = ratdoc.display("rating", rating).toInteger() 22: 23: if (userList.contains(user)) 24: { 25: currentCount = topUsers.get(user, rating) 26: currentCount ++ 27: topUsers.put(user, currentCount) 28: } 29: else 30: { 31: userList.add(user) 32: topUsers.put(user, 1) 33: } 34: 35: /* --- And now the ratings per page statistics --- */ 36: if (pageList.contains(page)) 37: { 38: upAndDown = pageRatings.get(page) 39: if(stars > 3) 40: { 41: upAndDown[0]++ 42: }else{ 43: upAndDown[1]++ 44: } 45: pageRatings.put(page, upAndDown) 46: }else{ 47: int[] upAndDown = new int[2] 48: if(stars > 3) 49: { 50: upAndDown[0] = 1 51: upAndDown[1] = 0 52: }else{ 53: upAndDown[0] = 0 54: upAndDown[1] = 1 55: } 56: pageRatings.put(page, upAndDown) 57: pageList.add(page) 58: } 59: 60: } 61: 62: /* now we want to save the results as objects in another page so that they can 63: be easily retrieved. First, we check what users already have entries. */ 64: int i = 0 65: for(user in allPerUserRatings) 66: { 67: username = rat2doc.display("username", user) 68: existingUsersIndex.put(username, i) 69: i++ 70: } 71: 72: /* save the count of ratings given for each user as an object of the class 73: 'XWiki.PerUserRatingsClass'. If the user already has an object, we will change 74: this. Otherwise, a new one gets created. */ 75: for(user in userList) 76: { 77: count = topUsers.get(user).toInteger() 78: if(existingUsersIndex.containsKey(user)) 79: { 80: currentIndex = existingUsersIndex.get(user) 81: perUserObject = rat2doc.getObject('XWiki.PerUserRatingsClass', currentIndex) 82: perUserObject.set('numberOfRatings', count) 83: }else{ 84: perUserObject = rat2doc.newObject('XWiki.PerUserRatingsClass') 85: perUserObject.set('username', user) 86: perUserObject.set('numberOfRatings', count) 87: } 88: } 89: 90: /* Now, let's save the statistics per page - similar to per user*/ 91: i = 0 92: for(page in allPerPageRatings) 93: { 94: pagename = rat2doc.display("pagename", page) 95: existingPagesIndex.put(pagename, i) 96: i++ 97: } 98: for(page in pageList) 99: { 100: upAndDowns = pageRatings.get(page) 101: if(existingPagesIndex.containsKey(page)) 102: { 103: currentIndex = existingPagesIndex.get(page) 104: perPageObject = rat2doc.getObject('XWiki.PerPageRatingsClass', currentIndex) 105: perPageObject.set('upratings', upAndDowns[0].toInteger()) 106: perPageObject.set('downratings', upAndDowns[1].toInteger()) 107: }else{ 108: perPageObject = rat2doc.newObject('XWiki.PerPageRatingsClass') 109: perPageObject.set('pagename', page) 110: perPageObject.set('upratings', upAndDowns[0].toInteger()) 111: perPageObject.set('downratings', upAndDowns[1].toInteger()) 112: } 113: } 114: 115: /* Finally, we save the changes to the objects */ 116: rat2doc.save('Updated Stats', true) 117: 118: /* Print the (up to) 5 users with the most rating 119: !< currently it sorts by users names, not their contributions 120: sortedList = userList.sort(comparator) 121: if(sortedList.size() > 5) 122: { 123: sortedList.removeRange(5, sortedList.size()) 124: } 125: */ 126: 127: %>
Welcome
Welcome to this XWiki!
Quick Links
DSSF 2008
DCNM 2009
HCCF 2010
Document Index
Sandbox
My Recent Modifications
ASSIGNMENTNAME