HaikuDepot: Class and method to retrieve a user rating summary
This commit is contained in:
parent
0177c6b4be
commit
c76894073a
@ -428,6 +428,48 @@ PackageInfo::AddUserRating(const UserRating& rating)
|
||||
}
|
||||
|
||||
|
||||
RatingSummary
|
||||
PackageInfo::CalculateRatingSummary() const
|
||||
{
|
||||
RatingSummary summary;
|
||||
summary.ratingCount = fUserRatings.CountItems();
|
||||
summary.averageRating = 0.0f;
|
||||
int starRatingCount = sizeof(summary.ratingCountByStar) / sizeof(int);
|
||||
for (int i = 0; i < starRatingCount; i++)
|
||||
summary.ratingCountByStar[i] = 0;
|
||||
|
||||
if (summary.ratingCount <= 0)
|
||||
return summary;
|
||||
|
||||
float ratingSum = 0.0f;
|
||||
|
||||
for (int i = 0; i < summary.ratingCount; i++) {
|
||||
float rating = fUserRatings.ItemAtFast(i).Rating();
|
||||
|
||||
if (rating < 0.0f)
|
||||
rating = 0.0f;
|
||||
else if (rating > 5.0f)
|
||||
rating = 5.0f;
|
||||
|
||||
ratingSum += rating;
|
||||
|
||||
if (rating <= 1.0f)
|
||||
summary.ratingCountByStar[0]++;
|
||||
else if (rating <= 2.0f)
|
||||
summary.ratingCountByStar[1]++;
|
||||
else if (rating <= 3.0f)
|
||||
summary.ratingCountByStar[2]++;
|
||||
else if (rating <= 4.0f)
|
||||
summary.ratingCountByStar[3]++;
|
||||
else if (rating <= 5.0f)
|
||||
summary.ratingCountByStar[4]++;
|
||||
}
|
||||
|
||||
summary.averageRating = ratingSum / summary.ratingCount;
|
||||
return summary;
|
||||
}
|
||||
|
||||
|
||||
// #pragma mark -
|
||||
|
||||
|
||||
|
@ -104,6 +104,15 @@ private:
|
||||
typedef List<UserRating, false> UserRatingList;
|
||||
|
||||
|
||||
class RatingSummary {
|
||||
public:
|
||||
float averageRating;
|
||||
int ratingCount;
|
||||
|
||||
int ratingCountByStar[5];
|
||||
};
|
||||
|
||||
|
||||
class PublisherInfo {
|
||||
public:
|
||||
PublisherInfo();
|
||||
@ -167,6 +176,11 @@ public:
|
||||
|
||||
bool AddUserRating(const UserRating& rating);
|
||||
|
||||
const UserRatingList& UserRatings() const
|
||||
{ return fUserRatings; }
|
||||
|
||||
RatingSummary CalculateRatingSummary() const;
|
||||
|
||||
private:
|
||||
BitmapRef fIcon;
|
||||
BString fTitle;
|
||||
|
Loading…
Reference in New Issue
Block a user