File:  [LON-CAPA] / loncom / interface / lonfeedback.pm
Revision 1.169: download - view: text, annotated - select for diffs
Tue Oct 4 18:27:32 2005 UTC (18 years, 8 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- remove course_id as an argument to loncoursedata::get_classlist

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

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