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