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

www www at source.lon-capa.org
Wed Jan 4 18:31:37 EST 2012


www		Wed Jan  4 23:31:37 2012 EDT

  Modified files:              
    /loncom/interface	lonfeedback.pm 
  Log:
  Work towards bugs #2174 and #6543
  
  
Index: loncom/interface/lonfeedback.pm
diff -u loncom/interface/lonfeedback.pm:1.320 loncom/interface/lonfeedback.pm:1.321
--- loncom/interface/lonfeedback.pm:1.320	Wed Jan  4 20:12:40 2012
+++ loncom/interface/lonfeedback.pm	Wed Jan  4 23:31:36 2012
@@ -1,7 +1,7 @@
 # The LearningOnline Network
 # Feedback
 #
-# $Id: lonfeedback.pm,v 1.320 2012/01/04 20:12:40 www Exp $
+# $Id: lonfeedback.pm,v 1.321 2012/01/04 23:31:36 www Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -2821,18 +2821,7 @@
 
 sub getfeedbackstats {
     my %record=&getfeedbackrecords(@_);
-    my $subnumber=$record{'subnumber'};
-    my $points=0;
-    my $likes=0;
-    foreach my $key (keys(%record)) {
-        if ($key=~/^\d+\:points$/) {
-            $points+=$record{$key};
-        }
-        if ($key=~/^\d+\:likes$/) {
-            $likes+=$record{$key};
-        }
-    }
-    return ($subnumber,$points,$likes);
+    return ($record{'subnumber'},$record{'points'},$record{'totallikes'});
 }
 
 # Store feedback credit
@@ -2859,10 +2848,14 @@
     unless ($uname) { $uname=$env{'user.name'}; }
     unless ($udom)  { $udom=$env{'user.domain'}; }
     unless ($course) { $course=$env{'request.course.id'}; }
-    my %record=('likes_user'   => $env{'user.name'},
-                'likes_domain' => $env{'user.domain'},
-                'likes' => $likes);
-    return &Apache::lonnet::cstore(\%record,'_feedback');
+    my %record=&getfeedbackrecords($uname,$udom,$course);
+    my $totallikes=$record{'totallikes'};
+    $totallikes+=$likes;
+    my %newrecord=('likes_user'   => $env{'user.name'},
+                   'likes_domain' => $env{'user.domain'},
+                   'likes' => $likes,
+                   'totallikes' => $totallikes);
+    return &Apache::lonnet::cstore(\%newrecord,'_feedback');
 }
 
 
@@ -2964,7 +2957,7 @@
                      $env{'course.'.$env{'request.course.id'}.'.domain'},
 		     $env{'course.'.$env{'request.course.id'}.'.num'});
     }
-    my %record=&Apache::lonnet::restore('_discussion');
+    my %record=&getdiscussionrecords();
     my ($temp)=keys(%record);
     unless ($temp=~/^error\:/) {
        my %newrecord=();
@@ -2979,6 +2972,60 @@
     return $status.'<br />';   
 }
 
+
+# Routine to get the complete discussion records
+
+sub getdiscussionrecords {
+    my ($uname,$udom,$course)=@_;
+    unless ($uname) { $uname=$env{'user.name'}; }
+    unless ($udom)  { $udom=$env{'user.domain'}; }
+    unless ($course) { $course=$env{'request.course.id'}; }
+    my %record=&Apache::lonnet::restore('_discussion',$course,$udom,$uname);
+    return %record;
+}
+
+# Routine to get discussion statistics
+
+sub getdiscussionstats {
+    my %record=&getdiscussionrecords(@_);
+    return ($record{'subnumber'},$record{'points'},$record{'totallikes'});
+}
+
+
+# Store discussion credit
+
+sub storediscussionpoints {
+    my ($points,$uname,$udom,$course)=@_;
+    unless ($points) { $points=0; }
+    unless ($uname) { $uname=$env{'user.name'}; }
+    unless ($udom)  { $udom=$env{'user.domain'}; }
+    unless ($course) { $course=$env{'request.course.id'}; }
+    my %record=('grader_user'   => $env{'user.name'},
+                'grader_domain' => $env{'user.domain'},
+                'points' => $points);
+    return &Apache::lonnet::cstore(\%record,'_discussion');
+}
+
+# Store discussion "likes"
+
+sub storediscussionlikes {
+    my ($likes,$uname,$udom,$course)=@_;
+    unless ($likes) { $likes=0; }
+    if ($likes>0) { $likes=1; }
+    if ($likes<0) { $likes=-1; }
+    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'};
+    $totallikes+=$likes;
+    my %newrecord=('likes_user'   => $env{'user.name'},
+                   'likes_domain' => $env{'user.domain'},
+                   'likes' => $likes,
+                   'totallikes' => $totallikes);
+    return &Apache::lonnet::cstore(\%newrecord,'_discussion');
+}
+
 sub get_discussion_info {
     my ($idx,%contrib) = @_;
     my $changelast = 0;
@@ -3782,6 +3829,61 @@
 		     '0','0','','',$env{'form.previous'},undef,undef,undef,
                      undef,undef,undef,$group);
       return OK;
+  } elsif (($env{'form.like'}) || ($env{'form.unlike'})) {
+# ----------------------------------------------------------------- Like/unlike
+      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);
+      my %contrib=&Apache::lonnet::restore($symb,$env{'request.course.id'},
+                          $env{'course.'.$env{'request.course.id'}.'.domain'},
+                          $env{'course.'.$env{'request.course.id'}.'.num'});
+# Get all who like or unlike this
+      my $currentlikers=$contrib{$idx.':likers'};
+      my $currentunlikers=$contrib{$idx.':unlikers'};
+      my $likes=$contrib{$idx.':likes'};
+# Find out if they already voted
+      my $alreadyflag=0;
+      my $thisuser=$env{'user.name'}.':'.$env{'user.domain'};
+      if ($env{'form.like'}) {
+          if ($currentlikers=~/\.\Q$thisuser\E\./) {
+              $alreadyflag=1;
+          } else {
+              $currentunlikers=~s/\.\Q$thisuser\E\.//;
+              $currentlikers.='.'.$thisuser.'.';
+              $likes++;
+          } 
+      } else {
+          if ($currentunlikers=~/\.\Q$thisuser\E\./) {
+              $alreadyflag=1;
+          } else {
+              $currentlikers=~s/\.\Q$thisuser\E\.//;
+              $currentunlikers.='.'.$thisuser.'.';
+              $likes--;
+          } 
+      }
+      my $result;
+      unless ($alreadyflag) {
+          my %newhash=($idx.':likes'    => $likes,
+                       $idx.':likers'   => $currentlikers,
+                       $idx.':unlikers' => $currentunlikers);
+          if (&Apache::lonnet::store(\%newhash,$symb,$env{'request.course.id'},
+                           $env{'course.'.$env{'request.course.id'}.'.domain'},
+                           $env{'course.'.$env{'request.course.id'}.'.num'}) eq 'ok') {
+              if ($env{'form.like'}) {
+                  &storediscussionlikes(1,$contrib{$idx.':sendername'},$contrib{$idx.':senderdomain'});
+                  $result=&mt("Registered 'Like'");
+              } else {
+                  &storediscussionlikes(-1,$contrib{$idx.':sendername'},$contrib{$idx.':senderdomain'});
+                  $result=&mt("Registered 'Unlike'");
+              }
+          } else {
+              $result=&mt("Failed to register vote");
+          }
+      }
+      &redirect_back($r,$feedurl,$result.'<br />',
+                     '0','0','','',$env{'form.previous'},undef,undef,undef,
+                     undef,undef,undef,$group);
+      return OK;
   } elsif ($env{'form.cmd'}=~/^(threadedoff|threadedon)$/) {
       my ($symb,$feedurl)=&get_feedurl_and_clean_symb($env{'form.symb'});
       if ($env{'form.cmd'} eq 'threadedon') {




More information about the LON-CAPA-cvs mailing list