--- loncom/interface/lonfeedback.pm	2005/11/18 13:35:30	1.175
+++ loncom/interface/lonfeedback.pm	2006/03/15 22:06:08	1.185
@@ -1,7 +1,7 @@
 # The LearningOnline Network
 # Feedback
 #
-# $Id: lonfeedback.pm,v 1.175 2005/11/18 13:35:30 www Exp $
+# $Id: lonfeedback.pm,v 1.185 2006/03/15 22:06:08 www Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -906,16 +906,16 @@ sub build_posting_display {
                             } else {
                                 @{$$namesort{$lastname}{$firstname}} = ("$idx");
                             }
-                            if ($env{'course.'.$env{'request.course.id'}.'.allow_discussion_post_editing'} =~ m/yes/i) {
+                            if (&editing_allowed()) {
                                 if (($env{'user.domain'} eq $contrib{$idx.':senderdomain'}) && ($env{'user.name'} eq $contrib{$idx.':sendername'})) {
                                     $sender.=' <a href="/adm/feedback?editdisc='.
                                          $escsymb.':::'.$idx;
-                                    if ($newpostsflag) {
+                                    if ($$newpostsflag) {
                                         $sender .= '&previous='.$prevread;
                                     }
                                     $sender .= '" '.$target.'>'.&mt('Edit').'</a>';                                             
                                     unless ($seeid) {
-                                        $sender.=" <a href=\"javascript:studentdelete('$escsymb','$idx','$newpostsflag','$prevread')";
+                                        $sender.=" <a href=\"javascript:studentdelete('$escsymb','$idx','$$newpostsflag','$prevread')";
                                         $sender .= '">'.&mt('Delete').'</a>';
                                     }
                                 }
@@ -925,22 +925,22 @@ sub build_posting_display {
                                     unless ($studenthidden) {
 			                $sender.=' <a href="/adm/feedback?unhide='.
 				                $escsymb.':::'.$idx;
-                                        if ($newpostsflag) {
-                                             $sender .= '&previous='.$prevread;
+                                        if ($$newpostsflag) {
+                                            $sender .= '&previous='.$prevread;
                                         }
                                         $sender .= '">'.&mt('Make Visible').'</a>';
                                     }
 			        } else {
 				    $sender.=' <a href="/adm/feedback?hide='.
 				        $escsymb.':::'.$idx;
-                                    if ($newpostsflag) {
+                                    if ($$newpostsflag) {
                                         $sender .= '&previous='.$prevread;
                                     }
                                     $sender .= '">'.&mt('Hide').'</a>';
 			        }                     
 			        $sender.=' <a href="/adm/feedback?deldisc='.
 				        $escsymb.':::'.$idx;
-                                if ($newpostsflag) {
+                                if ($$newpostsflag) {
                                     $sender .= '&previous='.$prevread;
                                 }
                                 $sender .= '">'.&mt('Delete').'</a>';
@@ -976,8 +976,8 @@ sub build_posting_display {
 						 ($env{'request.course.sec'}?'/'.$env{'request.course.sec'}:''))) {
 			    $sender.=' <a href="/adm/feedback?replydisc='.
 			            $escsymb.':::'.$idx;
-                            if ($newpostsflag) {
-                                    $sender .= '&previous='.$prevread;
+                            if ($$newpostsflag) {
+                                $sender .= '&previous='.$prevread;
                             }
                             $sender .= '" '.$target.'>'.&mt('Reply').'</a>';
                         }
@@ -2224,7 +2224,7 @@ $html
 <meta http-equiv="pragma" content="no-cache" />
 <meta HTTP-EQUIV="Refresh" CONTENT="2; url=$feedurl" />
 </head>
-<body bgcolor="#FFFFFF" onLoad='if (window.name!="loncapaclient") { this.document.reldt.submit(); self.close(); }'>
+<body bgcolor="#FFFFFF" onLoad='if (window.name!="loncapaclient") { this.document.reldt.submit(); self.window.close(); }'>
 <img align="right" src="$logo" />
 $typestyle
 <b>Sent $sendsomething message(s), and $sendposts post(s).</b>
@@ -2264,7 +2264,7 @@ ENDNOREDIR
   my $logo=&Apache::loncommon::lonhttpdurl('/adm/lonIcons/lonlogos.gif');
   $r->print (<<ENDNOREDIRTWO);
 </head>
-<body bgcolor="#FFFFFF" onLoad='if (window.name!="loncapaclient") { self.close(); }'>
+<body bgcolor="#FFFFFF" onLoad='if (window.name!="loncapaclient") { self.window.close(); }'>
 <img align="right" src="$logo" />
 <b>$nofeed</b>
 <br /><a href="$feedurl">$continue</a>
@@ -2315,7 +2315,7 @@ sub screen_header {
 		'<a href="/adm/preferences?action=changescreenname">'.&mt('Change Screenname').'</a>';
         }
         $discussoptions.='<br /><label><input type="checkbox" name="blog" /> '.
-	    &mt('Add to my course blog').'</label>';
+	    &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) { 
@@ -2478,14 +2478,16 @@ sub feedback_available {
 }
 
 sub send_msg {
-  my ($feedurl,$email,$citations,$attachmenturl,%to)=@_;
+  my ($title,$feedurl,$email,$citations,$attachmenturl,%to)=@_;
   my $status='';
   my $sendsomething=0;
+  if ($title=~/^Error/) { $title=&mt('Feedback').': '.$title; }
+  unless ($title=~/\w/) { $title=&mt('Feedback'); }
   foreach (keys %to) {
     if ($_) {
       my $declutter=&Apache::lonnet::declutter($feedurl);
       unless (&Apache::lonmsg::user_normal_msg(split(/\:/,$_),
-               'Feedback ['.$declutter.']',$email,$citations,$feedurl,
+               $title.' ['.$declutter.']',$email,$citations,$feedurl,
                 $attachmenturl)=~/ok/) {
 	$status.='<br />'.&mt('Error sending message to').' '.$_.'<br />';
       } else {
@@ -2539,7 +2541,6 @@ sub adddiscuss {
     }
     if (($symb) && ($email)) {
         if ($env{'form.editdisc'}) {
-            my %newcontrib = ();
             $contrib{'ip'}=$ENV{'REMOTE_ADDR'};
             $contrib{'host'}=$Apache::lonnet::perlvar{'lonHostID'};
             $contrib{'timestamp'} = time;
@@ -2582,12 +2583,8 @@ sub adddiscuss {
                 }
             }
             $contrib{'history'} .= $oldcontrib{$oldidx.':timestamp'};
-            foreach (keys %contrib) {
-                my $key = $oldidx.':'.&Apache::lonnet::escape($oldsymb).':'.$_;                                                                               
-                $newcontrib{$key} = $contrib{$_};
-            }
             my $put_reply = &Apache::lonnet::putstore($env{'request.course.id'},
-                  \%newcontrib,
+                  $oldsymb,$oldidx,\%contrib,
                   $env{'course.'.$env{'request.course.id'}.'.domain'},
                   $env{'course.'.$env{'request.course.id'}.'.num'});
             $status='Editing class discussion'.($anon?' (anonymous)':'');
@@ -2992,7 +2989,15 @@ sub handler {
 
   &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
          ['hide','unhide','deldisc','postdata','preview','replydisc','editdisc','cmd','symb','onlyunread','allposts','onlyunmark','previous','markread','markonread','markondisp','toggoff','toggon','modifydisp','changes','navtime','navmaps','navurl','sortposts','applysort','rolefilter','statusfilter','sectionpick','posterlist','userpick','attach','origpage','currnewattach','deloldattach','keepold','allversions','export']);
-
+  if ($env{'form.editdisc'}) {
+      if (!(&editing_allowed())) {
+          my $symb=(split(/\:\:\:/,$env{'form.editdisc'}))[0];
+          my ($map,$id,$url)=&Apache::lonnet::decode_symb($symb);
+          my $feedurl=&Apache::lonnet::clutter($url);
+          &redirect_back($r,$feedurl,&mt('Editing not permitted').'<br />',                     '0','0','','',$env{'form.previous'},'','','',);
+          return OK;
+      }
+  } 
   if ($env{'form.discsymb'}) {
       my ($symb,$feedurl) = &get_feedurl_and_clean_symb($env{'form.discsymb'});
       my $readkey = $symb.'_read';
@@ -3128,7 +3133,7 @@ $html
 <meta http-equiv="pragma" content="no-cache" />
 <meta HTTP-EQUIV="Refresh" CONTENT="2; url=$feedurl" />
 </head>
-<body bgcolor="#FFFFFF" onLoad='if (window.name!="loncapaclient") { this.document.reldt.submit(); self.close(); }'>
+<body bgcolor="#FFFFFF" onLoad='if (window.name!="loncapaclient") { this.document.reldt.submit(); self.window.close(); }'>
 <img align="right" src="$logo" />
 $textline
 <form name="reldt" action="$feedurl" target="loncapaclient">
@@ -3187,6 +3192,18 @@ ENDREDIR
       my ($symb,$idx)=split(/\:\:\:/,$entry);
       ($symb,my $feedurl)=&get_feedurl_and_clean_symb($symb);
 
+      my $crs='/'.$env{'request.course.id'};
+      if ($env{'request.course.sec'}) {
+          $crs.='_'.$env{'request.course.sec'};
+      }
+      $crs=~s/\_/\//g;
+      my $seeid=&Apache::lonnet::allowed('rin',$crs);
+
+      if ($env{'form.hide'} && !$seeid && !(&editing_allowed())) {
+          &redirect_back($r,$feedurl,&mt('Deletion not permitted').'<br />',                 '0','0','','',$env{'form.previous'},'','','',);
+          return OK;
+      }
+
       my %contrib=&Apache::lonnet::restore($symb,$env{'request.course.id'},
                           $env{'course.'.$env{'request.course.id'}.'.domain'},
 		          $env{'course.'.$env{'request.course.id'}.'.num'});
@@ -3194,13 +3211,6 @@ ENDREDIR
       my $currenthidden=$contrib{'hidden'};
       my $currentstudenthidden=$contrib{'studenthidden'};
 
-      my $crs='/'.$env{'request.course.id'};
-      if ($env{'request.course.sec'}) {
-	  $crs.='_'.$env{'request.course.sec'};
-      }
-      $crs=~s/\_/\//g;
-      my $seeid=&Apache::lonnet::allowed('rin',$crs);
-
       if ($env{'form.hide'}) {
 	  $currenthidden.='.'.$idx.'.';
 	  unless ($seeid) {
@@ -3347,6 +3357,7 @@ ENDREDIR
 	  $r->send_http_header;
 # Unable to give feedback
 	  &no_redirect_back($r,$feedurl);
+	  return OK;
       }
 # --------------------------------------------------- Print login screen header
       unless ($env{'form.sendit'}) {
@@ -3410,7 +3421,7 @@ ENDREDIR
       my ($typestyle,%to) = &decide_receiver($feedurl);
 
 # Actually send mail
-      my ($status,$numsent)=&send_msg($feedurl,$email,$citations,
+      my ($status,$numsent)=&send_msg(&clear_out_html($env{'form.subject'}),$feedurl,$email,$citations,
 				      $attachmenturl,%to);
 
 # Discussion? Store that.
@@ -3476,5 +3487,34 @@ sub get_feedurl_and_clean_symb {
     my $feedurl = &get_feedurl($symb);
     return ($symb,$feedurl);
 }
+
+sub editing_allowed {
+    my $can_edit = 0;
+    my $cid = $env{'request.course.id'};
+    my $role = (split(/\./,$env{'request.role'}))[0];
+    my $section = $env{'request.course.sec'};
+    my $allow_editing_config = 
+	$env{'course.'.$cid.'.allow_discussion_post_editing'};
+    if ($allow_editing_config =~ m/^\s*yes\s*$/i) {
+        $can_edit = 1;
+    } else {
+	foreach my $editor (split(/,/,$allow_editing_config)) {
+	    my ($editor_role,$editor_sec) = split(/:/,$editor);
+	    if ($editor_role eq $role
+		&& defined($editor_sec)
+		&& defined($section)
+		&& $editor_sec eq $section) {
+		$can_edit = 1;
+		last;
+	    }
+	    if ($editor_role eq $role
+		&& !defined($editor_sec)) {
+		$can_edit = 1;
+	    }
+	}
+    }
+    return $can_edit;
+}
+
 1;
 __END__