diff --git a/docs/changelog/7.x.x.txt b/docs/changelog/7.x.x.txt index 4ad2815d9..4aceef805 100644 --- a/docs/changelog/7.x.x.txt +++ b/docs/changelog/7.x.x.txt @@ -2,6 +2,8 @@ - Prevent Help index from trying to link to non-existant documentation - fix: can't see the send private message link - fix: delete and become user links are shown for user list after deleting user + - fix: Threads and CollabSystem have new way to calculate ratings more + accurately - fix: Inbox shows Next page link when is empty - fix: Change Owner in Security Tab in Assets is not always Working - All toolbar icons now have a class called "toolbarIcon" so they can be diff --git a/docs/upgrades/upgrade_7.4.5-7.4.6.pl b/docs/upgrades/upgrade_7.4.5-7.4.6.pl index ac4b1db99..8bfaab836 100644 --- a/docs/upgrades/upgrade_7.4.5-7.4.6.pl +++ b/docs/upgrades/upgrade_7.4.5-7.4.6.pl @@ -21,6 +21,7 @@ my $quiet; # this line required my $session = start(); # this line required # upgrade functions go here +recalculateThreadRatings($session); finish($session); # this line required @@ -32,6 +33,24 @@ finish($session); # this line required # # and here's our code #} +#---------------------------------------------------------------------------- +# Have Threads recalculate their own ratings +sub recalculateThreadRatings { + my $session = shift; + print "\tRecalculating Thread ratings. This may take a while... " unless ($quiet); + + my $root = WebGUI::Asset->getRoot($session); + my $threadIds + = $root->getLineage(["descendants"], { + includeOnlyClasses => ["WebGUI::Asset::Post::Thread"] + }); + + for my $threadId (@$threadIds) { + WebGUI::Asset->newByDynamicClass($session, $threadId)->updateThreadRating; + } + + print "OK!\n" unless $quiet; +} # ---- DO NOT EDIT BELOW THIS LINE ---- diff --git a/lib/WebGUI/Asset/Post/Thread.pm b/lib/WebGUI/Asset/Post/Thread.pm index c1bc2de92..50a99636a 100644 --- a/lib/WebGUI/Asset/Post/Thread.pm +++ b/lib/WebGUI/Asset/Post/Thread.pm @@ -791,26 +791,28 @@ Update the cumulative ratings in this thread =cut sub updateThreadRating { - my $self = shift; - my $ratingSumSQL = <session->db->quickArray($ratingSumSQL, [$self->getId]); - $self->update({threadRating=>$sum}); - my $parent = $self->getParent; - if (defined $parent) { - $parent->recalculateRating; - } else { - $self->session->errorHandler->error("Couldn't get parent for thread ".$self->getId); - } + my $self = shift; + my $session = $self->session; + + my $calcRating = 0; + my $postIds = $self->getLineage(["descendants","self"], { + includeOnlyClasses => ["WebGUI::Asset::Post","WebGUI::Asset::Post::Thread"], + }); + + $calcRating += $session->db->quickScalar( + "SELECT SUM(rating) FROM Post_rating WHERE assetId IN (".$session->db->quoteAndJoin($postIds).")" + ); + + $self->update({ + threadRating => $calcRating + }); + + my $parent = $self->getParent; + if (defined $parent) { + $parent->recalculateRating; + } else { + $self->session->errorHandler->error("Couldn't get parent for thread ".$self->getId); + } } diff --git a/lib/WebGUI/Asset/Wobject/Collaboration.pm b/lib/WebGUI/Asset/Wobject/Collaboration.pm index c71bf3f31..68a5d62ad 100644 --- a/lib/WebGUI/Asset/Wobject/Collaboration.pm +++ b/lib/WebGUI/Asset/Wobject/Collaboration.pm @@ -1166,14 +1166,34 @@ Calculates the rating of this forum from its threads and stores the new value in =cut sub recalculateRating { - my $self = shift; - my ($count) = $self->session->db->quickArray("select count(*) from Thread left join asset on Thread.assetId=asset.assetId - left join Post on Thread.assetId=Post.assetId where asset.parentId=?",[$self->getId]); - $count = $count || 1; - my ($sum) = $self->session->db->quickArray("select sum(Post.rating) from Thread left join asset on Thread.assetId=asset.assetId - left join Post on Thread.assetId=Post.assetId where asset.parentId=?",[$self->getId]); - my $average = round($sum/$count); - $self->update({rating=>$average}); + my $self = shift; + + # Get the number of threads + my ($count) + = $self->session->db->quickArray( + "select count(*) from Thread + left join asset on Thread.assetId=asset.assetId + left join Post on Thread.assetId=Post.assetId + AND Thread.revisionDate = (SELECT MAX(revisionDate) FROM Thread t WHERE t.assetId=asset.assetId) + where asset.parentId=?", + [$self->getId] + ); + $count = $count || 1; + + # Get the ratings of all the threads + my ($sum) + = $self->session->db->quickArray( + "SELECT SUM(Thread.threadRating) + FROM Thread + LEFT JOIN asset ON Thread.assetId=asset.assetId + LEFT JOIN Post ON Thread.assetId=Post.assetId + AND Thread.revisionDate = (SELECT MAX(revisionDate) FROM Thread t WHERE t.assetId=asset.assetId) + WHERE asset.parentId=?", + [$self->getId] + ); + + my $average = round($sum/$count); + $self->update({rating=>$average}); }