--- loncom/interface/lonfeedback.pm	2006/11/29 04:25:23	1.222
+++ loncom/interface/lonfeedback.pm	2006/12/08 00:28:59	1.231
@@ -1,7 +1,7 @@
 # The LearningOnline Network
 # Feedback
 #
-# $Id: lonfeedback.pm,v 1.222 2006/11/29 04:25:23 raeburn Exp $
+# $Id: lonfeedback.pm,v 1.231 2006/12/08 00:28:59 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -44,7 +44,6 @@ use HTML::LCParser();
 use Apache::lonspeller();
 use Apache::longroup;
 use Cwd;
-use lib '/home/httpd/lib/perl/';
 use LONCAPA;
 
 sub discussion_open {
@@ -93,6 +92,11 @@ sub list_discussion {
         }
     }
 
+    my ($blocked,$blocktext) = &blocking_posts('boards',1);
+    if ($blocked) {
+        return $blocktext;
+    }
+
     my @bgcols = ("#cccccc","#eeeeee");
     my $discussiononly=0;
     if ($mode eq 'board') { $discussiononly=1; }
@@ -103,7 +107,7 @@ sub list_discussion {
 	$crs.='_'.$env{'request.course.sec'};
     }
     $crs=~s/\_/\//g;
-    unless ($ressymb) {	$ressymb=&Apache::lonnet::symbread(); }
+    unless ($ressymb) { $ressymb=&Apache::lonnet::symbread(); }
     unless ($ressymb) { return ''; }
     $ressymb=&wrap_symb($ressymb);
     my $encsymb=&Apache::lonenc::check_encrypt($ressymb);
@@ -791,6 +795,25 @@ sub action_links_bar {
     return $discussion;
 }
 
+sub blocking_posts {
+    my ($type,$showstatus) = @_;
+    my %setters;
+    my ($blocked,$output);
+    my ($startblock,$endblock) =
+          &Apache::loncommon::blockcheck(\%setters,$type);
+    if ($startblock && $endblock) {
+        $blocked = 1;
+        if ($showstatus) {
+            my $showstart = &Apache::lonlocal::locallocaltime($startblock);
+            my $showend = &Apache::lonlocal::locallocaltime($endblock);
+            $output = &mt('Discussion postings will not be viewable for resources in this course between [_1] and [_2] because communication is being blocked.',$showstart, $showend).'<br />'.
+                    &Apache::loncommon::build_block_table($startblock,$endblock,
+                                                         \%setters);
+        }
+    }
+    return ($blocked,$output);
+}
+
 sub postingform_display {
     my ($mode,$ressymb,$now,$subject,$comment,$outputtarget,$attachnum,
         $currnewattach,$currdelold,$group) = @_;
@@ -827,11 +850,16 @@ ENDDISCUSS
     if ($group ne '') {
         $postingform .='<input type="hidden" name="group" value="'.$group.'" />';
     }
+    my ($blockblog) = &blocking_posts('blogs');
+    if (!$blockblog) {
+        $postingform .= &add_blog_checkbox();
+    }
     $postingform .= "</form>\n";
     if ($outputtarget ne 'tex') {
         $postingform .= &generate_attachments_button('',$attachnum,$ressymb,
                                                      $now,$currnewattach,
-                                                     $currdelold,'',$mode);
+                                                     $currdelold,'',$mode,
+                                                     $blockblog);
         if ((ref($currnewattach) eq 'ARRAY') && (@{$currnewattach} > 0)) {
             $newattachmsg = '<br /><b>'.$lt{'newa'}.'</b><br />';
             if (@{$currnewattach} > 1) {
@@ -1532,7 +1560,7 @@ END
               if ($idx > 0) {
                   my %subversions = ();
                   &get_post_versions(\%subversions,$contrib{$idx.':subject'},1,$numoldver);
-                  $subject = &mt('Re: ')..$subversions{$numoldver};
+                  $subject = &mt('Re: ').$subversions{$numoldver};
               }
               $subject = &HTML::Entities::encode($subject,'<>&"');
           } else {
@@ -1707,7 +1735,8 @@ END
         if (@currnewattach > 0) {
             $attachnum += @currnewattach;
         }
-        $r->print(&generate_attachments_button($postidx,$attachnum,$ressymb,$now,\@currnewattach,\@currdelold,$numoldver));
+        my ($blockblog) = &blocking_posts('blogs');
+        $r->print(&generate_attachments_button($postidx,$attachnum,$ressymb,$now,\@currnewattach,\@currdelold,$numoldver,'',$blockblog));
         if ($attachnum > 0) {
             if (@currnewattach > 0) {
                 $newattachmsg .= '<br /><b>'.&mt('New attachments').'</b><br />';
@@ -2543,15 +2572,20 @@ sub screen_header {
 	    &Apache::lonnet::allowed('pch',
 				     $env{'request.course.id'}.
 				     ($env{'request.course.sec'}?'/'.$env{'request.course.sec'}:''))) {
-	    $discussoptions='<label><input type="radio" name="discuss" value="nonanon" checked="checked" /> '.
-		&mt('Contribution to course discussion of resource');
-	    $discussoptions.='</label><br /><label><input type="radio" name="discuss" value="anon" /> '.
-		&mt('Anonymous contribution to course discussion of resource').
-		' <i>('.&mt('name only visible to course faculty').')</i></label> '.
-		'<a href="/adm/preferences?action=changescreenname">'.&mt('Change Screenname').'</a>';
+            my ($blocked) = &blocking_posts('boards');
+            if (!$blocked) {
+	        $discussoptions='<label><input type="radio" name="discuss" value="nonanon" checked="checked" /> '.
+		    &mt('Contribution to course discussion of resource');
+	        $discussoptions.='</label><br /><label><input type="radio" name="discuss" value="anon" /> '.
+		    &mt('Anonymous contribution to course discussion of resource').
+		    ' <i>('.&mt('name only visible to course faculty').')</i></label> '.
+		    '<a href="/adm/preferences?action=changescreenname">'.&mt('Change Screenname').'</a>';
+            }
+        }
+        my ($blockblog) = &blocking_posts('blogs');
+        if (!$blockblog) {
+            $discussoptions.= &add_blog_checkbox();
         }
-        $discussoptions.='<br /><label><input type="checkbox" name="blog" /> '.
-	    &mt('Add to my public course blog').'</label>';
     }
     if ($msgoptions) { $msgoptions='<h2><img src="'.&Apache::loncommon::lonhttpdurl('/adm/lonMisc/feedback.gif').'" />'.&mt('Sending Messages').'</h2>'.$msgoptions; }
     if ($discussoptions) { 
@@ -2676,7 +2710,7 @@ sub decide_receiver {
   my %to=();
   if ($env{'form.discuss'} eq 'author' ||$author) {
     $typestyle.='Submitting as Author Feedback<br />';
-    $feedurl=~/^\/res\/(\w+)\/(\w+)\//;
+    $feedurl=~ m{^/res/($LONCAPA::domain_re)/($LONCAPA::username_re)/};
     $to{$2.':'.$1}=1;
   }
   if ($env{'form.discuss'} eq 'question' ||$question) {
@@ -2782,10 +2816,11 @@ sub adddiscuss {
 	$contrib{'anonymous'}='true';
     }
     if (($symb) && ($email)) {
+        my $now = time;
         if ($env{'form.editdisc'}) {
             $contrib{'ip'}=$ENV{'REMOTE_ADDR'};
             $contrib{'host'}=$Apache::lonnet::perlvar{'lonHostID'};
-            $contrib{'timestamp'} = time;
+            $contrib{'timestamp'} = $now;
             $contrib{'history'} = '';
             my $numoldver = 0;
             my ($oldsymb,$oldidx)=split(/\:\:\:/,$env{'form.editdisc'});
@@ -2836,7 +2871,7 @@ sub adddiscuss {
                      $env{'course.'.$env{'request.course.id'}.'.domain'},
 		     $env{'course.'.$env{'request.course.id'}.'.num'});
         }
-        my %storenewentry=($symb => time);
+        my %storenewentry=($symb => $now);
         $status.='<br />'.&mt('Updating discussion time').': '.
         &Apache::lonnet::put('discussiontimes',\%storenewentry,
                      $env{'course.'.$env{'request.course.id'}.'.domain'},
@@ -2857,6 +2892,48 @@ sub adddiscuss {
     return $status.'<br />';   
 }
 
+sub get_discussion_info {
+    my ($idx,%contrib) = @_;
+    my $changelast = 0;
+    my $count = 0;
+    my $hiddenflag = 0;
+    my $deletedflag = 0;
+    my ($hidden,$deleted,%info,$newlastdisc);
+    my $version = $contrib{'version'};
+    if ($version) {
+        for (my $id=$version; $id>0; $id--) {
+            my $vkeys=$contrib{$id.':keys'};
+            my @keys=split(/:/,$vkeys);
+            if (grep(/^hidden$/,@keys)) {
+                if (!$hiddenflag) {
+                    $hidden = $contrib{$id.':hidden'};
+                    $hiddenflag = 1;
+                }
+            } elsif (grep(/^deleted$/,@keys)) {
+                if (!$deletedflag) {
+                    $deleted = $contrib{$id.':deleted'};
+                    $deletedflag = 1;
+                }
+            } else {
+                if (($hidden !~/\.$id\./) && ($deleted !~/\.$id\./)) {
+                    $count++;
+                    $info{$count}{'id'} = $id;
+                    $info{$count}{'timestamp'}=$contrib{$id.':timestamp'}; 
+                }
+            }
+        }
+        if ($info{'1'}{'id'} == $idx) {
+            $changelast = 1;
+            if ($count > 1) { 
+                $newlastdisc =  $info{'2'}{'timestamp'};
+            } else {
+                $newlastdisc = 0;
+            }
+        }
+    }
+    return ($changelast,$newlastdisc);
+}
+
 # ----------------------------------------------------------- Preview function
 
 sub show_preview {
@@ -3048,7 +3125,8 @@ sub process_attachments {
 }
 
 sub generate_attachments_button {
-    my ($idx,$attachnum,$ressymb,$now,$currnewattach,$deloldattach,$numoldver,$mode) = @_;
+    my ($idx,$attachnum,$ressymb,$now,$currnewattach,$deloldattach,
+        $numoldver,$mode,$blockblog) = @_;
     my $origpage = $ENV{'REQUEST_URI'};
     my $att=$attachnum.' '.&mt("attachments");
     my %lt = &Apache::lonlocal::texthash(
@@ -3060,6 +3138,9 @@ sub generate_attachments_button {
 $lt{'clic'}:&nbsp;<input type="button" value="$att"
 onClick="if (typeof(document.mailform.onsubmit)=='function') {document.mailform.onsubmit();};this.form.comment.value=escape(document.mailform.comment.value);this.form.subject.value=escape(document.mailform.subject.value);
 END
+    if (!$blockblog) {
+        $response .= 'setblogvalue();';
+    }
     unless ($mode eq 'board') {
         $response .= 'javascript:anonchk();';
     }
@@ -3199,6 +3280,26 @@ sub construct_attachmenturl {
     return $newattachmenturl; 
 }
 
+sub add_blog_checkbox {
+    my ($checkstatus);
+    if ($env{'form.blog'}) {
+        $checkstatus = 'checked="checked"';
+    }
+    my $output = '
+<script type="text/javascript">
+function setblogvalue() {
+    if (document.mailform.blog.checked) {
+        document.attachment.blog.value = 1;
+    } else {
+        document.attachment.blog.value = 0;
+    }
+}
+</script><br />
+<label><input type="checkbox" name="blog" '.$checkstatus.' /> '.
+&mt('Add to my public course blog').'</label><br />'."\n";
+    return $output;
+}
+
 sub has_discussion {
     my $resourcesref = shift;
     my $navmap = Apache::lonnavmaps::navmap->new();
@@ -3501,7 +3602,16 @@ ENDREDIR
       if ( ($env{'form.hide'}) && (!$seeid) ) {
 	  $newhash{'studenthidden'} = $currentstudenthidden;
       }
-
+      if ($env{'form.hide'}) {
+          my $changelast = 0;
+          my $newlast;
+          ($changelast,$newlast) = &get_discussion_info($idx,%contrib);
+          if ($changelast) {
+              &Apache::lonnet::put('discussiontimes',{$symb => $newlast},
+                     $env{'course.'.$env{'request.course.id'}.'.domain'},
+                     $env{'course.'.$env{'request.course.id'}.'.num'});
+          }
+      }
       &Apache::lonnet::store(\%newhash,$symb,$env{'request.course.id'},
                            $env{'course.'.$env{'request.course.id'}.'.domain'},
 			   $env{'course.'.$env{'request.course.id'}.'.num'});
@@ -3530,6 +3640,11 @@ ENDREDIR
       my %contrib=&Apache::lonnet::restore($symb,$env{'request.course.id'},
                           $env{'course.'.$env{'request.course.id'}.'.domain'},
 		          $env{'course.'.$env{'request.course.id'}.'.num'});
+      my ($changelast,$newlast) = &get_discussion_info($idx,%contrib);
+      if ($changelast) {
+          &Apache::lonnet::put('discussiontimes',{$symb => $newlast},
+                   $env{'course.'.$env{'request.course.id'}.'.domain'},                   $env{'course.'.$env{'request.course.id'}.'.num'});
+      }
       my %newhash=('deleted' => $contrib{'deleted'}.".$idx.");
       &Apache::lonnet::store(\%newhash,$symb,$env{'request.course.id'},
 			   $env{'course.'.$env{'request.course.id'}.'.domain'},