--- loncom/interface/lonfeedback.pm 2012/03/07 01:26:48 1.290.2.7.2.2 +++ loncom/interface/lonfeedback.pm 2012/03/09 15:09:39 1.290.2.7.2.3 @@ -1,7 +1,7 @@ # The LearningOnline Network # Feedback # -# $Id: lonfeedback.pm,v 1.290.2.7.2.2 2012/03/07 01:26:48 raeburn Exp $ +# $Id: lonfeedback.pm,v 1.290.2.7.2.3 2012/03/09 15:09:39 raeburn Exp $ # # Copyright Michigan State University Board of Trustees # @@ -956,6 +956,9 @@ sub build_posting_display { my $thisuser=$env{'user.name'}.':'.$env{'user.domain'}; # Array with likes to figure out averages, etc. my @theselikes=(); +# Hashes containing likes and unlikes for this user. + my %userlikes=(); + my %userunlikes=(); # Is the user allowed to see the real name behind anonymous postings? my $see_anonymous = &Apache::lonnet::allowed('rin',$env{'request.course.id'}.($env{'request.course.sec'}?'/'.$env{'request.course.sec'}:'')); @@ -993,10 +996,23 @@ sub build_posting_display { next if ($contrib{$idx.':deleted'}); next if ($contrib{$idx.':hidden'}); unless ((($hiddens{$idx}) && (!$seeid)) || ($deletions{$idx}) || (!$contrib{$idx.':message'})) { - push(@theselikes,$likes{$symb.':'.$idx.':likes'}); + if ($likes{$symb.':'.$idx.':likes'} ne '') { + push(@theselikes,$likes{$symb.':'.$idx.':likes'}); + if (ref($likes{$symb.':'.$idx.':likers'}) eq 'HASH') { + if (exists($likes{$symb.':'.$idx.':likers'}{$thisuser})) { + $userlikes{$idx} = 1; + } + } + if (ref($likes{$symb.':'.$idx.':unlikers'}) eq 'HASH') { + if (exists($likes{$symb.':'.$idx.':unlikers'}{$thisuser})) { + $userunlikes{$idx} = 1; + } + } + } } } -# Figure out average likes and standard deviation if there are enough discussions to warrant that +# Figure out average likes and standard deviation if there are enough +# discussions to warrant that my $ave=0; my $stddev=10000; if ($#theselikes>1) { @@ -1053,7 +1069,7 @@ sub build_posting_display { } else { $$replies[$$depth[$idx]]=1; } - unless ((($hiddens{$idx}) && (!$seeid)) || ($deletions{$idx})) { + unless ((($hiddens{$idx}) && (!$seeid)) || ($deletions{$idx})) { $$visible++; if ($contrib{$idx.':history'}) { if ($contrib{$idx.':history'} =~ /:/) { @@ -1390,15 +1406,15 @@ sub build_posting_display { $message. ''; # Put in the like and unlike buttons - unless ($likes{$symb.':'.$idx.':likers'}=~/\,\Q$thisuser\E\,/) { - $$discussionitems[$idx].=' '.&discussion_link($symb,''.&mt('Like').'','like',$idx,$$newpostsflag,$prevread,&group_args($group),&mt("Like this posting")); - } else { + if ($userlikes{$idx}) { $$discussionitems[$idx].=''.&mt('You like this posting').''; - } - unless ($likes{$symb.':'.$idx.':unlikers'}=~/\,\Q$thisuser\E\,/) { - $$discussionitems[$idx].=' '.&discussion_link($symb,''.&mt('Unlike').'','unlike',$idx,$$newpostsflag,$prevread,&group_args($group),&mt("Unlike this posting")); } else { + $$discussionitems[$idx].=' '.&discussion_link($symb,''.&mt('Like').'','like',$idx,$$newpostsflag,$prevread,&group_args($group),&mt("Like this posting")); + } + if ($userunlikes{$idx}) { $$discussionitems[$idx].=''.&mt('You unlike this posting').''; + } else { + $$discussionitems[$idx].=' '.&discussion_link($symb,''.&mt('Unlike').'','unlike',$idx,$$newpostsflag,$prevread,&group_args($group),&mt("Unlike this posting")); } my $thislikes=$likes{$symb.':'.$idx.':likes'}; if ($thislikes>0) { @@ -1513,12 +1529,12 @@ sub get_post_contents { my $sender = $$plainname; if ($$showaboutme) { $sender = &Apache::loncommon::aboutmewrapper( - $$plainname, - $$contrib{$idx.':sendername'}, - $$contrib{$idx.':senderdomain'}); + $$plainname, + $$contrib{$idx.':sendername'}, + $$contrib{$idx.':senderdomain'}); } if ($seeid) { - $sender .= ' ('. $$contrib{$idx.':sendername'}.':'. + $sender .= ' ('.$$contrib{$idx.':sendername'}.':'. $$contrib{$idx.':senderdomain'}.')'; } my $attachmenturls = $$contrib{$idx.':attachmenturl'}; @@ -3919,6 +3935,7 @@ ENDREDIR my $entry=$env{'form.like'}?$env{'form.like'}:$env{'form.unlike'}; my ($symb,$idx)=split(/\:\:\:/,$entry); ($symb,my $feedurl)=&get_feedurl_and_clean_symb($symb); + # # Likes and unlikes are in db-file "disclikes" of the course # The prefix is the $symb to identify the resource discussion, @@ -3929,34 +3946,52 @@ ENDREDIR $env{'course.'.$env{'request.course.id'}.'.domain'}, $env{'course.'.$env{'request.course.id'}.'.num'}, '^'.$prefix); -# Get all who like or unlike this - my $currentlikers=$contrib{$prefix.'likers'}; - my $currentunlikers=$contrib{$prefix.'unlikers'}; +# Get current like or unlike status for the $idx for this user. + my $thisuser=$env{'user.name'}.':'.$env{'user.domain'}; + my ($userlikes,$userunlikes); + if (ref($contrib{$prefix.'likers'}) eq 'HASH') { + if (exists($contrib{$prefix.'likers'}{$thisuser})) { + $userlikes = 1; + } + } + if (ref($contrib{$prefix.'unlikers'}) eq 'HASH') { + if (exists($contrib{$prefix.'unlikers'}{$thisuser})) { + $userunlikes = 1; + } + } + # Get the current "likes" count my $likes=$contrib{$prefix.'likes'}; # Find out if they already voted -# Users cannot like a post twice, or unlike it twice. They can change their mind, though +# Users cannot like a post twice, or unlike it twice. +# They can change their mind, though. my $alreadyflag=0; my $thisuser=$env{'user.name'}.':'.$env{'user.domain'}; if ($env{'form.like'}) { - if ($currentlikers=~/\,\Q$thisuser\E\,/) { + if ($userlikes) { $alreadyflag=1; + } elsif ($userunlikes) { + delete($contrib{$prefix.'unlikers'}{$thisuser}); + $likes++; } else { - if ($currentunlikers=~/\,\Q$thisuser\E\,/) { - $currentunlikers=~s/\,\Q$thisuser\E\,//g; + if (ref($contrib{$prefix.'likers'}) eq 'HASH') { + $contrib{$prefix.'likers'}{$thisuser} = 1; } else { - $currentlikers.=','.$thisuser.','; + $contrib{$prefix.'likers'} = {$thisuser => 1}; } $likes++; } } else { - if ($currentunlikers=~/\,\Q$thisuser\E\,/) { + if ($userunlikes) { $alreadyflag=1; + } elsif ($userlikes) { + delete($contrib{$prefix.'likers'}{$thisuser}); + $likes--; } else { - if ($currentlikers=~/\,\Q$thisuser\E\,/) { - $currentlikers=~s/\,\Q$thisuser\E\,//g; + if (ref($contrib{$prefix.'unlikers'}) eq 'HASH') { + $contrib{$prefix.'unlikers'}{$thisuser} = 1; } else { - $currentunlikers.=','.$thisuser.','; + $contrib{$prefix.'unlikers'} = {$thisuser => 1}; } $likes--; } @@ -3965,8 +4000,8 @@ ENDREDIR # $alreadyflag would be 1 if they tried to double-like or double-unlike unless ($alreadyflag) { my %newhash=($prefix.'likes' => $likes, - $prefix.'likers' => $currentlikers, - $prefix.'unlikers' => $currentunlikers); + $prefix.'likers' => $contrib{$prefix.'likers'}, + $prefix.'unlikers' => $contrib{$prefix.'unlikers'}); # Store data in db-file "disclikes" if (&Apache::lonnet::put('disclikes', \%newhash, @@ -4200,6 +4235,7 @@ ENDREDIR $override = 1; } my $message=&clear_out_html($env{'form.comment'},$override); + # Assemble email my ($email,$citations)=&assemble_email($message,$prevattempts,