Wiki source code of UpdateRatings

Last modified by Holger Dick on 2010/08/20 15:48

Hide last authors
Holger Dick 71.1 1 #if($xwiki.hasAdminRights())
Holger Dick 67.1 2 <%
Holger Dick 62.1 3 ratdoc = xwiki.getDocument("XWiki.RatStats")
Holger Dick 68.1 4 rat2doc = xwiki.getDocument("XWiki.RatingStats")
5 userList = xwiki.getArrayList()
6 topUsers = xwiki.getHashMap()
7 pageList = xwiki.getArrayList()
8 pageRatings = xwiki.getHashMap()
Holger Dick 63.1 9 existingUsersIndex = xwiki.getHashMap()
Holger Dick 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')
Holger Dick 63.1 15
Holger Dick 61.1 16 /* Create a hashtable with all users and the count of their ratings */
17 for(rating in allRatings)
Holger Dick 63.1 18 {
19 /* --- first the top-rater statistics --- */
20 user = ratdoc.display("user", rating)
21 page = ratdoc.display("page", rating)
Holger Dick 63.2 22 stars = ratdoc.display("rating", rating).toInteger()
Holger Dick 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 }
Holger Dick 61.1 35
Holger Dick 63.1 36 /* --- And now the ratings per page statistics --- */
37 if (pageList.contains(page))
38 {
39 upAndDown = pageRatings.get(page)
Holger Dick 63.2 40 if(stars > 3)
Holger Dick 61.1 41 {
Holger Dick 63.1 42 upAndDown[0]++
43 }else{
44 upAndDown[1]++
Holger Dick 61.1 45 }
46 pageRatings.put(page, upAndDown)
Holger Dick 63.1 47 }else{
48 int[] upAndDown = new int[2]
Holger Dick 63.2 49 if(stars > 3)
Holger Dick 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 {
Holger Dick 65.1 68 username = rat2doc.display("username", user)
Holger Dick 63.1 69 existingUsersIndex.put(username, i)
70 i++
Holger Dick 61.1 71 }
Holger Dick 1.1 72
Holger Dick 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
Holger Dick 65.1 75 this. Otherwise, a new one gets created. */
Holger Dick 63.1 76 for(user in userList)
Holger Dick 61.1 77 {
Holger Dick 63.2 78 count = topUsers.get(user).toInteger()
Holger Dick 65.1 79 if(existingUsersIndex.containsKey(user))
Holger Dick 63.1 80 {
81 currentIndex = existingUsersIndex.get(user)
82 perUserObject = rat2doc.getObject('XWiki.PerUserRatingsClass', currentIndex)
Holger Dick 63.2 83 perUserObject.set('numberOfRatings', count)
Holger Dick 63.1 84 }else{
85 perUserObject = rat2doc.newObject('XWiki.PerUserRatingsClass')
86 perUserObject.set('username', user)
Holger Dick 63.2 87 perUserObject.set('numberOfRatings', count)
Holger Dick 63.1 88 }
89 }
Holger Dick 24.1 90
Holger Dick 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 }
Holger Dick 63.1 115
116 /* Finally, we save the changes to the objects */
Holger Dick 66.2 117 rat2doc.save('Updated Stats', true)
Holger Dick 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
Holger Dick 67.1 128 %>
Holger Dick 71.1 129 #end