diff --git a/docs/changelog/7.x.x.txt b/docs/changelog/7.x.x.txt index 4766d279d..d042dab6c 100644 --- a/docs/changelog/7.x.x.txt +++ b/docs/changelog/7.x.x.txt @@ -31,6 +31,7 @@ - fix: Project manager tasks get cached (not clearing) when bouncing between edit and new - fix: Creating a new account when purchasing something properly redirects users to the checkout page - fix: Group expiration dates on subscriptions are now correct + - fix: Rating numbers drop after rating asset (perlDreamer Consulting, LLC) 7.3.8 - Fixed a template variable rewriting problem with HTML::Template::Expr diff --git a/docs/upgrades/upgrade_7.3.8-7.3.9.pl b/docs/upgrades/upgrade_7.3.8-7.3.9.pl index 3d6e717a2..eab830465 100644 --- a/docs/upgrades/upgrade_7.3.8-7.3.9.pl +++ b/docs/upgrades/upgrade_7.3.8-7.3.9.pl @@ -12,6 +12,7 @@ use lib "../../lib"; use strict; use Getopt::Long; use WebGUI::Session; +use WebGUI::Asset; my $toVersion = "7.3.9"; # make this match what version you're going to @@ -22,7 +23,7 @@ my $session = start(); # this line required # upgrade functions go here fixCalendarFeedsLastUpdatedField($session); - +addThreadRatingColumn($session); finish($session); # this line required @@ -42,6 +43,29 @@ sub fixCalendarFeedsLastUpdatedField { $session->db->write("alter table Calendar_feeds modify column lastUpdated datetime"); } +##------------------------------------------------- +sub addThreadRatingColumn { + my $session = shift; + print "\tAdding Thread rating column\n" unless ($quiet); + $session->db->write("alter table Thread add column threadRating integer default 0"); + my $root = WebGUI::Asset->getRoot($session); + my $threads = $root->getLineage( + ['descendents'], + { + returnObjects => 1, + includeOnlyClasses => ['WebGUI::Asset::Post::Thread'], + includeArchived => 1, + }, + ); + foreach my $thread ( @{ $threads} ) { + ##Fix all double accounting on the thread + $thread->recalculatePostRating; + + ##Recalculate the thread rating and also update the CS! + $thread->updateThreadRating; + } +} + # ---- DO NOT EDIT BELOW THIS LINE ---- diff --git a/lib/WebGUI/Asset/Post.pm b/lib/WebGUI/Asset/Post.pm index fe458f789..336cfa070 100644 --- a/lib/WebGUI/Asset/Post.pm +++ b/lib/WebGUI/Asset/Post.pm @@ -622,6 +622,32 @@ sub incrementViews { #------------------------------------------------------------------- +=head2 insertUserPostRating ( rating ) + +Register the user's rating against this post. + +=head3 rating + +An integer indicating either thumbss up (+1) or thumbs down (-1) + +=cut + +sub insertUserPostRating { + my $self = shift; + my $rating = shift; + return undef unless ($rating == -1 || $rating == 1); + return undef if $self->hasRated; + $self->session->db->write("insert into Post_rating (assetId,userId,ipAddress,dateOfRating,rating) values (?,?,?,?,?)", + [$self->getId, + $self->session->user->userId, + $self->session->env->getIp, + $self->session->datetime->time(), + $rating,] + ); +} + +#------------------------------------------------------------------- + =head2 isNew ( ) Returns a boolean indicating whether this post is new (not an edit). @@ -851,7 +877,7 @@ Stores a rating against this post. =head3 rating -An integer between 1 and 5 (5 being best) to rate this post with. +An integer indicating either thumbss up (+1) or thumbs down (-1) =cut @@ -859,21 +885,31 @@ sub rate { my $self = shift; my $rating = shift; return undef unless ($rating == -1 || $rating == 1); - unless ($self->hasRated) { - $self->session->db->write("insert into Post_rating (assetId,userId,ipAddress,dateOfRating,rating) values (" - .$self->session->db->quote($self->getId).", ".$self->session->db->quote($self->session->user->userId).", ".$self->session->db->quote($self->session->env->getIp).", - ".$self->session->datetime->time().", ".$self->session->db->quote($rating).")"); - my ($sum) = $self->session->db->quickArray("select sum(rating) from Post_rating where assetId=".$self->session->db->quote($self->getId)); - $self->update({rating=>$sum}); - $self->getThread->rate($rating); - if ($self->session->setting->get("useKarma")) { - $self->session->user->karma(-$self->getThread->getParent->get("karmaSpentToRate"), "Rated Post ".$self->getId, "Rated a CS Post."); - my $u = WebGUI::User->new($self->session, $self->get("ownerUserId")); - $u->karma($self->getThread->getParent->get("karmaRatingMultiplier"), "Post ".$self->getId." Rated by ".$self->session->user->userId, "Had post rated."); - } + return undef if $self->hasRated; + $self->insertUserPostRating($rating); + $self->recalculatePostRating(); + $self->getThread->updateThreadRating(); + if ($self->session->setting->get("useKarma")) { + $self->session->user->karma(-$self->getThread->getParent->get("karmaSpentToRate"), "Rated Post ".$self->getId, "Rated a CS Post."); + my $u = WebGUI::User->new($self->session, $self->get("ownerUserId")); + $u->karma($self->getThread->getParent->get("karmaRatingMultiplier"), "Post ".$self->getId." Rated by ".$self->session->user->userId, "Had post rated."); } } +#------------------------------------------------------------------- + +=head2 recalculatePostRating ( ) + +Sum all the entries for this post from the ratings table and update its composite rating. + +=cut + +sub recalculatePostRating { + my $self = shift; + my ($sum) = $self->session->db->quickArray("select sum(rating) from Post_rating where assetId=?", [$self->getId]); + $self->update({rating=>$sum}); +} + #------------------------------------------------------------------- sub rethreadUnder { my $self = shift; diff --git a/lib/WebGUI/Asset/Post/Thread.pm b/lib/WebGUI/Asset/Post/Thread.pm index 639be1241..17959fc08 100644 --- a/lib/WebGUI/Asset/Post/Thread.pm +++ b/lib/WebGUI/Asset/Post/Thread.pm @@ -145,7 +145,12 @@ sub definition { noFormPost=>1, fieldType=>"integer", defaultValue=>10 - } + }, + threadRating => { + noFormPost=>1, + fieldType=>"hidden", + defaultValue=>undef + }, }, }); return $class->SUPER::definition($session,$definition); @@ -565,20 +570,17 @@ sub rate { my $self = shift; my $rating = shift; return undef unless ($rating == -1 || $rating == 1); - unless ($self->hasRated) { - $self->session->db->write("insert into Post_rating (assetId,userId,ipAddress,dateOfRating,rating) values (" - .$self->session->db->quote($self->getId).", ".$self->session->db->quote($self->session->user->userId).", ".$self->session->db->quote($self->session->env->getIp).", - ".$self->session->datetime->time().", ".$self->session->db->quote($rating).")"); - my ($sum) = $self->session->db->quickArray("select sum(Post.rating) from Post left join asset on Post.assetId=asset.assetId where Post.threadId=".$self->session->db->quote($self->getId)." and Post.rating>0"); - $self->update({rating=>$sum}); - if ($self->session->setting->get("useKarma")) { - my $poster = WebGUI::User->new($self->session, $self->get("ownerUserId")); - $poster->karma($rating*$self->getParent->get("karmaRatingMultiplier"),"collaboration rating","someone rated post ".$self->getId); - my $rater = WebGUI::User->new($self->session->user->userId); - $rater->karma(-$self->getParent->get("karmaSpentToRate"),"collaboration rating","spent karma to rate post ".$self->getId); - } - $self->getParent->recalculateRating; + return undef if $self->hasRated; + $self->SUPER::rate($rating); + + ##Thread specific karma adjustment for CS + if ($self->session->setting->get("useKarma")) { + my $poster = WebGUI::User->new($self->session, $self->get("ownerUserId")); + $poster->karma($rating*$self->getParent->get("karmaRatingMultiplier"),"collaboration rating","someone rated post ".$self->getId); + my $rater = WebGUI::User->new($self->session->user->userId); + $rater->karma(-$self->getParent->get("karmaSpentToRate"),"collaboration rating","spent karma to rate post ".$self->getId); } + } @@ -767,6 +769,33 @@ sub unsubscribe { } +#------------------------------------------------------------------- + +=head2 updateThreadRating ( ) + +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}); + $self->getParent->recalculateRating; +} + + #------------------------------------------------------------------- sub view { my $self = shift; diff --git a/lib/WebGUI/Asset/Wobject/Collaboration.pm b/lib/WebGUI/Asset/Wobject/Collaboration.pm index 7be7ac5b2..5a4d5aad2 100644 --- a/lib/WebGUI/Asset/Wobject/Collaboration.pm +++ b/lib/WebGUI/Asset/Wobject/Collaboration.pm @@ -83,7 +83,7 @@ sub appendPostListTemplateVars { } else { $url = $post->getUrl."#".$post->getId; } - push(@{$var->{post_loop}}, { + my %postVars = ( %{$post->get}, "id"=>$post->getId, "url"=>$url, @@ -108,7 +108,11 @@ sub appendPostListTemplateVars { "user.isPoster"=>$post->isPoster, "avatar.url"=>$post->getAvatarUrl, %lastReply - }); + ); + if ($row->{className} eq 'WebGUI::Asset::Post::Thread') { + $postVars{'rating'} = $post->get('threadRating'); + } + push(@{$var->{post_loop}}, \%postVars ); $i++; } $p->appendTemplateVars($var); @@ -945,10 +949,10 @@ Calculates the rating of this forum from its threads and stores the new value in 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->session->db->quote($self->getId)." and Post.rating>0"); + 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->session->db->quote($self->getId)." and Post.rating>0"); + left join Post on Thread.assetId=Post.assetId where asset.parentId=?",[$self->getId]); my $average = round($sum/$count); $self->update({rating=>$average}); }