File:  [LON-CAPA] / loncom / interface / lonfeedback.pm
Revision 1.167: download - view: text, annotated - select for diffs
Mon Jun 6 20:40:01 2005 UTC (18 years, 11 months ago) by www
Branches: MAIN
CVS tags: version_2_0_1, version_2_0_0, version_1_99_3, version_1_99_2, version_1_99_1, version_1_99_0, HEAD
Bug #3945: there have been enough complaints from authors by now ...

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

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