File:  [LON-CAPA] / loncom / interface / lonfeedback.pm
Revision 1.275: download - view: text, annotated - select for diffs
Thu Jul 9 12:53:28 2009 UTC (14 years, 11 months ago) by kalberla
Branches: MAIN
CVS tags: bz2851, HEAD
Moved blocking status table to popup.

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

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