[LON-CAPA-cvs] cvs: loncom /interface lonfeedback.pm

raeburn raeburn at source.lon-capa.org
Sat Mar 17 00:02:29 EDT 2012


raeburn		Sat Mar 17 04:02:29 2012 EDT

  Modified files:              
    /loncom/interface	lonfeedback.pm 
  Log:
  - Change to aggregate data stored for likes/votes totals for posters.
    - replace totalvotes with totalunlikes (totalvotes now computed
      when needed from totalunlikes + totallikes, to avoid multiple counts
      in totalvotes when voter changes his/her mind.
    - context (like, unlike, delete, undelete, hide, unhide) stored for each
      record in poster's aggregate data instead of likes (was 1 or -1).
    - deletion, undeletion, hiding and unhiding a post cause totalunlikes and 
      totallikes for poster to be updated appropriately.
  
  
-------------- next part --------------
Index: loncom/interface/lonfeedback.pm
diff -u loncom/interface/lonfeedback.pm:1.348 loncom/interface/lonfeedback.pm:1.349
--- loncom/interface/lonfeedback.pm:1.348	Sat Mar 17 02:55:15 2012
+++ loncom/interface/lonfeedback.pm	Sat Mar 17 04:02:28 2012
@@ -1,7 +1,7 @@
 # The LearningOnline Network
 # Feedback
 #
-# $Id: lonfeedback.pm,v 1.348 2012/03/17 02:55:15 raeburn Exp $
+# $Id: lonfeedback.pm,v 1.349 2012/03/17 04:02:28 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -3204,7 +3204,8 @@
 
 sub getdiscussionstats {
     my %record=&getdiscussionrecords(@_);
-    return ($record{'subnumber'},$record{'points'},$record{'totallikes'},$record{'totalvotes'});
+    my $totalvotes = $record{'totallikes'} + $record{'totalunlikes'};
+    return ($record{'subnumber'},$record{'points'},$record{'totallikes'},$totalvotes);
 }
 
 # Calculate discussion karma
@@ -3268,23 +3269,20 @@
 # Store discussion "likes"
 
 sub storediscussionlikes {
-    my ($likes,$uname,$udom,$course)=@_;
-    unless ($likes) { $likes=0; }
-    if ($likes>0) { $likes=1; }
-    if ($likes<0) { $likes=-1; }
+    my ($chglikes,$chgunlikes,$uname,$udom,$course,$context)=@_;
     unless ($uname) { $uname=$env{'user.name'}; }
     unless ($udom)  { $udom=$env{'user.domain'}; }
     unless ($course) { $course=$env{'request.course.id'}; }
     my %record=&getdiscussionrecords($uname,$udom,$course);
     my $totallikes=$record{'totallikes'};
-    my $totalvotes=$record{'totalvotes'};
-    $totallikes+=$likes;
-    $totalvotes++;
+    my $totalunlikes=$record{'totalunlikes'};
+    $totallikes += $chglikes;
+    $totalunlikes += $chgunlikes;
     my %newrecord=('likes_user'   => $env{'user.name'},
                    'likes_domain' => $env{'user.domain'},
-                   'likes' => $likes,
-                   'totallikes' => $totallikes,
-                   'totalvotes' => $totalvotes);
+                   'totallikes'   => $totallikes,
+                   'totalunlikes' => $totalunlikes,
+                   'context'      => $context);
     my $status=&Apache::lonnet::cstore(\%newrecord,'_discussion',$course,$udom,$uname);
     if ($status eq 'ok') {
         &updatekarma($uname,$udom,$course);
@@ -4070,11 +4068,43 @@
                                    $cdom,$cnum);
           }
       }
-      &Apache::lonnet::store(\%newhash,$symb,$env{'request.course.id'},
-                            $cdom,$cnum);
-
-      &redirect_back($r,$feedurl,&mt('Changed discussion status').'<br />',
-		     '0','0','','',$env{'form.previous'},undef,undef,undef,
+      my $result;
+      if (&Apache::lonnet::store(\%newhash,$symb,$env{'request.course.id'},
+                                 $cdom,$cnum) eq 'ok') {
+          my $prefix=$symb.':'.$idx.':';
+          my %likes=&Apache::lonnet::dump('disclikes',$cdom,$cnum,
+                                          '^'.$prefix);
+          my ($totallikes,$totalunlikes);
+          if (ref($likes{$prefix.'likers'}) eq 'HASH') {
+              $totallikes = scalar(keys(%{$likes{$prefix.'likers'}}));
+          }
+          if (ref($likes{$prefix.'unlikers'}) eq 'HASH') {
+              $totalunlikes = scalar(keys(%{$likes{$prefix.'unlikers'}}));
+          }
+          if ($totallikes || $totalunlikes) {
+              my ($chglikes,$chgunlikes,$context);
+              if ($env{'form.hide'}) {
+                  $chglikes = -1 * $totallikes;
+                  $chgunlikes = -1 * $totalunlikes;
+                  $context = 'hide';
+              } else {
+                  $chglikes = $totallikes;
+                  $chgunlikes = $totalunlikes;
+                  $context = 'unhide';
+              }
+              &storediscussionlikes($chglikes,$chgunlikes,
+                                    $contrib{$idx.':sendername'},
+                                    $contrib{$idx.':senderdomain'},
+                                    $env{'request.course.id'},
+                                    $context);
+          
+          }
+          $result = &mt('Changed discussion status');
+      } else {
+          $result = &mt('Discussion status unchanged'); 
+      }
+      &redirect_back($r,$feedurl,$result.'<br />','0','0','','',
+		     $env{'form.previous'},undef,undef,undef,
                      undef,undef,undef,$group);
       return OK;
   } elsif (($env{'form.like'}) || ($env{'form.unlike'})) {
@@ -4126,11 +4156,13 @@
 # Users cannot like a post twice, or unlike it twice.
 # They can change their mind, though.
                   my $alreadyflag=0;
+                  my $votetype;
                   if ($env{'form.like'}) {
                       if ($userlikes) {
                           $alreadyflag=1;
                       } elsif ($userunlikes) {
                           delete($likes{$prefix.'unlikers'}{$thisuser});
+                          $votetype = 'switch';
                           $likescount++;
                       } else {
                           if (ref($likes{$prefix.'likers'}) eq 'HASH') {
@@ -4145,6 +4177,7 @@
                           $alreadyflag=1;
                       } elsif ($userlikes) {
                           delete($likes{$prefix.'likers'}{$thisuser});
+                          $votetype = 'switch';
                           $likescount--;
                       } else {
                           if (ref($likes{$prefix.'unlikers'}) eq 'HASH') {
@@ -4169,11 +4202,32 @@
 # Store data in db-file "disclikes"
                       if (&Apache::lonnet::put('disclikes',\%newhash,$cdom,$cnum) eq 'ok') {
 # Also store with the person who posted the liked/unliked entry
+                          my ($chglike,$chgunlike);
                           if ($env{'form.like'}) {
-                              &storediscussionlikes(1,$contrib{$idx.':sendername'},$contrib{$idx.':senderdomain'});
+                              if ($votetype eq 'switch') {
+                                  $chglike = 0;
+                                  $chgunlike = -1;
+                              } else {
+                                  $chglike = 1;
+                                  $chgunlike = 0;
+                              } 
+                              &storediscussionlikes($chglike,$chgunlike,
+                                                    $contrib{$idx.':sendername'},
+                                                    $contrib{$idx.':senderdomain'},
+                                                    $env{'request.course.id'},'like');
                               $result=&mt("Registered 'Like'");
                           } else {
-                             &storediscussionlikes(-1,$contrib{$idx.':sendername'},$contrib{$idx.':senderdomain'});
+                              if ($votetype eq 'switch') {
+                                  $chglike = -1;
+                                  $chgunlike = 0;
+                              } else {
+                                  $chglike = 0;
+                                  $chgunlike = 1;
+                              }
+                             &storediscussionlikes($chglike,$chgunlike,
+                                                   $contrib{$idx.':sendername'},
+                                                   $contrib{$idx.':senderdomain'},
+                                                   $env{'request.course.id'},'unlike');
                              $result=&mt("Registered 'Unlike'");
                           }
                       } else {
@@ -4225,10 +4279,35 @@
                                $cdom,$cnum);
       }
       my %newhash=('deleted' => $contrib{'deleted'}.".$idx.");
-      &Apache::lonnet::store(\%newhash,$symb,$env{'request.course.id'},
-                             $cdom,$cnum);
-      &redirect_back($r,$feedurl,&mt('Changed discussion status').'<br />',
-		     '0','0','','',$env{'form.previous'},undef,undef,undef,
+
+      my $result;
+      if (&Apache::lonnet::store(\%newhash,$symb,$env{'request.course.id'},
+                             $cdom,$cnum) eq 'ok') {
+          $result = &mt('Changed discussion status');
+          my $prefix=$symb.':'.$idx.':';
+          my %likes=&Apache::lonnet::dump('disclikes',$cdom,$cnum,
+                                          '^'.$prefix);
+          my ($totallikes,$totalunlikes);
+          if (ref($likes{$prefix.'likers'}) eq 'HASH') {
+              $totallikes = scalar(keys(%{$likes{$prefix.'likers'}}));
+          }
+          if (ref($likes{$prefix.'unlikers'}) eq 'HASH') {
+              $totalunlikes = scalar(keys(%{$likes{$prefix.'unlikers'}}));
+          }
+          if ($totallikes || $totalunlikes) {
+              my $chglikes = -1 * $totallikes;
+              my $chgunlikes = -1 * $totalunlikes;
+              &storediscussionlikes($chglikes,$chgunlikes,
+                                    $contrib{$idx.':sendername'},
+                                    $contrib{$idx.':senderdomain'},
+                                    $env{'request.course.id'},
+                                    'delete');
+          }
+      } else {
+          $result = &mt('Discussion status unchanged');
+      } 
+      &redirect_back($r,$feedurl,$result.'<br />','0','0','','',
+		     $env{'form.previous'},undef,undef,undef,
                      undef,undef,undef,$group);
       return OK;
   } elsif ($env{'form.preview'}) {
@@ -4289,13 +4368,45 @@
       my $ressymb=&wrap_symb($symb);
       $r->print(&Apache::loncommon::start_page('Undelete all deleted discussion entries'));
       if (&can_see_hidden('',$ressymb,$feedurl,$group,$cdom,$cnum)) {
-           if (&Apache::lonnet::store({'deleted' => ''},$symb,$env{'request.course.id'},
-                                      $cdom,$cnum) eq 'ok') {
-               $r->print(&Apache::lonhtmlcommon::confirm_success(&mt("Undeleted all entries")));
-           } else {
-               $r->print(&Apache::lonhtmlcommon::confirm_success(&mt("Failed to undelete entries"),1));
-           }
-           $r->print("<br /><a href='$feedurl'>".&mt("Return and reload")."</a>");
+          my %contrib=&Apache::lonnet::restore($symb,$env{'request.course.id'},
+                                                $cdom,$cnum);
+          $contrib{'deleted'} =~ s/^\.//;
+          $contrib{'deleted'} =~ s/\.$//;
+          if ($contrib{'deleted'} ne '') {
+              if (&Apache::lonnet::store({'deleted' => ''},$symb,$env{'request.course.id'},
+                                          $cdom,$cnum) eq 'ok') {
+                  my %likes=&Apache::lonnet::dump('disclikes',$cdom,$cnum,'^'.$symb.':');
+                  my @ids = split(/\.\./,$contrib{'deleted'});
+                  my (%chglikes,%chgunlikes);
+                  foreach my $idx (@ids) {
+                      my $uname = $contrib{$idx.':sendername'};
+                      my $udom = $contrib{$idx.':senderdomain'};
+                      my ($totallikes,$totalunlikes);
+                      if (ref($likes{$symb.':'.$idx.':likers'}) eq 'HASH') {
+                          $totallikes = scalar(keys(%{$likes{$symb.':'.$idx.':likers'}}));
+                      }
+                      if (ref($likes{$symb.':'.$idx.':unlikers'}) eq 'HASH') {
+                          $totalunlikes = scalar(keys(%{$likes{$symb.':'.$idx.':unlikers'}}));
+                      }
+                      if ($totallikes || $totalunlikes) {
+                          $chglikes{$uname.':'.$udom} += $totallikes;
+                          $chgunlikes{$uname.':'.$udom} += $totalunlikes;
+                      }
+                  }
+                  foreach my $user (keys(%chglikes)) {
+                      my ($uname,$udom) = split(/:/,$user);
+                      &storediscussionlikes($chglikes{$user},$chgunlikes{$user},
+                                            $uname,$udom,$env{'request.course.id'},
+                                            'undelete');
+                  }
+                  $r->print(&Apache::lonhtmlcommon::confirm_success(&mt("Undeleted all entries")));
+              } else {
+                  $r->print(&Apache::lonhtmlcommon::confirm_success(&mt("Failed to undelete entries"),1));
+              }
+          } else {
+              $r->print(&Apache::lonhtmlcommon::confirm_success(&mt("No entries to undelete"),1));
+          }
+          $r->print("<br /><a href='$feedurl'>".&mt("Return and reload")."</a>");
       }
       $r->print(&Apache::loncommon::end_page());
       return OK;
@@ -4654,6 +4765,8 @@
 
 =item list_discussion()
 
+=item can_see_discussion()
+
 =item send_feedback_link()
 
 =item send_message_link()


More information about the LON-CAPA-cvs mailing list