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;
}
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;
}
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);
}
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;
}
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;
}
Aggregations