use of org.xwiki.ratings.AverageRating in project xwiki-platform by xwiki.
the class AbstractRatingsManager method getAverageRatingFromQuery.
@Override
public AverageRating getAverageRatingFromQuery(String fromsql, String wheresql, String method) throws RatingsException {
try {
String fromsql2 = fromsql + ", BaseObject as avgobj, FloatProperty as avgvote, StringProperty as avgmethod ";
String wheresql2 = (wheresql.equals("") ? "where " : wheresql + " and ") + "doc.fullName=avgobj.name and avgobj.className='" + getAverageRatingsClassName() + "' and avgobj.id=avgvote.id.id and avgvote.id.name='" + AVERAGERATING_CLASS_FIELDNAME_AVERAGEVOTE + "' and avgobj.id=avgmethod.id.id and avgmethod.id.name='" + AVERAGERATING_CLASS_FIELDNAME_AVERAGEVOTE_METHOD + "' and avgmethod.value='" + method + "'";
String sql = "select sum(avgvote.value) as vote, count(avgvote.value) as nbvotes from XWikiDocument as doc " + fromsql2 + wheresql2;
if (logger.isDebugEnabled()) {
logger.debug("Running average rating with sql " + sql);
}
getXWikiContext().put("lastsql", sql);
List result = getXWiki().getStore().search(sql, 0, 0, getXWikiContext());
float vote = ((Number) ((Object[]) result.get(0))[0]).floatValue();
int nbvotes = ((Number) ((Object[]) result.get(0))[1]).intValue();
AverageRating avgr = new MemoryAverageRating(null, nbvotes, vote / nbvotes, method);
return avgr;
} catch (XWikiException e) {
throw new RatingsException(e);
}
}
use of org.xwiki.ratings.AverageRating in project xwiki-platform by xwiki.
the class AbstractRatingsManager method updateAverageRating.
@Override
public void updateAverageRating(DocumentReference documentRef, Rating rating, int oldVote, String method) throws RatingsException {
// we only update if we are in stored mode and if the vote changed
if (isAverageRatingStored(documentRef) && oldVote != rating.getVote()) {
AverageRating aRating = calcAverageRating(documentRef, method);
AverageRating averageRating = getAverageRating(documentRef, method, true);
averageRating.setAverageVote(aRating.getAverageVote());
averageRating.setNbVotes(aRating.getNbVotes());
averageRating.save();
/*
* StoredAverageRating averageRating = (StoredAverageRating) getAverageRating(container, method, true,
* context); int diffTotal = rating.getVote() - oldVote; int diffNbVotes = (oldVote==0) ? 1 : 0; int
* oldNbVotes = averageRating.getNbVotes(); averageRating.setNbVotes(oldNbVotes + diffNbVotes);
* averageRating.setAverageVote((averageRating.getAverageVote()*oldNbVotes + diffTotal) / (oldNbVotes +
* diffNbVotes));
*/
}
}
Aggregations