File:  [LON-CAPA] / loncom / interface / lonfeedback.pm
Revision 1.273.4.1: download - view: text, annotated - select for diffs
Mon Oct 12 18:51:43 2009 UTC (14 years, 7 months ago) by raeburn
Branches: version_2_9_X
CVS tags: version_2_8_99_0, GCI_2
Diff to branchpoint 1.273: preferred, unified
- Backport 1.277.

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

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