--- loncom/interface/lonfeedback.pm	2006/01/11 08:31:37	1.178
+++ loncom/interface/lonfeedback.pm	2006/03/19 21:43:21	1.186
@@ -1,7 +1,7 @@
 # The LearningOnline Network
 # Feedback
 #
-# $Id: lonfeedback.pm,v 1.178 2006/01/11 08:31:37 albertel Exp $
+# $Id: lonfeedback.pm,v 1.186 2006/03/19 21:43:21 albertel 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>';
                         }
@@ -1303,8 +1303,7 @@ sub mail_screen {
   if (exists($env{'form.origpage'})) {
       &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},['subject','comment','currnewattach','addnewattach','deloldattach','delnewattach','timestamp','idx','anondiscuss','discuss','blog']);
   }
-  my $bodytag=&Apache::loncommon::bodytag('Resource Feedback and Discussion',
-                                          '','onLoad="window.focus();setposttype();"');
+
   my $title=&Apache::lonnet::gettitle($feedurl);
   if (!$title) { $title = $feedurl; }
   my $quote='';
@@ -1442,15 +1441,8 @@ END
       &process_attachments(\@currnewattach,\@currdelold,\@keepold);
   }
   my $latexHelp=&Apache::loncommon::helpLatexCheatsheet();
-  my $htmlheader=&Apache::lonhtmlcommon::htmlareaheaders();
   my $send=&mt('Send');
-  my $html=&Apache::lonxml::xmlbegin();
-  $r->print(<<END);
-$html
-<head>
-<title>The LearningOnline Network with CAPA</title>
-<meta http-equiv="pragma" content="no-cache"></meta>
-$htmlheader
+  my $js= <<END;
 <script type="text/javascript">
 //<!--
     function gosubmit() {
@@ -1504,8 +1496,15 @@ $htmlheader
     $anonscript
 //-->
 </script>
-</head>
-$bodytag
+END
+
+  my $onload = 'onLoad="window.focus();setposttype();"';
+  my $start_page=
+      &Apache::loncommon::start_page('Resource Feedback and Discussion',$js,
+				     {'add_entries' => $onload});
+
+  $r->print(<<END);
+$start_page
 <h2><tt>$title</tt></h2>
 <form action="/adm/feedback" method="post" name="mailform"
 enctype="multipart/form-data">
@@ -1602,7 +1601,8 @@ END
     }
     $r->print(&generate_preview_button().
               &Apache::lonhtmlcommon::htmlareaselectactive('comment').
-              '</body></html>');
+	      &Apache::loncommon::end_page());
+
 }
 
 sub print_display_options {
@@ -1978,11 +1978,7 @@ sub print_showposters {
                           $env{'course.'.$env{'request.course.id'}.'.num'});
     my %namesort = ();
     my %postcounts = ();
-    my %lt=&Apache::lonlocal::texthash(
-                     'diso' => 'Discussion filtering options',
-    );
-    my $bodytag=&Apache::loncommon::bodytag('Discussion options',
-                                          '','');
+
     if ($contrib{'version'}) {
         for (my $idx=1;$idx<=$contrib{'version'};$idx++) {
             my $hidden=($contrib{'hidden'}=~/\.$idx\./);
@@ -2014,14 +2010,11 @@ sub print_showposters {
             }
         }
     }
-    my $html=&Apache::lonxml::xmlbegin();
+
+    my $start_page = &Apache::loncommon::start_page('Discussion options');
+
     $r->print(<<END);
-$html
-<head>
-<title>$lt{'diso'}</title>
-<meta http-equiv="pragma" content="no-cache" />
-</head>
-$bodytag
+$start_page
  <form name="pickpostersform" method="post">
   <table border="0">
    <tr>
@@ -2048,6 +2041,8 @@ END
             }
         }
     }
+
+    my $end_page   = &Apache::loncommon::end_page();
     $r->print(<<END);
      </table>
     </td>
@@ -2058,8 +2053,7 @@ END
 <input type="hidden" name="userpick" value="$symb" />
 <input type="button" name="store" value="Display posts" onClick="javascript:document.pickpostersform.submit()" />
 </form>
-</body>
-</html>
+$end_page
 END
 }
 
@@ -2067,7 +2061,8 @@ sub get_post_versions {
     my ($versions,$incoming,$htmldecode,$numver) = @_;
     if ($incoming =~ /^<version num="0">/) {
         my $p = HTML::LCParser->new(\$incoming);
-        my $done = 0;                                                                       
+        my $done = 0; 
+
         while ( (my $token = $p->get_tag("version")) && (!$done)) {
             my $num = $token->[1]{num};
             my $text = $p->get_text("/version");
@@ -2128,21 +2123,15 @@ sub fail_redirect {
   my ($r,$feedurl) = @_;
   if ($feedurl=~/^\/adm\//) { $feedurl.='?register=1' };
   my $logo=&Apache::loncommon::lonhttpdurl('/adm/lonIcons/lonlogos.gif');
-  my $html=&Apache::lonxml::xmlbegin();
-  $r->print (<<ENDFAILREDIR);
-$html
-<head>
-<title>Feedback not sent</title>
-<meta http-equiv="pragma" content="no-cache" />
-<meta HTTP-EQUIV="Refresh" CONTENT="2; url=$feedurl" />
-</head>
-<body bgcolor="#FFFFFF">
+  $r->print(&Apache::loncommon::start_page('Feedback not sent',undef,
+					   {'redirect'  => [2,$feedurl],
+					    'only_body' => 1,}));
+  $r->print(<<ENDFAILREDIR);
 <img align="right" src="$logo" />
 <b>Sorry, no recipients  ...</b>
 <br /><a href="$feedurl">Continue</a>
-</body>
-</html>
 ENDFAILREDIR
+  $r->print(&Apache::loncommon::end_page());
 }
 
 sub redirect_back {
@@ -2216,15 +2205,16 @@ sub redirect_back {
   }
   $feedurl=&Apache::lonenc::check_encrypt($feedurl);
   my $logo=&Apache::loncommon::lonhttpdurl('/adm/lonIcons/lonlogos.gif');
-  my $html=&Apache::lonxml::xmlbegin();
-  $r->print (<<ENDREDIR);
-$html
-<head>
-<title>Feedback sent</title>
-<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(); }'>
+  my $onload = 'onLoad=\'if (window.name!="loncapaclient") { this.document.reldt.submit(); self.window.close(); }\'';
+  my $start_page=
+      &Apache::loncommon::start_page('New posts marked as read',undef,
+				     {'redirect'    => [2,$feedurl],
+				      'only_body'   => 1,
+				      'add_entries' => $onload});
+  my $end_page = &Apache::loncommon::end_page();
+
+  $r->print(<<ENDREDIR);
+$start_page
 <img align="right" src="$logo" />
 $typestyle
 <b>Sent $sendsomething message(s), and $sendposts post(s).</b>
@@ -2239,8 +2229,7 @@ $sectag
 $userpicktag
 </form>
 <br /><a href="$feedurl">Continue</a>
-</body>
-</html>
+$end_page
 ENDREDIR
 }
 
@@ -2264,7 +2253,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>
@@ -2314,8 +2303,8 @@ sub screen_header {
 		' <i>('.&mt('name only visible to course faculty').')</i></label> '.
 		'<a href="/adm/preferences?action=changescreenname">'.&mt('Change Screenname').'</a>';
         }
-        #$discussoptions.='<br /><label><input type="checkbox" name="blog" /> '.
-	#    &mt('Add to my public course blog').'</label>';
+        $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) { 
@@ -2478,14 +2467,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 +2530,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 +2572,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 +2978,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';
@@ -3120,22 +3114,21 @@ END
       &Apache::loncommon::content_type($r,'text/html');
       $r->send_http_header;
       my $logo=&Apache::loncommon::lonhttpdurl('/adm/lonIcons/lonlogos.gif');
-      my $html=&Apache::lonxml::xmlbegin();
+      my $onload = 'onLoad=\'if (window.name!="loncapaclient") { this.document.reldt.submit(); self.window.close(); }\'';
+      my $start_page=
+	  &Apache::loncommon::start_page('New posts marked as read',undef,
+					 {'redirect'    => [2,$feedurl],
+					  'only_body'   => 1,
+					  'add_entries' => $onload});
+      my $end_page = &Apache::loncommon::end_page();
       $r->print (<<ENDREDIR);
-$html
-<head>
-<title>New posts marked as read</title>
-<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(); }'>
+$start_page
 <img align="right" src="$logo" />
 $textline
 <form name="reldt" action="$feedurl" target="loncapaclient">
 </form>
 <br /><a href="$feedurl">Continue</a>
-</body>
-</html>
+$end_page
 ENDREDIR
       return OK;
   } elsif ($env{'form.modifydisp'}) {
@@ -3187,6 +3180,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 +3199,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) {
@@ -3411,7 +3409,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.
@@ -3477,5 +3475,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__