File:  [LON-CAPA] / loncom / interface / lonfeedback.pm
Revision 1.246: download - view: text, annotated - select for diffs
Tue Mar 20 00:08:09 2007 UTC (17 years, 2 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- error checking course email addresses
   - both at the setting stage for proper form and that the user exists
   - at the sending stage for proper form

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

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