Search in sources :

Example 1 with Rating

use of org.xwiki.ratings.Rating in project xwiki-platform by xwiki.

the class DefaultRatingsManager method setRating.

@Override
public Rating setRating(DocumentReference documentRef, DocumentReference author, int vote) throws RatingsException {
    Rating rating = getRating(documentRef, author);
    int oldVote;
    if (rating == null) {
        oldVote = 0;
        try {
            rating = new DefaultRating(documentRef, author, vote, getXWikiContext(), this);
        } catch (XWikiException e) {
            throw new RatingsException(RatingsException.MODULE_PLUGIN_RATINGS, e.getCode(), "Failed to create new rating object", e);
        }
    } else {
        oldVote = rating.getVote();
        rating.setVote(vote);
        rating.setDate(new Date());
    }
    // Indicate that we start modifying the rating
    this.observationManager.notify(new UpdatingRatingEvent(documentRef, rating, oldVote), null);
    boolean updateFailed = true;
    try {
        // update rating
        rating.save();
        // update average rating count
        updateAverageRatings(documentRef, rating, oldVote);
        updateFailed = false;
    } finally {
        if (updateFailed) {
            // Indicate that the we start modifying the rating
            this.observationManager.notify(new UpdatingRatingEvent(documentRef, rating, oldVote), null);
        } else {
            // Indicate that we finished updating the rating
            this.observationManager.notify(new UpdateRatingEvent(documentRef, rating, oldVote), null);
        }
    }
    return rating;
}
Also used : UpdatingRatingEvent(org.xwiki.ratings.UpdatingRatingEvent) UpdateRatingEvent(org.xwiki.ratings.UpdateRatingEvent) Rating(org.xwiki.ratings.Rating) RatingsException(org.xwiki.ratings.RatingsException) XWikiException(com.xpn.xwiki.XWikiException) Date(java.util.Date)

Example 2 with Rating

use of org.xwiki.ratings.Rating in project xwiki-platform by xwiki.

the class SeparatePageRatingsManager method getRatings.

@Override
public List<Rating> getRatings(DocumentReference documentRef, int start, int count, boolean asc) throws RatingsException {
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("Calling separate page manager code for ratings");
    }
    String sql = ", BaseObject as obj, StringProperty as parentprop where doc.fullName=obj.name and obj.className=?" + " and obj.id=parentprop.id.id and parentprop.id.name=?" + " and parentprop.value=?" + " and obj.name not in (select obj2.name from BaseObject as obj2, StringProperty as statusprop where obj2.className=?" + " and obj2.id=statusprop.id.id and statusprop.id.name=? and (statusprop.value=? or statusprop.value= ?) and obj.id=obj2.id) order by doc.date " + (asc ? "asc" : "desc");
    List<?> params = new ArrayList<String>(Arrays.asList(getRatingsClassName(), RATING_CLASS_FIELDNAME_PARENT, entityReferenceSerializer.serialize(documentRef), getRatingsClassName(), "status", "moderated", "refused"));
    List<Rating> ratings = new ArrayList<Rating>();
    try {
        List<DocumentReference> ratingPageReferenceList = getXWikiContext().getWiki().getStore().searchDocumentReferences(sql, params, getXWikiContext());
        for (DocumentReference ratingPageReference : ratingPageReferenceList) {
            ratings.add(getRatingFromDocument(documentRef, getXWiki().getDocument(ratingPageReference, getXWikiContext())));
        }
    } catch (XWikiException e) {
        throw new RatingsException(e);
    }
    return ratings;
}
Also used : Rating(org.xwiki.ratings.Rating) ArrayList(java.util.ArrayList) RatingsException(org.xwiki.ratings.RatingsException) DocumentReference(org.xwiki.model.reference.DocumentReference) XWikiException(com.xpn.xwiki.XWikiException)

Example 3 with Rating

use of org.xwiki.ratings.Rating 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);
}
Also used : AverageRating(org.xwiki.ratings.AverageRating) Rating(org.xwiki.ratings.Rating) AverageRating(org.xwiki.ratings.AverageRating) DocumentReference(org.xwiki.model.reference.DocumentReference)

Example 4 with Rating

use of org.xwiki.ratings.Rating in project xwiki-platform by xwiki.

the class DefaultRatingsManager method getRatings.

@Override
public List<Rating> getRatings(DocumentReference documentRef, int start, int count, boolean asc) throws RatingsException {
    if (logger.isDebugEnabled()) {
        logger.debug("Calling default manager code for ratings");
    }
    try {
        int skipped = 0;
        int nb = 0;
        XWikiDocument doc = getXWiki().getDocument(documentRef, getXWikiContext());
        List<BaseObject> bobjects = doc.getObjects(getRatingsClassName());
        if (bobjects != null) {
            List<Rating> ratings = new ArrayList<Rating>();
            for (BaseObject bobj : bobjects) {
                if (bobj != null) {
                    if (skipped < start) {
                        skipped++;
                    } else {
                        ratings.add(getDefaultRating(documentRef, bobj));
                        nb++;
                    }
                    if ((count != 0) && (nb == count)) {
                        break;
                    }
                }
            }
            return ratings;
        }
    } catch (XWikiException e) {
        e.printStackTrace();
    }
    return null;
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) Rating(org.xwiki.ratings.Rating) ArrayList(java.util.ArrayList) XWikiException(com.xpn.xwiki.XWikiException) BaseObject(com.xpn.xwiki.objects.BaseObject)

Example 5 with Rating

use of org.xwiki.ratings.Rating in project xwiki-platform by xwiki.

the class SeparatePageRatingsManager method setRating.

@Override
public Rating setRating(DocumentReference documentRef, DocumentReference author, int vote) throws RatingsException {
    Rating rating = getRating(documentRef, author);
    int oldVote;
    if (rating == null) {
        oldVote = 0;
        rating = new SeparatePageRating(documentRef, author, vote, getXWikiContext(), this);
    } else {
        oldVote = rating.getVote();
        rating.setVote(vote);
        rating.setDate(new Date());
    }
    // Indicate that we start modifying the rating
    this.observationManager.notify(new UpdatingRatingEvent(documentRef, rating, oldVote), null);
    boolean updateFailed = true;
    try {
        // saving rating
        rating.save();
        // update the average rating
        updateAverageRatings(documentRef, rating, oldVote);
        updateFailed = false;
    } finally {
        if (updateFailed) {
            // Indicate that the we start modifying the rating
            this.observationManager.notify(new UpdatingRatingEvent(documentRef, rating, oldVote), null);
        } else {
            // Indicate that we finished updating the rating
            this.observationManager.notify(new UpdateRatingEvent(documentRef, rating, oldVote), null);
        }
    }
    return rating;
}
Also used : UpdatingRatingEvent(org.xwiki.ratings.UpdatingRatingEvent) UpdateRatingEvent(org.xwiki.ratings.UpdateRatingEvent) Rating(org.xwiki.ratings.Rating) Date(java.util.Date)

Aggregations

Rating (org.xwiki.ratings.Rating)5 XWikiException (com.xpn.xwiki.XWikiException)3 ArrayList (java.util.ArrayList)2 Date (java.util.Date)2 DocumentReference (org.xwiki.model.reference.DocumentReference)2 RatingsException (org.xwiki.ratings.RatingsException)2 UpdateRatingEvent (org.xwiki.ratings.UpdateRatingEvent)2 UpdatingRatingEvent (org.xwiki.ratings.UpdatingRatingEvent)2 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)1 BaseObject (com.xpn.xwiki.objects.BaseObject)1 AverageRating (org.xwiki.ratings.AverageRating)1