File:  [LON-CAPA] / loncom / interface / lonfeedback.pm
Revision 1.133: download - view: text, annotated - select for diffs
Mon Nov 15 21:35:37 2004 UTC (19 years, 6 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- even more better encrypturl support in discussions

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

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