File:  [LON-CAPA] / loncom / interface / lonfeedback.pm
Revision 1.274: download - view: text, annotated - select for diffs
Thu May 28 19:38:33 2009 UTC (14 years, 11 months ago) by kalberla
Branches: MAIN
CVS tags: bz5969, HEAD, BZ5971-printing-apage
Changes on feedback_link bar. Now even shown when "Post Discussion" link is not shown.

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

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