File:  [LON-CAPA] / loncom / interface / lonfeedback.pm
Revision 1.232: download - view: text, annotated - select for diffs
Fri Dec 8 20:09:29 2006 UTC (17 years, 7 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
Bug 4972

"Send Message" link now displayed when discussion is closed and hidden.

Also shown when discussion is blocked.

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

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