added rating subsystem to discussion

This commit is contained in:
JT Smith 2003-10-05 20:20:33 +00:00
parent e3516d209e
commit 3a60989c48
5 changed files with 94 additions and 5 deletions

View file

@ -153,11 +153,12 @@ delete from international where languageId=1 and namespace='WebGUI' and internat
insert into international (internationalId,languageId,namespace,message,lastUpdated,context) values (512,1,'WebGUI','Go to next thread.', 1065280309,NULL);
delete from international where languageId=1 and namespace='WebGUI' and internationalId=513;
insert into international (internationalId,languageId,namespace,message,lastUpdated,context) values (513,1,'WebGUI','Go to previous thread.', 1065280287,NULL);
delete from international where languageId=1 and namespace='WebGUI' and internationalId=1019;
insert into international (internationalId,languageId,namespace,message,lastUpdated,context) values (1019,1,'WebGUI','Back to thread list.', 1065280160,'Return to the list of threads in a discussion.');
delete from international where languageId=1 and namespace='WebGUI' and internationalId=1018;
insert into international (internationalId,languageId,namespace,message,lastUpdated,context) values (1018,1,'WebGUI','Start a new thread.', 1065279960,'Add a new line of discussion to a forum.');
delete from international where languageId=1 and namespace='WebGUI' and internationalId=1020;
insert into international (internationalId,languageId,namespace,message,lastUpdated,context) values (1020,1,'WebGUI','Rating', 1065280882,'How useful/interesting a user thinks another poster\'s post is.');
alter table forumThread add column status varchar(30) not null default 'approved';
create table forumPostRating ( forumPostId int not null, userId int not null, ipAddress varchar(16), dateOfRating int, rating int not null);
alter table forumPost add column rating int not null default 0;
insert into international (internationalId,languageId,namespace,message,lastUpdated,context) values (1021,1,'WebGUI','Rate Message', 1065356764,'A label indicating that the following links are to be used for discussion post ratings.');
alter table forumThread add column rating int not null;

View file

@ -8,6 +8,12 @@ use WebGUI::Session;
use WebGUI::SQL;
sub canPost {
my ($self, $userId) = @_;
$userId = $session{user}{userId} unless ($userId);
return WebGUI::Privilege::isInGroup($self->get("groupToPost"));
}
sub create {
my ($self, $data) = @_;
$data->{forumId} = "new";

View file

@ -5,6 +5,7 @@ use WebGUI::DateTime;
use WebGUI::Forum::Thread;
use WebGUI::Session;
use WebGUI::SQL;
use WebGUI::Utility;
sub canEdit {
my ($self, $userId) = @_;
@ -54,6 +55,16 @@ sub getThread {
return $self->{_thread};
}
sub hasRated {
my ($self, $userId, $ipAddress) = @_;
$userId = $session{user}{userId} unless ($userId);
$ipAddress = $session{env}{REMOTE_ADDR} unless ($ipAddress);
my ($flag) = WebGUI::SQL->quickArray("select count(*) from forumPostRating where forumPostId="
.$self->get("forumPostId")." and ((userId=$userId and userId<>1) or (userId=1 and
ipAddress='$ipAddress'))");
return $flag;
}
sub incrementViews {
my ($self) = @_;
WebGUI::SQL->write("update forumPost set views=views+1 where forumPostId=".$self->get("forumPostId"));
@ -96,6 +107,25 @@ sub new {
}
}
sub rate {
my ($self, $rating, $userId, $ipAddress) = @_;
$userId = $session{user}{userId} unless ($userId);
$ipAddress = $session{env}{REMOTE_ADDR} unless ($ipAddress);
WebGUI::SQL->write("insert into forumPostRating (forumPostId,userId,ipAddress,dateOfRating,rating) values ("
.$self->get("forumPostId").", $userId, ".quote($ipAddress).", ".WebGUI::DateTime::time().", $rating)");
$self->recalculateRating;
}
sub recalculateRating {
my ($self) = @_;
my ($count) = WebGUI::SQL->quickArray("select count(*) from forumPostRating where forumPostId=".$self->get("forumPostId"));
$count = $count || 1;
my ($sum) = WebGUI::SQL->quickArray("select sum(rating) from forumPostRating where forumPostId=".$self->get("forumPostId"));
my $average = round($sum/$count);
$self->set({rating=>$average});
$self->getThread->recalculateRating;
}
sub set {
my ($self, $data) = @_;
$data->{forumPostId} = $self->get("forumPostId") unless ($data->{forumPostId});

View file

@ -6,6 +6,7 @@ use WebGUI::Forum;
use WebGUI::Forum::Post;
use WebGUI::Session;
use WebGUI::SQL;
use WebGUI::Utility;
sub create {
my ($self, $data, $postData) = @_;
@ -110,6 +111,15 @@ sub new {
}
}
sub recalculateRating {
my ($self) = @_;
my ($count) = WebGUI::SQL->quickArray("select count(*) from forumPost where forumThreadId=".$self->get("forumThreadId")." and rating>0");
$count = $count || 1;
my ($sum) = WebGUI::SQL->quickArray("select sum(rating) from forumPost where forumThreadId=".$self->get("forumThreadId")." and rating>0");
my $average = round($sum/$count);
$self->set({rating=>$average});
}
sub set {
my ($self, $data) = @_;
$data->{forumThreadId} = $self->get("forumThreadId") unless ($data->{forumThreadId});

View file

@ -51,6 +51,10 @@ sub _formatPreviousThreadURL {
return WebGUI::URL::append($_[0],"forumOp=previousThread&amp;forumThreadId=".$_[1]);
}
sub _formatRatePostURL {
return WebGUI::URL::append($_[0],"forumOp=ratePost&amp;forumPostId=".$_[1]."&amp;rating=".$_[2]);
}
sub _formatReplyPostURL {
return WebGUI::URL::append($_[0],"forumOp=post&amp;parentId=".$_[1]);
}
@ -89,6 +93,7 @@ sub _getPostTemplateVars {
}
$sth->finish;
}
$var->{canPost} = $forum->canPost;
$var->{'post.date.value'} = _formatPostDate($post->get("dateOfPost"));
$var->{'post.date.label'} = WebGUI::International::get(245);
$var->{'post.date.epoch'} = $post->get("dateOfPost");
@ -109,6 +114,13 @@ sub _getPostTemplateVars {
$var->{'post.user.Profile'} = _formatUserProfileURL($post->get("userId"));
$var->{'post.url'} = _formatThreadURL($callback,$post->get("forumPostId"));
$var->{'post.id'} = $post->get("forumPostId");
$var->{'post.rate.label'} = WebGUI::International::get(1021);
$var->{'post.rate.url.1'} = _formatRatePostURL($callback,$post->get("forumPostId"),1);
$var->{'post.rate.url.2'} = _formatRatePostURL($callback,$post->get("forumPostId"),2);
$var->{'post.rate.url.3'} = _formatRatePostURL($callback,$post->get("forumPostId"),3);
$var->{'post.rate.url.4'} = _formatRatePostURL($callback,$post->get("forumPostId"),4);
$var->{'post.rate.url.5'} = _formatRatePostURL($callback,$post->get("forumPostId"),5);
$var->{'post.hasRated'} = $post->hasRated;
$var->{'post.reply.label'} = WebGUI::International::get(577);
$var->{'post.reply.url'} = _formatReplyPostURL($callback,$post->get("forumPostId"));
$var->{'post.edit.label'} = WebGUI::International::get(575);
@ -153,14 +165,23 @@ sub viewForum {
my (%var, @thread_loop);
$var{'thread.new.url'} = _formatNewThreadURL($callback,$forumId);
$var{'thread.new.label'} = WebGUI::International::get(1018);
my $forum = WebGUI::Forum->new($forumId);
$var{canPost} = $forum->canPost;
$var{'thread.subject.label'} = WebGUI::International::get(229);
$var{'thread.date.label'} = WebGUI::International::get(245);
$var{'thread.user.label'} = WebGUI::International::get(244);
$var{"thread.views.label"} = WebGUI::International::get(514);
$var{"thread.replies.label"} = WebGUI::International::get(1016);
$var{'thread.rating.label'} = WebGUI::International::get(1020);
$var{"thread.last.label"} = WebGUI::International::get(1017);
my $p = WebGUI::Paginator->new($callback);
$p->setDataByQuery("select * from forumThread where forumId=".$forumId." order by isSticky desc, lastPostDate desc");
$var{firstPage} = $p->getFirstPageLink;
$var{lastPage} = $p->getLastPageLink;
$var{nextPage} = $p->getNextPageLink;
$var{pageList} = $p->getPageLinks;
$var{previousPage} = $p->getPreviousPageLink;
$var{multiplePages} = ($p->getNumberOfPages > 1);
my $threads = $p->getPageData;
foreach my $thread (@$threads) {
my $root = WebGUI::Forum::Post->new($thread->{rootPostId});
@ -170,9 +191,15 @@ sub viewForum {
} else {
$last = WebGUI::Forum::Post->new($thread->{lastPostId});
}
my @rating_loop;
for (my $i=0;$i>=$thread->{rating};$i++) {
push(@rating_loop,{'thread.rating_loop.count'=>$i});
}
push(@thread_loop,{
'thread.views'=>$thread->{views},
'thread.replies'=>$thread->{replies},
'thread.rating'=>$thread->{rating},
'thread.rating_loop'=>\@rating_loop,
'thread.isSticky'=>$thread->{isSticky},
'thread.isLocked'=>$thread->{isLocked},
'thread.root.subject'=>_chopSubject($root->get("subject")),
@ -263,6 +290,7 @@ sub www_post {
});
if ($var->{isReply}) {
my $reply = WebGUI::Forum::Post->new($session{form}{parentId});
return WebGUI::Privilege::insufficient unless ($reply->getThread->getForum->canPost);
$var->{'form.begin'} .= WebGUI::Form::hidden({
name=>'parentId',
value=>$reply->get("forumPostId")
@ -283,6 +311,7 @@ sub www_post {
value=>$session{form}{forumId}
});
$forum = WebGUI::Forum->new($session{form}{forumId});
return WebGUI::Privilege::insufficient unless ($forum->canPost);
if ($forum->isModerator) {
$var->{'sticky.label'} = WebGUI::International::get(1013);
$var->{'sticky.form'} = WebGUI::Form::yesNo({
@ -307,6 +336,7 @@ sub www_post {
}
if ($var->{isEdit}) {
my $post = WebGUI::Forum::Post->new($session{form}{forumPostId});
return WebGUI::Privilege::insufficient unless ($post->getThread->getForum->canPost);
$subject = $post->get("subject");
$message = $post->get("message");
$forum = $post->getThread->getForum;
@ -358,7 +388,6 @@ sub www_postSave {
my $forumId = $session{form}{forumId};
my $threadId = $session{form}{forumThreadId};
my $postId = $session{form}{forumPostId};
my $thread;
my %postData = (
message=>$session{form}{message},
subject=>$session{form}{subject}
@ -371,6 +400,7 @@ sub www_postSave {
if ($session{form}{parentId} > 0) { # reply
%postData = (%postData, %postDataNew);
my $parentPost = WebGUI::Forum::Post->new($session{form}{parentId});
return WebGUI::Privilege::insufficient unless ($parentPost->getThread->getForum->canPost);
$parentPost->getThread->subscribe($session{user}{userId}) if ($session{form}{subscribe});
$parentPost->getThread->lock if ($session{form}{isLocked});
$postData{forumThreadId} = $parentPost->getThread->get("forumThreadId");
@ -380,12 +410,15 @@ sub www_postSave {
}
if ($session{form}{forumPostId} > 0) { # edit
my $post = WebGUI::Forum::Post->new($session{form}{forumPostId});
return WebGUI::Privilege::insufficient unless ($post->getThread->getForum->canPost);
$post->set(\%postData);
return www_viewThread($callback,$post->get("forumPostId"));
}
if ($forumId) { # new post
%postData = (%postData, %postDataNew);
$thread = WebGUI::Forum::Thread->create({
my $forum = WebGUI::Forum->new($forumId);
return WebGUI::Privilege::insufficient unless ($forum->canPost);
my $thread = WebGUI::Forum::Thread->create({
forumId=>$forumId,
isSticky=>$session{form}{isSticky},
isLocked=>$session{form}{isLocked}
@ -406,6 +439,14 @@ sub www_previousThread {
}
}
sub www_ratePost {
my ($callback) = @_;
my $post = WebGUI::Forum::Post->new($session{form}{forumPostId});
return WebGUI::Privilege::insufficient() unless ($post->getThread->getForum->canPost);
$post->rate($session{form}{rating}) unless ($post->hasRated);
return www_viewThread($callback,$session{form}{forumPostId});
}
sub www_viewThread {
my ($callback, $postId) = @_;
$postId = $session{form}{forumPostId} unless ($postId);
@ -430,6 +471,7 @@ sub www_viewThread {
$var->{'thread.next.label'} = WebGUI::International::get(512);
$var->{'thread.list.url'} = $callback;
$var->{'thread.list.label'} = WebGUI::International::get(1019);
$var->{canPost} = $forum->canPost;
return WebGUI::Template::process(WebGUI::Template::get(1,"Forum/Thread"), $var);
}