File:  [LON-CAPA] / loncom / interface / lonfeedback.pm
Revision 1.370.2.1: download - view: text, annotated - select for diffs
Tue May 30 02:17:45 2017 UTC (6 years, 11 months ago) by raeburn
Branches: version_2_11_X
CVS tags: version_2_11_2_uiuc, version_2_11_2_msu, version_2_11_2_educog, version_2_11_2
- For 2.11
  - Backport 1.373

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

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