File:  [LON-CAPA] / loncom / interface / lonfeedback.pm
Revision 1.224: download - view: text, annotated - select for diffs
Thu Nov 30 04:05:47 2006 UTC (17 years, 6 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
Posts to bulletin boards can now be added to a user's course blog (previously this was only the case for replies).

State of "Add to course blog" checkbox is preserved when user uploads attachments at secondary screen before returning to main composition screen to submit post.

Fixed a typo in &mt('Re: ').

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

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