File:  [LON-CAPA] / loncom / interface / lonfeedback.pm
Revision 1.114: download - view: text, annotated - select for diffs
Tue Aug 10 18:01:25 2004 UTC (19 years, 10 months ago) by sakharuk
Branches: MAIN
CVS tags: HEAD
Bug 2838 (print attached discussions too) is fixed (finally). I am open to change any info (which can be relevant or not) but the general scheme seems works.

    1: # The LearningOnline Network
    2: # Feedback
    3: #
    4: # $Id: lonfeedback.pm,v 1.114 2004/08/10 18:01:25 sakharuk Exp $
    5: #
    6: # Copyright Michigan State University Board of Trustees
    7: #
    8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    9: #
   10: # LON-CAPA is free software; you can redistribute it and/or modify
   11: # it under the terms of the GNU General Public License as published by
   12: # the Free Software Foundation; either version 2 of the License, or
   13: # (at your option) any later version.
   14: #
   15: # LON-CAPA is distributed in the hope that it will be useful,
   16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   18: # GNU General Public License for more details.
   19: #
   20: # You should have received a copy of the GNU General Public License
   21: # along with LON-CAPA; if not, write to the Free Software
   22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   23: #
   24: # /home/httpd/html/adm/gpl.txt
   25: #
   26: # http://www.lon-capa.org/
   27: #
   28: ###
   29: 
   30: package Apache::lonfeedback;
   31: 
   32: use strict;
   33: use Apache::Constants qw(:common);
   34: use Apache::lonmsg();
   35: use Apache::loncommon();
   36: use Apache::lontexconvert();
   37: use Apache::lonlocal; # must not have ()
   38: use Apache::lonhtmlcommon();
   39: use HTML::LCParser();
   40: use Apache::lonspeller();
   41: 
   42: sub discussion_open {
   43:     my ($status)=@_;
   44:     if (defined($status) &&
   45: 	!($status eq 'CAN_ANSWER' || $status eq 'CANNOT_ANSWER'
   46: 	  || $status eq 'OPEN')) {
   47: 	return 0;
   48:     }
   49:     my $close=&Apache::lonnet::EXT('resource.0.discussend');
   50:     if (defined($close) && $close ne '' && $close < time) {
   51: 	return 0;
   52:     }
   53:     return 1;
   54: }
   55: 
   56: sub discussion_visible {
   57:     my ($status)=@_;
   58:     if (not &discussion_open($status)) {
   59: 	my $hidden=&Apache::lonnet::EXT('resource.0.discusshide');
   60: 	if (lc($hidden) eq 'yes' or $hidden eq '' or !defined($hidden))  {
   61: 	    return 0;
   62: 	}
   63:     }
   64:     return 1;
   65: }
   66: 
   67: sub list_discussion {
   68:     my ($mode,$status,$symb,$ttarget,$tagstack,$parstack,$parser,$safeeval)=@_;
   69:     my $outputtarget=$ENV{'form.grade_target'};
   70:     if (not &discussion_visible($status)) { return ''; }
   71:     my @bgcols = ("#cccccc","#eeeeee");
   72:     my $discussiononly=0;
   73:     if ($mode eq 'board') { $discussiononly=1; }
   74:     unless ($ENV{'request.course.id'}) { return ''; }
   75:     my $crs='/'.$ENV{'request.course.id'};
   76:     my $cid=$ENV{'request.course.id'};
   77:     if ($ENV{'request.course.sec'}) {
   78: 	$crs.='_'.$ENV{'request.course.sec'};
   79:     }                 
   80:     $crs=~s/\_/\//g;
   81:     unless ($symb) {
   82: 	$symb=&Apache::lonnet::symbread();
   83:     }
   84:     unless ($symb) { return ''; }
   85:     my %usernamesort = ();
   86:     my %namesort =();
   87:     my %subjectsort = ();
   88: # backward compatibility (bulletin boards used to be 'wrapped')
   89:     my $ressymb=$symb;
   90:     if ($mode eq 'board') {
   91:         unless ($ressymb =~ m|bulletin___\d+___adm/wrapper|) {
   92:             $ressymb=~s|(bulletin___\d+___)|$1adm/wrapper|;
   93:         }
   94:     }
   95: 
   96: # Get discussion display settings for this discussion
   97:     my $lastkey = $ressymb.'_lastread';
   98:     my $showkey = $ressymb.'_showonlyunread';
   99:     my $markkey = $ressymb.'_showonlyunmark',
  100:     my $visitkey = $ressymb.'_visit';
  101:     my $ondispkey = $ressymb.'_markondisp';
  102:     my $userpickkey = $ressymb.'_userpick';
  103:     my $toggkey = $ressymb.'_readtoggle';
  104:     my $readkey = $ressymb.'_read';
  105: 
  106:     my %dischash = &Apache::lonnet::get('nohist_'.$ENV{'request.course.id'}.'_discuss',[$lastkey,$showkey,$markkey,$visitkey,$ondispkey,$userpickkey,$toggkey,$readkey],$ENV{'user.domain'},$ENV{'user.name'});
  107:     my %discinfo = ();
  108:     my $showonlyunread = 0;
  109:     my $showunmark = 0; 
  110:     my $markondisp = 0;
  111:     my $prevread = 0;
  112:     my $previous = 0;
  113:     my $visit = 0;
  114:     my $newpostsflag = 0;
  115:     my @posters = split/\&/,$dischash{$userpickkey};
  116: 
  117: # Retain identification of "NEW" posts identified in last display, if continuing 'previous' browsing of posts.
  118:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},['previous','sortposts','rolefilter','statusfilter','sectionpick','totposters']);
  119:     my $sortposts = $ENV{'form.sortposts'};
  120:     my $rolefilter = $ENV{'form.rolefilter'};
  121:     my $statusfilter = $ENV{'form.statusfilter'};
  122:     my $sectionpick = $ENV{'form.sectionpick'};
  123:     my $totposters = $ENV{'form.totposters'};
  124:     $previous = $ENV{'form.previous'};
  125:     if ($previous > 0) {
  126:         $prevread = $previous;
  127:     } elsif (defined($dischash{$lastkey})) {
  128:         unless ($dischash{$lastkey} eq '') {
  129:             $prevread = $dischash{$lastkey};
  130:         }
  131:     }
  132: 
  133: # Get information about students and non-students in course for filtering display of posts
  134:     my %roleshash = ();
  135:     my %roleinfo = ();
  136:     if ($rolefilter) {
  137:         %roleshash = &Apache::lonnet::dump('nohist_userroles',$ENV{'course.'.$ENV{'request.course.id'}.'.domain'},$ENV{'course.'.$ENV{'request.course.id'}.'.num'});
  138:         foreach (keys %roleshash) {
  139:             my ($role,$uname,$udom,$sec) = split/:/,$_;
  140:             my ($end,$start) = split/:/,$roleshash{$_};
  141:             my $now = time;
  142:             my $status = 'Active';
  143:             if (($now < $start) || ($end > 0 && $now > $end)) {
  144:                 $status = 'Expired';
  145:             }
  146:             push @{$roleinfo{$uname.':'.$udom}}, $role.':'.$sec.':'.$status;
  147:         }
  148:         my ($classlist) = &Apache::loncoursedata::get_classlist(
  149:                               $ENV{'request.course.id'},
  150:                               $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
  151:                               $ENV{'course.'.$ENV{'request.course.id'}.'.num'});
  152:         my $sec_index = &Apache::loncoursedata::CL_SECTION();
  153:         my $status_index = &Apache::loncoursedata::CL_STATUS();
  154:         while (my ($student,$data) = each %$classlist) {
  155:             my ($section,$status) = ($data->[$sec_index],
  156:                                  $data->[$status_index]);
  157:             push @{$roleinfo{$student}}, 'st:'.$section.':'.$status;
  158:         }
  159:     }
  160: 
  161: # Get discussion display default settings for user
  162:     my %userenv = &Apache::lonnet::get('environment',['discdisplay','discmarkread'],$ENV{'user.domain'},$ENV{'user.name'});
  163:     my $discdisplay=$userenv{'discdisplay'};
  164:     if ($discdisplay eq 'unread') {
  165:         $showonlyunread = 1;
  166:     }
  167:     my $discmarkread=$userenv{'discmarkread'};
  168:     if ($discmarkread eq 'ondisp') {
  169:         $markondisp = 1;
  170:     }
  171: 
  172: # Override user's default if user specified display setting for this discussion
  173:     if (defined($dischash{$ondispkey})) {
  174:         $markondisp = $dischash{$ondispkey};
  175:     }
  176:     if ($markondisp) {
  177:         $discinfo{$lastkey} = time;
  178:     }
  179: 
  180:     if (defined($dischash{$showkey})) {
  181:         $showonlyunread = $dischash{$showkey};
  182:     }
  183: 
  184:     if (defined($dischash{$markkey})) {
  185:         $showunmark = $dischash{$markkey};
  186:     }
  187: 
  188:     if (defined($dischash{$visitkey})) {
  189:         $visit = $dischash{$visitkey};
  190:     }
  191:     $visit ++;
  192: 
  193:     my $seeid=&Apache::lonnet::allowed('rin',$crs);
  194:     my $viewgrades=(&Apache::lonnet::allowed('vgr',$crs)
  195: 	&& ($symb=~/\.(problem|exam|quiz|assess|survey|form)$/));
  196:     my @discussionitems=();
  197:     my %shown = ();
  198:     my @posteridentity=();
  199:     my %contrib=&Apache::lonnet::restore($ressymb,$ENV{'request.course.id'},
  200: 			  $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
  201: 			  $ENV{'course.'.$ENV{'request.course.id'}.'.num'});
  202:     my $visible=0;
  203:     my @depth=();
  204:     my @original=();
  205:     my @index=();
  206:     my @replies=();
  207:     my %alldiscussion=();
  208:     my %notshown = ();
  209:     my %newitem = ();
  210:     my $maxdepth=0;
  211: 
  212:     my $target='';
  213:     unless ($ENV{'browser.interface'} eq 'textual' ||
  214: 	    $ENV{'environment.remote'} eq 'off' ) {
  215: 	$target='target="LONcom"';
  216:     }
  217: 
  218:     my $now = time;
  219:     $discinfo{$visitkey} = $visit;
  220: 
  221:     &Apache::lonnet::put('nohist_'.$ENV{'request.course.id'}.'_discuss',\%discinfo,$ENV{'user.domain'},$ENV{'user.name'});
  222: 
  223:     if ($contrib{'version'}) {
  224:         my $oldest = $contrib{'1:timestamp'};
  225:         if ($prevread eq '0') {
  226:             $prevread = $oldest-1;
  227:         }
  228: 	for (my $id=1;$id<=$contrib{'version'};$id++) {
  229: 	    my $idx=$id;
  230:             my $posttime = $contrib{$idx.':timestamp'};
  231:             if ($prevread <= $posttime) {
  232:                 $newpostsflag = 1;
  233:             }
  234: 	    my $hidden=($contrib{'hidden'}=~/\.$idx\./);
  235:             my $studenthidden=($contrib{'studenthidden'}=~/\.$idx\./);
  236: 	    my $deleted=($contrib{'deleted'}=~/\.$idx\./);
  237: 	    my $origindex='0.';
  238:             my $numoldver=0;
  239: 	    if ($contrib{$idx.':replyto'}) {
  240:                 if ( (($ENV{'environment.threadeddiscussion'}) && (($sortposts eq '') || ($sortposts eq 'ascdate'))) || ($sortposts eq 'thread')) {
  241: # this is a follow-up message
  242: 		    $original[$idx]=$original[$contrib{$idx.':replyto'}];
  243: 		    $depth[$idx]=$depth[$contrib{$idx.':replyto'}]+1;
  244: 		    $origindex=$index[$contrib{$idx.':replyto'}];
  245: 		    if ($depth[$idx]>$maxdepth) { $maxdepth=$depth[$idx]; }
  246:                 } else {
  247:                     $original[$idx]=0;
  248:                     $depth[$idx]=0;
  249:                 }
  250: 	    } else {
  251: # this is an original message
  252: 		$original[$idx]=0;
  253: 		$depth[$idx]=0;
  254: 	    }
  255: 	    if ($replies[$depth[$idx]]) {
  256: 		$replies[$depth[$idx]]++;
  257: 	    } else {
  258: 		$replies[$depth[$idx]]=1;
  259: 	    }
  260: 	    unless ((($hidden) && (!$seeid)) || ($deleted)) {
  261: 		$visible++;
  262:                 if ($contrib{$idx.':history'}) {
  263:                     if ($contrib{$idx.':history'} =~ /:/) {
  264:                         my @oldversions = split/:/,$contrib{$idx.':history'};
  265:                         $numoldver = @oldversions;
  266:                     } else {
  267:                         $numoldver = 1;
  268:                     } 
  269:                 }
  270: 		my ($message,$subject);
  271:          	if ($idx > 0) {
  272:                     if ($contrib{$idx.':message'} =~ /^<version num="0">/) {
  273:                         my %versions = (); 
  274:                         &get_post_versions(\%versions,$contrib{$idx.':message'},$numoldver);
  275:                         $message = &HTML::Entities::decode($versions{$numoldver});
  276:                     } else {
  277:                         $message = $contrib{$idx.':message'};
  278:                     }
  279:                 } else {
  280:                     $message=$contrib{$idx.':message'};
  281:                 }
  282:                 my $attachmenturls = $contrib{$idx.':attachmenturl'}; 
  283: 		$message=~s/\n/\<br \/\>/g;
  284: 		$message=&Apache::lontexconvert::msgtexconverted($message);
  285:          	if ($idx > 0) {
  286:                     if ($contrib{$idx.':subject'} =~ /^<version num="0"/) {
  287:                         my %versions = ();
  288:                         &get_post_versions(\%versions,$contrib{$idx.':subject'},$numoldver);
  289:                         $subject = &HTML::Entities::decode($versions{$numoldver});
  290:                     } else {
  291:                         $subject = $contrib{$idx.':subject'};
  292:                     }
  293:                 } else {
  294:                     $subject=$contrib{$idx.':subject'};
  295:                 }
  296:                 if (defined($subject)) {
  297:                     $subject=~s/\n/\<br \/\>/g;
  298:                     $subject=&Apache::lontexconvert::msgtexconverted($subject);
  299:                 }
  300: 		if ($attachmenturls) {
  301:                     my %attachments = ();
  302:                     my %currattach = ();
  303:                     &extract_attachments($attachmenturls,$idx,$numoldver,\$message,\%attachments,\%currattach);
  304: 		}
  305: 		if ($message) {
  306: 		    if ($hidden) {
  307: 			$message='<font color="#888888">'.$message.'</font>';
  308:                         if ($studenthidden) {
  309:                             $message .='<br /><br />Deleted by poster (student).';
  310:                         }
  311: 		    }
  312: 		    my $screenname=&Apache::loncommon::screenname(
  313: 					    $contrib{$idx.':sendername'},
  314: 					    $contrib{$idx.':senderdomain'});
  315: 		    my $plainname=&Apache::loncommon::nickname(
  316: 					    $contrib{$idx.':sendername'},
  317: 					    $contrib{$idx.':senderdomain'});
  318: 		    
  319: 		    my $sender=&mt('Anonymous');
  320: # Set up for sorting by subject
  321:                     if ($contrib{$idx.':subject'} eq '') {
  322:                         if (defined($subjectsort{'__No subject'})) {
  323:                             push @{$subjectsort{'__No subject'}}, $idx;
  324:                         } else {
  325:                             @{$subjectsort{'__No subject'}} = ("$idx");
  326:                         }
  327:                     } else {
  328:                         if (defined($subjectsort{$contrib{$idx.':subject'}})) {
  329:                             push @{$subjectsort{$contrib{$idx.':subject'}}}, $idx;
  330:                         } else {
  331:                             @{$subjectsort{$contrib{$idx.':subject'}}} = ("$idx");
  332:                         }
  333:                     }
  334: 		    if ((!$contrib{$idx.':anonymous'}) || ($seeid)) {
  335: 			$sender=&Apache::loncommon::aboutmewrapper(
  336: 					 $plainname,
  337: 					 $contrib{$idx.':sendername'},
  338: 					 $contrib{$idx.':senderdomain'}).' ('.
  339: 					 $contrib{$idx.':sendername'}.' at '.
  340: 					 $contrib{$idx.':senderdomain'}.')';
  341: 			if ($contrib{$idx.':anonymous'}) {
  342: 			    $sender.=' ['.&mt('anonymous').'] '.
  343: 				$screenname;
  344: 			}
  345: # Set up for sorting by domain, then username
  346:                         unless (defined($usernamesort{$contrib{$idx.':senderdomain'}})) {
  347:                             %{$usernamesort{$contrib{$idx.':senderdomain'}}} = ();
  348:                         }
  349:                         if (defined($usernamesort{$contrib{$idx.':senderdomain'}}{$contrib{$idx.':sendername'}})) {
  350:                             push @{$usernamesort{$contrib{$idx.':senderdomain'}}{$contrib{$idx.':sendername'}}}, $idx;
  351:                         } else {
  352:                             @{$usernamesort{$contrib{$idx.':senderdomain'}}{$contrib{$idx.':sendername'}}} = ("$idx");
  353:                         }
  354: # Set up for sorting by last name, then first name
  355:                         my %names = &Apache::lonnet::get('environment',['firstname','lastname'],
  356:                                   $contrib{$idx.':senderdomain'},$contrib{$idx.':sendername'});
  357:                         my $lastname = $names{'lastname'};
  358:                         my $firstname = $names{'firstname'};
  359:                         if ($lastname eq '') {
  360:                             $lastname = '_';
  361:                         }
  362:                         if ($firstname eq '') {
  363:                             $firstname = '_';
  364:                         }
  365:                         unless (defined($namesort{$lastname})) {
  366:                             %{$namesort{$lastname}} = ();
  367:                         }
  368:                         if (defined($namesort{$lastname}{$firstname})) {
  369:                             push @{$namesort{$lastname}{$firstname}}, $idx;
  370:                         } else {
  371:                             @{$namesort{$lastname}{$firstname}} = ("$idx");
  372:                         }
  373:                         if ($ENV{"course.$cid.allow_discussion_post_editing"} =~ m/yes/i) {
  374:                             if (($ENV{'user.domain'} eq $contrib{$idx.':senderdomain'}) && ($ENV{'user.name'} eq $contrib{$idx.':sendername'})) {
  375:                                 $sender.=' <a href="/adm/feedback?editdisc='.
  376:                                     $ressymb.':::'.$idx;
  377:                                 if ($newpostsflag) {
  378:                                     $sender .= '&previous='.$prevread;
  379:                                 }
  380:                                 $sender .= '" '.$target.'>'.&mt('Edit').'</a>';                                      unless ($seeid) {
  381:                                     $sender.=" <a href=\"javascript:studentdelete('$ressymb','$idx','$newpostsflag','$prevread')";
  382:                                     $sender .= '">'.&mt('Delete').'</a>';
  383:                                 }
  384:                             }
  385:                         }
  386: 			if ($seeid) {
  387: 			    if ($hidden) {
  388:                                 unless ($studenthidden) {
  389: 				    $sender.=' <a href="/adm/feedback?unhide='.
  390: 				        $ressymb.':::'.$idx;
  391:                                     if ($newpostsflag) {
  392:                                         $sender .= '&previous='.$prevread;
  393:                                     }
  394:                                     $sender .= '">'.&mt('Make Visible').'</a>';
  395:                                 }
  396: 			    } else {
  397: 				$sender.=' <a href="/adm/feedback?hide='.
  398: 				    $ressymb.':::'.$idx;
  399:                                 if ($newpostsflag) {
  400:                                     $sender .= '&previous='.$prevread;
  401:                                 }
  402:                                 $sender .= '">'.&mt('Hide').'</a>';
  403: 			    }                     
  404: 			    $sender.=' <a href="/adm/feedback?deldisc='.
  405: 				    $ressymb.':::'.$idx;
  406:                             if ($newpostsflag) {
  407:                                 $sender .= '&previous='.$prevread;
  408:                             }
  409:                             $sender .= '">'.&mt('Delete').'</a>';
  410: 			}
  411: 		    } else {
  412: 			if ($screenname) {
  413: 			    $sender='<i>'.$screenname.'</i>';
  414: 			}
  415: # Set up for sorting by domain, then username for anonymous
  416:                         unless (defined($usernamesort{'__anon'})) {
  417:                             %{$usernamesort{'__anon'}} = ();
  418:                         }
  419:                         if (defined($usernamesort{'__anon'}{'__anon'})) {
  420:                             push @{$usernamesort{'__anon'}{'__anon'}}, $idx;
  421:                         } else {
  422:                             @{$usernamesort{'__anon'}{'__anon'}} = ("$idx");
  423:                         }
  424: # Set up for sorting by last name, then first name for anonymous
  425:                         unless (defined($namesort{'__anon'})) {
  426:                             %{$namesort{'__anon'}} = ();
  427:                         }
  428:                         if (defined($namesort{'__anon'}{'__anon'})) {
  429:                             push @{$namesort{'__anon'}{'__anon'}}, $idx;
  430:                         } else {
  431:                             @{$namesort{'__anon'}{'__anon'}} = ("$idx");
  432:                         }
  433: 		    }
  434: 		    if (&discussion_open($status) &&
  435: 			&Apache::lonnet::allowed('pch',
  436: 						 $ENV{'request.course.id'}.
  437: 						 ($ENV{'request.course.sec'}?'/'.$ENV{'request.course.sec'}:''))) {
  438: 			$sender.=' <a href="/adm/feedback?replydisc='.
  439: 			    $ressymb.':::'.$idx;
  440:                         if ($newpostsflag) {
  441:                             $sender .= '&previous='.$prevread;
  442:                         }
  443:                         $sender .= '" '.$target.'>'.&mt('Reply').'</a>';
  444: 		    }
  445: 		    my $vgrlink;
  446: 		    if ($viewgrades) {
  447: 			$vgrlink=&Apache::loncommon::submlink('Submissions',
  448:             $contrib{$idx.':sendername'},$contrib{$idx.':senderdomain'},$symb);
  449: 		    }
  450:                     my $ctlink;
  451:                     if ($dischash{$readkey}=~/\.$idx\./) { 
  452:                         $ctlink = '<b>'.&mt('Mark unread').'?</b>&nbsp;<input type="checkbox" name="postunread_'.$idx.'" />';
  453:                     } else {
  454:                         $ctlink = '<b>'.&mt('Mark read').'?</b>&nbsp;<input type="checkbox" name="postread_'.$idx.'" />';
  455:                     }
  456: #figure out at what position this needs to print
  457: 		    my $thisindex=$idx;
  458: 		    if ( (($ENV{'environment.threadeddiscussion'}) && (($sortposts eq '') || ($sortposts eq 'ascdate'))) || ($sortposts eq 'thread')) {
  459: 			$thisindex=$origindex.substr('00'.$replies[$depth[$idx]],-2,2);
  460: 		    }
  461: 		    $alldiscussion{$thisindex}=$idx;
  462:                     $shown{$idx} = 0;
  463:                     $index[$idx]=$thisindex;
  464:                     my $spansize = 2;
  465:                     if ($showonlyunread && $prevread > $posttime) {
  466:                         $notshown{$idx} = 1;
  467:                     } elsif ($showunmark && $dischash{$readkey}=~/\.$idx\./) {
  468:                         $notshown{$idx} = 1;
  469:                     } else {
  470: # apply filters
  471:                         my $uname = $contrib{$idx.':sendername'};
  472:                         my $udom = $contrib{$idx.':senderdomain'};
  473:                         my $poster = $uname.':'.$udom;
  474:                         my $rolematch = '';
  475:                         my $skiptest = 1;
  476:                         if ($totposters > 0) {
  477:                             if (grep/^$poster$/,@posters) {
  478:                                 $shown{$idx} = 1;
  479:                             }
  480:                         } else {
  481:                             if ($rolefilter) {
  482:                                 if ($rolefilter eq 'all') {
  483:                                     $rolematch = '([^:]+)';
  484:                                 } else {
  485:                                     $rolematch = $rolefilter;
  486:                                     $skiptest = 0;
  487:                                 }
  488:                             }
  489:                             if ($sectionpick) {
  490:                                 if ($sectionpick eq 'all') {
  491:                                     $rolematch .= ':([^:]*)';
  492:                                 } else {
  493:                                     $rolematch .= ':'.$sectionpick;
  494:                                     $skiptest = 0;
  495:                                 }
  496:                             }
  497:                             if ($statusfilter) {
  498:                                 if ($statusfilter eq 'all') {
  499:                                     $rolematch .= ':([^:]+)';
  500:                                 } else {
  501:                                     $rolematch .= ':'.$statusfilter;
  502:                                     $skiptest = 0;
  503:                                 }
  504:                             }
  505:                             if ($skiptest) {
  506:                                 $shown{$idx} = 1;
  507:                             } else {
  508:                                 foreach my $role (@{$roleinfo{$poster}}) {
  509:                                     if ($role =~ m/^$rolematch$/) {
  510:                                         $shown{$idx} = 1;
  511:                                         last;
  512:                                     }
  513:                                 }
  514:                             }
  515:                         }
  516:                     }
  517:                     unless ($notshown{$idx} == 1) {
  518:                         if ($prevread > 0 && $prevread <= $posttime) {
  519:                             $newitem{$idx} = 1;
  520:                             $discussionitems[$idx] .= '
  521:                              <p><table border="0" width="100%">
  522:                               <tr><td align="left"><font color="#FF0000"><b>NEW</b></font></td>';
  523:                         } else {
  524:                             $newitem{$idx} = 0;
  525:                             $discussionitems[$idx] .= '
  526:                              <p><table border="0" width="100%">
  527:                               <tr><td align="left">&nbsp;</td>';
  528:                         }
  529:                         $discussionitems[$idx] .= '<td align ="left">&nbsp;&nbsp;'.
  530:                             '<b>'.$subject.'</b>&nbsp;&nbsp;'.
  531:                             $sender.'</b> '.$vgrlink.' ('.
  532:                             &Apache::lonlocal::locallocaltime($posttime).')</td>';
  533:                         if ($dischash{$toggkey}) {
  534:                             $discussionitems[$idx].='<td align="right">&nbsp;&nbsp;'.
  535:                               $ctlink.'</td>';
  536:                         }
  537:                         $discussionitems[$idx].= '</tr></table><blockquote>'.$message.'</blockquote></p>';
  538:                         if ($contrib{$idx.':history'}) {
  539:                             my @postversions = ();
  540:                             $discussionitems[$idx] .= '<br />'.&mt('This post has been edited by the author.');
  541:                             if ($seeid) {
  542:                                 $discussionitems[$idx] .= '&nbsp;&nbsp;<a href="/adm/feedback?allversions='.$ressymb.':::'.$idx.'">'.&mt('Display all versions').'</a>';
  543:                             }
  544:                             $discussionitems[$idx].='<br/>'.&mt('Earlier version(s) were posted on: ');
  545:                             if ($contrib{$idx.':history'} =~ m/:/) {
  546:                                 @postversions = split/:/,$contrib{$idx.':history'};
  547:                             } else {
  548:                                 @postversions = ("$contrib{$idx.':history'}");
  549:                             }
  550:                             for (my $i=0; $i<@postversions; $i++) {
  551:                                 my $version = $i+1;
  552:                                 $discussionitems[$idx] .= '<b>'.$version.'.</b> - '.&Apache::lonlocal::locallocaltime($postversions[$i]).'  ';
  553:                             }
  554:                         }
  555:                     }
  556:                 }
  557:             }
  558: 	}
  559:     }
  560: 
  561:     my $discussion='';
  562: 
  563:     my $function = &Apache::loncommon::get_users_function();
  564:     my $color = &Apache::loncommon::designparm($function.'.tabbg',
  565:                                                     $ENV{'user.domain'});
  566:     my %lt = &Apache::lonlocal::texthash(
  567:         'cuse' => 'Current discussion settings',
  568:         'allposts' => 'All posts',
  569:         'unread' => 'New posts only',
  570:         'unmark' => 'Unread only',
  571:         'ondisp' => 'Once displayed',
  572:         'onmark' => 'Once marked not NEW',
  573:         'toggoff' => 'Off',
  574:         'toggon' => 'On',
  575:         'disa' => 'Posts to be displayed',
  576:         'npce' => 'Posts cease to be marked "NEW"',
  577:         'epcb' => 'Each post can be toggled read/unread', 
  578:         'chgt' => 'Change',
  579:         'disp' => 'Display',
  580:         'nolo' => 'Not new',
  581:         'togg' => 'Toggle read/unread',
  582:     );
  583: 
  584:     my $currdisp = $lt{'allposts'};
  585:     my $currmark = $lt{'onmark'};
  586:     my $currtogg = $lt{'toggoff'};
  587:     my $dispchange = $lt{'unread'};
  588:     my $markchange = $lt{'ondisp'};
  589:     my $toggchange = $lt{'toggon'};
  590:     my $chglink = '/adm/feedback?modifydisp='.$ressymb;
  591:     my $displinkA = 'onlyunread';
  592:     my $displinkB = 'onlyunmark';
  593:     my $marklink = 'markondisp';
  594:     my $togglink = 'toggon';
  595: 
  596:     if ($markondisp) {
  597:         $currmark = $lt{'ondisp'};
  598:         $markchange = $lt{'onmark'};
  599:         $marklink = 'markonread';
  600:     }
  601: 
  602:     if ($showonlyunread) {
  603:         $currdisp = $lt{'unread'};
  604:         $dispchange = $lt{'allposts'};
  605:         $displinkA = 'allposts';
  606:     }
  607: 
  608:     if ($showunmark) {
  609:         $currdisp = $lt{'unmark'};
  610:         $dispchange = $lt{'unmark'};
  611:         $displinkA='allposts';
  612:         $displinkB='onlyunread';
  613:         $showonlyunread = 0;
  614:     }
  615: 
  616:     if ($dischash{$toggkey}) {
  617:         $currtogg = $lt{'toggon'};
  618:         $toggchange = $lt{'toggoff'};
  619:         $togglink = 'toggoff';
  620:     } 
  621:    
  622:     $chglink .= '&changes='.$displinkA.'_'.$displinkB.'_'.$marklink.'_'.$togglink;
  623: 
  624:     if ($newpostsflag) {
  625:         $chglink .= '&previous='.$prevread;
  626:     }
  627: 
  628:     if ($visible) {
  629: # Print the discusssion
  630: 	if ($outputtarget ne 'tex') {
  631:             my $colspan=$maxdepth+1;
  632:             $discussion.= qq|
  633: <script>
  634:    function studentdelete (symb,idx,newflag,previous) {
  635:        var symbparm = symb+':::'+idx
  636:        var prevparm = ""
  637:        if (newflag == 1) {
  638:            prevparm = "&previous="+previous
  639:        }
  640:        if (confirm("Are you sure you want to delete this post?\\nDeleted posts will no longer be visible to you and other students,\\nbut will continue to be visible to your instructor")) {
  641:            document.location.href = "/adm/feedback?hide="+symbparm+prevparm
  642:        }  
  643:    }
  644: </script>
  645:             |;
  646: 	    $discussion.='<form name="readchoices" method="post" action="/adm/feedback?chgreads='.$symb.'"><table bgcolor="#AAAAAA" cellpadding="2" cellspacing="2" border="0">';
  647: 	    $discussion .='<tr><td bgcolor="#DDDDBB" colspan="'.$colspan.'">'.
  648: 		'<table border="0" width="100%" bgcolor="#DDDDBB"><tr>';
  649: 	    if ($visible>2) {
  650: 		$discussion.='<td align="left">'.
  651: 		    '<a href="/adm/feedback?threadedon='.$ressymb;
  652: 		if ($newpostsflag) {
  653: 		    $discussion .= '&previous='.$prevread;
  654: 		}
  655: 		$discussion .='">'.&mt('Threaded View').'</a>&nbsp;&nbsp;'.
  656: 		    '<a href="/adm/feedback?threadedoff='.$ressymb;
  657: 		if ($newpostsflag) {
  658: 		    $discussion .= '&previous='.$prevread;
  659: 		}
  660: 		$discussion .='">'.&mt('Chronological View').'</a>&nbsp;&nbsp;
  661:                               <a href= "/adm/feedback?sortfilter='.$ressymb;
  662:                 if ($newpostsflag) {
  663:                     $discussion .= '&previous='.$prevread;
  664:                 }
  665:                 $discussion .='">'.&mt('Sorting/Filtering options').'</a>&nbsp;&nbsp';
  666:             } else {
  667:                 $discussion .= '<td align="left">';
  668:             }
  669:             $discussion .='<a href= "/adm/feedback?export='.$ressymb;
  670:             if ($newpostsflag) {
  671:                 $discussion .= '&previous='.$prevread;
  672:             }
  673:             $discussion .= '">'.&mt('Export').'?</a>&nbsp;&nbsp;</td>';
  674: 	    if ($newpostsflag) {
  675: 		if (!$markondisp) {
  676: 		    $discussion .='<td align="right"><a href="/adm/feedback?markread='.$ressymb.'">'.&mt('Mark NEW posts no longer new').'</a>&nbsp;&nbsp;';
  677: 		} else {
  678: 		    $discussion .= '<td>&nbsp;</td>';
  679: 		}
  680: 	    } else {
  681: 		$discussion .= '<td>&nbsp;</td>';
  682: 	    }
  683: 	    $discussion .= '</tr></table></td></tr>';
  684: 	} else {
  685: 	    $discussion.='<tex>\vskip 0 mm\noindent\makebox[2 cm][b]{\hrulefill}'.
  686:                          '\textbf{DISCUSSIONS}\makebox[2 cm][b]{\hrulefill}'.
  687:                          '\vskip 0 mm\noindent\textbf{'.$lt{'cuse'}.'}:\vskip 0 mm'.
  688:                          '\noindent\textbf{'.$lt{'disa'}.'}: \textit{'.$currdisp.'}\vskip 0 mm'.
  689:                          '\noindent\textbf{'.$lt{'npce'}.'}: \textit{'.$currmark.'}</tex>';
  690: 	}
  691:         my $numhidden = keys %notshown;
  692:         if ($numhidden > 0) {
  693:             my $colspan = $maxdepth+1;
  694:             $discussion.="\n".'<tr><td bgcolor="#CCCCCC" colspan="'.$colspan.'">'.
  695:                          '<a href="/adm/feedback?allposts='.$ressymb;
  696:             if ($newpostsflag) {
  697:                 $discussion .= '&previous='.$prevread;
  698:             }
  699:             $discussion .= '">'.&mt('Show all posts').'</a> '.&mt('to display').' '.
  700:                          $numhidden.' ';
  701:             if ($showunmark) {
  702:                 $discussion .= &mt('posts previously marked read');
  703:             } else {
  704:                 $discussion .= &mt('previously viewed posts');
  705:             }
  706:             $discussion .= '<br/></td></tr>';
  707:         }
  708: 
  709: # Choose sort mechanism
  710:         my @showposts = ();
  711:         if ($sortposts eq 'descdate') {
  712:             @showposts = (sort { $b <=> $a } keys %alldiscussion);
  713:         } elsif ($sortposts eq 'thread') {
  714:             @showposts = (sort { $a <=> $b } keys %alldiscussion);
  715:         } elsif ($sortposts eq 'subject') {
  716:             foreach (sort keys %subjectsort) {
  717:                 push @showposts, @{$subjectsort{$_}};
  718:             }
  719:         } elsif ($sortposts eq 'username') {
  720:             foreach my $domain (sort keys %usernamesort) {
  721:                 foreach (sort keys %{$usernamesort{$domain}}) {
  722:                     push @showposts, @{$usernamesort{$domain}{$_}};
  723:                 }
  724:             }
  725:         } elsif ($sortposts eq 'lastfirst') {
  726:             foreach my $last (sort keys %namesort) {
  727:                  foreach (sort keys %{$namesort{$last}}) {
  728:                      push @showposts, @{$namesort{$last}{$_}};
  729:                  }
  730:             }
  731:         } else {
  732:             $sortposts = 'ascdate';
  733:             @showposts =  (sort { $a <=> $b } keys %alldiscussion);
  734:         }
  735:         foreach (@showposts) {
  736:             unless (($sortposts eq 'thread') || ($sortposts eq 'ascdate' && $ENV{'environment.threadeddiscussion'})) {
  737:                 $alldiscussion{$_} = $_;
  738:             }
  739:             unless ( ($notshown{$alldiscussion{$_}} eq '1') || ($shown{$alldiscussion{$_}} == 0) ) {
  740:                 if ($outputtarget ne 'tex') {
  741: 		    $discussion.="\n<tr>";
  742: 		}
  743: 	        my $thisdepth=$depth[$alldiscussion{$_}];
  744:                 if ($outputtarget ne 'tex') {
  745: 		    for (1..$thisdepth) {
  746: 			$discussion.='<td>&nbsp;&nbsp;&nbsp;</td>';
  747: 		    }
  748: 		}
  749: 	        my $colspan=$maxdepth-$thisdepth+1;
  750:                 if ($outputtarget ne 'tex') {
  751: 		    $discussion.='<td  bgcolor="'.$bgcols[$newitem{$alldiscussion{$_}}].'" colspan="'.$colspan.'">'.
  752:                              $discussionitems[$alldiscussion{$_}].
  753: 	                     '</td></tr>';
  754: 		} else {
  755: 		    #cleanup block
  756: 		    $discussionitems[$alldiscussion{$_}]=~s/<table([^>]*)>/<table TeXwidth="90 mm">/;
  757: 		    $discussionitems[$alldiscussion{$_}]=~s/<tr([^>]*)><td([^>]*)>/<tr><td TeXwidth="20 mm" align="left">/;
  758:                     my $threadinsert='';
  759:                     if ($thisdepth > 0) {
  760: 			$threadinsert='<br /><strong>Reply: '.$thisdepth.'</strong>';
  761: 		    }
  762: 		    $discussionitems[$alldiscussion{$_}]=~s/<\/td><td([^>]*)>/$threadinsert<\/td><td TeXwidth="65 mm" align="left">/;
  763: 		    $discussionitems[$alldiscussion{$_}]=~s/<a([^>]+)>(Edit|Hide|Delete|Reply|Submissions)<\/a>//g;
  764:                     $discussionitems[$alldiscussion{$_}]=~s/(<b>|<\/b>|<\/a>|<a([^>]+)>)//g;
  765: 
  766: 		    $discussionitems[$alldiscussion{$_}]='<tex>\vskip 0 mm\noindent\makebox[2 cm][b]{\hrulefill}</tex>'.$discussionitems[$alldiscussion{$_}];
  767: 		    if ($_ eq $showposts[-1]) {
  768: 			$discussionitems[$alldiscussion{$_}].='<tex>\keephidden{ENDOFPROBLEM}\vskip 0.5mm\noindent\makebox[\textwidth/$number_of_columns][b]{\hrulefill}\end{document}</tex>';
  769: 		    }
  770: 		    $discussion.=$discussionitems[$alldiscussion{$_}];
  771: 		}
  772: 	    }
  773:         }
  774: 	if ($outputtarget ne 'tex') {
  775:             my $colspan=$maxdepth+1;
  776:             $discussion .= <<END;
  777:             <tr bgcolor="#FFFFFF">
  778:              <td colspan="$colspan" valign="top">
  779:               <table border="0" bgcolor="#FFFFFF" width="100%" cellspacing="2" cellpadding="2">
  780:                <tr>
  781:                 <td align="left">
  782:                  <table border="0" cellpadding="0" cellspacing="4">
  783:                   <tr>
  784:                    <td>
  785:                     <font size="-1"><b>$lt{'cuse'}</b>:</td>
  786:                    <td>&nbsp;</td>
  787:                    <td><font size="-1">
  788: END
  789:             if ($newpostsflag) {
  790:                 $discussion .= 
  791:                    '1.&nbsp;'.$lt{'disp'}.'&nbsp;-&nbsp;<i>'.$currdisp.'</i>&nbsp;&nbsp;2.&nbsp;'.$lt{'nolo'}.'&nbsp;-&nbsp;<i>'.$currmark.'</i>';
  792:                 if ($dischash{$toggkey}) {
  793:                    $discussion .= '&nbsp;&nbsp;3.&nbsp;'.$lt{'togg'}.'&nbsp;-&nbsp;<i>'.$currtogg.'</i>';
  794:                 }
  795:             } else {
  796:                 if ($dischash{$toggkey}) {
  797:                    $discussion .= '1.&nbsp;'.$lt{'disp'}.'&nbsp;-&nbsp;<i>'.$currdisp.'</i>&nbsp;2.&nbsp;'.$lt{'togg'}.'&nbsp;-&nbsp;<i>'.$currtogg.'</i>';
  798:                 } else {
  799:                     $discussion .=
  800:                          $lt{'disp'}.'&nbsp;-&nbsp;<i>'.$currdisp.'</i>';
  801:                 }
  802:             }
  803:             $discussion .= <<END;
  804:                    </font></td>
  805:                    <td>&nbsp;</td>
  806:                    <td>
  807:                     <font size="-1"><b><a href="$chglink">$lt{'chgt'}</a>?</font></b>
  808:                    </td>
  809:                   </tr>
  810:                  </table>
  811:                 </td>
  812: END
  813:             if ($dischash{$toggkey}) {
  814:                 my $storebutton = &mt('Store read/unread changes');
  815:                 $discussion.='<td align="right">'.
  816:               '<input type="hidden" name="discsymb" value="'.$ressymb.'">'."\n".
  817:               '<input type="button" name="readoptions" value="'.$storebutton.'"'.
  818:               ' onClick="this.form.submit();">'."\n".
  819:               '</td>';
  820:             }
  821:             $discussion .= (<<END);
  822:                </tr>
  823:               </table>
  824:              </td>
  825:             </tr>
  826:            </table>
  827:            <br /><br /></form>
  828: END
  829:         } 
  830:     }
  831:     if ($discussiononly) {
  832:         my $now = time;
  833:         my $attachnum = 0;
  834:         my $newattachmsg = '';
  835:         my @currnewattach = ();
  836:         my @currdelold = ();
  837:         my $comment = '';
  838:         my $subject = '';
  839:         if ($ENV{'form.origpage'}) {
  840:             &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},['addnewattach','deloldattach','delnewattach','timestamp','idx','subject','comment']);
  841:             $subject = &HTML::Entities::encode($ENV{'form.subject'},'<>&"');
  842:             $comment = &HTML::Entities::encode($ENV{'form.comment'},'<>&"');
  843:             my @keepold = ();
  844:             &process_attachments(\@currnewattach,\@currdelold,\@keepold);
  845:             if (@currnewattach > 0) {
  846:                 $attachnum += @currnewattach;
  847:             }
  848:         }
  849: 	$discussion.=(<<ENDDISCUSS);
  850: <form action="/adm/feedback" method="post" name="mailform" enctype="multipart/form-data">
  851: <input type="submit" name="discuss" value="Post Discussion" />
  852: <input type="submit" name="anondiscuss" value="Post Anonymous Discussion" />
  853: <input type="hidden" name="symb" value="$ressymb" />
  854: <input type="hidden" name="sendit" value="true" />
  855: <input type="hidden" name="timestamp" value="$now" />
  856: <br /><a name="newpost"></a>
  857: <font size="1">Note: in anonymous discussion, your name is visible only 
  858: to course faculty</font><br />
  859: <b>Title:</b>&nbsp;<input type="text" name="subject" value="$subject" size="30" /><br /><br />
  860: <textarea name="comment" cols="80" rows="14" wrap="hard">$comment</textarea>
  861: ENDDISCUSS
  862:         if ($ENV{'form.origpage'}) {
  863:             $discussion.='<input type="hidden" name="origpage" value="'.$ENV{'form.origpage'}.'" />'."\n";
  864:             foreach (@currnewattach) {
  865:                 $discussion.='<input type="hidden" name="currnewattach" value="'.$_.'" />'."\n";
  866:             }
  867:         }
  868:         $discussion.="</form>\n";
  869:         if ($outputtarget ne 'tex') {
  870:             $discussion.=&generate_attachments_button('',$attachnum,$ressymb,$now,\@currnewattach,\@currdelold,'',$mode);
  871:             if (@currnewattach > 0) {
  872:                 $newattachmsg .= '<b>New attachments</b><br />';
  873:                 if (@currnewattach > 1) {
  874:                     $newattachmsg .= '<ol>';
  875:                     foreach my $item (@currnewattach) {
  876:                         $item =~ m#.*/([^/]+)$#;
  877:                         $newattachmsg .= '<li><a href="'.$item.'">'.$1.'</a></li>'."\n";
  878:                     }
  879:                     $newattachmsg .= '</ol>'."\n";
  880:                 } else {
  881:                     $currnewattach[0] =~ m#.*/([^/]+)$#;
  882:                     $newattachmsg .= '<a href="'.$currnewattach[0].'">'.$1.'</a><br />'."\n";
  883:                 }
  884:             }
  885:             $discussion.=$newattachmsg;
  886: 	    $discussion.=&generate_preview_button();
  887: 	}
  888:     } else {
  889: 	if (&discussion_open($status) &&
  890: 	    &Apache::lonnet::allowed('pch',
  891: 				   $ENV{'request.course.id'}.
  892: 	($ENV{'request.course.sec'}?'/'.$ENV{'request.course.sec'}:''))) {
  893: 	    if ($outputtarget ne 'tex') {
  894: 		$discussion.='<table bgcolor="#BBBBBB"><tr><td><a href="/adm/feedback?replydisc='.
  895: 		    $symb.':::" '.$target.'>'.
  896: 		    '<img src="/adm/lonMisc/chat.gif" border="0" />'.
  897: 		    &mt('Post Discussion').'</a></td></tr></table>';
  898: 	    }
  899: 	}
  900:     }
  901:     return $discussion;
  902: }
  903: 
  904: sub mail_screen {
  905:   my ($r,$feedurl,$options) = @_;
  906:   if (exists($ENV{'form.origpage'})) {
  907:       &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},['subject','comment','currnewattach','addnewattach','deloldattach','delnewattach','timestamp','idx','anondiscuss','discuss']);
  908:   }
  909:   my $bodytag=&Apache::loncommon::bodytag('Resource Feedback and Discussion',
  910:                                           '','onLoad="window.focus();setposttype();"');
  911:   my $title=&Apache::lonnet::gettitle($feedurl);
  912:   if (!$title) { $title = $feedurl; }
  913:   my $quote='';
  914:   my $subject = '';
  915:   my $comment = '';
  916:   my $prevtag = '';
  917:   my $parentmsg = '';
  918:   my ($symb,$idx,$attachmenturls);
  919:   my $numoldver = 0;
  920:   my $attachmsg = '';
  921:   my $newattachmsg = '';
  922:   my @currnewattach = ();
  923:   my @currdelold = ();
  924:   my @keepold = ();
  925:   my %attachments = ();
  926:   my %currattach = ();
  927:   my $attachnum = 0;
  928:   my $anonchk = (<<END);
  929:   function anonchk() {
  930:      if (document.mailform.anondiscuss.checked == true) {
  931:           document.attachment.anondiscuss.value = '1'
  932:      }
  933:      if (document.mailform.discuss.checked == true) {
  934:           document.attachment.discuss.value = '1'
  935:      }
  936:      return
  937:    }
  938: END
  939:   my $anonscript;
  940:   if (exists($ENV{'form.origpage'})) {
  941:       $anonscript = (<<END);
  942:   function setposttype() {
  943:       var anondisc = $ENV{'form.anondiscuss'};
  944:       var disc = $ENV{'form.discuss'};
  945:       if (anondisc == 1) {
  946:           document.mailform.anondiscuss.checked = true
  947:       }
  948:       if (disc == 1) {
  949:           document.mailform.discuss.checked = true
  950:       }
  951:       return
  952:   }
  953: END
  954:   } else {
  955:       $anonscript = (<<END);
  956:   function setposttype() {
  957:       return
  958:   }
  959: END
  960:   }
  961:   if (($ENV{'form.replydisc'}) || ($ENV{'form.editdisc'})) {
  962:       if ($ENV{'form.replydisc'}) {
  963:           ($symb,$idx)=split(/\:\:\:/,$ENV{'form.replydisc'});
  964:       } else {
  965:           ($symb,$idx)=split(/\:\:\:/,$ENV{'form.editdisc'});
  966:       }
  967:       my %contrib=&Apache::lonnet::restore($symb,$ENV{'request.course.id'},
  968: 					   $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
  969: 					   $ENV{'course.'.$ENV{'request.course.id'}.'.num'});
  970:       unless (($contrib{'hidden'}=~/\.$idx\./) || ($contrib{'deleted'}=~/\.$idx\./)) {
  971:           if ($contrib{$idx.':history'}) {
  972:               if ($contrib{$idx.':history'} =~ /:/) {
  973:                   my @oldversions = split/:/,$contrib{$idx.':history'};
  974:                   $numoldver = @oldversions;
  975:               } else {
  976:                   $numoldver = 1;
  977:               }
  978:           }
  979:           if ($ENV{'form.replydisc'}) {
  980:               if ($contrib{$idx.':history'}) {
  981:                   if ($contrib{$idx.':history'} =~ /:/) {
  982:                       my @oldversions = split/:/,$contrib{$idx.':history'};
  983:                       $numoldver = @oldversions;
  984:                   } else {
  985:                       $numoldver = 1;
  986:                   }
  987:               }
  988:               my $message;
  989:               if ($idx > 0) {
  990:                   if ($contrib{$idx.':message'} =~ /^<version num="0"/) {
  991:                       my %versions = ();
  992:                       &get_post_versions(\%versions,$contrib{$idx.':message'},$numoldver);
  993:                       $message = &HTML::Entities::decode($versions{$numoldver});
  994:                   } else {
  995:                       $message = $contrib{$idx.':message'};
  996:                   }
  997:               } else {
  998: 	          $message=$contrib{$idx.':message'};
  999:               }
 1000: 	      $message=~s/\n/\<br \/\>/g;
 1001: 	      $quote='<blockquote>'.&Apache::lontexconvert::msgtexconverted($message).'</blockquote>';
 1002:               if ($idx > 0) {
 1003:                   if ($contrib{$idx.':subject'} =~ /^<version num="0"/) {
 1004:                       my %versions = ();
 1005:                       &get_post_versions(\%versions,$contrib{$idx.':subject'},$numoldver);
 1006:                       $subject = &HTML::Entities::decode($versions{$numoldver});
 1007:                   } else {
 1008:                       $subject = $contrib{$idx.':subject'};
 1009:                   }
 1010:                   $subject = 'Re: '.$subject;
 1011:               }
 1012:               $subject = &HTML::Entities::encode($subject,'<>&"');
 1013:           } else {
 1014:               $attachmenturls = $contrib{$idx.':attachmenturl'};
 1015:               if ($contrib{$idx.':message'} =~ /^<version num="0">/) {
 1016:                   my %versions = ();
 1017:                   &get_post_versions(\%versions,$contrib{$idx.':message'},$numoldver);
 1018:                   $comment = $versions{$numoldver};
 1019:               } else {
 1020:                   $comment = &HTML::Entities::encode($contrib{$idx.':message'},'<>&"');
 1021:               }
 1022:               if ($contrib{$idx.':subject'} =~ /<version num="0">/) {
 1023:                   my %versions = ();
 1024:                   &get_post_versions(\%versions,$contrib{$idx.':subject'},$numoldver);
 1025:                   $subject = $versions{$numoldver}; 
 1026:               } else {
 1027:                   $subject = &HTML::Entities::encode($contrib{$idx.':subject'},'<>&"');
 1028:               }
 1029:               if (defined($contrib{$idx.':replyto'})) {
 1030:                   $parentmsg = $contrib{$idx.':replyto'};
 1031:               }
 1032:               unless (exists($ENV{'form.origpage'})) {
 1033:                   my $anonflag = 0;
 1034:                   if ($contrib{$idx.':anonymous'}) {
 1035:                       $anonflag = 1;
 1036:                   }
 1037:                   $anonscript = (<<END);
 1038:   function setposttype () {
 1039:       var currtype = $anonflag
 1040:       if (currtype == 1) {
 1041:           document.mailform.elements.discuss.checked = false
 1042:           document.mailform.elements.anondiscuss.checked = true
 1043:       }
 1044:       if (currtype == 0) {
 1045:           document.mailform.elements.anondiscuss.checked = false
 1046:           document.mailform.elements.discuss.checked = true
 1047:       }
 1048:       return
 1049:   }
 1050: END
 1051:               }
 1052:           }
 1053:       }
 1054:       if ($ENV{'form.previous'}) {
 1055:           $prevtag = '<input type="hidden" name="previous" value="'.$ENV{'form.previous'}.'" />';
 1056:       }
 1057:   }
 1058: 
 1059:   if ($ENV{'form.origpage'}) {
 1060:       $subject = $ENV{'form.subject'};
 1061:       $comment = $ENV{'form.comment'};
 1062:       &process_attachments(\@currnewattach,\@currdelold,\@keepold);
 1063:   }
 1064:   my $latexHelp=&Apache::loncommon::helpLatexCheatsheet();
 1065:   my $htmlheader=&Apache::lonhtmlcommon::htmlareaheaders();
 1066:   my $send=&mt('Send');
 1067:   $r->print(<<END);
 1068: <html>
 1069: <head>
 1070: <title>The LearningOnline Network with CAPA</title>
 1071: <meta http-equiv="pragma" content="no-cache"></meta>
 1072: $htmlheader
 1073: <script type="text/javascript">
 1074: //<!--
 1075:     function gosubmit() {
 1076:         var rec=0;
 1077:         if (typeof(document.mailform.elements.author)!="undefined") {
 1078:           if (document.mailform.elements.author.checked) {
 1079:              rec=1;
 1080:           } 
 1081:         }
 1082:         if (typeof(document.mailform.elements.question)!="undefined") {
 1083:           if (document.mailform.elements.question.checked) {
 1084:              rec=1;
 1085:           } 
 1086:         }
 1087:         if (typeof(document.mailform.elements.course)!="undefined") {
 1088:           if (document.mailform.elements.course.checked) {
 1089:              rec=1;
 1090:           } 
 1091:         }
 1092:         if (typeof(document.mailform.elements.policy)!="undefined") {
 1093:           if (document.mailform.elements.policy.checked) {
 1094:              rec=1;
 1095:           } 
 1096:         }
 1097:         if (typeof(document.mailform.elements.discuss)!="undefined") {
 1098:           if (document.mailform.elements.discuss.checked) {
 1099:              rec=1;
 1100:           } 
 1101:         }
 1102:         if (typeof(document.mailform.elements.anondiscuss)!="undefined") {
 1103:           if (document.mailform.elements.anondiscuss.checked) {
 1104:              rec=1;
 1105:           } 
 1106:         }
 1107: 
 1108:         if (rec) {
 1109:             if (typeof(document.mailform.onsubmit)!='undefined') {
 1110: 		document.mailform.onsubmit();
 1111: 	    }
 1112: 	    document.mailform.submit();
 1113:         } else {
 1114:             alert('Please check a feedback type.');
 1115: 	}
 1116:     }
 1117:     $anonchk
 1118:     $anonscript
 1119: //-->
 1120: </script>
 1121: </head>
 1122: $bodytag
 1123: <h2><tt>$title</tt></h2>
 1124: <form action="/adm/feedback" method="post" name="mailform"
 1125: enctype="multipart/form-data">
 1126: $prevtag
 1127: <input type="hidden" name="postdata" value="$feedurl" />
 1128: END
 1129:   if ($ENV{'form.replydisc'}) {
 1130:       $r->print(<<END);
 1131: <input type="hidden" name="replydisc" value="$ENV{'form.replydisc'}" />
 1132: END
 1133:   } elsif ($ENV{'form.editdisc'}) {
 1134:      $r->print(<<END);
 1135: <input type="hidden" name="editdisc" value ="$ENV{'form.editdisc'}" />
 1136: <input type="hidden" name="parentmsg" value ="$parentmsg" />
 1137: END
 1138:   }
 1139:   $r->print(<<END);
 1140: Please check at least one of the following feedback types:
 1141: $options<hr />
 1142: $quote
 1143: <p>My question/comment/feedback:</p>
 1144: <p>
 1145: $latexHelp
 1146: Title: <input type="text" name="subject" size="30" value="$subject" /></p>
 1147: <p>
 1148: <textarea name="comment" id="comment" cols="60" rows="10" wrap="hard">$comment
 1149: </textarea></p>
 1150: <p>
 1151: END
 1152:     if ( ($ENV{'form.editdisc'}) || ($ENV{'form.replydisc'}) ) {
 1153:         if ($ENV{'form.origpage'}) {
 1154:             foreach (@currnewattach) {
 1155:                 $r->print('<input type="hidden" name="currnewattach" value="'.$_.'" />'."\n");
 1156:             }
 1157:             foreach (@currdelold) {
 1158:                 $r->print('<input type="hidden" name="deloldattach" value="'.$_.'" />'."\n");
 1159:             }
 1160:         }
 1161:         if ($ENV{'form.editdisc'}) {
 1162:             if ($attachmenturls) {
 1163:                 &extract_attachments($attachmenturls,$idx,$numoldver,\$attachmsg,\%attachments,\%currattach,\@currdelold);
 1164:                 $attachnum = scalar(keys %currattach);
 1165:                 foreach (keys %currattach) {
 1166:                     $r->print('<input type="hidden" name="keepold" value="'.$_.'" />'."\n");
 1167:                 }
 1168:             }
 1169:         }
 1170:     } else {
 1171:         $r->print(<<END);
 1172: Attachment (128 KB max size): <input type="file" name="attachment" />
 1173: </p>
 1174: END
 1175:     }
 1176:     $r->print(<<END);
 1177: <p>
 1178: <input type="hidden" name="sendit" value="1" />
 1179: <input type="button" value="$send" onClick='gosubmit();' />
 1180: </p>
 1181: </form>
 1182: END
 1183:     if ($ENV{'form.editdisc'} || $ENV{'form.replydisc'}) {
 1184:         my $now = time;
 1185:         my $ressymb = $symb;
 1186:         my $postidx = '';
 1187:         if ($ENV{'form.editdisc'}) {
 1188:             $postidx = $idx;
 1189:         }
 1190:         if (@currnewattach > 0) {
 1191:             $attachnum += @currnewattach;
 1192:         }
 1193:         $r->print(&generate_attachments_button($postidx,$attachnum,$ressymb,$now,\@currnewattach,\@currdelold,$numoldver));
 1194:         if ($attachnum > 0) {
 1195:             if (@currnewattach > 0) {
 1196:                 $newattachmsg .= '<b>New attachments</b><br />';
 1197:                 if (@currnewattach > 1) {
 1198:                     $newattachmsg .= '<ol>';
 1199:                     foreach my $item (@currnewattach) {
 1200:                         $item =~ m#.*/([^/]+)$#;
 1201:                         $newattachmsg .= '<li><a href="'.$item.'">'.$1.'</a></li>'."\n";
 1202:                     }
 1203:                     $newattachmsg .= '</ol>'."\n";
 1204:                 } else {
 1205:                     $currnewattach[0] =~ m#.*/([^/]+)$#;
 1206:                     $newattachmsg .= '<a href="'.$currnewattach[0].'">'.$1.'</a><br />'."\n";
 1207:                 }
 1208:             }
 1209:             if ($attachmsg) {
 1210:                 $r->print("<b>Retained attachments</b>:$attachmsg<br />\n");
 1211:             }
 1212:             if ($newattachmsg) {
 1213:                 $r->print("$newattachmsg<br />");
 1214:             }
 1215:         }
 1216:     }
 1217:     $r->print(&generate_preview_button().
 1218:               &Apache::lonhtmlcommon::htmlareaselectactive('comment').
 1219:               '</body></html>');
 1220: }
 1221: 
 1222: sub print_display_options {
 1223:     my ($r,$symb,$previous,$dispchgA,$dispchgB,$markchg,$toggchg,$feedurl) = @_;
 1224:  # backward compatibility (bulletin boards used to be 'wrapped')
 1225:     if ($feedurl=~m|^/adm/wrapper/adm/.*/bulletinboard$|) {
 1226:         $feedurl=~s|^/adm/wrapper||;
 1227:     }
 1228: 
 1229:     my $function = &Apache::loncommon::get_users_function();
 1230:     my $tabcolor = &Apache::loncommon::designparm($function.'.tabbg',
 1231:                                                     $ENV{'user.domain'});
 1232:     my $bodytag=&Apache::loncommon::bodytag('Discussion options',
 1233:                                           '','');
 1234: 
 1235:     my %lt = &Apache::lonlocal::texthash(
 1236:         'dido' => 'Discussion display options',
 1237:         'pref' => 'Display Preference',
 1238:         'curr' => 'Current setting ',
 1239:         'actn' => 'Action',
 1240:         'deff' => 'Default for all discussions',
 1241:         'prca' => 'Preferences can be set for this discussion that determine ....',
 1242:         'whpo' => 'Which posts are displayed when you display this bulletin board or resource, and',
 1243:         'unwh' => 'Under what circumstances posts are identified as "NEW", and',
 1244:         'wipa' => 'Whether individual posts can be marked as read/unread',
 1245:         'allposts' => 'All posts',
 1246:         'unread' => 'New posts only',
 1247:         'unmark' => 'Posts not marked read',
 1248:         'ondisp' => 'Once displayed',
 1249:         'onmark' => 'Once marked not NEW ',
 1250:         'toggon' => 'Shown',
 1251:         'toggoff' => 'Not shown',
 1252:         'disa' => 'Posts displayed?',
 1253:         'npmr' => 'New posts cease to be identified as "NEW"?',
 1254:         'dotm' => 'Option to mark each post as read/unread?',  
 1255:         'chgt' => 'Change to ',
 1256:         'mkdf' => 'Set to ',
 1257:         'yhni' => 'You have not indicated that you wish to change any of the discussion settings',
 1258:         'ywbr' => 'You will be returned to the previous page if you click OK.'
 1259:     );
 1260: 
 1261:     my $dispchangeA = $lt{'unread'};
 1262:     my $dispchangeB = $lt{'unmark'};
 1263:     my $markchange = $lt{'ondisp'};
 1264:     my $toggchange = $lt{'toggon'};
 1265:     my $currdisp = $lt{'allposts'};
 1266:     my $currmark = $lt{'onmark'};
 1267:     my $discdisp = 'allposts';
 1268:     my $discmark = 'onmark';
 1269:     my $currtogg = $lt{'toggoff'};
 1270:     my $disctogg = 'toggoff';
 1271:                                                                                       
 1272:     if ($dispchgA eq 'allposts') {
 1273:         $dispchangeA = $lt{'allposts'};
 1274:         $currdisp = $lt{'unread'};
 1275:         $discdisp = 'unread';
 1276:     }
 1277: 
 1278:     if ($markchg eq 'markonread') {
 1279:         $markchange = $lt{'onmark'};
 1280:         $currmark = $lt{'ondisp'};
 1281:         $discmark = 'ondisp';
 1282:     }
 1283: 
 1284:     if ($dispchgB eq 'onlyunread') {
 1285:         $dispchangeB = $lt{'unread'};
 1286:         $currdisp = $lt{'unmark'};
 1287:         $discdisp = 'unmark';
 1288:     }
 1289:     if ($toggchg eq 'toggoff') {
 1290:         $toggchange = $lt{'toggoff'};
 1291:         $currtogg = $lt{'toggon'};
 1292:         $disctogg = 'toggon';
 1293:     }
 1294:     $r->print(<<END);
 1295: <html>
 1296: <head>
 1297: <title>$lt{'dido'}</title>
 1298: <meta http-equiv="pragma" content="no-cache" />
 1299: <script>
 1300: function discdispChk(caller) {
 1301:     var disctogg = '$toggchg'
 1302:     if (caller == 0) {
 1303:         if (document.modifydisp.discdisp[0].checked == true) {
 1304:             if (document.modifydisp.discdisp[1].checked == true) {
 1305:                 document.modifydisp.discdisp[1].checked = false
 1306:             }
 1307:         }
 1308:     }
 1309:     if (caller == 1) {
 1310:         if (document.modifydisp.discdisp[1].checked == true) {
 1311:             if (document.modifydisp.discdisp[0].checked == true) {
 1312:                 document.modifydisp.discdisp[0].checked = false
 1313:             }
 1314:             if (disctogg == 'toggon') {
 1315:                 document.modifydisp.disctogg.checked = true
 1316:             }
 1317:             if (disctogg == 'toggoff') {
 1318:                 document.modifydisp.disctogg.checked = false
 1319:             }
 1320:         }
 1321:     }
 1322:     if (caller == 2) {
 1323:         var dispchgB = '$dispchgB'
 1324:         if (disctogg == 'toggoff') {
 1325:             if (document.modifydisp.disctogg.checked == true) {
 1326:                 if (dispchgB == 'onlyunmark') {
 1327:                     document.modifydisp.discdisp[1].checked = false
 1328:                 }
 1329:             }
 1330:         }
 1331:     }  
 1332: }
 1333: 
 1334: function setDisp() {
 1335:     var prev = "$previous"
 1336:     var chktotal = 0
 1337:     if (document.modifydisp.discdisp[0].checked == true) {
 1338:         document.modifydisp.$dispchgA.value = "$symb"
 1339:         chktotal ++
 1340:     }
 1341:     if (document.modifydisp.discdisp[1].checked == true) {
 1342:         document.modifydisp.$dispchgB.value = "$symb"
 1343:         chktotal ++
 1344:     }
 1345:     if (document.modifydisp.discmark.checked == true) {
 1346:         document.modifydisp.$markchg.value = "$symb"
 1347:         chktotal ++
 1348:     }
 1349:     if (document.modifydisp.disctogg.checked == true) {
 1350:         document.modifydisp.$toggchg.value = "$symb"
 1351:         chktotal ++
 1352:     }
 1353:     if (chktotal > 0) { 
 1354:         document.modifydisp.submit()
 1355:     } else {
 1356:         if(confirm("$lt{'yhni'}. \\n$lt{'ywbr'}"))      {
 1357:             if (prev > 0) {
 1358:                 location.href = "$feedurl?previous=$previous"
 1359:             } else {
 1360:                 location.href = "$feedurl"
 1361:             }
 1362:         }
 1363:     }
 1364: }
 1365: </script>
 1366: </head>
 1367: $bodytag
 1368: <form name="modifydisp" method="post" action="/adm/feedback">
 1369: $lt{'sdpf'}<br/> $lt{'prca'}  <ol><li>$lt{'whpo'}</li><li>$lt{'unwh'}</li><li>$lt{'wipa'}</li></ol>
 1370: <br />
 1371: <table border="0" cellpadding="0" cellspacing="0">
 1372:  <tr>
 1373:   <td width="100%" bgcolor="#000000">
 1374:    <table width="100%" border="0" cellpadding="1" cellspacing="0">
 1375:     <tr>
 1376:      <td width="100%" bgcolor="#000000">
 1377:       <table border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
 1378:        <tr bgcolor="$tabcolor">
 1379:         <td><b>$lt{'pref'}</b></td>
 1380:         <td><b>$lt{'curr'}</b></td>
 1381:         <td><b>$lt{'actn'}?</b></td>
 1382:        </tr>
 1383:        <tr bgcolor="#dddddd">
 1384:        <td>$lt{'disa'}</td>
 1385:        <td>$lt{$discdisp}</td>
 1386:        <td><input type="checkbox" name="discdisp" onClick="discdispChk('0')" />&nbsp;$lt{'chgt'} "$dispchangeA"
 1387:            <br />
 1388:            <input type="checkbox" name="discdisp" onClick="discdispChk('1')" />&nbsp;$lt{'chgt'} "$dispchangeB"
 1389:        </td>
 1390:       </tr><tr bgcolor="#eeeeee">
 1391:        <td>$lt{'npmr'}</td>
 1392:        <td>$lt{$discmark}</td>
 1393:        <td><input type="checkbox" name="discmark" />$lt{'chgt'} "$markchange"</td>
 1394:       </tr><tr bgcolor="#dddddd">
 1395:        <td>$lt{'dotm'}</td>
 1396:        <td>$lt{$disctogg}</td>
 1397:        <td><input type="checkbox" name="disctogg" onClick="discdispChk('2')" />$lt{'chgt'} "$toggchange"</td>
 1398:       </tr>
 1399:      </table>
 1400:     </td>
 1401:    </tr>
 1402:   </table>
 1403:  </td>
 1404: </tr>
 1405: </table>
 1406: <br />
 1407: <br />
 1408: <input type="hidden" name="previous" value="$previous" />
 1409: <input type="hidden" name="$dispchgA" value=""/>
 1410: <input type="hidden" name="$dispchgB" value=""/>
 1411: <input type="hidden" name="$markchg" value=""/>
 1412: <input type="hidden" name="$toggchg" value="" />
 1413: <input type="button" name="sub" value="Store Changes" onClick="javascript:setDisp()" />
 1414: <br />
 1415: <br />
 1416: </form>
 1417: </body>
 1418: </html>
 1419: END
 1420:     return;
 1421: }
 1422: 
 1423: sub print_sortfilter_options {
 1424:     my ($r,$symb,$previous,$feedurl) = @_;
 1425:  # backward compatibility (bulletin boards used to be 'wrapped')
 1426:     if ($feedurl=~m|^/adm/wrapper/adm/.*/bulletinboard$|) {
 1427:         $feedurl=~s|^/adm/wrapper||;
 1428:     }
 1429:     my @sections = ();
 1430:     my $section_sel = '';
 1431:     my $numsections = 0;
 1432:     my $numvisible = 5;
 1433:     my ($classlist) = &Apache::loncoursedata::get_classlist(
 1434:                               $ENV{'request.course.id'},
 1435:                               $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
 1436:                               $ENV{'course.'.$ENV{'request.course.id'}.'.num'});
 1437:                                                                                    
 1438:     my $sec_index = &Apache::loncoursedata::CL_SECTION();
 1439:     my $status_index = &Apache::loncoursedata::CL_STATUS();
 1440:     my %sectioncount = ();
 1441:     while (my ($student,$data) = each %$classlist) {
 1442:         my ($section,$status) = ($data->[$sec_index],
 1443:                                  $data->[$status_index]);
 1444:         unless ($section eq '' || $section =~ /^\s*$/) {
 1445:             if (!defined($sectioncount{$section})) {
 1446:                 $sectioncount{$section} = 1;
 1447:                 $numsections ++;
 1448:             } else {
 1449:                 $sectioncount{$section} ++;
 1450:             }
 1451:         }
 1452:     }
 1453:                                                                                    
 1454:     if ($ENV{'request.course.sec'} !~ /^\s*$/) {
 1455:         @sections = ($ENV{'request.course.sec'});
 1456:         $numvisible = 1;
 1457:     } else {
 1458:         @sections = sort {$a cmp $b} keys(%sectioncount);
 1459:         unshift(@sections,'all'); # Put 'all' at the front of the list
 1460:         if ($numsections < 4) {
 1461:             $numvisible = $numsections + 1;
 1462:         }
 1463:     }
 1464:     foreach (@sections) {
 1465:         $section_sel .= "  <option value=\"$_\" />$_\n";
 1466:     }
 1467:                                                                                    
 1468:     my $function = &Apache::loncommon::get_users_function();
 1469:     my $tabcolor = &Apache::loncommon::designparm($function.'.tabbg',
 1470:                                                     $ENV{'user.domain'});
 1471:     my $bodytag=&Apache::loncommon::bodytag('Discussion options',
 1472:                                           '','');
 1473:     my %lt = &Apache::lonlocal::texthash(
 1474:         'diso' => 'Discussion sorting and filtering options',
 1475:         'diop' => 'Display Options',
 1476:         'curr' => 'Current setting ',
 1477:         'actn' => 'Action',
 1478:         'prca' => 'Options can be set that control the sort order of the posts, in addition to which posts are displayed.',
 1479:         'soor' => 'Sort order',
 1480:         'disp' => 'Specific user roles',
 1481:         'actv' => 'Specific role status',
 1482:         'spse' => 'Specific sections',
 1483:         'psub' => 'Pick specific users (by name)',
 1484:         'shal' => 'Show a list of current posters'
 1485:     );
 1486:     $r->print(<<END);
 1487: <html>
 1488: <head>
 1489: <title>$lt{'diso'}</title>
 1490: <meta http-equiv="pragma" content="no-cache" />
 1491: </head>
 1492: $bodytag
 1493: <form name="modifyshown" method="post" action="/adm/feedback">
 1494: <b>$lt{'diso'}</b><br/> $lt{'prca'}
 1495: <br /><br />
 1496: <table border="0">
 1497:  <tr>
 1498:   <td><b>$lt{'soor'}</b></td>
 1499:   <td>&nbsp;</td>
 1500:   <td><b>$lt{'disp'}</b></td>
 1501:   <td>&nbsp;</td>
 1502:   <td><b>$lt{'actv'}</b></td>
 1503:   <td>&nbsp;</td>
 1504:   <td><b>$lt{'spse'}</b></td>
 1505:   <td>&nbsp;</td>
 1506:   <td><b>$lt{'psub'}</b></td>
 1507:  </tr>
 1508:  <tr>
 1509:   <td>
 1510:    <select name="sortposts">
 1511:     <option value="ascdate" />Date order - oldest first
 1512:     <option value="descdate" />Date order - newest first
 1513:     <option value="thread" />Threaded
 1514:     <option value="subject" />By subject
 1515:     <option value="username" />By domain and username
 1516:     <option value="lastfirst" />By last name, first name
 1517:    </select>
 1518:   </td>
 1519:   <td>&nbsp;</td>
 1520:   <td>
 1521:    <select name="rolefilter" multiple="true" size="5">
 1522:     <option value="all" />All users
 1523:     <option value="st" />Students
 1524:     <option value="cc" />Course Coordinators
 1525:     <option value="in" />Instructors
 1526:     <option value="ta" />TAs
 1527:     <option value="pr" />Exam proctors
 1528:     <option value="cr" />Custom roles
 1529:    </select>
 1530:   </td>
 1531:   <td>&nbsp;</td>
 1532:   <td>
 1533:    <select name="statusfilter">
 1534:     <option value="all" />Roles of any status
 1535:     <option value="Active" />Only active roles
 1536:     <option value="Expired" />Only inactive roles
 1537:    </select>
 1538:   </td>
 1539:   <td>&nbsp;</td>
 1540:   <td>
 1541:    <select name="sectionpick" multiple="true" size="$numvisible">
 1542:     $section_sel
 1543:    </select>
 1544:   </td>
 1545:   <td>&nbsp;</td>
 1546:   <td><input type="checkbox" name="posterlist" value="$symb" />$lt{'shal'}</td>
 1547:  </tr>
 1548: </table>
 1549: <br />
 1550: <br />
 1551: <input type="hidden" name="previous" value="$previous" />
 1552: <input type="hidden" name="applysort" value="$symb" />
 1553: <input type="button" name="sub" value="Store Changes" onClick="javascript:document.modifyshown.submit()" />
 1554: <br />
 1555: <br />
 1556: </form>
 1557: </body>
 1558: </html>
 1559: END
 1560: }
 1561: 
 1562: sub print_showposters {
 1563:     my ($r,$symb,$previous,$feedurl,$sortposts) = @_;
 1564:  # backward compatibility (bulletin boards used to be 'wrapped')
 1565:     if ($feedurl=~m|^/adm/wrapper/adm/.*/bulletinboard$|) {
 1566:         $feedurl=~s|^/adm/wrapper||;
 1567:     }
 1568: # backward compatibility (bulletin boards used to be 'wrapped')
 1569:     my $ressymb=$symb;
 1570:     if ($ressymb =~ /bulletin___\d+___/) {
 1571:         unless ($ressymb =~ m|bulletin___\d+___adm/wrapper|) {
 1572:             $ressymb=~s|(bulletin___\d+___)|$1adm/wrapper|;
 1573:         }
 1574:     }
 1575:     my $crs='/'.$ENV{'request.course.id'};
 1576:     if ($ENV{'request.course.sec'}) {
 1577:         $crs.='_'.$ENV{'request.course.sec'};
 1578:     }
 1579:     $crs=~s/\_/\//g;
 1580:     my $seeid=&Apache::lonnet::allowed('rin',$crs);
 1581:     my %contrib=&Apache::lonnet::restore($ressymb,$ENV{'request.course.id'},
 1582:                           $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
 1583:                           $ENV{'course.'.$ENV{'request.course.id'}.'.num'});
 1584:     my %namesort = ();
 1585:     my %postcounts = ();
 1586:     my %lt=&Apache::lonlocal::texthash(
 1587:                      'diso' => 'Discussion filtering options',
 1588:     );
 1589:     my $bodytag=&Apache::loncommon::bodytag('Discussion options',
 1590:                                           '','');
 1591:     if ($contrib{'version'}) {
 1592:         for (my $idx=1;$idx<=$contrib{'version'};$idx++) {
 1593:             my $hidden=($contrib{'hidden'}=~/\.$idx\./);
 1594:             my $deleted=($contrib{'deleted'}=~/\.$idx\./);
 1595:             unless ((($hidden) && (!$seeid)) || ($deleted)) {
 1596:                 if ((!$contrib{$idx.':anonymous'}) || ($seeid)) {
 1597:                     my %names = &Apache::lonnet::get('environment',['firstname','lastname'],$contrib{$idx.':senderdomain'},$contrib{$idx.':sendername'});
 1598:                     my $lastname = $names{'lastname'};
 1599:                     my $firstname = $names{'firstname'};
 1600:                     if ($lastname eq '') {
 1601:                         $lastname = '_';
 1602:                     }
 1603:                     if ($firstname eq '') {
 1604:                         $firstname = '_';
 1605:                     }
 1606:                     unless (defined($namesort{$lastname})) {
 1607:                         %{$namesort{$lastname}} = ();
 1608:                     }
 1609:                     my $poster =  $contrib{$idx.':sendername'}.':'.$contrib{$idx.':senderdomain'};
 1610:                     $postcounts{$poster} ++;
 1611:                     if (defined($namesort{$lastname}{$firstname})) {
 1612:                         if (!grep/^$poster$/,@{$namesort{$lastname}{$firstname}}) {
 1613:                             push @{$namesort{$lastname}{$firstname}}, $poster;
 1614:                         }
 1615:                     } else {
 1616:                         @{$namesort{$lastname}{$firstname}} = ("$poster");
 1617:                     }
 1618:                 }
 1619:             }
 1620:         }
 1621:     }
 1622:     $r->print(<<END);
 1623: <html>
 1624: <head>
 1625: <title>$lt{'diso'}</title>
 1626: <meta http-equiv="pragma" content="no-cache" />
 1627: </head>
 1628: $bodytag
 1629:  <form name="pickpostersform" method="post">
 1630:   <table border="0">
 1631:    <tr>
 1632:     <td bgcolor="#777777">
 1633:      <table border="0" cellpadding="3">
 1634:       <tr bgcolor="#e6ffff">
 1635:        <td><b>No.</b></td>
 1636:        <td><b>Select</b></td>
 1637:        <td><b>Fullname</b><font color="#999999">(Username/domain)</font></td>
 1638:        <td><b>Posts</td>
 1639:       </tr>
 1640: END
 1641:     my $count = 0;
 1642:     foreach my $last (sort keys %namesort) {
 1643:         foreach my $first (sort keys %{$namesort{$last}}) {
 1644:             foreach (sort @{$namesort{$last}{$first}}) {
 1645:                 my ($uname,$udom) = split/:/,$_;
 1646:                 if (!$uname || !$udom) { 
 1647:                     next;
 1648:                 } else {
 1649:                     $count ++;
 1650:                     $r->print('<tr bgcolor="#ffffe6"><td align="right">'.$count.'</td><td align="center"><input name="stuinfo" type="checkbox" value="'.$_.'" /></td><td>'.$last.', '.$first.' ('.$uname.','.$udom.')</td><td>'.$postcounts{$_}.'</td></tr>');
 1651:                 }
 1652:             }
 1653:         }
 1654:     }
 1655:     $r->print(<<END);
 1656:      </table>
 1657:     </td>
 1658:    </tr>
 1659:   </table>
 1660: <br />
 1661: <input type="hidden" name="sortposts" value="$sortposts" />
 1662: <input type="hidden" name="userpick" value="$symb" />
 1663: <input type="button" name="store" value="Display posts" onClick="javascript:document.pickpostersform.submit()" />
 1664: </form>
 1665: </body>
 1666: </html>
 1667: END
 1668: }
 1669: 
 1670: sub get_post_versions {
 1671:     my ($versions,$incoming,$numver) = @_;
 1672:     my $p = HTML::LCParser->new(\$incoming);
 1673:     my $done = 0;                                                                       
 1674:     while ( (my $token = $p->get_tag("version")) && (!$done)) {
 1675:         my $num = $token->[1]{num};
 1676:         my $text = $p->get_text("/version");
 1677:         if (defined($numver)) {
 1678:             if ($num == $numver) {
 1679:                 $$versions{$numver}=$text;
 1680:                 $done = 1;
 1681:             }
 1682:         } else {
 1683:             $$versions{$num}=$text;
 1684:         }
 1685:     }
 1686:     return;
 1687: }
 1688: 
 1689: sub get_post_attachments {
 1690:     my ($attachments,$attachmenturls) = @_;
 1691:     my $num;
 1692:     my $p = HTML::LCParser->new(\$attachmenturls);
 1693:     while (my $token = $p->get_tag("attachment","filename","post"))  {
 1694:         if ($token->[0] eq "attachment") {
 1695:             $num = $token->[1]{id};
 1696:             %{$$attachments{$num}} =();
 1697:         } elsif ($token->[0] eq "filename") {
 1698:             $$attachments{$num}{'filename'} = $p->get_text("/filename");
 1699:         } elsif ($token->[0] eq "post") {
 1700:             my $id = $token->[1]{id};
 1701:             $$attachments{$num}{$id} = $p->get_text("/post");
 1702:         }
 1703:     }
 1704:     return;
 1705: }
 1706: 
 1707: sub fail_redirect {;
 1708:   my ($r,$feedurl) = @_;
 1709:   if ($feedurl=~/^\/adm\//) { $feedurl.='?register=1' };
 1710:   $r->print (<<ENDFAILREDIR);
 1711: <html>
 1712: <head><title>Feedback not sent</title>
 1713: <meta http-equiv="pragma" content="no-cache" />
 1714: <meta HTTP-EQUIV="Refresh" CONTENT="2; url=$feedurl" />
 1715: </head>
 1716: <body bgcolor="#FFFFFF">
 1717: <img align="right" src="/adm/lonIcons/lonlogos.gif" />
 1718: <b>Sorry, no recipients  ...</b>
 1719: </body>
 1720: </html>
 1721: ENDFAILREDIR
 1722: }
 1723: 
 1724: sub redirect_back {
 1725:   my ($r,$feedurl,$typestyle,$sendsomething,$sendposts,$status,$previous,$sort,$rolefilter,$statusfilter,$secpick,$numpicks) = @_;
 1726:   my $sorttag = '';
 1727:   my $roletag = '';
 1728:   my $statustag = '';
 1729:   my $sectag = '';
 1730:   my $userpicktag = '';
 1731:   my $qrystr = '';
 1732:   my $prevtag = '';
 1733:  # backward compatibility (bulletin boards used to be 'wrapped')
 1734:   if ($feedurl=~m|^/adm/wrapper/adm/.*/bulletinboard$|) {
 1735:       $feedurl=~s|^/adm/wrapper||;
 1736:   }
 1737:   if ($feedurl=~/^\/adm\//) { $feedurl.='?register=1' };
 1738:   if ($previous > 0) {
 1739:       $qrystr = 'previous='.$previous;
 1740:       if ($feedurl =~ /\?register=1/) {
 1741:           $feedurl .= '&'.$qrystr;
 1742:       } else {
 1743:           $feedurl .= '?'.$qrystr;
 1744:       }
 1745:       $prevtag = '<input type="hidden" name="previous" value="'.$previous.'" />';
 1746:   }
 1747:   if (defined($sort)) {
 1748:       my $sortqry = 'sortposts='.$sort;
 1749:       if (($feedurl =~ /\?register=1/) || ($feedurl =~ /\?previous=/)) {
 1750:           $feedurl .= '&'.$sortqry;
 1751:       } else {
 1752:           $feedurl .= '?'.$sortqry;
 1753:       }
 1754:       $sorttag = '<input type="hidden" name="sortposts" value="'.$sort.'" />';
 1755:       if ( (defined($numpicks)) && ($numpicks > 0) ) {
 1756:           my $userpickqry = 'totposters='.$numpicks;
 1757:           $feedurl .= '&'.$userpickqry;
 1758:           $userpicktag = '<input type="hidden" name="totposters" value="'.$numpicks.'" />';
 1759:       } else {
 1760:           my $roleqry = 'rolefilter='.$rolefilter;
 1761:           $feedurl .= '&'.$roleqry;
 1762:           $roletag = '<input type="hidden" name="rolefilter" value="'.$rolefilter.'" />';
 1763:           $feedurl .= '&statusfilter='.$statusfilter;
 1764:           $statustag ='<input type="hidden" name="statusfilter" value="'.$statusfilter.'" />';
 1765:           $feedurl .= '&sectionpick='.$secpick;
 1766:           $sectag = '<input type="hidden" name="sectionpick" value="'.$secpick.'" />';
 1767:       }
 1768:   }
 1769:   $r->print (<<ENDREDIR);
 1770: <html>
 1771: <head>
 1772: <title>Feedback sent</title>
 1773: <meta http-equiv="pragma" content="no-cache" />
 1774: <meta HTTP-EQUIV="Refresh" CONTENT="2; url=$feedurl" />
 1775: </head>
 1776: <body bgcolor="#FFFFFF" onLoad='if (window.name!="loncapaclient") { this.document.reldt.submit(); self.close(); }'>
 1777: <img align="right" src="/adm/lonIcons/lonlogos.gif" />
 1778: $typestyle
 1779: <b>Sent $sendsomething message(s), and $sendposts post(s).</b>
 1780: <font color="red">$status</font>
 1781: <form name="reldt" action="$feedurl" target="loncapaclient">
 1782: $prevtag
 1783: $sorttag
 1784: $statustag
 1785: $roletag
 1786: $sectag
 1787: $userpicktag
 1788: </form>
 1789: </body>
 1790: </html>
 1791: ENDREDIR
 1792: }
 1793: 
 1794: sub no_redirect_back {
 1795:   my ($r,$feedurl) = @_;
 1796:   my $nofeed=&mt('Sorry, no feedback possible on this resource  ...');
 1797:   $r->print (<<ENDNOREDIR);
 1798: <html>
 1799: <head><title>Feedback not sent</title>
 1800: <meta http-equiv="pragma" content="no-cache" />
 1801: ENDNOREDIR
 1802: 
 1803:   if ($feedurl!~/^\/adm\/feedback/) { 
 1804:     $r->print('<meta HTTP-EQUIV="Refresh" CONTENT="2; url='.$feedurl.'">');
 1805:   }
 1806:   
 1807:   $r->print (<<ENDNOREDIRTWO);
 1808: </head>
 1809: <body bgcolor="#FFFFFF" onLoad='if (window.name!="loncapaclient") { self.close(); }'>
 1810: <img align="right" src="/adm/lonIcons/lonlogos.gif" />
 1811: <b>$nofeed</b>
 1812: </body>
 1813: </html>
 1814: ENDNOREDIRTWO
 1815: }
 1816: 
 1817: sub screen_header {
 1818:     my ($feedurl) = @_;
 1819:     my $msgoptions='';
 1820:     my $discussoptions='';
 1821:     unless (($ENV{'form.replydisc'}) || ($ENV{'form.editdisc'})) {
 1822: 	if (($feedurl=~/^\/res\//) && ($feedurl!~/^\/res\/adm/)) {
 1823: 	    $msgoptions= 
 1824: 		'<p><input type="checkbox" name="author" /> '.
 1825: 		&mt('Feedback to resource author').'</p>';
 1826: 	}
 1827: 	if (&feedback_available(1)) {
 1828: 	    $msgoptions.=
 1829: 		'<br /><input type="checkbox" name="question" /> '.
 1830: 		&mt('Question about resource content');
 1831: 	}
 1832: 	if (&feedback_available(0,1)) {
 1833: 	    $msgoptions.=
 1834: 		'<br /><input type="checkbox" name="course" /> '.
 1835: 		&mt('Question/Comment/Feedback about course content');
 1836: 	}
 1837: 	if (&feedback_available(0,0,1)) {
 1838: 	    $msgoptions.=
 1839: 		'<br /><input type="checkbox" name="policy" /> '.
 1840: 		&mt('Question/Comment/Feedback about course policy');
 1841: 	}
 1842:     }
 1843:     if ($ENV{'request.course.id'}) {
 1844: 	if (&discussion_open() &&
 1845: 	    &Apache::lonnet::allowed('pch',
 1846: 				     $ENV{'request.course.id'}.
 1847: 				     ($ENV{'request.course.sec'}?'/'.$ENV{'request.course.sec'}:''))) {
 1848: 	    $discussoptions='<input type="checkbox" name="discuss" onClick="this.form.anondiscuss.checked=false;" '.
 1849: 		($ENV{'form.replydisc'}?' checked="1"':'').' /> '.
 1850: 		&mt('Contribution to course discussion of resource');
 1851: 	    $discussoptions.='<br /><input type="checkbox" name="anondiscuss" onClick="this.form.discuss.checked=false;" /> '.
 1852: 		&mt('Anonymous contribution to course discussion of resource').
 1853: 		' <i>('.&mt('name only visible to course faculty').')</i>';
 1854:       }
 1855:     }
 1856:     if ($msgoptions) { $msgoptions='<h2><img src="/adm/lonMisc/feedback.gif" />'.&mt('Sending Messages').'</h2>'.$msgoptions; }
 1857:     if ($discussoptions) { 
 1858: 	$discussoptions='<h2><img src="/adm/lonMisc/chat.gif" />'.&mt('Discussion Contributions').'</h2>'.$discussoptions; }
 1859:     return $msgoptions.$discussoptions;
 1860: }
 1861: 
 1862: sub resource_output {
 1863:   my ($feedurl) = @_;
 1864:   my $usersaw=&Apache::lonnet::ssi_body($feedurl);
 1865:   $usersaw=~s/\<body[^\>]*\>//gi;
 1866:   $usersaw=~s/\<\/body\>//gi;
 1867:   $usersaw=~s/\<html\>//gi;
 1868:   $usersaw=~s/\<\/html\>//gi;
 1869:   $usersaw=~s/\<head\>//gi;
 1870:   $usersaw=~s/\<\/head\>//gi;
 1871:   $usersaw=~s/action\s*\=/would_be_action\=/gi;
 1872:   return $usersaw;
 1873: }
 1874: 
 1875: sub clear_out_html {
 1876:   my ($message,$override)=@_;
 1877:   unless (&Apache::lonhtmlcommon::htmlareablocked()) { return $message; }
 1878: # Always allow the <m>-tag
 1879:   my %html=(M=>1);
 1880: # Check if more is allowed
 1881:   my $cid=$ENV{'request.course.id'};
 1882:   if (($ENV{"course.$cid.allow_limited_html_in_feedback"} =~ m/yes/i) ||
 1883:       ($override)) {
 1884:       # allows <B> <I> <P> <A> <LI> <OL> <UL> <EM> <BR> <TT> <STRONG> 
 1885:       # <BLOCKQUOTE> <DIV .*> <DIV> <IMG> <M> <SPAN> <H1> <H2> <H3> <H4> <SUB>
 1886:       # <SUP>
 1887:       %html=(B=>1, I=>1, P=>1, A=>1, LI=>1, OL=>1, UL=>1, EM=>1,
 1888: 	     BR=>1, TT=>1, STRONG=>1, BLOCKQUOTE=>1, DIV=>1, IMG=>1,
 1889: 	     M=>1, SUB=>1, SUP=>1, SPAN=>1, 
 1890: 	     H1=>1, H2=>1, H3=>1, H4=>1, H5=>1);
 1891:   }
 1892: # Do the substitution of everything that is not explicitly allowed
 1893:   $message =~ s/\<(\/?\s*(\w+)[^\>\<]*)/
 1894: 	  {($html{uc($2)}&&(length($1)<1000))?"\<$1":"\&lt;$1"}/ge;
 1895:   $message =~ s/(\<?\s*(\w+)[^\<\>]*)\>/
 1896: 	  {($html{uc($2)}&&(length($1)<1000))?"$1\>":"$1\&gt;"}/ge;
 1897:   return $message;
 1898: }
 1899: 
 1900: sub assemble_email {
 1901:   my ($feedurl,$message,$prevattempts,$usersaw,$useranswer)=@_;
 1902:   my $email=<<"ENDEMAIL";
 1903: Refers to <a href="$feedurl">$feedurl</a>
 1904: 
 1905: $message
 1906: ENDEMAIL
 1907:     my $citations=<<"ENDCITE";
 1908: <h2>Previous attempts of student (if applicable)</h2>
 1909: $prevattempts
 1910: <br /><hr />
 1911: <h2>Original screen output (if applicable)</h2>
 1912: $usersaw
 1913: <h2>Correct Answer(s) (if applicable)</h2>
 1914: $useranswer
 1915: ENDCITE
 1916:   return ($email,$citations);
 1917: }
 1918: 
 1919: sub secapply {
 1920:     my $rec=shift;
 1921:     my $defaultflag=shift;
 1922:     $rec=~s/\s+//g;
 1923:     $rec=~s/\@/\:/g;
 1924:     my ($adr,$sections)=($rec=~/^([^\(]+)\(([^\)]+)\)/);
 1925:     if ($sections) {
 1926: 	foreach (split(/\;/,$sections)) {
 1927:             if (($_ eq $ENV{'request.course.sec'}) ||
 1928:                 ($defaultflag && ($_ eq '*'))) {
 1929:                 return $adr; 
 1930:             }
 1931:         }
 1932:     } else {
 1933:        return $rec;
 1934:     }
 1935:     return '';
 1936: }
 1937: 
 1938: sub decide_receiver {
 1939:   my ($feedurl,$author,$question,$course,$policy,$defaultflag) = @_;
 1940:   my $typestyle='';
 1941:   my %to=();
 1942:   if ($ENV{'form.author'}||$author) {
 1943:     $typestyle.='Submitting as Author Feedback<br>';
 1944:     $feedurl=~/^\/res\/(\w+)\/(\w+)\//;
 1945:     $to{$2.':'.$1}=1;
 1946:   }
 1947:   if ($ENV{'form.question'}||$question) {
 1948:     $typestyle.='Submitting as Question<br>';
 1949:     foreach (split(/\,/,
 1950: 		   $ENV{'course.'.$ENV{'request.course.id'}.'.question.email'})
 1951: 	     ) {
 1952: 	my $rec=&secapply($_,$defaultflag);
 1953:         if ($rec) { $to{$rec}=1; }
 1954:     } 
 1955:   }
 1956:   if ($ENV{'form.course'}||$course) {
 1957:     $typestyle.='Submitting as Comment<br />';
 1958:     foreach (split(/\,/,
 1959: 		   $ENV{'course.'.$ENV{'request.course.id'}.'.comment.email'})
 1960: 	     ) {
 1961: 	my $rec=&secapply($_,$defaultflag);
 1962:         if ($rec) { $to{$rec}=1; }
 1963:     } 
 1964:   }
 1965:   if ($ENV{'form.policy'}||$policy) {
 1966:     $typestyle.='Submitting as Policy Feedback<br />';
 1967:     foreach (split(/\,/,
 1968: 		   $ENV{'course.'.$ENV{'request.course.id'}.'.policy.email'})
 1969: 	     ) {
 1970: 	my $rec=&secapply($_,$defaultflag);
 1971:         if ($rec) { $to{$rec}=1; }
 1972:     } 
 1973:   }
 1974:   if ((scalar(%to) eq '0') && (!$defaultflag)) {
 1975:      ($typestyle,%to)=
 1976: 	 &decide_receiver($feedurl,$author,$question,$course,$policy,1);
 1977:   }
 1978:   return ($typestyle,%to);
 1979: }
 1980: 
 1981: sub feedback_available {
 1982:     my ($question,$course,$policy)=@_;
 1983:     my ($typestyle,%to)=&decide_receiver('',0,$question,$course,$policy);
 1984:     return scalar(%to);
 1985: }
 1986: 
 1987: sub send_msg {
 1988:   my ($feedurl,$email,$citations,$attachmenturl,%to)=@_;
 1989:   my $status='';
 1990:   my $sendsomething=0;
 1991:   foreach (keys %to) {
 1992:     if ($_) {
 1993:       my $declutter=&Apache::lonnet::declutter($feedurl);
 1994:       unless (&Apache::lonmsg::user_normal_msg(split(/\:/,$_),
 1995:                'Feedback ['.$declutter.']',$email,$citations,$feedurl,
 1996:                 $attachmenturl)=~/ok/) {
 1997: 	$status.='<br />'.&mt('Error sending message to').' '.$_.'<br />';
 1998:       } else {
 1999: 	$sendsomething++;
 2000:       }
 2001:     }
 2002:   }
 2003: 
 2004:     my %record=&Apache::lonnet::restore('_feedback');
 2005:     my ($temp)=keys %record;
 2006:     unless ($temp=~/^error\:/) {
 2007:        my %newrecord=();
 2008:        $newrecord{'resource'}=$feedurl;
 2009:        $newrecord{'subnumber'}=$record{'subnumber'}+1;
 2010:        unless (&Apache::lonnet::cstore(\%newrecord,'_feedback') eq 'ok') {
 2011: 	   $status.='<br />'.&mt('Not registered').'<br />';
 2012:        }
 2013:     }
 2014:        
 2015:   return ($status,$sendsomething);
 2016: }
 2017: 
 2018: sub adddiscuss {
 2019:     my ($symb,$email,$anon,$attachmenturl,$subject)=@_;
 2020:     my $status='';
 2021:     if (&discussion_open() &&
 2022: 	&Apache::lonnet::allowed('pch',$ENV{'request.course.id'}.
 2023:         ($ENV{'request.course.sec'}?'/'.$ENV{'request.course.sec'}:''))) {
 2024: 
 2025:     my %contrib=('message'      => $email,
 2026:                  'sendername'   => $ENV{'user.name'},
 2027:                  'senderdomain' => $ENV{'user.domain'},
 2028:                  'screenname'   => $ENV{'environment.screenname'},
 2029:                  'plainname'    => $ENV{'environment.firstname'}.' '.
 2030: 		                   $ENV{'environment.middlename'}.' '.
 2031:                                    $ENV{'environment.lastname'}.' '.
 2032:                                    $ENV{'enrironment.generation'},
 2033:                  'attachmenturl'=> $attachmenturl,
 2034:                  'subject'      => $subject);
 2035:     if ($ENV{'form.replydisc'}) {
 2036: 	$contrib{'replyto'}=(split(/\:\:\:/,$ENV{'form.replydisc'}))[1];
 2037:     }
 2038:     if ($anon) {
 2039: 	$contrib{'anonymous'}='true';
 2040:     }
 2041:     if (($symb) && ($email)) {
 2042:         if ($ENV{'form.editdisc'}) {
 2043:             my %newcontrib = ();
 2044:             $contrib{'ip'}=$ENV{'REMOTE_ADDR'};
 2045:             $contrib{'host'}=$Apache::lonnet::perlvar{'lonHostID'};
 2046:             $contrib{'timestamp'} = time;
 2047:             $contrib{'history'} = '';
 2048:             my $numoldver = 0;
 2049:             my ($oldsymb,$oldidx)=split(/\:\:\:/,$ENV{'form.editdisc'});
 2050:             $oldsymb=~s|(bulletin___\d+___)adm/wrapper/|$1|;
 2051: # get timestamp for last post and history
 2052:             my %oldcontrib=&Apache::lonnet::restore($oldsymb,$ENV{'request.course.id'},
 2053:                      $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
 2054:                      $ENV{'course.'.$ENV{'request.course.id'}.'.num'});
 2055:             if (defined($oldcontrib{$oldidx.':replyto'})) {
 2056:                 $contrib{'replyto'} = $oldcontrib{$oldidx.':replyto'};
 2057:             }
 2058:             if (defined($oldcontrib{$oldidx.':history'})) {
 2059:                 if ($oldcontrib{$oldidx.':history'} =~ /:/) {
 2060:                     my @oldversions = split/:/,$oldcontrib{$oldidx.':history'};
 2061:                     $numoldver = @oldversions;
 2062:                 } else {
 2063:                     $numoldver = 1;
 2064:                 }
 2065:                 $contrib{'history'} = $oldcontrib{$oldidx.':history'}.':';
 2066:             }
 2067:             my $numnewver = $numoldver + 1;
 2068:             if (defined($oldcontrib{$oldidx.':subject'})) {
 2069:                 if ($oldcontrib{$oldidx.':subject'} =~ /^<version num="0">/) {
 2070:                     $contrib{'subject'} = '<version num="'.$numnewver.'">'.&HTML::Entities::encode($contrib{'subject'},'<>&"').'</version>';
 2071:                     $contrib{'subject'} = $oldcontrib{$oldidx.':subject'}.$contrib{'subject'};
 2072:                 } else {
 2073:                     $contrib{'subject'} = '<version num="0">'.&HTML::Entities::encode($oldcontrib{$oldidx.':subject'},'<>&"').'</version><version num="1">'.&HTML::Entities::encode($contrib{'subject'},'<>&"').'</version>';
 2074:                 }
 2075:             } 
 2076:             if (defined($oldcontrib{$oldidx.':message'})) {
 2077:                 if ($oldcontrib{$oldidx.':message'} =~ /^<version num="0">/) {
 2078:                     $contrib{'message'} = '<version num="'.$numnewver.'">'.&HTML::Entities::encode($contrib{'message'},'<>&"').'</version>';
 2079:                     $contrib{'message'} = $oldcontrib{$oldidx.':message'}.$contrib{'message'};
 2080:                 } else {
 2081:                     $contrib{'message'} = '<version num="0">'.&HTML::Entities::encode($oldcontrib{$oldidx.':message'},'<>&"').'</version><version num="1">'.&HTML::Entities::encode($contrib{'message'},'<>&"').'</version>';
 2082:                 }
 2083:             }
 2084:             $contrib{'history'} .= $oldcontrib{$oldidx.':timestamp'};
 2085:             foreach (keys %contrib) {
 2086:                 my $key = $oldidx.':'.&Apache::lonnet::escape($oldsymb).':'.$_;                                                                               
 2087:                 $newcontrib{$key} = $contrib{$_};
 2088:             }
 2089:             my $put_reply = &Apache::lonnet::putstore($ENV{'request.course.id'},
 2090:                   \%newcontrib,
 2091:                   $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
 2092:                   $ENV{'course.'.$ENV{'request.course.id'}.'.num'});
 2093:             $status='Editing class discussion'.($anon?' (anonymous)':'');
 2094:         } else {
 2095:            $status='Adding to class discussion'.($anon?' (anonymous)':'').': '.
 2096:            &Apache::lonnet::store(\%contrib,$symb,$ENV{'request.course.id'},
 2097:                      $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
 2098: 		     $ENV{'course.'.$ENV{'request.course.id'}.'.num'});
 2099:         }
 2100:         my %storenewentry=($symb => time);
 2101:         $status.='<br />'.&mt('Updating discussion time').': '.
 2102:         &Apache::lonnet::put('discussiontimes',\%storenewentry,
 2103:                      $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
 2104: 		     $ENV{'course.'.$ENV{'request.course.id'}.'.num'});
 2105:     }
 2106:     my %record=&Apache::lonnet::restore('_discussion');
 2107:     my ($temp)=keys %record;
 2108:     unless ($temp=~/^error\:/) {
 2109:        my %newrecord=();
 2110:        $newrecord{'resource'}=$symb;
 2111:        $newrecord{'subnumber'}=$record{'subnumber'}+1;
 2112:        $status.='<br />'.&mt('Registering').': '.
 2113:                &Apache::lonnet::cstore(\%newrecord,'_discussion');
 2114:     }
 2115:     } else {
 2116: 	$status.='Failed.';
 2117:     }
 2118:     return $status.'<br />';   
 2119: }
 2120: 
 2121: # ----------------------------------------------------------- Preview function
 2122: 
 2123: sub show_preview {
 2124:     my $r=shift;
 2125:     my $message=&clear_out_html($ENV{'form.comment'});
 2126:     $message=~s/\n/\<br \/\>/g;
 2127:     $message=&Apache::lonspeller::markeduptext($message);
 2128:     $message=&Apache::lontexconvert::msgtexconverted($message);
 2129:     my $subject=&clear_out_html($ENV{'form.subject'});
 2130:     $subject=~s/\n/\<br \/\>/g;
 2131:     $subject=&Apache::lontexconvert::msgtexconverted($subject);
 2132:     $r->print('<table border="2"><tr><td>'.
 2133:        '<b>Subject:</b> '.$subject.'<br /><br />'.
 2134:        $message.'</td></tr></table>');
 2135: }
 2136: 
 2137: sub generate_preview_button {
 2138:     my $pre=&mt("Show Preview and Check Spelling");
 2139:     return(<<ENDPREVIEW);
 2140: <form name="preview" action="/adm/feedback?preview=1" method="post" target="preview">
 2141: <input type="hidden" name="subject">
 2142: <input type="hidden" name="comment" />
 2143: <input type="button" value="$pre"
 2144: onClick="if (typeof(document.mailform.onsubmit)!='undefined') {document.mailform.onsubmit();};this.form.comment.value=document.mailform.comment.value;this.form.subject.value=document.mailform.subject.value;this.form.submit();" />
 2145: </form>
 2146: ENDPREVIEW
 2147: }
 2148: 
 2149: sub modify_attachments {
 2150:     my ($r,$currnewattach,$currdelold,$symb,$idx,$attachmenturls)=@_;
 2151:     my $subject=&clear_out_html($ENV{'form.subject'});
 2152:     $subject=~s/\n/\<br \/\>/g;
 2153:     $subject=&Apache::lontexconvert::msgtexconverted($subject);
 2154:     my $timestamp=$ENV{'form.timestamp'};
 2155:     my $numoldver=$ENV{'form.numoldver'};
 2156:     my $bodytag=&Apache::loncommon::bodytag('Discussion Post Attachments',
 2157:                                           '','');
 2158:     my $msg = '';
 2159:     my %attachments = ();
 2160:     my %currattach = ();
 2161:     if ($idx) {
 2162:         &extract_attachments($attachmenturls,$idx,$numoldver,\$msg,\%attachments,\%currattach,$currdelold);
 2163:     }
 2164:     $r->print(<<END);
 2165: <html>
 2166: <head>
 2167: <title>Managing Attachments</title>
 2168: <script>
 2169:  function setAction () {
 2170:    document.modattachments.action = document.modattachments.origpage.value;
 2171:    document.modattachments.submit();
 2172:  }
 2173: </script> 
 2174: </head>
 2175: $bodytag
 2176: <form name="modattachments" method="post" enctype="multipart/form-data" action="/adm/feedback?attach=$symb">
 2177:  <table border="2">
 2178:   <tr>
 2179:    <td>
 2180:     <b>Subject:</b>$subject</b><br /><br />
 2181: END
 2182:     if ($idx) {
 2183:         if ($attachmenturls) {
 2184:             my @currold = keys %currattach;
 2185:             if (@currold > 0) {
 2186:                 $r->print("The following attachments were part of the most recent saved version of this posting.<br />Check the checkboxes for any you wish to remove<br />\n");  
 2187:                 foreach my $id (@currold) {
 2188:                     my $attachurl = &HTML::Entities::decode($attachments{$id}{'filename'}); 
 2189:                     $attachurl =~ m#/([^/]+)$#;
 2190:                     $r->print('<input type="checkbox" name="deloldattach" value="'.$id.'" />&nbsp;'.$1.'<br />'."\n");
 2191:                 }
 2192:                 $r->print("<br />");
 2193:             }
 2194:         }
 2195:     }
 2196:     if (@{$currnewattach} > 0) {
 2197:         $r->print("The following attachments have been uploaded for inclusion with this posting.<br />Check the checkboxes for any you wish to remove<br />\n");
 2198:         foreach (@{$currnewattach}) {
 2199:             $_ =~ m#/([^/]+)$#;
 2200:             $r->print('<input type="checkbox" name="delnewattach" value="'.$_.'" />&nbsp;'.$1.'<br />'."\n");
 2201:         }
 2202:         $r->print("<br />"); 
 2203:     }
 2204:     $r->print(<<END);
 2205:    Add a new attachment to this post.&nbsp;<input type="file" name="addnewattach" /><input type="button" name="upload" value="Upload" onClick="this.form.submit()" />    
 2206:    </td>
 2207:   </tr>
 2208:  </table>
 2209: <input type="hidden" name="subject" value="$ENV{'form.subject'}" />
 2210: <input type="hidden" name="comment" value="$ENV{'form.comment'}" />
 2211: <input type="hidden" name="timestamp" value="$ENV{'form.timestamp'}" />
 2212: <input type="hidden" name="idx" value="$ENV{'form.idx'}" />
 2213: <input type="hidden" name="numoldver" value="$ENV{'form.numoldver'}" />
 2214: <input type="hidden" name="origpage" value="$ENV{'form.origpage'}" />
 2215: <input type="hidden" name="anondiscuss" value="$ENV{'form.anondiscuss'}" />
 2216: <input type="hidden" name="discuss" value="$ENV{'form.discuss'}" />
 2217: END
 2218:     foreach (@{$currnewattach}) {
 2219:         $r->print('<input type="hidden" name="currnewattach" value="'.$_.'" />'."\n");
 2220:     }
 2221:     foreach (@{$currdelold}) {
 2222:         $r->print('<input type="hidden" name="deloldattach" value="'.$_.'" />'."\n");
 2223:     }
 2224:     $r->print(<<END);
 2225:  <input type="button" name="rtntoedit" value="Store Changes" onClick="setAction()"/>
 2226: </form>
 2227: </body>
 2228: </html>
 2229: END
 2230:     return;
 2231: }
 2232: 
 2233: sub process_attachments {
 2234:     my ($currnewattach,$currdelold,$keepold) = @_;
 2235:     if (exists($ENV{'form.currnewattach'})) {
 2236:         if (ref($ENV{'form.currnewattach'}) eq 'ARRAY') {
 2237:             @{$currnewattach} = @{$ENV{'form.currnewattach'}};
 2238:         } else {
 2239:             $$currnewattach[0] = $ENV{'form.currnewattach'};
 2240:         }
 2241:     }
 2242:     if (exists($ENV{'form.deloldattach'})) {
 2243:         if (ref($ENV{'form.deloldattach'}) eq 'ARRAY') {
 2244:             @{$currdelold} = @{$ENV{'form.deloldattach'}};
 2245:         } else {
 2246:             $$currdelold[0] = $ENV{'form.deloldattach'};
 2247:         }
 2248:     }
 2249:     if (exists($ENV{'form.delnewattach'})) {
 2250:         my @currdelnew = ();
 2251:         my @currnew = ();
 2252:         if (ref($ENV{'form.delnewattach'}) eq 'ARRAY') {
 2253:             @currdelnew = @{$ENV{'form.delnewattach'}};
 2254:         } else {
 2255:             $currdelnew[0] = $ENV{'form.delnewattach'};
 2256:         }
 2257:         foreach my $newone (@{$currnewattach}) {
 2258:             my $delflag = 0;
 2259:             foreach (@currdelnew) {
 2260:                 if ($newone eq $_) {
 2261:                     $delflag = 1;
 2262:                     last;
 2263:                 }
 2264:             }
 2265:             unless ($delflag) {
 2266:                 push @currnew, $newone;
 2267:             }
 2268:         }
 2269:         @{$currnewattach} = @currnew;
 2270:     }
 2271:     if (exists($ENV{'form.keepold'})) {
 2272:         if (ref($ENV{'form.keepold'}) eq 'ARRAY') {
 2273:             @{$keepold} = @{$ENV{'form.keepold'}};
 2274:         } else {
 2275:             $$keepold[0] = $ENV{'form.keepold'};
 2276:         }
 2277:     }
 2278: }
 2279: 
 2280: sub generate_attachments_button {
 2281:     my ($idx,$attachnum,$ressymb,$now,$currnewattach,$deloldattach,$numoldver,$mode) = @_;
 2282:     my $origpage = $ENV{'REQUEST_URI'};
 2283:     my $att=$attachnum.' '.&mt("attachments");
 2284:     my $response = (<<END);
 2285: <form name="attachment" action="/adm/feedback?attach=$ressymb" method="post">
 2286: Click to add/remove attachments:&nbsp;<input type="button" value="$att"
 2287: onClick="this.form.subject.value=document.mailform.subject.value;this.form.comment.value=document.mailform.comment.value;
 2288: END
 2289:     unless ($mode eq 'board') {
 2290:         $response .= 'javascript:anonchk();';
 2291:     }
 2292:     $response .= (<<ENDATTACH);
 2293: this.form.submit();" />
 2294: <input type="hidden" name="origpage" value="$origpage" />
 2295: <input type="hidden" name="idx" value="$idx" />
 2296: <input type="hidden" name="timestamp" value="$now" />
 2297: <input type="hidden" name="subject" />
 2298: <input type="hidden" name="comment" />
 2299: <input type="hidden" name="anondiscuss" value = "0";
 2300: <input type="hidden" name="discuss" value = "0";
 2301: <input type="hidden" name="numoldver" value="$numoldver" />
 2302: ENDATTACH
 2303:     if (defined($deloldattach)) {
 2304:         if (@{$deloldattach} > 0) {
 2305:             foreach (@{$deloldattach}) {
 2306:                 $response .= '<input type="hidden" name="deloldattach" value="'.$_.'" />'."\n";
 2307:             }
 2308:         }
 2309:     }
 2310:     if (defined($currnewattach)) {
 2311:         if (@{$currnewattach} > 0) {
 2312:             foreach (@{$currnewattach}) {
 2313:                 $response .= '<input type="hidden" name="currnewattach" value="'.$_.'" />'."\n";
 2314:             }
 2315:         }
 2316:     }
 2317:     $response .= '</form>';
 2318:     return $response;
 2319: }
 2320: 
 2321: sub extract_attachments {
 2322:     my ($attachmenturls,$idx,$numoldver,$message,$attachments,$currattach,$currdelold) = @_;
 2323:     if ($attachmenturls =~ m/^<attachment id="0">/) {
 2324:         &get_post_attachments($attachments,$attachmenturls);
 2325:         foreach my $id (sort keys %{$attachments}) {
 2326:             if (exists($$attachments{$id}{$numoldver})) {
 2327:                 if (defined($currdelold)) {
 2328:                     if (@{$currdelold} > 0) {
 2329:                         unless (grep/^$id$/,@{$currdelold}) {
 2330:                             $$currattach{$id} = $$attachments{$id}{$numoldver}; 
 2331:                         }
 2332:                     } else {
 2333:                         $$currattach{$id} = $$attachments{$id}{$numoldver};
 2334:                     }
 2335:                 } else {
 2336:                     $$currattach{$id} = $$attachments{$id}{$numoldver};
 2337:                 }
 2338:             }
 2339:         }
 2340:         my @attached = (sort { $a <=> $b } keys %{$currattach});
 2341:         if (@attached == 1) {
 2342:             my $id = $attached[0];
 2343:             my $attachurl = &HTML::Entities::decode($$attachments{$id}{'filename'});
 2344:             $attachurl=~m|/([^/]+)$|;
 2345:             $$message.='<br /><a href="'.$attachurl.'"><tt>'.
 2346:             $1.'</tt></a><br />';
 2347:             &Apache::lonnet::allowuploaded('/adm/feedback',
 2348:                                    $attachurl);
 2349:         } elsif (@attached > 1) {
 2350:             $$message.='<ol>';
 2351:             foreach (@attached) {
 2352:                 my $id = $_;
 2353:                 my $attachurl = &HTML::Entities::decode($$attachments{$id}{'filename'});
 2354:                 my ($fname)
 2355:                   =($attachurl=~m|/([^/]+)$|);
 2356:                 $$message .= '<li><a href="'.$attachurl.
 2357:                   '"><tt>'.
 2358:                   $fname.'</tt></a></li>';
 2359:                 &Apache::lonnet::allowuploaded('/adm/feedback',
 2360:                                  $attachurl);
 2361:             }
 2362:             $$message .= '</ol><br />';
 2363:         }
 2364:     } else {
 2365:         my ($fname)
 2366:            =($attachmenturls=~m|/([^/]+)$|);
 2367:         $$message .='<p>'.&mt('Attachment').
 2368:            ': <a href="'.$attachmenturls.
 2369:            '"><tt>'.
 2370:            $fname.'</tt></a></p>';
 2371:            $$attachments{0} = $attachmenturls;
 2372:            $$currattach{'0'} = 'n';
 2373:            &Apache::lonnet::allowuploaded('/adm/feedback',
 2374:                              $attachmenturls);
 2375:     }
 2376: }
 2377: 
 2378: sub construct_attachmenturl {
 2379:     my ($currnewattach,$keepold,$symb,$idx)=@_;
 2380:     my $oldattachmenturl;
 2381:     my $newattachmenturl;
 2382:     my $startnum = 0;
 2383:     my $currver = 0;
 2384:     if (($ENV{'form.editdisc'}) && ($idx)) {
 2385:         my %contrib=&Apache::lonnet::restore($symb,$ENV{'request.course.id'},
 2386:                        $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
 2387:                        $ENV{'course.'.$ENV{'request.course.id'}.'.num'});
 2388:         $oldattachmenturl = $contrib{$idx.':attachmenturl'};
 2389:         if ($contrib{$idx.':history'}) {
 2390:             if ($contrib{$idx.':history'} =~ /:/) {
 2391:                 my @oldversions = split/:/,$contrib{$idx.':history'};
 2392:                 $currver = 1 + scalar(@oldversions);
 2393:             } else {
 2394:                 $currver = 2;
 2395:             }
 2396:         } else {
 2397:             $currver = 1;
 2398:         }
 2399:         if ($oldattachmenturl) {
 2400:             if ($oldattachmenturl =~ m/^<attachment id="0">/) {
 2401:                 my %attachments = ();
 2402:                 my $prevver = $currver-1;
 2403:                 &get_post_attachments(\%attachments,$oldattachmenturl);
 2404:                 my $numattach = keys %attachments;
 2405:                 $startnum += $numattach;
 2406:                 foreach my $num (sort {$a <=> $b} keys %attachments) {
 2407:                     $newattachmenturl .= '<attachment id="'.$num.'"><filename>'.$attachments{$num}{'filename'}.'</filename>';
 2408:                     foreach (sort {$a <=> $b} keys %{$attachments{$num}}) { 
 2409:                         $newattachmenturl .= '<post id="'.$_.'">'.$attachments{$num}{$_}.'</post>';
 2410:                     }
 2411:                     if (grep/^$num$/,@{$keepold}) {
 2412:                         $newattachmenturl .= '<post id="'.$currver.'">'.$attachments{$num}{$prevver}.'</post>';
 2413:                     }
 2414:                     $newattachmenturl .= '</attachment>';
 2415:                 }
 2416:             } else {
 2417:                 $newattachmenturl = '<attachment id="0"><filename>'.&HTML::Entities::encode($oldattachmenturl).'<post id="0">n</post>';
 2418:                 unless (grep/^0$/,@{$keepold}) {
 2419:                     $newattachmenturl .= '<post id="1">n</post>';
 2420:                 }
 2421:                 $newattachmenturl .= '</attachment>';
 2422:                 $startnum ++;
 2423:             }
 2424:         }
 2425:     }
 2426:     for (my $i=0; $i<@{$currnewattach}; $i++) {
 2427:         my $attachnum = $startnum + $i;
 2428:         $newattachmenturl .= '<attachment id="'.$attachnum.'"><filename>'.&HTML::Entities::encode($$currnewattach[$i]).'</filename><post id="'.$currver.'">n</post></attachment>';
 2429:     }
 2430:     return $newattachmenturl; 
 2431: }
 2432:   
 2433: sub handler {
 2434:   my $r = shift;
 2435:   if ($r->header_only) {
 2436:      &Apache::loncommon::content_type($r,'text/html');
 2437:      $r->send_http_header;
 2438:      return OK;
 2439:   }
 2440: 
 2441: # --------------------------- Get query string for limited number of parameters
 2442: 
 2443:   &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
 2444:          ['hide','unhide','deldisc','postdata','preview','replydisc','editdisc','threadedon','threadedoff','onlyunread','allposts','onlyunmark','previous','markread','markonread','markondisp','toggoff','toggon','modifydisp','changes','navmaps','navurl','sortfilter','sortposts','applysort','rolefilter','statusfilter','sectionpick','posterlist','userpick','attach','origpage','currnewattach','deloldattach','keepold','allversions']);
 2445:   if ($ENV{'form.discsymb'}) {
 2446:       my $symb = $ENV{'form.discsymb'};
 2447:       my $readkey = $symb.'_read';
 2448:       my %readinghash = ();
 2449:       my $chgcount = 0;
 2450:       %readinghash = &Apache::lonnet::get('nohist_'.$ENV{'request.course.id'}.'_discuss',[$readkey],$ENV{'user.domain'},$ENV{'user.name'});
 2451:       foreach my $key (keys %ENV) {
 2452:           if ($key =~ m/^form\.postunread_(\d+)/) {
 2453:               if ($readinghash{$readkey} =~ /\.$1\./) {
 2454:                   $readinghash{$readkey} =~ s/\.$1\.//;
 2455:                   $chgcount ++;
 2456:               }
 2457:           } elsif ($key =~ m/^form\.postread_(\d+)/) {
 2458:               unless ($readinghash{$readkey} =~ /\.$1\./) {
 2459:                   $readinghash{$readkey} .= '.'.$1.'.';
 2460:                   $chgcount ++;
 2461:               }
 2462:           }
 2463:       }
 2464:       if ($chgcount > 0) {
 2465:           &Apache::lonnet::put('nohist_'.$ENV{'request.course.id'}.'_discuss',\%readinghash,$ENV{'user.domain'},$ENV{'user.name'});
 2466:       }
 2467:       &Apache::loncommon::content_type($r,'text/html');
 2468:       $r->send_http_header;
 2469:       my ($map,$ind,$url)=&Apache::lonnet::decode_symb($symb);
 2470:       my $previous=$ENV{'form.previous'};
 2471:       my $feedurl = &Apache::lonnet::clutter($url);
 2472:       &redirect_back($r,$feedurl,&mt('Marked postings read/unread').'<br />','0','0','',$previous,'','','',);
 2473:       return OK;
 2474:   }
 2475:   if ($ENV{'form.allversions'}) {
 2476:       &Apache::loncommon::content_type($r,'text/html');
 2477:       $r->send_http_header;
 2478:       my $bodytag=&Apache::loncommon::bodytag('Discussion Post Versions',
 2479:                                           '','');
 2480:       $r->print (<<END);
 2481: <html>
 2482: <head>
 2483: <title>Post Versions</title>
 2484: <meta http-equiv="pragma" content="no-cache" />
 2485: </head>
 2486: $bodytag
 2487: END
 2488:       my $crs='/'.$ENV{'request.course.id'};
 2489:       if ($ENV{'request.course.sec'}) {
 2490:           $crs.='_'.$ENV{'request.course.sec'};
 2491:       }
 2492:       $crs=~s/\_/\//g;
 2493:       my $seeid=&Apache::lonnet::allowed('rin',$crs);
 2494:       my ($symb,$idx)=split(/\:\:\:/,$ENV{'form.allversions'});
 2495:       my $ressymb=$symb;
 2496:       unless ($ressymb =~ m|bulletin___\d+___adm/wrapper|) {
 2497:           $ressymb=~s|(bulletin___\d+___)|$1adm/wrapper|;
 2498:       }
 2499:       if ($idx > 0) {
 2500:           my %contrib=&Apache::lonnet::restore($ressymb,$ENV{'request.course.id'},
 2501:                $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
 2502:                $ENV{'course.'.$ENV{'request.course.id'}.'.num'});
 2503:           if ($contrib{$idx.':history'}) {
 2504:               my $attachmenturls = $contrib{$idx.':attachmenturl'};
 2505:               my @postversions = ();
 2506:               my %messages = ();
 2507:               my %subjects = ();
 2508:               if ($contrib{$idx.':history'} =~ m/:/) {
 2509:                   @postversions = split/:/,$contrib{$idx.':history'};
 2510:               } else {
 2511:                   @postversions = ("$contrib{$idx.':history'}");
 2512:               }
 2513:               if (@postversions > 0) {
 2514:                   &get_post_versions(\%messages,$contrib{$idx.':message'});
 2515:                   &get_post_versions(\%subjects,$contrib{$idx.':subject'});
 2516:                   push @postversions,$contrib{$idx.':timestamp'};
 2517:                   my $screenname=&Apache::loncommon::screenname(
 2518:                                             $contrib{$idx.':sendername'},
 2519:                                             $contrib{$idx.':senderdomain'});
 2520:                   my $plainname=&Apache::loncommon::nickname(
 2521:                                             $contrib{$idx.':sendername'},
 2522:                                             $contrib{$idx.':senderdomain'});
 2523:                   my $sender=&Apache::loncommon::aboutmewrapper(
 2524:                                      $plainname,
 2525:                                      $contrib{$idx.':sendername'},
 2526:                                      $contrib{$idx.':senderdomain'}).' ('.
 2527:                                      $contrib{$idx.':sendername'}.' at '.
 2528:                                      $contrib{$idx.':senderdomain'}.')';
 2529:                   if ($contrib{$idx.':anonymous'}) {
 2530:                       $sender.=' ['.&mt('anonymous').'] '.$screenname;
 2531:                   }
 2532:                   $r->print('<b>'.$sender.'</b><br /><ul>');
 2533:                   for (my $i=0; $i<@postversions; $i++) {
 2534:                       my ($timesent,$message,$subject,$attachmsg);
 2535:                       $timesent = &Apache::lonlocal::locallocaltime($postversions[$i]);
 2536:                       $message=&HTML::Entities::decode($messages{$i});
 2537:                       $subject=&HTML::Entities::decode($subjects{$i});
 2538:                       $message=~s/\n/\<br \/\>/g;
 2539:                       $message=&Apache::lontexconvert::msgtexconverted($message);
 2540:                       $subject=~s/\n/\<br \/\>/g;
 2541:                       $subject=&Apache::lontexconvert::msgtexconverted($subject);
 2542:                       if ($attachmenturls) {
 2543:                           my %attachments = ();
 2544:                           my %currattach = ();
 2545:                           &extract_attachments($attachmenturls,$idx,$i,\$attachmsg,\%attachments,\%currattach);
 2546:                       }
 2547:                       if ($attachmsg) {
 2548:                           $attachmsg = '<br />Attachments:'.$attachmsg.'<br />';
 2549:                       } else {
 2550:                           $attachmsg = '<br />';
 2551:                       }
 2552:                       $r->print (<<END);
 2553: <li><b>$subject</b>, $timesent<br />
 2554: $message<br />
 2555: $attachmsg</li>
 2556: END
 2557:                   }
 2558:                   $r->print('</ul></body></html>');
 2559:               }
 2560:           }
 2561:       }
 2562:       return OK;
 2563:   }
 2564:   if ($ENV{'form.posterlist'}) {
 2565:       &Apache::loncommon::content_type($r,'text/html');
 2566:       $r->send_http_header;
 2567:       my $symb=$ENV{'form.posterlist'};
 2568:       my $sortposts = $ENV{'form.sortposts'};
 2569:       my ($map,$ind,$url)=&Apache::lonnet::decode_symb($symb);
 2570:       my $previous=$ENV{'form.previous'};
 2571:       my $feedurl = &Apache::lonnet::clutter($url);
 2572:  # backward compatibility (bulletin boards used to be 'wrapped')
 2573:       if ($feedurl=~m|^/adm/wrapper/adm/.*/bulletinboard$|) {
 2574:           $feedurl=~s|^/adm/wrapper||;
 2575:       }
 2576:       &print_showposters($r,$symb,$previous,$feedurl,$sortposts);
 2577:       return OK;
 2578:   }
 2579:   if ($ENV{'form.userpick'}) {
 2580:       &Apache::loncommon::content_type($r,'text/html');
 2581:       $r->send_http_header;
 2582:       my $symb=$ENV{'form.userpick'};
 2583:       my ($map,$ind,$url)=&Apache::lonnet::decode_symb($symb);
 2584:       my $previous=$ENV{'form.previous'};
 2585: # backward compatibility (bulletin boards used to be 'wrapped')
 2586:       my $ressymb=$symb;
 2587:       unless ($ressymb =~ m|bulletin___\d+___adm/wrapper|) {
 2588:           $ressymb=~s|(bulletin___\d+___)|$1adm/wrapper|;
 2589:       }
 2590:       my $sort=$ENV{'form.sortposts'};
 2591:       my @posters = ();
 2592:       if (ref($ENV{'form.stuinfo'}) eq 'ARRAY') {
 2593:           @posters = $ENV{'form.stuinfo'};
 2594:       } else {
 2595:           $posters[0] = $ENV{'form.stuinfo'};
 2596:       }
 2597:       my $numpicks = @posters;
 2598:       if (defined($ENV{'form.userpick'})) {
 2599:           my %discinfo = ();
 2600:           $discinfo{$ressymb.'_userpick'} = join('&',@posters);
 2601:           &Apache::lonnet::put('nohist_'.$ENV{'request.course.id'}.'_discuss',\%discinfo,$ENV{'user.domain'},$ENV{'user.name'});
 2602:       }
 2603:       my $feedurl = &Apache::lonnet::clutter($url);
 2604:  # backward compatibility (bulletin boards used to be 'wrapped')
 2605:       if ($feedurl=~m|^/adm/wrapper/adm/.*/bulletinboard$|) {
 2606:           $feedurl=~s|^/adm/wrapper||;
 2607:       }
 2608:       &redirect_back($r,$feedurl,&mt('Changed sort/filter').'<br />','0','0','',$previous,$sort,'','','',$numpicks);
 2609:       return OK;
 2610:   }
 2611:   if ($ENV{'form.applysort'}) {
 2612:       &Apache::loncommon::content_type($r,'text/html');
 2613:       $r->send_http_header;
 2614:       my $symb=$ENV{'form.applysort'};
 2615:       my ($map,$ind,$url)=&Apache::lonnet::decode_symb($symb);
 2616:       my $previous=$ENV{'form.previous'};
 2617:       my $sort = $ENV{'form.sortposts'};
 2618:       my $rolefilter = $ENV{'form.rolefilter'};
 2619:       my $statusfilter = $ENV{'form.statusfilter'};
 2620:       my $secpick = $ENV{'form.sectionpick'};
 2621:       my $feedurl = &Apache::lonnet::clutter($url);
 2622:  # backward compatibility (bulletin boards used to be 'wrapped')
 2623:       if ($feedurl=~m|^/adm/wrapper/adm/.*/bulletinboard$|) {
 2624:           $feedurl=~s|^/adm/wrapper||;
 2625:       }
 2626:       &redirect_back($r,$feedurl,&mt('Changed sort/filter').'<br />','0','0','',$previous,$sort,$rolefilter,$statusfilter,$secpick);
 2627:       return OK;
 2628:   } elsif ($ENV{'form.sortfilter'}) {
 2629:       &Apache::loncommon::content_type($r,'text/html');
 2630:       $r->send_http_header;
 2631:       my $symb=$ENV{'form.sortfilter'};
 2632:       my ($map,$ind,$url)=&Apache::lonnet::decode_symb($symb);
 2633:       my $previous=$ENV{'form.previous'};
 2634:       my $feedurl = &Apache::lonnet::clutter($url);
 2635:  # backward compatibility (bulletin boards used to be 'wrapped')
 2636:       if ($feedurl=~m|^/adm/wrapper/adm/.*/bulletinboard$|) {
 2637:           $feedurl=~s|^/adm/wrapper||;
 2638:       }
 2639:       &print_sortfilter_options($r,$symb,$previous,$feedurl);
 2640:       return OK;
 2641:   } elsif ($ENV{'form.navmaps'}) {
 2642:       my %discinfo = ();
 2643:       my @resources = ();
 2644:       if ($ENV{'form.navmaps'} =~ /:/) {
 2645:           @resources = split/:/,$ENV{'form.navmaps'};
 2646:       } else {
 2647:           @resources = ("$ENV{'form.navmaps'}");
 2648:       }
 2649:       my $numitems = @resources;
 2650:       my $feedurl = '/adm/navmaps';
 2651:       if ($ENV{'form.navurl'}) {
 2652:           $feedurl .= '?'.$ENV{'form.navurl'};
 2653:       }
 2654:       my %lt = &Apache::lonlocal::texthash(
 2655:           'mnpa' => 'Marked "New" posts as read in a total of',
 2656:           'robb' => 'resources/bulletin boards.'
 2657:       );       
 2658:       foreach (@resources) {
 2659: # backward compatibility (bulletin boards used to be 'wrapped')
 2660:           my $ressymb=$_;
 2661:           if ($ressymb =~ m/bulletin___\d+___/) {
 2662:               unless ($ressymb =~ m|bulletin___\d+___adm/wrapper|) {
 2663:                   $ressymb=~s|(bulletin___\d+___)|$1adm/wrapper/|;
 2664:               }
 2665:           }
 2666:           my $lastkey = $ressymb.'_lastread';
 2667:           $discinfo{$lastkey} = time;
 2668:       }
 2669:       &Apache::lonnet::put('nohist_'.$ENV{'request.course.id'}.'_discuss',\%discinfo,$ENV{'user.domain'},$ENV{'user.name'});
 2670:       &Apache::loncommon::content_type($r,'text/html');
 2671:       $r->send_http_header;
 2672:       $r->print (<<ENDREDIR);
 2673: <html>
 2674: <head>
 2675: <title>New posts marked as read</title>
 2676: <meta http-equiv="pragma" content="no-cache" />
 2677: <meta HTTP-EQUIV="Refresh" CONTENT="2; url=$feedurl" />
 2678: </head>
 2679: <body bgcolor="#FFFFFF" onLoad='if (window.name!="loncapaclient") { this.document.reldt.submit(); self.close(); }'>
 2680: <img align="right" src="/adm/lonIcons/lonlogos.gif" />
 2681: <b>$lt{'mnpa'} $numitems $lt{'robb'}</b>
 2682: <form name="reldt" action="$feedurl" target="loncapaclient">
 2683: </form>
 2684: </body>
 2685: </html>
 2686: ENDREDIR
 2687:       return OK;
 2688:   } elsif ($ENV{'form.modifydisp'}) {
 2689:       &Apache::loncommon::content_type($r,'text/html');
 2690:       $r->send_http_header;
 2691:       my $symb=$ENV{'form.modifydisp'};
 2692:       my ($map,$ind,$url)=&Apache::lonnet::decode_symb($symb);
 2693:       my $previous=$ENV{'form.previous'};
 2694:       my ($dispchgA,$dispchgB,$markchg,$toggchg) = split/_/,$ENV{'form.changes'};
 2695:       my $feedurl = &Apache::lonnet::clutter($url);
 2696:  # backward compatibility (bulletin boards used to be 'wrapped')  
 2697:       if ($feedurl=~m|^/adm/wrapper/adm/.*/bulletinboard$|) {
 2698:           $feedurl=~s|^/adm/wrapper||;
 2699:       }
 2700:       &print_display_options($r,$symb,$previous,$dispchgA,$dispchgB,$markchg,$toggchg,$feedurl);
 2701:       return OK;
 2702:   } elsif (($ENV{'form.markondisp'}) || ($ENV{'form.markonread'}) || ($ENV{'form.allposts'}) || ($ENV{'form.onlyunread'}) || $ENV{'form.onlyunmark'} || $ENV{'form.toggoff'} || $ENV{'form.toggon'} ) {
 2703:       &Apache::loncommon::content_type($r,'text/html');
 2704:       $r->send_http_header;
 2705:       my $previous=$ENV{'form.previous'};
 2706:       my ($map,$ind,$url);
 2707:       if ( ($ENV{'form.toggoff'}) || ($ENV{'form.toggon'}) ) {
 2708: # ------------------------------ Modify setting for read/unread toggle for each post 
 2709:           my $symb=$ENV{'form.toggoff'}?$ENV{'form.toggoff'}:$ENV{'form.toggon'};
 2710:           my $ressymb = $symb;
 2711:           ($map,$ind,$url)=&Apache::lonnet::decode_symb($symb);
 2712:           unless ($ressymb =~ m|bulletin___\d+___adm/wrapper|) {
 2713:               $ressymb=~s|(bulletin___\d+___)|$1adm/wrapper|;
 2714:           }
 2715:           my %discinfo = ();
 2716:           my $toggkey = $ressymb.'_readtoggle';
 2717:           if ($ENV{'form.toggon'}) {
 2718:               $discinfo{$toggkey} = 1;
 2719:           } elsif ($ENV{'form.toggoff'}) {
 2720:               $discinfo{$toggkey} = 0;
 2721:           }
 2722:           &Apache::lonnet::put('nohist_'.$ENV{'request.course.id'}.'_discuss',\%discinfo,$ENV{'user.domain'},$ENV{'user.name'});
 2723:       }
 2724:       if (($ENV{'form.markondisp'}) || ($ENV{'form.markonread'})) {
 2725: # ---------------------- Modify setting for identification of 'NEW' posts in this discussion
 2726:           my $symb=$ENV{'form.markondisp'}?$ENV{'form.markondisp'}:$ENV{'form.markonread'};
 2727:           my $ressymb = $symb;
 2728:           ($map,$ind,$url)=&Apache::lonnet::decode_symb($symb);
 2729:           unless ($ressymb =~ m|bulletin___\d+___adm/wrapper|) {
 2730:               $ressymb=~s|(bulletin___\d+___)|$1adm/wrapper|;
 2731:           }
 2732:           my %discinfo = ();
 2733:           my $lastkey = $ressymb.'_lastread';
 2734:           my $ondispkey = $ressymb.'_markondisp';
 2735:           if ($ENV{'form.markondisp'}) {
 2736:               $discinfo{$lastkey} = time;
 2737:               $discinfo{$ondispkey} = 1;
 2738:           } elsif ($ENV{'form.markonread'}) {
 2739:               if ( $previous > 0 ) {
 2740:                   $discinfo{$lastkey} = $previous;
 2741:               }
 2742:               $discinfo{$ondispkey} = 0;
 2743:           }
 2744:           &Apache::lonnet::put('nohist_'.$ENV{'request.course.id'}.'_discuss',\%discinfo,$ENV{'user.domain'},$ENV{'user.name'});
 2745:       }
 2746:       if (($ENV{'form.allposts'}) || ($ENV{'form.onlyunread'}) || ($ENV{'form.onlyunmark'}) ) {
 2747: # ----------------------------------------------------------------- Modify display setting for this discussion 
 2748:           my $symb;
 2749:           if ($ENV{'form.allposts'}) {
 2750:               $symb = $ENV{'form.allposts'};
 2751:           } elsif ($ENV{'form.onlyunread'}) {
 2752:               $symb = $ENV{'form.onlyunread'};
 2753:           } else {
 2754:               $symb = $ENV{'form.onlyunmark'};
 2755:           }
 2756:           my $ressymb = $symb;
 2757:           ($map,$ind,$url)=&Apache::lonnet::decode_symb($symb);
 2758:           unless ($ressymb =~ m|bulletin___\d+___adm/wrapper|) {
 2759:               $ressymb=~s|(bulletin___\d+___)|$1adm/wrapper|;
 2760:           }
 2761:           my %discinfo = ();
 2762:           if ($ENV{'form.allposts'}) {
 2763:               $discinfo{$ressymb.'_showonlyunread'} = 0;
 2764:               $discinfo{$ressymb.'_showonlyunmark'} = 0;
 2765:           } elsif ($ENV{'form.onlyunread'}) {
 2766:               $discinfo{$ressymb.'_showonlyunread'} = 1;
 2767:           } else {
 2768:               $discinfo{$ressymb.'_showonlyunmark'} = 1;
 2769:           }
 2770:           &Apache::lonnet::put('nohist_'.$ENV{'request.course.id'}.'_discuss',\%discinfo,$ENV{'user.domain'},$ENV{'user.name'});
 2771:       }
 2772:       if (($ENV{'form.markonread'}) || ($ENV{'form.allposts'}) || ($ENV{'form.onlyunread'}) || ($ENV{'form.onlyunmark'}) ||($ENV{'form.toggoff'}) || ($ENV{'form.toggon'}) ) {
 2773:           &redirect_back($r,&Apache::lonnet::clutter($url),&mt('Changed display status').'<br />','0','0','',$previous);
 2774:       } else {
 2775:           &redirect_back($r,&Apache::lonnet::clutter($url),&mt('Changed display status').'<br />','0','0');
 2776:       }
 2777:       return OK;
 2778:   } elsif ($ENV{'form.markread'}) {
 2779: # ----------------------------------------------------------------- Mark new posts not NEW 
 2780:       &Apache::loncommon::content_type($r,'text/html');
 2781:       $r->send_http_header;
 2782:       my $symb=$ENV{'form.markread'};
 2783:       my $ressymb = $symb;
 2784:       my ($map,$ind,$url)=&Apache::lonnet::decode_symb($symb);
 2785:       unless ($ressymb =~ m|bulletin___\d+___adm/wrapper|) {
 2786:           $ressymb=~s|(bulletin___\d+___)|$1adm/wrapper|;
 2787:       }
 2788:       my %discinfo = ();
 2789:       my $lastkey = $ressymb.'_lastread';
 2790:       $discinfo{$lastkey} = time;
 2791:       &Apache::lonnet::put('nohist_'.$ENV{'request.course.id'}.'_discuss',\%discinfo,$ENV{'user.domain'},$ENV{'user.name'});
 2792:       &redirect_back($r,&Apache::lonnet::clutter($url),&mt('Changed reading status').'<br />','0','0');
 2793:       return OK;
 2794:   } elsif (($ENV{'form.hide'}) || ($ENV{'form.unhide'})) {
 2795: # ----------------------------------------------------------------- Hide/unhide
 2796:     &Apache::loncommon::content_type($r,'text/html');
 2797:     $r->send_http_header;
 2798: 
 2799:     my $entry=$ENV{'form.hide'}?$ENV{'form.hide'}:$ENV{'form.unhide'};
 2800: 
 2801:     my ($symb,$idx)=split(/\:\:\:/,$entry);
 2802:     my ($map,$ind,$url)=&Apache::lonnet::decode_symb($symb);
 2803: 
 2804:     my %contrib=&Apache::lonnet::restore($symb,$ENV{'request.course.id'},
 2805:                      $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
 2806: 		     $ENV{'course.'.$ENV{'request.course.id'}.'.num'});
 2807: 
 2808:         
 2809:     my $currenthidden=$contrib{'hidden'};
 2810:     my $currentstudenthidden=$contrib{'studenthidden'};
 2811: 
 2812:     my $crs='/'.$ENV{'request.course.id'};
 2813:     if ($ENV{'request.course.sec'}) {
 2814:         $crs.='_'.$ENV{'request.course.sec'};
 2815:     }
 2816:     $crs=~s/\_/\//g;
 2817:     my $seeid=&Apache::lonnet::allowed('rin',$crs);
 2818:     
 2819:     if ($ENV{'form.hide'}) {
 2820: 	$currenthidden.='.'.$idx.'.';
 2821:         unless ($seeid) {
 2822:             $currentstudenthidden.='.'.$idx.'.';
 2823:         }
 2824:     } else {
 2825:         $currenthidden=~s/\.$idx\.//g;
 2826:     }
 2827:     my %newhash=('hidden' => $currenthidden);
 2828:     if ( ($ENV{'form.hide'}) && (!$seeid) ) {
 2829:         $newhash{'studenthidden'} = $currentstudenthidden;
 2830:     }
 2831: 
 2832:     &Apache::lonnet::store(\%newhash,$symb,$ENV{'request.course.id'},
 2833:                      $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
 2834: 		     $ENV{'course.'.$ENV{'request.course.id'}.'.num'});
 2835: 
 2836:     &redirect_back($r,&Apache::lonnet::clutter($url),
 2837:        &mt('Changed discussion status').'<br />','0','0','',$ENV{'form.previous'});
 2838:   } elsif (($ENV{'form.threadedon'}) || ($ENV{'form.threadedoff'})) {
 2839:       &Apache::loncommon::content_type($r,'text/html');
 2840:       $r->send_http_header;
 2841:       if ($ENV{'form.threadedon'}) {
 2842: 	  &Apache::lonnet::put('environment',{'threadeddiscussion' => 'on'});
 2843: 	  &Apache::lonnet::appenv('environment.threadeddiscussion' => 'on');
 2844:       } else {
 2845:  	  &Apache::lonnet::del('environment',['threadeddiscussion']);
 2846: 	  &Apache::lonnet::delenv('environment\.threadeddiscussion');
 2847:       }
 2848:       my $symb=$ENV{'form.threadedon'}?$ENV{'form.threadedon'}:$ENV{'form.threadedoff'};
 2849:       my ($map,$ind,$url)=&Apache::lonnet::decode_symb($symb);
 2850:       &redirect_back($r,&Apache::lonnet::clutter($url),
 2851: 		     &mt('Changed discussion view mode').'<br />','0','0','',$ENV{'form.previous'});
 2852:   } elsif ($ENV{'form.deldisc'}) {
 2853: # --------------------------------------------------------------- Hide for good
 2854:     &Apache::loncommon::content_type($r,'text/html');
 2855:     $r->send_http_header;
 2856: 
 2857:     my $entry=$ENV{'form.deldisc'};
 2858: 
 2859:     my ($symb,$idx)=split(/\:\:\:/,$entry);
 2860:     my ($map,$ind,$url)=&Apache::lonnet::decode_symb($symb);
 2861: 
 2862:     my %contrib=&Apache::lonnet::restore($symb,$ENV{'request.course.id'},
 2863:                      $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
 2864: 		     $ENV{'course.'.$ENV{'request.course.id'}.'.num'});
 2865: 
 2866:         
 2867:     my $currentdeleted=$contrib{'deleted'};
 2868:     
 2869:     $currentdeleted.='.'.$idx.'.';
 2870: 
 2871:     my %newhash=('deleted' => $currentdeleted);
 2872: 
 2873:     &Apache::lonnet::store(\%newhash,$symb,$ENV{'request.course.id'},
 2874:                      $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
 2875: 		     $ENV{'course.'.$ENV{'request.course.id'}.'.num'});
 2876: 
 2877:     &redirect_back($r,&Apache::lonnet::clutter($url),
 2878:        &mt('Changed discussion status').'<br />','0','0','',$ENV{'form.previous'});
 2879:   } elsif ($ENV{'form.preview'}) {
 2880: # -------------------------------------------------------- User wants a preview
 2881:       $r->content_type('text/html');
 2882:       $r->send_http_header;
 2883:       &show_preview($r);
 2884:   } elsif ($ENV{'form.attach'}) {
 2885: # -------------------------------------------------------- Work on attachments
 2886:       &Apache::loncommon::content_type($r,'text/html');
 2887:       $r->send_http_header;
 2888:       &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},['subject','comment','addnewattach','delnewattach','timestamp','numoldver','idx','anondiscuss','discuss']);
 2889:       my @currnewattach = ();
 2890:       my @currdelold = ();
 2891:       my @keepold = ();
 2892:       &process_attachments(\@currnewattach,\@currdelold,\@keepold);
 2893:       if (exists($ENV{'form.addnewattach.filename'})) {
 2894:           unless (length($ENV{'form.addnewattach'})>131072) {
 2895:               my $subdir = 'feedback/'.$ENV{'form.timestamp'};
 2896:               my $newattachment=&Apache::lonnet::userfileupload('addnewattach',undef,$subdir);
 2897:               push @currnewattach, $newattachment;
 2898:           }
 2899:       }
 2900:       my $attachmenturls = '';
 2901:       my $idx = $ENV{'form.idx'};
 2902:       my $symb = $ENV{'form.attach'};
 2903:       if ($idx) {
 2904:           my %contrib=&Apache::lonnet::restore($symb,$ENV{'request.course.id'},
 2905:                          $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
 2906:                          $ENV{'course.'.$ENV{'request.course.id'}.'.num'});
 2907:           $attachmenturls = $contrib{$idx.':attachmenturl'};
 2908:       }
 2909:       &modify_attachments($r,\@currnewattach,\@currdelold,$symb,$idx,$attachmenturls);
 2910:   } elsif ($ENV{'form.chgreads'}) {
 2911:       &Apache::loncommon::content_type($r,'text/html');
 2912:       $r->send_http_header;
 2913:       my ($map,$ind,$url)=&Apache::lonnet::decode_symb($ENV{'form.chgreads'});
 2914:       &redirect_back($r,&Apache::lonnet::clutter($url),
 2915:        &mt('Changed read status').'<br />','0','0');
 2916:   } else {
 2917: # ------------------------------------------------------------- Normal feedback
 2918:   my $feedurl=$ENV{'form.postdata'};
 2919:   $feedurl=~s/^http\:\/\///;
 2920:   $feedurl=~s/^$ENV{'SERVER_NAME'}//;
 2921:   $feedurl=~s/^$ENV{'HTTP_HOST'}//;
 2922:   $feedurl=~s/\?.+$//;
 2923: 
 2924:   my $symb;
 2925:   if ($ENV{'form.replydisc'}) {
 2926:       $symb=(split(/\:\:\:/,$ENV{'form.replydisc'}))[0];
 2927:       my ($map,$id,$url)=&Apache::lonnet::decode_symb($symb);
 2928:       $feedurl=&Apache::lonnet::clutter($url);
 2929:   } elsif ($ENV{'form.editdisc'}) {
 2930:       $symb=(split(/\:\:\:/,$ENV{'form.editdisc'}))[0];
 2931:       my ($map,$id,$url)=&Apache::lonnet::decode_symb($symb);
 2932:       $feedurl=&Apache::lonnet::clutter($url);
 2933:   } elsif ($ENV{'form.origpage'}) {
 2934:       $symb=""; 
 2935:   } else {
 2936:       $symb=&Apache::lonnet::symbread($feedurl);
 2937:   }
 2938:   unless ($symb) {
 2939:       $symb=$ENV{'form.symb'};
 2940:       if ($symb) {
 2941: 	  my ($map,$id,$url)=&Apache::lonnet::decode_symb($symb);
 2942:           $feedurl=&Apache::lonnet::clutter($url);
 2943:       }
 2944:   }
 2945:   my $goahead=1;
 2946:   if ($feedurl=~/\.(problem|exam|quiz|assess|survey|form)$/) {
 2947:       unless ($symb) { $goahead=0; }
 2948:   }
 2949:   # backward compatibility (bulletin boards used to be 'wrapped')
 2950:   if ($feedurl=~m|^/adm/wrapper/adm/.*/bulletinboard$|) {
 2951:       $feedurl=~s|^/adm/wrapper||;
 2952:   }
 2953:   if ($goahead) {
 2954: # Go ahead with feedback, no ambiguous reference
 2955:     &Apache::loncommon::content_type($r,'text/html');
 2956:     $r->send_http_header;
 2957:   
 2958:     if (
 2959:       (
 2960:        ($feedurl=~m:^/res:) && ($feedurl!~m:^/res/adm:)
 2961:       ) 
 2962:       || 
 2963:       ($ENV{'request.course.id'} && ($feedurl!~m:^/adm:))
 2964:       ||
 2965:       ($ENV{'request.course.id'} && ($symb=~/^bulletin\_\_\_/))
 2966:      ) {
 2967: # --------------------------------------------------- Print login screen header
 2968:     unless ($ENV{'form.sendit'}) {
 2969:       my $options=&screen_header($feedurl);
 2970:       if ($options) {
 2971:         &mail_screen($r,$feedurl,$options);
 2972:       } else {
 2973: 	&fail_redirect($r,$feedurl);
 2974:       }
 2975:     } else {
 2976:       
 2977: # Get previous user input
 2978:       my $prevattempts=&Apache::loncommon::get_previous_attempt(
 2979:             $symb,$ENV{'user.name'},$ENV{'user.domain'},
 2980:             $ENV{'request.course.id'});
 2981: 
 2982: # Get output from resource
 2983:       my $usersaw=&resource_output($feedurl);
 2984: 
 2985: # Get resource answer (need to allow student to view grades for this to work)
 2986:       &Apache::lonnet::appenv(('allowed.vgr'=>'F'));
 2987:       my $useranswer=&Apache::loncommon::get_student_answers(
 2988:                        $symb,$ENV{'user.name'},$ENV{'user.domain'},
 2989: 		       $ENV{'request.course.id'});
 2990:       &Apache::lonnet::delenv('allowed.vgr');
 2991: # Get attachments, if any, and not too large
 2992:       my $attachmenturl='';
 2993:       if (($ENV{'form.origpage'}) || ($ENV{'form.editdisc'}) || ($ENV{'form.replydisc'})) {
 2994:           my ($symb,$idx);
 2995:           if ($ENV{'form.replydisc'}) {
 2996:               ($symb,$idx)=split(/\:\:\:/,$ENV{'form.replydisc'});
 2997:           } elsif ($ENV{'form.editdisc'}) {
 2998:               ($symb,$idx)=split(/\:\:\:/,$ENV{'form.editdisc'});
 2999:           } elsif ($ENV{'form.origpage'}) {
 3000:               $symb = $ENV{'form.symb'};
 3001:           }
 3002:           my @currnewattach = ();
 3003:           my @deloldattach = ();
 3004:           my @keepold = ();
 3005:           &process_attachments(\@currnewattach,\@deloldattach,\@keepold);
 3006:           $symb=~s|(bulletin___\d+___)adm/wrapper/|$1|;
 3007:           $attachmenturl=&construct_attachmenturl(\@currnewattach,\@keepold,$symb,$idx);
 3008:       } elsif ($ENV{'form.attachment.filename'}) {
 3009: 	  unless (length($ENV{'form.attachment'})>131072) {
 3010: 	      $attachmenturl=&Apache::lonnet::userfileupload('attachment',undef,'feedback');
 3011: 	  }
 3012:       }
 3013: # Filter HTML out of message (could be nasty)
 3014:       my $message=&clear_out_html($ENV{'form.comment'});
 3015: 
 3016: # Assemble email
 3017:       my ($email,$citations)=&assemble_email($feedurl,$message,$prevattempts,
 3018:           $usersaw,$useranswer);
 3019:  
 3020: # Who gets this?
 3021:       my ($typestyle,%to) = &decide_receiver($feedurl);
 3022: 
 3023: # Actually send mail
 3024:       my ($status,$numsent)=&send_msg($feedurl,$email,$citations,
 3025:           $attachmenturl,%to);
 3026: 
 3027: # Discussion? Store that.
 3028: 
 3029:       my $numpost=0;
 3030:       if ($ENV{'form.discuss'}) {
 3031:           my $subject = &clear_out_html($ENV{'form.subject'});
 3032: 	  $typestyle.=&adddiscuss($symb,$message,0,$attachmenturl,$subject);
 3033: 	  $numpost++;
 3034:       }
 3035: 
 3036:       if ($ENV{'form.anondiscuss'}) {
 3037:           my $subject = &clear_out_html($ENV{'form.subject'});
 3038: 	  $typestyle.=&adddiscuss($symb,$message,1,$attachmenturl,$subject);
 3039: 	  $numpost++;
 3040:       }
 3041: 
 3042: 
 3043: # Receipt screen and redirect back to where came from
 3044:       &redirect_back($r,$feedurl,$typestyle,$numsent,$numpost,$status,$ENV{'form.previous'});
 3045: 
 3046:     }
 3047:    } else {
 3048: # Unable to give feedback
 3049:     &no_redirect_back($r,$feedurl);
 3050:    }
 3051:   } else {
 3052: # Ambiguous Problem Resource
 3053:       if ( &Apache::lonnet::mod_perl_version() == 2 ) {
 3054: 	  &Apache::lonnet::cleanenv();
 3055:       }
 3056:       $r->internal_redirect('/adm/ambiguous');
 3057:   }
 3058: }
 3059:   return OK;
 3060: } 
 3061: 
 3062: 1;
 3063: __END__

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>