Wiki source code of UpdateRatings

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

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