use of org.xwiki.ratings.AverageRating in project xwiki-platform by xwiki.
the class SimpleReputationAlgorithm method getUserReputation.
/**
* Gets or calculates the user reputation.
*
* @param documentRef the document which the ratings are for
* @param username Person to calculate the reputation for
* @return AverageRating of the voter
*/
public AverageRating getUserReputation(DocumentReference documentRef, DocumentReference username) throws ReputationException {
try {
AverageRating aveRating = getRatingsManager(documentRef).getAverageRating(username, RatingsManager.RATING_REPUTATION_METHOD_AVERAGE);
float oldRep = aveRating.getAverageVote();
aveRating.setAverageVote(aveRating.getAverageVote() * 100 / getTotalReputation());
totalReputation += aveRating.getAverageVote() - oldRep;
return aveRating;
} catch (RatingsException e) {
throw new ReputationException(e);
}
}
use of org.xwiki.ratings.AverageRating in project xwiki-platform by xwiki.
the class AbstractRatingsManager method calcAverageRating.
@Override
public AverageRating calcAverageRating(DocumentReference documentRef, String method) throws RatingsException {
int nbVotes = 0;
int balancedNbVotes = 0;
float totalVote = 0;
float averageVote = 0;
List<Rating> ratings = getRatings(documentRef, 0, 0, true);
if (ratings == null) {
return null;
}
for (Rating rating : ratings) {
if (method.equals(RATING_REPUTATION_METHOD_BALANCED)) {
DocumentReference author = rating.getAuthor();
// we should not include votes of himself to a user
if (!author.equals(documentRef)) {
AverageRating reputation = getUserReputation(author);
if ((reputation == null) || (reputation.getAverageVote() == 0)) {
totalVote += rating.getVote();
balancedNbVotes++;
} else {
totalVote += rating.getVote() * reputation.getAverageVote();
balancedNbVotes += reputation.getAverageVote();
}
}
} else {
totalVote += rating.getVote();
balancedNbVotes++;
}
nbVotes++;
}
if (balancedNbVotes != 0) {
averageVote = totalVote / balancedNbVotes;
}
return new MemoryAverageRating(documentRef, nbVotes, averageVote, method);
}
use of org.xwiki.ratings.AverageRating in project xwiki-platform by xwiki.
the class DefaultReputationAlgorithm method updateReputation.
@Override
public void updateReputation(DocumentReference documentRef, Rating rating, int oldVote) {
// we only update if we are in stored mode and if the vote changed
if (oldVote != rating.getVote()) {
// voter reputation. This will give points to the voter
try {
AverageRating voterRating = calcNewVoterReputation(rating.getAuthor(), documentRef, rating, oldVote);
// we need to save this reputation if it has changed
try {
getRatingsManager(documentRef).updateUserReputation(rating.getAuthor(), voterRating);
} catch (RatingsException re) {
if (logger.isErrorEnabled()) {
logger.error("Error while storing reputation for user " + rating.getAuthor(), re);
}
}
} catch (ReputationException e) {
if (e.getCode() != ReputationException.ERROR_REPUTATION_NOT_IMPLEMENTED) {
// we should log this error
if (logger.isErrorEnabled()) {
logger.error("Error while calculating voter reputation " + rating.getAuthor() + " for document " + documentRef, e);
}
}
}
// author reputation. This will be giving points to the creator of a document or comment
try {
XWikiDocument doc = getXWiki().getDocument(documentRef, getXWikiContext());
AverageRating authorRating = calcNewContributorReputation(doc.getCreatorReference(), documentRef, rating, oldVote);
// we need to save the author reputation
try {
getRatingsManager(documentRef).updateUserReputation(doc.getCreatorReference(), authorRating);
} catch (RatingsException re) {
if (logger.isErrorEnabled()) {
logger.error("Error while storing reputation for user " + doc.getCreatorReference().getName(), re);
}
}
} catch (ReputationException e) {
if (e.getCode() != ReputationException.ERROR_REPUTATION_NOT_IMPLEMENTED) {
// we should log this error
if (logger.isErrorEnabled()) {
logger.error("Error while calculating author reputation for document " + documentRef, e);
}
}
} catch (XWikiException e) {
if (logger.isErrorEnabled()) {
logger.error("Error while calculating author reputation for document " + documentRef, e);
}
}
// all authors reputation. This will be used to give points to all participants to a document
try {
Map<String, AverageRating> authorsRatings = calcNewAuthorsReputation(documentRef, rating, oldVote);
// TODO this is not implemented yet
} catch (ReputationException e) {
if (e.getCode() != ReputationException.ERROR_REPUTATION_NOT_IMPLEMENTED) {
// we should log this error
if (logger.isErrorEnabled()) {
logger.error("Error while calculating authors reputation for document " + documentRef, e);
}
}
}
}
}
use of org.xwiki.ratings.AverageRating in project xwiki-platform by xwiki.
the class SimpleReputationAlgorithm method calcNewContributorReputation.
@Override
public AverageRating calcNewContributorReputation(DocumentReference contributor, DocumentReference documentRef, Rating rating, int oldVote) throws ReputationException {
DocumentReference voter = rating.getAuthor();
float voterRep = getUserReputation(documentRef, voter).getAverageVote();
float constantX = getConstantX();
float constantY = getConstantY();
AverageRating currentRep = getUserReputation(documentRef, contributor);
currentRep.setAverageVote(currentRep.getAverageVote() + (rating.getVote() + constantX) * voterRep / constantY);
notimplemented();
return null;
}
use of org.xwiki.ratings.AverageRating in project xwiki-platform by xwiki.
the class AbstractRatingsManager method updateUserReputation.
@Override
public void updateUserReputation(DocumentReference author, AverageRating voterRating) throws RatingsException {
try {
// We should update the user rating
AverageRating rating = getAverageRating(author, voterRating.getMethod(), true);
rating.setAverageVote(voterRating.getAverageVote());
rating.setMethod(voterRating.getMethod());
rating.setNbVotes(voterRating.getNbVotes());
rating.save();
} catch (XWikiException e) {
throw new RatingsException(e);
}
}
Aggregations