1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.ratings.internal

File SimpleReputationAlgorithm.java

 

Coverage histogram

../../../../img/srcFileCovDistChart0.png
83% of files have more coverage

Code metrics

6
32
9
1
175
105
15
0.47
3.56
9
1.67

Classes

Class Line # Actions
SimpleReputationAlgorithm 49 32 0% 15 47
0.00%
 

Contributing tests

No tests hitting this source file were found.

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.ratings.internal;
21   
22    import java.util.List;
23    import java.util.Map;
24   
25    import javax.inject.Named;
26    import javax.inject.Singleton;
27   
28    import org.xwiki.component.annotation.Component;
29    import org.xwiki.model.reference.DocumentReference;
30    import org.xwiki.ratings.AverageRating;
31    import org.xwiki.ratings.Rating;
32    import org.xwiki.ratings.RatingsException;
33    import org.xwiki.ratings.RatingsManager;
34    import org.xwiki.ratings.ReputationAlgorithm;
35    import org.xwiki.ratings.ReputationException;
36   
37    import com.xpn.xwiki.XWikiException;
38   
39    /**
40    * Default very simple reputation algorithm. It won't include recalculation put only flow level reputation
41    *
42    * @version $Id: 126ba63d36ee3640d7b19e9289ba82e57f3d68ce $
43    * @see ReputationAlgorithm
44    * @since 6.4M3
45    */
46    @Component
47    @Singleton
48    @Named("simple")
 
49    public class SimpleReputationAlgorithm extends DefaultReputationAlgorithm
50    {
51    protected float totalReputation;
52   
53    protected float constantX = -2;
54   
55    protected float constantY = 50;
56   
57    /**
58    * Gets or calculates the user reputation.
59    *
60    * @param documentRef the document which the ratings are for
61    * @param username Person to calculate the reputation for
62    * @return AverageRating of the voter
63    */
 
64  0 toggle public AverageRating getUserReputation(DocumentReference documentRef, DocumentReference username)
65    throws ReputationException
66    {
67  0 try {
68  0 AverageRating aveRating =
69    getRatingsManager(documentRef).getAverageRating(username,
70    RatingsManager.RATING_REPUTATION_METHOD_AVERAGE);
71  0 float oldRep = aveRating.getAverageVote();
72  0 aveRating.setAverageVote(aveRating.getAverageVote() * 100 / getTotalReputation());
73  0 totalReputation += aveRating.getAverageVote() - oldRep;
74  0 return aveRating;
75    } catch (RatingsException e) {
76  0 throw new ReputationException(e);
77    }
78    }
79   
 
80  0 toggle @Override
81    public AverageRating calcNewVoterReputation(DocumentReference voter, DocumentReference documentRef, Rating rating,
82    int oldVote) throws ReputationException
83    {
84  0 notimplemented();
85  0 return null;
86    }
87   
 
88  0 toggle @Override
89    public AverageRating calcNewContributorReputation(DocumentReference contributor, DocumentReference documentRef,
90    Rating rating, int oldVote) throws ReputationException
91    {
92  0 DocumentReference voter = rating.getAuthor();
93  0 float voterRep = getUserReputation(documentRef, voter).getAverageVote();
94  0 float constantX = getConstantX();
95  0 float constantY = getConstantY();
96  0 AverageRating currentRep = getUserReputation(documentRef, contributor);
97  0 currentRep.setAverageVote(currentRep.getAverageVote() + (rating.getVote() + constantX) * voterRep / constantY);
98  0 notimplemented();
99  0 return null;
100    }
101   
102    /**
103    * Gets the total reputation.
104    *
105    * @return total reputation represented as a float
106    */
 
107  0 toggle private float getTotalReputation()
108    {
109  0 if (totalReputation == 0) {
110    // recalculate it
111  0 try {
112  0 List<Float> result =
113    getXWiki()
114    .search(
115    "select sum(prop.value) from XWikiDocument as doc, BaseObject as obj, FloatProperty as prop where doc.fullName=obj.name and obj.className='XWiki.XWikiUsers' and obj.id=prop.id.id and prop.id.name='averagevote'",
116    getXWikiContext());
117  0 if ((result == null) || (result.size() == 0)) {
118  0 totalReputation = 0;
119    } else {
120  0 totalReputation = result.get(0).floatValue();
121    }
122    } catch (XWikiException e) {
123  0 totalReputation = 0;
124    }
125    }
126  0 return (totalReputation <= 1) ? 1 : totalReputation;
127    }
128   
129    /**
130    * Gets x constant.
131    *
132    * @return x constant
133    */
 
134  0 toggle private float getConstantX()
135    {
136  0 return constantX;
137    }
138   
139    /**
140    * Gets y constant.
141    *
142    * @return y constant
143    */
 
144  0 toggle private float getConstantY()
145    {
146  0 return constantY;
147    }
148   
 
149  0 toggle @Override
150    public Map<String, AverageRating> calcNewAuthorsReputation(DocumentReference documentRef, Rating rating, int oldVote)
151    throws ReputationException
152    {
153  0 notimplemented();
154  0 return null;
155    }
156   
 
157  0 toggle @Override
158    public Map<String, AverageRating> recalcAllReputation() throws ReputationException
159    {
160  0 notimplemented();
161  0 return null;
162    }
163   
164    /**
165    * Marks methods that have not been implemented.
166    *
167    * @throws ReputationException when the method is called
168    */
 
169  0 toggle @Override
170    protected void notimplemented() throws ReputationException
171    {
172  0 throw new ReputationException(ReputationException.MODULE_PLUGIN_RATINGS_REPUTATION,
173    ReputationException.ERROR_REPUTATION_NOT_IMPLEMENTED, "Not implemented");
174    }
175    }