File:  [LON-CAPA] / loncom / interface / lonfeedback.pm
Revision 1.126: download - view: text, annotated - select for diffs
Wed Sep 15 06:17:11 2004 UTC (19 years, 8 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- eliminate the get call on disc prefs, already in $ENV

-  elimintate the screnname/plainname calls already have them in the message

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

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