Search in sources :

Example 6 with AverageRating

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);
    }
}
Also used : AverageRating(org.xwiki.ratings.AverageRating) List(java.util.List) BaseObject(com.xpn.xwiki.objects.BaseObject) RatingsException(org.xwiki.ratings.RatingsException) XWikiException(com.xpn.xwiki.XWikiException)

Example 7 with AverageRating

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));
             */
    }
}
Also used : AverageRating(org.xwiki.ratings.AverageRating)

Aggregations

AverageRating (org.xwiki.ratings.AverageRating)7 RatingsException (org.xwiki.ratings.RatingsException)4 XWikiException (com.xpn.xwiki.XWikiException)3 DocumentReference (org.xwiki.model.reference.DocumentReference)2 ReputationException (org.xwiki.ratings.ReputationException)2 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)1 BaseObject (com.xpn.xwiki.objects.BaseObject)1 List (java.util.List)1 Rating (org.xwiki.ratings.Rating)1