File:  [LON-CAPA] / loncom / interface / lonfeedback.pm
Revision 1.115: download - view: text, annotated - select for diffs
Tue Aug 10 18:25:53 2004 UTC (19 years, 10 months ago) by sakharuk
Branches: MAIN
CVS tags: HEAD
Cleanup for bug 2838 is done.

    1: # The LearningOnline Network
    2: # Feedback
    3: #
    4: # $Id: lonfeedback.pm,v 1.115 2004/08/10 18:25:53 sakharuk 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::lonhtmlcommon();
   39: use HTML::LCParser();
   40: use Apache::lonspeller();
   41: 
   42: sub discussion_open {
   43:     my ($status)=@_;
   44:     if (defined($status) &&
   45: 	!($status eq 'CAN_ANSWER' || $status eq 'CANNOT_ANSWER'
   46: 	  || $status eq 'OPEN')) {
   47: 	return 0;
   48:     }
   49:     my $close=&Apache::lonnet::EXT('resource.0.discussend');
   50:     if (defined($close) && $close ne '' && $close < time) {
   51: 	return 0;
   52:     }
   53:     return 1;
   54: }
   55: 
   56: sub discussion_visible {
   57:     my ($status)=@_;
   58:     if (not &discussion_open($status)) {
   59: 	my $hidden=&Apache::lonnet::EXT('resource.0.discusshide');
   60: 	if (lc($hidden) eq 'yes' or $hidden eq '' or !defined($hidden))  {
   61: 	    return 0;
   62: 	}
   63:     }
   64:     return 1;
   65: }
   66: 
   67: sub list_discussion {
   68:     my ($mode,$status,$symb)=@_;
   69:     my $outputtarget=$ENV{'form.grade_target'};
   70:     if (not &discussion_visible($status)) { return ''; }
   71:     my @bgcols = ("#cccccc","#eeeeee");
   72:     my $discussiononly=0;
   73:     if ($mode eq 'board') { $discussiononly=1; }
   74:     unless ($ENV{'request.course.id'}) { return ''; }
   75:     my $crs='/'.$ENV{'request.course.id'};
   76:     my $cid=$ENV{'request.course.id'};
   77:     if ($ENV{'request.course.sec'}) {
   78: 	$crs.='_'.$ENV{'request.course.sec'};
   79:     }                 
   80:     $crs=~s/\_/\//g;
   81:     unless ($symb) {
   82: 	$symb=&Apache::lonnet::symbread();
   83:     }
   84:     unless ($symb) { return ''; }
   85:     my %usernamesort = ();
   86:     my %namesort =();
   87:     my %subjectsort = ();
   88: # backward compatibility (bulletin boards used to be 'wrapped')
   89:     my $ressymb=$symb;
   90:     if ($mode eq 'board') {
   91:         unless ($ressymb =~ m|bulletin___\d+___adm/wrapper|) {
   92:             $ressymb=~s|(bulletin___\d+___)|$1adm/wrapper|;
   93:         }
   94:     }
   95: 
   96: # Get discussion display settings for this discussion
   97:     my $lastkey = $ressymb.'_lastread';
   98:     my $showkey = $ressymb.'_showonlyunread';
   99:     my $markkey = $ressymb.'_showonlyunmark',
  100:     my $visitkey = $ressymb.'_visit';
  101:     my $ondispkey = $ressymb.'_markondisp';
  102:     my $userpickkey = $ressymb.'_userpick';
  103:     my $toggkey = $ressymb.'_readtoggle';
  104:     my $readkey = $ressymb.'_read';
  105: 
  106:     my %dischash = &Apache::lonnet::get('nohist_'.$ENV{'request.course.id'}.'_discuss',[$lastkey,$showkey,$markkey,$visitkey,$ondispkey,$userpickkey,$toggkey,$readkey],$ENV{'user.domain'},$ENV{'user.name'});
  107:     my %discinfo = ();
  108:     my $showonlyunread = 0;
  109:     my $showunmark = 0; 
  110:     my $markondisp = 0;
  111:     my $prevread = 0;
  112:     my $previous = 0;
  113:     my $visit = 0;
  114:     my $newpostsflag = 0;
  115:     my @posters = split/\&/,$dischash{$userpickkey};
  116: 
  117: # Retain identification of "NEW" posts identified in last display, if continuing 'previous' browsing of posts.
  118:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},['previous','sortposts','rolefilter','statusfilter','sectionpick','totposters']);
  119:     my $sortposts = $ENV{'form.sortposts'};
  120:     my $rolefilter = $ENV{'form.rolefilter'};
  121:     my $statusfilter = $ENV{'form.statusfilter'};
  122:     my $sectionpick = $ENV{'form.sectionpick'};
  123:     my $totposters = $ENV{'form.totposters'};
  124:     $previous = $ENV{'form.previous'};
  125:     if ($previous > 0) {
  126:         $prevread = $previous;
  127:     } elsif (defined($dischash{$lastkey})) {
  128:         unless ($dischash{$lastkey} eq '') {
  129:             $prevread = $dischash{$lastkey};
  130:         }
  131:     }
  132: 
  133: # Get information about students and non-students in course for filtering display of posts
  134:     my %roleshash = ();
  135:     my %roleinfo = ();
  136:     if ($rolefilter) {
  137:         %roleshash = &Apache::lonnet::dump('nohist_userroles',$ENV{'course.'.$ENV{'request.course.id'}.'.domain'},$ENV{'course.'.$ENV{'request.course.id'}.'.num'});
  138:         foreach (keys %roleshash) {
  139:             my ($role,$uname,$udom,$sec) = split/:/,$_;
  140:             my ($end,$start) = split/:/,$roleshash{$_};
  141:             my $now = time;
  142:             my $status = 'Active';
  143:             if (($now < $start) || ($end > 0 && $now > $end)) {
  144:                 $status = 'Expired';
  145:             }
  146:             push @{$roleinfo{$uname.':'.$udom}}, $role.':'.$sec.':'.$status;
  147:         }
  148:         my ($classlist) = &Apache::loncoursedata::get_classlist(
  149:                               $ENV{'request.course.id'},
  150:                               $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
  151:                               $ENV{'course.'.$ENV{'request.course.id'}.'.num'});
  152:         my $sec_index = &Apache::loncoursedata::CL_SECTION();
  153:         my $status_index = &Apache::loncoursedata::CL_STATUS();
  154:         while (my ($student,$data) = each %$classlist) {
  155:             my ($section,$status) = ($data->[$sec_index],
  156:                                  $data->[$status_index]);
  157:             push @{$roleinfo{$student}}, 'st:'.$section.':'.$status;
  158:         }
  159:     }
  160: 
  161: # Get discussion display default settings for user
  162:     my %userenv = &Apache::lonnet::get('environment',['discdisplay','discmarkread'],$ENV{'user.domain'},$ENV{'user.name'});
  163:     my $discdisplay=$userenv{'discdisplay'};
  164:     if ($discdisplay eq 'unread') {
  165:         $showonlyunread = 1;
  166:     }
  167:     my $discmarkread=$userenv{'discmarkread'};
  168:     if ($discmarkread eq 'ondisp') {
  169:         $markondisp = 1;
  170:     }
  171: 
  172: # Override user's default if user specified display setting for this discussion
  173:     if (defined($dischash{$ondispkey})) {
  174:         $markondisp = $dischash{$ondispkey};
  175:     }
  176:     if ($markondisp) {
  177:         $discinfo{$lastkey} = time;
  178:     }
  179: 
  180:     if (defined($dischash{$showkey})) {
  181:         $showonlyunread = $dischash{$showkey};
  182:     }
  183: 
  184:     if (defined($dischash{$markkey})) {
  185:         $showunmark = $dischash{$markkey};
  186:     }
  187: 
  188:     if (defined($dischash{$visitkey})) {
  189:         $visit = $dischash{$visitkey};
  190:     }
  191:     $visit ++;
  192: 
  193:     my $seeid=&Apache::lonnet::allowed('rin',$crs);
  194:     my $viewgrades=(&Apache::lonnet::allowed('vgr',$crs)
  195: 	&& ($symb=~/\.(problem|exam|quiz|assess|survey|form)$/));
  196:     my @discussionitems=();
  197:     my %shown = ();
  198:     my @posteridentity=();
  199:     my %contrib=&Apache::lonnet::restore($ressymb,$ENV{'request.course.id'},
  200: 			  $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
  201: 			  $ENV{'course.'.$ENV{'request.course.id'}.'.num'});
  202:     my $visible=0;
  203:     my @depth=();
  204:     my @original=();
  205:     my @index=();
  206:     my @replies=();
  207:     my %alldiscussion=();
  208:     my %notshown = ();
  209:     my %newitem = ();
  210:     my $maxdepth=0;
  211: 
  212:     my $target='';
  213:     unless ($ENV{'browser.interface'} eq 'textual' ||
  214: 	    $ENV{'environment.remote'} eq 'off' ) {
  215: 	$target='target="LONcom"';
  216:     }
  217: 
  218:     my $now = time;
  219:     $discinfo{$visitkey} = $visit;
  220: 
  221:     &Apache::lonnet::put('nohist_'.$ENV{'request.course.id'}.'_discuss',\%discinfo,$ENV{'user.domain'},$ENV{'user.name'});
  222: 
  223:     if ($contrib{'version'}) {
  224:         my $oldest = $contrib{'1:timestamp'};
  225:         if ($prevread eq '0') {
  226:             $prevread = $oldest-1;
  227:         }
  228: 	for (my $id=1;$id<=$contrib{'version'};$id++) {
  229: 	    my $idx=$id;
  230:             my $posttime = $contrib{$idx.':timestamp'};
  231:             if ($prevread <= $posttime) {
  232:                 $newpostsflag = 1;
  233:             }
  234: 	    my $hidden=($contrib{'hidden'}=~/\.$idx\./);
  235:             my $studenthidden=($contrib{'studenthidden'}=~/\.$idx\./);
  236: 	    my $deleted=($contrib{'deleted'}=~/\.$idx\./);
  237: 	    my $origindex='0.';
  238:             my $numoldver=0;
  239: 	    if ($contrib{$idx.':replyto'}) {
  240:                 if ( (($ENV{'environment.threadeddiscussion'}) && (($sortposts eq '') || ($sortposts eq 'ascdate'))) || ($sortposts eq 'thread')) {
  241: # this is a follow-up message
  242: 		    $original[$idx]=$original[$contrib{$idx.':replyto'}];
  243: 		    $depth[$idx]=$depth[$contrib{$idx.':replyto'}]+1;
  244: 		    $origindex=$index[$contrib{$idx.':replyto'}];
  245: 		    if ($depth[$idx]>$maxdepth) { $maxdepth=$depth[$idx]; }
  246:                 } else {
  247:                     $original[$idx]=0;
  248:                     $depth[$idx]=0;
  249:                 }
  250: 	    } else {
  251: # this is an original message
  252: 		$original[$idx]=0;
  253: 		$depth[$idx]=0;
  254: 	    }
  255: 	    if ($replies[$depth[$idx]]) {
  256: 		$replies[$depth[$idx]]++;
  257: 	    } else {
  258: 		$replies[$depth[$idx]]=1;
  259: 	    }
  260: 	    unless ((($hidden) && (!$seeid)) || ($deleted)) {
  261: 		$visible++;
  262:                 if ($contrib{$idx.':history'}) {
  263:                     if ($contrib{$idx.':history'} =~ /:/) {
  264:                         my @oldversions = split/:/,$contrib{$idx.':history'};
  265:                         $numoldver = @oldversions;
  266:                     } else {
  267:                         $numoldver = 1;
  268:                     } 
  269:                 }
  270: 		my ($message,$subject);
  271:          	if ($idx > 0) {
  272:                     if ($contrib{$idx.':message'} =~ /^<version num="0">/) {
  273:                         my %versions = (); 
  274:                         &get_post_versions(\%versions,$contrib{$idx.':message'},$numoldver);
  275:                         $message = &HTML::Entities::decode($versions{$numoldver});
  276:                     } else {
  277:                         $message = $contrib{$idx.':message'};
  278:                     }
  279:                 } else {
  280:                     $message=$contrib{$idx.':message'};
  281:                 }
  282:                 my $attachmenturls = $contrib{$idx.':attachmenturl'}; 
  283: 		$message=~s/\n/\<br \/\>/g;
  284: 		$message=&Apache::lontexconvert::msgtexconverted($message);
  285:          	if ($idx > 0) {
  286:                     if ($contrib{$idx.':subject'} =~ /^<version num="0"/) {
  287:                         my %versions = ();
  288:                         &get_post_versions(\%versions,$contrib{$idx.':subject'},$numoldver);
  289:                         $subject = &HTML::Entities::decode($versions{$numoldver});
  290:                     } else {
  291:                         $subject = $contrib{$idx.':subject'};
  292:                     }
  293:                 } else {
  294:                     $subject=$contrib{$idx.':subject'};
  295:                 }
  296:                 if (defined($subject)) {
  297:                     $subject=~s/\n/\<br \/\>/g;
  298:                     $subject=&Apache::lontexconvert::msgtexconverted($subject);
  299:                 }
  300: 		if ($attachmenturls) {
  301:                     my %attachments = ();
  302:                     my %currattach = ();
  303:                     &extract_attachments($attachmenturls,$idx,$numoldver,\$message,\%attachments,\%currattach);
  304: 		}
  305: 		if ($message) {
  306: 		    if ($hidden) {
  307: 			$message='<font color="#888888">'.$message.'</font>';
  308:                         if ($studenthidden) {
  309:                             $message .='<br /><br />Deleted by poster (student).';
  310:                         }
  311: 		    }
  312: 		    my $screenname=&Apache::loncommon::screenname(
  313: 					    $contrib{$idx.':sendername'},
  314: 					    $contrib{$idx.':senderdomain'});
  315: 		    my $plainname=&Apache::loncommon::nickname(
  316: 					    $contrib{$idx.':sendername'},
  317: 					    $contrib{$idx.':senderdomain'});
  318: 		    
  319: 		    my $sender=&mt('Anonymous');
  320: # Set up for sorting by subject
  321:                     if ($contrib{$idx.':subject'} eq '') {
  322:                         if (defined($subjectsort{'__No subject'})) {
  323:                             push @{$subjectsort{'__No subject'}}, $idx;
  324:                         } else {
  325:                             @{$subjectsort{'__No subject'}} = ("$idx");
  326:                         }
  327:                     } else {
  328:                         if (defined($subjectsort{$contrib{$idx.':subject'}})) {
  329:                             push @{$subjectsort{$contrib{$idx.':subject'}}}, $idx;
  330:                         } else {
  331:                             @{$subjectsort{$contrib{$idx.':subject'}}} = ("$idx");
  332:                         }
  333:                     }
  334: 		    if ((!$contrib{$idx.':anonymous'}) || ($seeid)) {
  335: 			$sender=&Apache::loncommon::aboutmewrapper(
  336: 					 $plainname,
  337: 					 $contrib{$idx.':sendername'},
  338: 					 $contrib{$idx.':senderdomain'}).' ('.
  339: 					 $contrib{$idx.':sendername'}.' at '.
  340: 					 $contrib{$idx.':senderdomain'}.')';
  341: 			if ($contrib{$idx.':anonymous'}) {
  342: 			    $sender.=' ['.&mt('anonymous').'] '.
  343: 				$screenname;
  344: 			}
  345: # Set up for sorting by domain, then username
  346:                         unless (defined($usernamesort{$contrib{$idx.':senderdomain'}})) {
  347:                             %{$usernamesort{$contrib{$idx.':senderdomain'}}} = ();
  348:                         }
  349:                         if (defined($usernamesort{$contrib{$idx.':senderdomain'}}{$contrib{$idx.':sendername'}})) {
  350:                             push @{$usernamesort{$contrib{$idx.':senderdomain'}}{$contrib{$idx.':sendername'}}}, $idx;
  351:                         } else {
  352:                             @{$usernamesort{$contrib{$idx.':senderdomain'}}{$contrib{$idx.':sendername'}}} = ("$idx");
  353:                         }
  354: # Set up for sorting by last name, then first name
  355:                         my %names = &Apache::lonnet::get('environment',['firstname','lastname'],
  356:                                   $contrib{$idx.':senderdomain'},$contrib{$idx.':sendername'});
  357:                         my $lastname = $names{'lastname'};
  358:                         my $firstname = $names{'firstname'};
  359:                         if ($lastname eq '') {
  360:                             $lastname = '_';
  361:                         }
  362:                         if ($firstname eq '') {
  363:                             $firstname = '_';
  364:                         }
  365:                         unless (defined($namesort{$lastname})) {
  366:                             %{$namesort{$lastname}} = ();
  367:                         }
  368:                         if (defined($namesort{$lastname}{$firstname})) {
  369:                             push @{$namesort{$lastname}{$firstname}}, $idx;
  370:                         } else {
  371:                             @{$namesort{$lastname}{$firstname}} = ("$idx");
  372:                         }
  373:                         if ($ENV{"course.$cid.allow_discussion_post_editing"} =~ m/yes/i) {
  374:                             if (($ENV{'user.domain'} eq $contrib{$idx.':senderdomain'}) && ($ENV{'user.name'} eq $contrib{$idx.':sendername'})) {
  375:                                 $sender.=' <a href="/adm/feedback?editdisc='.
  376:                                     $ressymb.':::'.$idx;
  377:                                 if ($newpostsflag) {
  378:                                     $sender .= '&previous='.$prevread;
  379:                                 }
  380:                                 $sender .= '" '.$target.'>'.&mt('Edit').'</a>';                                      unless ($seeid) {
  381:                                     $sender.=" <a href=\"javascript:studentdelete('$ressymb','$idx','$newpostsflag','$prevread')";
  382:                                     $sender .= '">'.&mt('Delete').'</a>';
  383:                                 }
  384:                             }
  385:                         }
  386: 			if ($seeid) {
  387: 			    if ($hidden) {
  388:                                 unless ($studenthidden) {
  389: 				    $sender.=' <a href="/adm/feedback?unhide='.
  390: 				        $ressymb.':::'.$idx;
  391:                                     if ($newpostsflag) {
  392:                                         $sender .= '&previous='.$prevread;
  393:                                     }
  394:                                     $sender .= '">'.&mt('Make Visible').'</a>';
  395:                                 }
  396: 			    } else {
  397: 				$sender.=' <a href="/adm/feedback?hide='.
  398: 				    $ressymb.':::'.$idx;
  399:                                 if ($newpostsflag) {
  400:                                     $sender .= '&previous='.$prevread;
  401:                                 }
  402:                                 $sender .= '">'.&mt('Hide').'</a>';
  403: 			    }                     
  404: 			    $sender.=' <a href="/adm/feedback?deldisc='.
  405: 				    $ressymb.':::'.$idx;
  406:                             if ($newpostsflag) {
  407:                                 $sender .= '&previous='.$prevread;
  408:                             }
  409:                             $sender .= '">'.&mt('Delete').'</a>';
  410: 			}
  411: 		    } else {
  412: 			if ($screenname) {
  413: 			    $sender='<i>'.$screenname.'</i>';
  414: 			}
  415: # Set up for sorting by domain, then username for anonymous
  416:                         unless (defined($usernamesort{'__anon'})) {
  417:                             %{$usernamesort{'__anon'}} = ();
  418:                         }
  419:                         if (defined($usernamesort{'__anon'}{'__anon'})) {
  420:                             push @{$usernamesort{'__anon'}{'__anon'}}, $idx;
  421:                         } else {
  422:                             @{$usernamesort{'__anon'}{'__anon'}} = ("$idx");
  423:                         }
  424: # Set up for sorting by last name, then first name for anonymous
  425:                         unless (defined($namesort{'__anon'})) {
  426:                             %{$namesort{'__anon'}} = ();
  427:                         }
  428:                         if (defined($namesort{'__anon'}{'__anon'})) {
  429:                             push @{$namesort{'__anon'}{'__anon'}}, $idx;
  430:                         } else {
  431:                             @{$namesort{'__anon'}{'__anon'}} = ("$idx");
  432:                         }
  433: 		    }
  434: 		    if (&discussion_open($status) &&
  435: 			&Apache::lonnet::allowed('pch',
  436: 						 $ENV{'request.course.id'}.
  437: 						 ($ENV{'request.course.sec'}?'/'.$ENV{'request.course.sec'}:''))) {
  438: 			$sender.=' <a href="/adm/feedback?replydisc='.
  439: 			    $ressymb.':::'.$idx;
  440:                         if ($newpostsflag) {
  441:                             $sender .= '&previous='.$prevread;
  442:                         }
  443:                         $sender .= '" '.$target.'>'.&mt('Reply').'</a>';
  444: 		    }
  445: 		    my $vgrlink;
  446: 		    if ($viewgrades) {
  447: 			$vgrlink=&Apache::loncommon::submlink('Submissions',
  448:             $contrib{$idx.':sendername'},$contrib{$idx.':senderdomain'},$symb);
  449: 		    }
  450:                     my $ctlink;
  451:                     if ($dischash{$readkey}=~/\.$idx\./) { 
  452:                         $ctlink = '<b>'.&mt('Mark unread').'?</b>&nbsp;<input type="checkbox" name="postunread_'.$idx.'" />';
  453:                     } else {
  454:                         $ctlink = '<b>'.&mt('Mark read').'?</b>&nbsp;<input type="checkbox" name="postread_'.$idx.'" />';
  455:                     }
  456: #figure out at what position this needs to print
  457: 		    my $thisindex=$idx;
  458: 		    if ( (($ENV{'environment.threadeddiscussion'}) && (($sortposts eq '') || ($sortposts eq 'ascdate'))) || ($sortposts eq 'thread')) {
  459: 			$thisindex=$origindex.substr('00'.$replies[$depth[$idx]],-2,2);
  460: 		    }
  461: 		    $alldiscussion{$thisindex}=$idx;
  462:                     $shown{$idx} = 0;
  463:                     $index[$idx]=$thisindex;
  464:                     my $spansize = 2;
  465:                     if ($showonlyunread && $prevread > $posttime) {
  466:                         $notshown{$idx} = 1;
  467:                     } elsif ($showunmark && $dischash{$readkey}=~/\.$idx\./) {
  468:                         $notshown{$idx} = 1;
  469:                     } else {
  470: # apply filters
  471:                         my $uname = $contrib{$idx.':sendername'};
  472:                         my $udom = $contrib{$idx.':senderdomain'};
  473:                         my $poster = $uname.':'.$udom;
  474:                         my $rolematch = '';
  475:                         my $skiptest = 1;
  476:                         if ($totposters > 0) {
  477:                             if (grep/^$poster$/,@posters) {
  478:                                 $shown{$idx} = 1;
  479:                             }
  480:                         } else {
  481:                             if ($rolefilter) {
  482:                                 if ($rolefilter eq 'all') {
  483:                                     $rolematch = '([^:]+)';
  484:                                 } else {
  485:                                     $rolematch = $rolefilter;
  486:                                     $skiptest = 0;
  487:                                 }
  488:                             }
  489:                             if ($sectionpick) {
  490:                                 if ($sectionpick eq 'all') {
  491:                                     $rolematch .= ':([^:]*)';
  492:                                 } else {
  493:                                     $rolematch .= ':'.$sectionpick;
  494:                                     $skiptest = 0;
  495:                                 }
  496:                             }
  497:                             if ($statusfilter) {
  498:                                 if ($statusfilter eq 'all') {
  499:                                     $rolematch .= ':([^:]+)';
  500:                                 } else {
  501:                                     $rolematch .= ':'.$statusfilter;
  502:                                     $skiptest = 0;
  503:                                 }
  504:                             }
  505:                             if ($skiptest) {
  506:                                 $shown{$idx} = 1;
  507:                             } else {
  508:                                 foreach my $role (@{$roleinfo{$poster}}) {
  509:                                     if ($role =~ m/^$rolematch$/) {
  510:                                         $shown{$idx} = 1;
  511:                                         last;
  512:                                     }
  513:                                 }
  514:                             }
  515:                         }
  516:                     }
  517:                     unless ($notshown{$idx} == 1) {
  518:                         if ($prevread > 0 && $prevread <= $posttime) {
  519:                             $newitem{$idx} = 1;
  520:                             $discussionitems[$idx] .= '
  521:                              <p><table border="0" width="100%">
  522:                               <tr><td align="left"><font color="#FF0000"><b>NEW</b></font></td>';
  523:                         } else {
  524:                             $newitem{$idx} = 0;
  525:                             $discussionitems[$idx] .= '
  526:                              <p><table border="0" width="100%">
  527:                               <tr><td align="left">&nbsp;</td>';
  528:                         }
  529:                         $discussionitems[$idx] .= '<td align ="left">&nbsp;&nbsp;'.
  530:                             '<b>'.$subject.'</b>&nbsp;&nbsp;'.
  531:                             $sender.'</b> '.$vgrlink.' ('.
  532:                             &Apache::lonlocal::locallocaltime($posttime).')</td>';
  533:                         if ($dischash{$toggkey}) {
  534:                             $discussionitems[$idx].='<td align="right">&nbsp;&nbsp;'.
  535:                               $ctlink.'</td>';
  536:                         }
  537:                         $discussionitems[$idx].= '</tr></table><blockquote>'.$message.'</blockquote></p>';
  538:                         if ($contrib{$idx.':history'}) {
  539:                             my @postversions = ();
  540:                             $discussionitems[$idx] .= '<br />'.&mt('This post has been edited by the author.');
  541:                             if ($seeid) {
  542:                                 $discussionitems[$idx] .= '&nbsp;&nbsp;<a href="/adm/feedback?allversions='.$ressymb.':::'.$idx.'">'.&mt('Display all versions').'</a>';
  543:                             }
  544:                             $discussionitems[$idx].='<br/>'.&mt('Earlier version(s) were posted on: ');
  545:                             if ($contrib{$idx.':history'} =~ m/:/) {
  546:                                 @postversions = split/:/,$contrib{$idx.':history'};
  547:                             } else {
  548:                                 @postversions = ("$contrib{$idx.':history'}");
  549:                             }
  550:                             for (my $i=0; $i<@postversions; $i++) {
  551:                                 my $version = $i+1;
  552:                                 $discussionitems[$idx] .= '<b>'.$version.'.</b> - '.&Apache::lonlocal::locallocaltime($postversions[$i]).'  ';
  553:                             }
  554:                         }
  555:                     }
  556:                 }
  557:             }
  558: 	}
  559:     }
  560: 
  561:     my $discussion='';
  562: 
  563:     my $function = &Apache::loncommon::get_users_function();
  564:     my $color = &Apache::loncommon::designparm($function.'.tabbg',
  565:                                                     $ENV{'user.domain'});
  566:     my %lt = &Apache::lonlocal::texthash(
  567:         'cuse' => 'Current discussion settings',
  568:         'allposts' => 'All posts',
  569:         'unread' => 'New posts only',
  570:         'unmark' => 'Unread only',
  571:         'ondisp' => 'Once displayed',
  572:         'onmark' => 'Once marked not NEW',
  573:         'toggoff' => 'Off',
  574:         'toggon' => 'On',
  575:         'disa' => 'Posts to be displayed',
  576:         'npce' => 'Posts cease to be marked "NEW"',
  577:         'epcb' => 'Each post can be toggled read/unread', 
  578:         'chgt' => 'Change',
  579:         'disp' => 'Display',
  580:         'nolo' => 'Not new',
  581:         'togg' => 'Toggle read/unread',
  582:     );
  583: 
  584:     my $currdisp = $lt{'allposts'};
  585:     my $currmark = $lt{'onmark'};
  586:     my $currtogg = $lt{'toggoff'};
  587:     my $dispchange = $lt{'unread'};
  588:     my $markchange = $lt{'ondisp'};
  589:     my $toggchange = $lt{'toggon'};
  590:     my $chglink = '/adm/feedback?modifydisp='.$ressymb;
  591:     my $displinkA = 'onlyunread';
  592:     my $displinkB = 'onlyunmark';
  593:     my $marklink = 'markondisp';
  594:     my $togglink = 'toggon';
  595: 
  596:     if ($markondisp) {
  597:         $currmark = $lt{'ondisp'};
  598:         $markchange = $lt{'onmark'};
  599:         $marklink = 'markonread';
  600:     }
  601: 
  602:     if ($showonlyunread) {
  603:         $currdisp = $lt{'unread'};
  604:         $dispchange = $lt{'allposts'};
  605:         $displinkA = 'allposts';
  606:     }
  607: 
  608:     if ($showunmark) {
  609:         $currdisp = $lt{'unmark'};
  610:         $dispchange = $lt{'unmark'};
  611:         $displinkA='allposts';
  612:         $displinkB='onlyunread';
  613:         $showonlyunread = 0;
  614:     }
  615: 
  616:     if ($dischash{$toggkey}) {
  617:         $currtogg = $lt{'toggon'};
  618:         $toggchange = $lt{'toggoff'};
  619:         $togglink = 'toggoff';
  620:     } 
  621:    
  622:     $chglink .= '&changes='.$displinkA.'_'.$displinkB.'_'.$marklink.'_'.$togglink;
  623: 
  624:     if ($newpostsflag) {
  625:         $chglink .= '&previous='.$prevread;
  626:     }
  627: 
  628:     if ($visible) {
  629: # Print the discusssion
  630: 	if ($outputtarget ne 'tex') {
  631:             my $colspan=$maxdepth+1;
  632:             $discussion.= qq|
  633: <script>
  634:    function studentdelete (symb,idx,newflag,previous) {
  635:        var symbparm = symb+':::'+idx
  636:        var prevparm = ""
  637:        if (newflag == 1) {
  638:            prevparm = "&previous="+previous
  639:        }
  640:        if (confirm("Are you sure you want to delete this post?\\nDeleted posts will no longer be visible to you and other students,\\nbut will continue to be visible to your instructor")) {
  641:            document.location.href = "/adm/feedback?hide="+symbparm+prevparm
  642:        }  
  643:    }
  644: </script>
  645:             |;
  646: 	    $discussion.='<form name="readchoices" method="post" action="/adm/feedback?chgreads='.$symb.'"><table bgcolor="#AAAAAA" cellpadding="2" cellspacing="2" border="0">';
  647: 	    $discussion .='<tr><td bgcolor="#DDDDBB" colspan="'.$colspan.'">'.
  648: 		'<table border="0" width="100%" bgcolor="#DDDDBB"><tr>';
  649: 	    if ($visible>2) {
  650: 		$discussion.='<td align="left">'.
  651: 		    '<a href="/adm/feedback?threadedon='.$ressymb;
  652: 		if ($newpostsflag) {
  653: 		    $discussion .= '&previous='.$prevread;
  654: 		}
  655: 		$discussion .='">'.&mt('Threaded View').'</a>&nbsp;&nbsp;'.
  656: 		    '<a href="/adm/feedback?threadedoff='.$ressymb;
  657: 		if ($newpostsflag) {
  658: 		    $discussion .= '&previous='.$prevread;
  659: 		}
  660: 		$discussion .='">'.&mt('Chronological View').'</a>&nbsp;&nbsp;
  661:                               <a href= "/adm/feedback?sortfilter='.$ressymb;
  662:                 if ($newpostsflag) {
  663:                     $discussion .= '&previous='.$prevread;
  664:                 }
  665:                 $discussion .='">'.&mt('Sorting/Filtering options').'</a>&nbsp;&nbsp';
  666:             } else {
  667:                 $discussion .= '<td align="left">';
  668:             }
  669:             $discussion .='<a href= "/adm/feedback?export='.$ressymb;
  670:             if ($newpostsflag) {
  671:                 $discussion .= '&previous='.$prevread;
  672:             }
  673:             $discussion .= '">'.&mt('Export').'?</a>&nbsp;&nbsp;</td>';
  674: 	    if ($newpostsflag) {
  675: 		if (!$markondisp) {
  676: 		    $discussion .='<td align="right"><a href="/adm/feedback?markread='.$ressymb.'">'.&mt('Mark NEW posts no longer new').'</a>&nbsp;&nbsp;';
  677: 		} else {
  678: 		    $discussion .= '<td>&nbsp;</td>';
  679: 		}
  680: 	    } else {
  681: 		$discussion .= '<td>&nbsp;</td>';
  682: 	    }
  683: 	    $discussion .= '</tr></table></td></tr>';
  684: 	} else {
  685: 	    $discussion.='<tex>\vskip 0 mm\noindent\makebox[2 cm][b]{\hrulefill}'.
  686:                          '\textbf{DISCUSSIONS}\makebox[2 cm][b]{\hrulefill}'.
  687:                          '\vskip 0 mm\noindent\textbf{'.$lt{'cuse'}.'}:\vskip 0 mm'.
  688:                          '\noindent\textbf{'.$lt{'disa'}.'}: \textit{'.$currdisp.'}\vskip 0 mm'.
  689:                          '\noindent\textbf{'.$lt{'npce'}.'}: \textit{'.$currmark.'}</tex>';
  690: 	}
  691:         my $numhidden = keys %notshown;
  692:         if ($numhidden > 0) {
  693:             my $colspan = $maxdepth+1;
  694:             $discussion.="\n".'<tr><td bgcolor="#CCCCCC" colspan="'.$colspan.'">'.
  695:                          '<a href="/adm/feedback?allposts='.$ressymb;
  696:             if ($newpostsflag) {
  697:                 $discussion .= '&previous='.$prevread;
  698:             }
  699:             $discussion .= '">'.&mt('Show all posts').'</a> '.&mt('to display').' '.
  700:                          $numhidden.' ';
  701:             if ($showunmark) {
  702:                 $discussion .= &mt('posts previously marked read');
  703:             } else {
  704:                 $discussion .= &mt('previously viewed posts');
  705:             }
  706:             $discussion .= '<br/></td></tr>';
  707:         }
  708: 
  709: # Choose sort mechanism
  710:         my @showposts = ();
  711:         if ($sortposts eq 'descdate') {
  712:             @showposts = (sort { $b <=> $a } keys %alldiscussion);
  713:         } elsif ($sortposts eq 'thread') {
  714:             @showposts = (sort { $a <=> $b } keys %alldiscussion);
  715:         } elsif ($sortposts eq 'subject') {
  716:             foreach (sort keys %subjectsort) {
  717:                 push @showposts, @{$subjectsort{$_}};
  718:             }
  719:         } elsif ($sortposts eq 'username') {
  720:             foreach my $domain (sort keys %usernamesort) {
  721:                 foreach (sort keys %{$usernamesort{$domain}}) {
  722:                     push @showposts, @{$usernamesort{$domain}{$_}};
  723:                 }
  724:             }
  725:         } elsif ($sortposts eq 'lastfirst') {
  726:             foreach my $last (sort keys %namesort) {
  727:                  foreach (sort keys %{$namesort{$last}}) {
  728:                      push @showposts, @{$namesort{$last}{$_}};
  729:                  }
  730:             }
  731:         } else {
  732:             $sortposts = 'ascdate';
  733:             @showposts =  (sort { $a <=> $b } keys %alldiscussion);
  734:         }
  735:         foreach (@showposts) {
  736:             unless (($sortposts eq 'thread') || ($sortposts eq 'ascdate' && $ENV{'environment.threadeddiscussion'})) {
  737:                 $alldiscussion{$_} = $_;
  738:             }
  739:             unless ( ($notshown{$alldiscussion{$_}} eq '1') || ($shown{$alldiscussion{$_}} == 0) ) {
  740:                 if ($outputtarget ne 'tex') {
  741: 		    $discussion.="\n<tr>";
  742: 		}
  743: 	        my $thisdepth=$depth[$alldiscussion{$_}];
  744:                 if ($outputtarget ne 'tex') {
  745: 		    for (1..$thisdepth) {
  746: 			$discussion.='<td>&nbsp;&nbsp;&nbsp;</td>';
  747: 		    }
  748: 		}
  749: 	        my $colspan=$maxdepth-$thisdepth+1;
  750:                 if ($outputtarget ne 'tex') {
  751: 		    $discussion.='<td  bgcolor="'.$bgcols[$newitem{$alldiscussion{$_}}].'" colspan="'.$colspan.'">'.
  752:                              $discussionitems[$alldiscussion{$_}].
  753: 	                     '</td></tr>';
  754: 		} else {
  755: 		    #cleanup block
  756: 		    $discussionitems[$alldiscussion{$_}]=~s/<table([^>]*)>/<table TeXwidth="90 mm">/;
  757: 		    $discussionitems[$alldiscussion{$_}]=~s/<tr([^>]*)><td([^>]*)>/<tr><td TeXwidth="20 mm" align="left">/;
  758:                     my $threadinsert='';
  759:                     if ($thisdepth > 0) {
  760: 			$threadinsert='<br /><strong>Reply: '.$thisdepth.'</strong>';
  761: 		    }
  762: 		    $discussionitems[$alldiscussion{$_}]=~s/<\/td><td([^>]*)>/$threadinsert<\/td><td TeXwidth="65 mm" align="left">/;
  763: 		    $discussionitems[$alldiscussion{$_}]=~s/<a([^>]+)>(Edit|Hide|Delete|Reply|Submissions)<\/a>//g;
  764:                     $discussionitems[$alldiscussion{$_}]=~s/(<b>|<\/b>|<\/a>|<a([^>]+)>)//g;
  765: 
  766: 		    $discussionitems[$alldiscussion{$_}]='<tex>\vskip 0 mm\noindent\makebox[2 cm][b]{\hrulefill}</tex>'.$discussionitems[$alldiscussion{$_}];
  767: 		    $discussion.=$discussionitems[$alldiscussion{$_}];
  768: 		}
  769: 	    }
  770:         }
  771: 	if ($outputtarget ne 'tex') {
  772:             my $colspan=$maxdepth+1;
  773:             $discussion .= <<END;
  774:             <tr bgcolor="#FFFFFF">
  775:              <td colspan="$colspan" valign="top">
  776:               <table border="0" bgcolor="#FFFFFF" width="100%" cellspacing="2" cellpadding="2">
  777:                <tr>
  778:                 <td align="left">
  779:                  <table border="0" cellpadding="0" cellspacing="4">
  780:                   <tr>
  781:                    <td>
  782:                     <font size="-1"><b>$lt{'cuse'}</b>:</td>
  783:                    <td>&nbsp;</td>
  784:                    <td><font size="-1">
  785: END
  786:             if ($newpostsflag) {
  787:                 $discussion .= 
  788:                    '1.&nbsp;'.$lt{'disp'}.'&nbsp;-&nbsp;<i>'.$currdisp.'</i>&nbsp;&nbsp;2.&nbsp;'.$lt{'nolo'}.'&nbsp;-&nbsp;<i>'.$currmark.'</i>';
  789:                 if ($dischash{$toggkey}) {
  790:                    $discussion .= '&nbsp;&nbsp;3.&nbsp;'.$lt{'togg'}.'&nbsp;-&nbsp;<i>'.$currtogg.'</i>';
  791:                 }
  792:             } else {
  793:                 if ($dischash{$toggkey}) {
  794:                    $discussion .= '1.&nbsp;'.$lt{'disp'}.'&nbsp;-&nbsp;<i>'.$currdisp.'</i>&nbsp;2.&nbsp;'.$lt{'togg'}.'&nbsp;-&nbsp;<i>'.$currtogg.'</i>';
  795:                 } else {
  796:                     $discussion .=
  797:                          $lt{'disp'}.'&nbsp;-&nbsp;<i>'.$currdisp.'</i>';
  798:                 }
  799:             }
  800:             $discussion .= <<END;
  801:                    </font></td>
  802:                    <td>&nbsp;</td>
  803:                    <td>
  804:                     <font size="-1"><b><a href="$chglink">$lt{'chgt'}</a>?</font></b>
  805:                    </td>
  806:                   </tr>
  807:                  </table>
  808:                 </td>
  809: END
  810:             if ($dischash{$toggkey}) {
  811:                 my $storebutton = &mt('Store read/unread changes');
  812:                 $discussion.='<td align="right">'.
  813:               '<input type="hidden" name="discsymb" value="'.$ressymb.'">'."\n".
  814:               '<input type="button" name="readoptions" value="'.$storebutton.'"'.
  815:               ' onClick="this.form.submit();">'."\n".
  816:               '</td>';
  817:             }
  818:             $discussion .= (<<END);
  819:                </tr>
  820:               </table>
  821:              </td>
  822:             </tr>
  823:            </table>
  824:            <br /><br /></form>
  825: END
  826:         } 
  827:     }
  828:     if ($discussiononly) {
  829:         my $now = time;
  830:         my $attachnum = 0;
  831:         my $newattachmsg = '';
  832:         my @currnewattach = ();
  833:         my @currdelold = ();
  834:         my $comment = '';
  835:         my $subject = '';
  836:         if ($ENV{'form.origpage'}) {
  837:             &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},['addnewattach','deloldattach','delnewattach','timestamp','idx','subject','comment']);
  838:             $subject = &HTML::Entities::encode($ENV{'form.subject'},'<>&"');
  839:             $comment = &HTML::Entities::encode($ENV{'form.comment'},'<>&"');
  840:             my @keepold = ();
  841:             &process_attachments(\@currnewattach,\@currdelold,\@keepold);
  842:             if (@currnewattach > 0) {
  843:                 $attachnum += @currnewattach;
  844:             }
  845:         }
  846: 	$discussion.=(<<ENDDISCUSS);
  847: <form action="/adm/feedback" method="post" name="mailform" enctype="multipart/form-data">
  848: <input type="submit" name="discuss" value="Post Discussion" />
  849: <input type="submit" name="anondiscuss" value="Post Anonymous Discussion" />
  850: <input type="hidden" name="symb" value="$ressymb" />
  851: <input type="hidden" name="sendit" value="true" />
  852: <input type="hidden" name="timestamp" value="$now" />
  853: <br /><a name="newpost"></a>
  854: <font size="1">Note: in anonymous discussion, your name is visible only 
  855: to course faculty</font><br />
  856: <b>Title:</b>&nbsp;<input type="text" name="subject" value="$subject" size="30" /><br /><br />
  857: <textarea name="comment" cols="80" rows="14" wrap="hard">$comment</textarea>
  858: ENDDISCUSS
  859:         if ($ENV{'form.origpage'}) {
  860:             $discussion.='<input type="hidden" name="origpage" value="'.$ENV{'form.origpage'}.'" />'."\n";
  861:             foreach (@currnewattach) {
  862:                 $discussion.='<input type="hidden" name="currnewattach" value="'.$_.'" />'."\n";
  863:             }
  864:         }
  865:         $discussion.="</form>\n";
  866:         if ($outputtarget ne 'tex') {
  867:             $discussion.=&generate_attachments_button('',$attachnum,$ressymb,$now,\@currnewattach,\@currdelold,'',$mode);
  868:             if (@currnewattach > 0) {
  869:                 $newattachmsg .= '<b>New attachments</b><br />';
  870:                 if (@currnewattach > 1) {
  871:                     $newattachmsg .= '<ol>';
  872:                     foreach my $item (@currnewattach) {
  873:                         $item =~ m#.*/([^/]+)$#;
  874:                         $newattachmsg .= '<li><a href="'.$item.'">'.$1.'</a></li>'."\n";
  875:                     }
  876:                     $newattachmsg .= '</ol>'."\n";
  877:                 } else {
  878:                     $currnewattach[0] =~ m#.*/([^/]+)$#;
  879:                     $newattachmsg .= '<a href="'.$currnewattach[0].'">'.$1.'</a><br />'."\n";
  880:                 }
  881:             }
  882:             $discussion.=$newattachmsg;
  883: 	    $discussion.=&generate_preview_button();
  884: 	}
  885:     } else {
  886: 	if (&discussion_open($status) &&
  887: 	    &Apache::lonnet::allowed('pch',
  888: 				   $ENV{'request.course.id'}.
  889: 	($ENV{'request.course.sec'}?'/'.$ENV{'request.course.sec'}:''))) {
  890: 	    if ($outputtarget ne 'tex') {
  891: 		$discussion.='<table bgcolor="#BBBBBB"><tr><td><a href="/adm/feedback?replydisc='.
  892: 		    $symb.':::" '.$target.'>'.
  893: 		    '<img src="/adm/lonMisc/chat.gif" border="0" />'.
  894: 		    &mt('Post Discussion').'</a></td></tr></table>';
  895: 	    }
  896: 	}
  897:     }
  898:     return $discussion;
  899: }
  900: 
  901: sub mail_screen {
  902:   my ($r,$feedurl,$options) = @_;
  903:   if (exists($ENV{'form.origpage'})) {
  904:       &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},['subject','comment','currnewattach','addnewattach','deloldattach','delnewattach','timestamp','idx','anondiscuss','discuss']);
  905:   }
  906:   my $bodytag=&Apache::loncommon::bodytag('Resource Feedback and Discussion',
  907:                                           '','onLoad="window.focus();setposttype();"');
  908:   my $title=&Apache::lonnet::gettitle($feedurl);
  909:   if (!$title) { $title = $feedurl; }
  910:   my $quote='';
  911:   my $subject = '';
  912:   my $comment = '';
  913:   my $prevtag = '';
  914:   my $parentmsg = '';
  915:   my ($symb,$idx,$attachmenturls);
  916:   my $numoldver = 0;
  917:   my $attachmsg = '';
  918:   my $newattachmsg = '';
  919:   my @currnewattach = ();
  920:   my @currdelold = ();
  921:   my @keepold = ();
  922:   my %attachments = ();
  923:   my %currattach = ();
  924:   my $attachnum = 0;
  925:   my $anonchk = (<<END);
  926:   function anonchk() {
  927:      if (document.mailform.anondiscuss.checked == true) {
  928:           document.attachment.anondiscuss.value = '1'
  929:      }
  930:      if (document.mailform.discuss.checked == true) {
  931:           document.attachment.discuss.value = '1'
  932:      }
  933:      return
  934:    }
  935: END
  936:   my $anonscript;
  937:   if (exists($ENV{'form.origpage'})) {
  938:       $anonscript = (<<END);
  939:   function setposttype() {
  940:       var anondisc = $ENV{'form.anondiscuss'};
  941:       var disc = $ENV{'form.discuss'};
  942:       if (anondisc == 1) {
  943:           document.mailform.anondiscuss.checked = true
  944:       }
  945:       if (disc == 1) {
  946:           document.mailform.discuss.checked = true
  947:       }
  948:       return
  949:   }
  950: END
  951:   } else {
  952:       $anonscript = (<<END);
  953:   function setposttype() {
  954:       return
  955:   }
  956: END
  957:   }
  958:   if (($ENV{'form.replydisc'}) || ($ENV{'form.editdisc'})) {
  959:       if ($ENV{'form.replydisc'}) {
  960:           ($symb,$idx)=split(/\:\:\:/,$ENV{'form.replydisc'});
  961:       } else {
  962:           ($symb,$idx)=split(/\:\:\:/,$ENV{'form.editdisc'});
  963:       }
  964:       my %contrib=&Apache::lonnet::restore($symb,$ENV{'request.course.id'},
  965: 					   $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
  966: 					   $ENV{'course.'.$ENV{'request.course.id'}.'.num'});
  967:       unless (($contrib{'hidden'}=~/\.$idx\./) || ($contrib{'deleted'}=~/\.$idx\./)) {
  968:           if ($contrib{$idx.':history'}) {
  969:               if ($contrib{$idx.':history'} =~ /:/) {
  970:                   my @oldversions = split/:/,$contrib{$idx.':history'};
  971:                   $numoldver = @oldversions;
  972:               } else {
  973:                   $numoldver = 1;
  974:               }
  975:           }
  976:           if ($ENV{'form.replydisc'}) {
  977:               if ($contrib{$idx.':history'}) {
  978:                   if ($contrib{$idx.':history'} =~ /:/) {
  979:                       my @oldversions = split/:/,$contrib{$idx.':history'};
  980:                       $numoldver = @oldversions;
  981:                   } else {
  982:                       $numoldver = 1;
  983:                   }
  984:               }
  985:               my $message;
  986:               if ($idx > 0) {
  987:                   if ($contrib{$idx.':message'} =~ /^<version num="0"/) {
  988:                       my %versions = ();
  989:                       &get_post_versions(\%versions,$contrib{$idx.':message'},$numoldver);
  990:                       $message = &HTML::Entities::decode($versions{$numoldver});
  991:                   } else {
  992:                       $message = $contrib{$idx.':message'};
  993:                   }
  994:               } else {
  995: 	          $message=$contrib{$idx.':message'};
  996:               }
  997: 	      $message=~s/\n/\<br \/\>/g;
  998: 	      $quote='<blockquote>'.&Apache::lontexconvert::msgtexconverted($message).'</blockquote>';
  999:               if ($idx > 0) {
 1000:                   if ($contrib{$idx.':subject'} =~ /^<version num="0"/) {
 1001:                       my %versions = ();
 1002:                       &get_post_versions(\%versions,$contrib{$idx.':subject'},$numoldver);
 1003:                       $subject = &HTML::Entities::decode($versions{$numoldver});
 1004:                   } else {
 1005:                       $subject = $contrib{$idx.':subject'};
 1006:                   }
 1007:                   $subject = 'Re: '.$subject;
 1008:               }
 1009:               $subject = &HTML::Entities::encode($subject,'<>&"');
 1010:           } else {
 1011:               $attachmenturls = $contrib{$idx.':attachmenturl'};
 1012:               if ($contrib{$idx.':message'} =~ /^<version num="0">/) {
 1013:                   my %versions = ();
 1014:                   &get_post_versions(\%versions,$contrib{$idx.':message'},$numoldver);
 1015:                   $comment = $versions{$numoldver};
 1016:               } else {
 1017:                   $comment = &HTML::Entities::encode($contrib{$idx.':message'},'<>&"');
 1018:               }
 1019:               if ($contrib{$idx.':subject'} =~ /<version num="0">/) {
 1020:                   my %versions = ();
 1021:                   &get_post_versions(\%versions,$contrib{$idx.':subject'},$numoldver);
 1022:                   $subject = $versions{$numoldver}; 
 1023:               } else {
 1024:                   $subject = &HTML::Entities::encode($contrib{$idx.':subject'},'<>&"');
 1025:               }
 1026:               if (defined($contrib{$idx.':replyto'})) {
 1027:                   $parentmsg = $contrib{$idx.':replyto'};
 1028:               }
 1029:               unless (exists($ENV{'form.origpage'})) {
 1030:                   my $anonflag = 0;
 1031:                   if ($contrib{$idx.':anonymous'}) {
 1032:                       $anonflag = 1;
 1033:                   }
 1034:                   $anonscript = (<<END);
 1035:   function setposttype () {
 1036:       var currtype = $anonflag
 1037:       if (currtype == 1) {
 1038:           document.mailform.elements.discuss.checked = false
 1039:           document.mailform.elements.anondiscuss.checked = true
 1040:       }
 1041:       if (currtype == 0) {
 1042:           document.mailform.elements.anondiscuss.checked = false
 1043:           document.mailform.elements.discuss.checked = true
 1044:       }
 1045:       return
 1046:   }
 1047: END
 1048:               }
 1049:           }
 1050:       }
 1051:       if ($ENV{'form.previous'}) {
 1052:           $prevtag = '<input type="hidden" name="previous" value="'.$ENV{'form.previous'}.'" />';
 1053:       }
 1054:   }
 1055: 
 1056:   if ($ENV{'form.origpage'}) {
 1057:       $subject = $ENV{'form.subject'};
 1058:       $comment = $ENV{'form.comment'};
 1059:       &process_attachments(\@currnewattach,\@currdelold,\@keepold);
 1060:   }
 1061:   my $latexHelp=&Apache::loncommon::helpLatexCheatsheet();
 1062:   my $htmlheader=&Apache::lonhtmlcommon::htmlareaheaders();
 1063:   my $send=&mt('Send');
 1064:   $r->print(<<END);
 1065: <html>
 1066: <head>
 1067: <title>The LearningOnline Network with CAPA</title>
 1068: <meta http-equiv="pragma" content="no-cache"></meta>
 1069: $htmlheader
 1070: <script type="text/javascript">
 1071: //<!--
 1072:     function gosubmit() {
 1073:         var rec=0;
 1074:         if (typeof(document.mailform.elements.author)!="undefined") {
 1075:           if (document.mailform.elements.author.checked) {
 1076:              rec=1;
 1077:           } 
 1078:         }
 1079:         if (typeof(document.mailform.elements.question)!="undefined") {
 1080:           if (document.mailform.elements.question.checked) {
 1081:              rec=1;
 1082:           } 
 1083:         }
 1084:         if (typeof(document.mailform.elements.course)!="undefined") {
 1085:           if (document.mailform.elements.course.checked) {
 1086:              rec=1;
 1087:           } 
 1088:         }
 1089:         if (typeof(document.mailform.elements.policy)!="undefined") {
 1090:           if (document.mailform.elements.policy.checked) {
 1091:              rec=1;
 1092:           } 
 1093:         }
 1094:         if (typeof(document.mailform.elements.discuss)!="undefined") {
 1095:           if (document.mailform.elements.discuss.checked) {
 1096:              rec=1;
 1097:           } 
 1098:         }
 1099:         if (typeof(document.mailform.elements.anondiscuss)!="undefined") {
 1100:           if (document.mailform.elements.anondiscuss.checked) {
 1101:              rec=1;
 1102:           } 
 1103:         }
 1104: 
 1105:         if (rec) {
 1106:             if (typeof(document.mailform.onsubmit)!='undefined') {
 1107: 		document.mailform.onsubmit();
 1108: 	    }
 1109: 	    document.mailform.submit();
 1110:         } else {
 1111:             alert('Please check a feedback type.');
 1112: 	}
 1113:     }
 1114:     $anonchk
 1115:     $anonscript
 1116: //-->
 1117: </script>
 1118: </head>
 1119: $bodytag
 1120: <h2><tt>$title</tt></h2>
 1121: <form action="/adm/feedback" method="post" name="mailform"
 1122: enctype="multipart/form-data">
 1123: $prevtag
 1124: <input type="hidden" name="postdata" value="$feedurl" />
 1125: END
 1126:   if ($ENV{'form.replydisc'}) {
 1127:       $r->print(<<END);
 1128: <input type="hidden" name="replydisc" value="$ENV{'form.replydisc'}" />
 1129: END
 1130:   } elsif ($ENV{'form.editdisc'}) {
 1131:      $r->print(<<END);
 1132: <input type="hidden" name="editdisc" value ="$ENV{'form.editdisc'}" />
 1133: <input type="hidden" name="parentmsg" value ="$parentmsg" />
 1134: END
 1135:   }
 1136:   $r->print(<<END);
 1137: Please check at least one of the following feedback types:
 1138: $options<hr />
 1139: $quote
 1140: <p>My question/comment/feedback:</p>
 1141: <p>
 1142: $latexHelp
 1143: Title: <input type="text" name="subject" size="30" value="$subject" /></p>
 1144: <p>
 1145: <textarea name="comment" id="comment" cols="60" rows="10" wrap="hard">$comment
 1146: </textarea></p>
 1147: <p>
 1148: END
 1149:     if ( ($ENV{'form.editdisc'}) || ($ENV{'form.replydisc'}) ) {
 1150:         if ($ENV{'form.origpage'}) {
 1151:             foreach (@currnewattach) {
 1152:                 $r->print('<input type="hidden" name="currnewattach" value="'.$_.'" />'."\n");
 1153:             }
 1154:             foreach (@currdelold) {
 1155:                 $r->print('<input type="hidden" name="deloldattach" value="'.$_.'" />'."\n");
 1156:             }
 1157:         }
 1158:         if ($ENV{'form.editdisc'}) {
 1159:             if ($attachmenturls) {
 1160:                 &extract_attachments($attachmenturls,$idx,$numoldver,\$attachmsg,\%attachments,\%currattach,\@currdelold);
 1161:                 $attachnum = scalar(keys %currattach);
 1162:                 foreach (keys %currattach) {
 1163:                     $r->print('<input type="hidden" name="keepold" value="'.$_.'" />'."\n");
 1164:                 }
 1165:             }
 1166:         }
 1167:     } else {
 1168:         $r->print(<<END);
 1169: Attachment (128 KB max size): <input type="file" name="attachment" />
 1170: </p>
 1171: END
 1172:     }
 1173:     $r->print(<<END);
 1174: <p>
 1175: <input type="hidden" name="sendit" value="1" />
 1176: <input type="button" value="$send" onClick='gosubmit();' />
 1177: </p>
 1178: </form>
 1179: END
 1180:     if ($ENV{'form.editdisc'} || $ENV{'form.replydisc'}) {
 1181:         my $now = time;
 1182:         my $ressymb = $symb;
 1183:         my $postidx = '';
 1184:         if ($ENV{'form.editdisc'}) {
 1185:             $postidx = $idx;
 1186:         }
 1187:         if (@currnewattach > 0) {
 1188:             $attachnum += @currnewattach;
 1189:         }
 1190:         $r->print(&generate_attachments_button($postidx,$attachnum,$ressymb,$now,\@currnewattach,\@currdelold,$numoldver));
 1191:         if ($attachnum > 0) {
 1192:             if (@currnewattach > 0) {
 1193:                 $newattachmsg .= '<b>New attachments</b><br />';
 1194:                 if (@currnewattach > 1) {
 1195:                     $newattachmsg .= '<ol>';
 1196:                     foreach my $item (@currnewattach) {
 1197:                         $item =~ m#.*/([^/]+)$#;
 1198:                         $newattachmsg .= '<li><a href="'.$item.'">'.$1.'</a></li>'."\n";
 1199:                     }
 1200:                     $newattachmsg .= '</ol>'."\n";
 1201:                 } else {
 1202:                     $currnewattach[0] =~ m#.*/([^/]+)$#;
 1203:                     $newattachmsg .= '<a href="'.$currnewattach[0].'">'.$1.'</a><br />'."\n";
 1204:                 }
 1205:             }
 1206:             if ($attachmsg) {
 1207:                 $r->print("<b>Retained attachments</b>:$attachmsg<br />\n");
 1208:             }
 1209:             if ($newattachmsg) {
 1210:                 $r->print("$newattachmsg<br />");
 1211:             }
 1212:         }
 1213:     }
 1214:     $r->print(&generate_preview_button().
 1215:               &Apache::lonhtmlcommon::htmlareaselectactive('comment').
 1216:               '</body></html>');
 1217: }
 1218: 
 1219: sub print_display_options {
 1220:     my ($r,$symb,$previous,$dispchgA,$dispchgB,$markchg,$toggchg,$feedurl) = @_;
 1221:  # backward compatibility (bulletin boards used to be 'wrapped')
 1222:     if ($feedurl=~m|^/adm/wrapper/adm/.*/bulletinboard$|) {
 1223:         $feedurl=~s|^/adm/wrapper||;
 1224:     }
 1225: 
 1226:     my $function = &Apache::loncommon::get_users_function();
 1227:     my $tabcolor = &Apache::loncommon::designparm($function.'.tabbg',
 1228:                                                     $ENV{'user.domain'});
 1229:     my $bodytag=&Apache::loncommon::bodytag('Discussion options',
 1230:                                           '','');
 1231: 
 1232:     my %lt = &Apache::lonlocal::texthash(
 1233:         'dido' => 'Discussion display options',
 1234:         'pref' => 'Display Preference',
 1235:         'curr' => 'Current setting ',
 1236:         'actn' => 'Action',
 1237:         'deff' => 'Default for all discussions',
 1238:         'prca' => 'Preferences can be set for this discussion that determine ....',
 1239:         'whpo' => 'Which posts are displayed when you display this bulletin board or resource, and',
 1240:         'unwh' => 'Under what circumstances posts are identified as "NEW", and',
 1241:         'wipa' => 'Whether individual posts can be marked as read/unread',
 1242:         'allposts' => 'All posts',
 1243:         'unread' => 'New posts only',
 1244:         'unmark' => 'Posts not marked read',
 1245:         'ondisp' => 'Once displayed',
 1246:         'onmark' => 'Once marked not NEW ',
 1247:         'toggon' => 'Shown',
 1248:         'toggoff' => 'Not shown',
 1249:         'disa' => 'Posts displayed?',
 1250:         'npmr' => 'New posts cease to be identified as "NEW"?',
 1251:         'dotm' => 'Option to mark each post as read/unread?',  
 1252:         'chgt' => 'Change to ',
 1253:         'mkdf' => 'Set to ',
 1254:         'yhni' => 'You have not indicated that you wish to change any of the discussion settings',
 1255:         'ywbr' => 'You will be returned to the previous page if you click OK.'
 1256:     );
 1257: 
 1258:     my $dispchangeA = $lt{'unread'};
 1259:     my $dispchangeB = $lt{'unmark'};
 1260:     my $markchange = $lt{'ondisp'};
 1261:     my $toggchange = $lt{'toggon'};
 1262:     my $currdisp = $lt{'allposts'};
 1263:     my $currmark = $lt{'onmark'};
 1264:     my $discdisp = 'allposts';
 1265:     my $discmark = 'onmark';
 1266:     my $currtogg = $lt{'toggoff'};
 1267:     my $disctogg = 'toggoff';
 1268:                                                                                       
 1269:     if ($dispchgA eq 'allposts') {
 1270:         $dispchangeA = $lt{'allposts'};
 1271:         $currdisp = $lt{'unread'};
 1272:         $discdisp = 'unread';
 1273:     }
 1274: 
 1275:     if ($markchg eq 'markonread') {
 1276:         $markchange = $lt{'onmark'};
 1277:         $currmark = $lt{'ondisp'};
 1278:         $discmark = 'ondisp';
 1279:     }
 1280: 
 1281:     if ($dispchgB eq 'onlyunread') {
 1282:         $dispchangeB = $lt{'unread'};
 1283:         $currdisp = $lt{'unmark'};
 1284:         $discdisp = 'unmark';
 1285:     }
 1286:     if ($toggchg eq 'toggoff') {
 1287:         $toggchange = $lt{'toggoff'};
 1288:         $currtogg = $lt{'toggon'};
 1289:         $disctogg = 'toggon';
 1290:     }
 1291:     $r->print(<<END);
 1292: <html>
 1293: <head>
 1294: <title>$lt{'dido'}</title>
 1295: <meta http-equiv="pragma" content="no-cache" />
 1296: <script>
 1297: function discdispChk(caller) {
 1298:     var disctogg = '$toggchg'
 1299:     if (caller == 0) {
 1300:         if (document.modifydisp.discdisp[0].checked == true) {
 1301:             if (document.modifydisp.discdisp[1].checked == true) {
 1302:                 document.modifydisp.discdisp[1].checked = false
 1303:             }
 1304:         }
 1305:     }
 1306:     if (caller == 1) {
 1307:         if (document.modifydisp.discdisp[1].checked == true) {
 1308:             if (document.modifydisp.discdisp[0].checked == true) {
 1309:                 document.modifydisp.discdisp[0].checked = false
 1310:             }
 1311:             if (disctogg == 'toggon') {
 1312:                 document.modifydisp.disctogg.checked = true
 1313:             }
 1314:             if (disctogg == 'toggoff') {
 1315:                 document.modifydisp.disctogg.checked = false
 1316:             }
 1317:         }
 1318:     }
 1319:     if (caller == 2) {
 1320:         var dispchgB = '$dispchgB'
 1321:         if (disctogg == 'toggoff') {
 1322:             if (document.modifydisp.disctogg.checked == true) {
 1323:                 if (dispchgB == 'onlyunmark') {
 1324:                     document.modifydisp.discdisp[1].checked = false
 1325:                 }
 1326:             }
 1327:         }
 1328:     }  
 1329: }
 1330: 
 1331: function setDisp() {
 1332:     var prev = "$previous"
 1333:     var chktotal = 0
 1334:     if (document.modifydisp.discdisp[0].checked == true) {
 1335:         document.modifydisp.$dispchgA.value = "$symb"
 1336:         chktotal ++
 1337:     }
 1338:     if (document.modifydisp.discdisp[1].checked == true) {
 1339:         document.modifydisp.$dispchgB.value = "$symb"
 1340:         chktotal ++
 1341:     }
 1342:     if (document.modifydisp.discmark.checked == true) {
 1343:         document.modifydisp.$markchg.value = "$symb"
 1344:         chktotal ++
 1345:     }
 1346:     if (document.modifydisp.disctogg.checked == true) {
 1347:         document.modifydisp.$toggchg.value = "$symb"
 1348:         chktotal ++
 1349:     }
 1350:     if (chktotal > 0) { 
 1351:         document.modifydisp.submit()
 1352:     } else {
 1353:         if(confirm("$lt{'yhni'}. \\n$lt{'ywbr'}"))      {
 1354:             if (prev > 0) {
 1355:                 location.href = "$feedurl?previous=$previous"
 1356:             } else {
 1357:                 location.href = "$feedurl"
 1358:             }
 1359:         }
 1360:     }
 1361: }
 1362: </script>
 1363: </head>
 1364: $bodytag
 1365: <form name="modifydisp" method="post" action="/adm/feedback">
 1366: $lt{'sdpf'}<br/> $lt{'prca'}  <ol><li>$lt{'whpo'}</li><li>$lt{'unwh'}</li><li>$lt{'wipa'}</li></ol>
 1367: <br />
 1368: <table border="0" cellpadding="0" cellspacing="0">
 1369:  <tr>
 1370:   <td width="100%" bgcolor="#000000">
 1371:    <table width="100%" border="0" cellpadding="1" cellspacing="0">
 1372:     <tr>
 1373:      <td width="100%" bgcolor="#000000">
 1374:       <table border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
 1375:        <tr bgcolor="$tabcolor">
 1376:         <td><b>$lt{'pref'}</b></td>
 1377:         <td><b>$lt{'curr'}</b></td>
 1378:         <td><b>$lt{'actn'}?</b></td>
 1379:        </tr>
 1380:        <tr bgcolor="#dddddd">
 1381:        <td>$lt{'disa'}</td>
 1382:        <td>$lt{$discdisp}</td>
 1383:        <td><input type="checkbox" name="discdisp" onClick="discdispChk('0')" />&nbsp;$lt{'chgt'} "$dispchangeA"
 1384:            <br />
 1385:            <input type="checkbox" name="discdisp" onClick="discdispChk('1')" />&nbsp;$lt{'chgt'} "$dispchangeB"
 1386:        </td>
 1387:       </tr><tr bgcolor="#eeeeee">
 1388:        <td>$lt{'npmr'}</td>
 1389:        <td>$lt{$discmark}</td>
 1390:        <td><input type="checkbox" name="discmark" />$lt{'chgt'} "$markchange"</td>
 1391:       </tr><tr bgcolor="#dddddd">
 1392:        <td>$lt{'dotm'}</td>
 1393:        <td>$lt{$disctogg}</td>
 1394:        <td><input type="checkbox" name="disctogg" onClick="discdispChk('2')" />$lt{'chgt'} "$toggchange"</td>
 1395:       </tr>
 1396:      </table>
 1397:     </td>
 1398:    </tr>
 1399:   </table>
 1400:  </td>
 1401: </tr>
 1402: </table>
 1403: <br />
 1404: <br />
 1405: <input type="hidden" name="previous" value="$previous" />
 1406: <input type="hidden" name="$dispchgA" value=""/>
 1407: <input type="hidden" name="$dispchgB" value=""/>
 1408: <input type="hidden" name="$markchg" value=""/>
 1409: <input type="hidden" name="$toggchg" value="" />
 1410: <input type="button" name="sub" value="Store Changes" onClick="javascript:setDisp()" />
 1411: <br />
 1412: <br />
 1413: </form>
 1414: </body>
 1415: </html>
 1416: END
 1417:     return;
 1418: }
 1419: 
 1420: sub print_sortfilter_options {
 1421:     my ($r,$symb,$previous,$feedurl) = @_;
 1422:  # backward compatibility (bulletin boards used to be 'wrapped')
 1423:     if ($feedurl=~m|^/adm/wrapper/adm/.*/bulletinboard$|) {
 1424:         $feedurl=~s|^/adm/wrapper||;
 1425:     }
 1426:     my @sections = ();
 1427:     my $section_sel = '';
 1428:     my $numsections = 0;
 1429:     my $numvisible = 5;
 1430:     my ($classlist) = &Apache::loncoursedata::get_classlist(
 1431:                               $ENV{'request.course.id'},
 1432:                               $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
 1433:                               $ENV{'course.'.$ENV{'request.course.id'}.'.num'});
 1434:                                                                                    
 1435:     my $sec_index = &Apache::loncoursedata::CL_SECTION();
 1436:     my $status_index = &Apache::loncoursedata::CL_STATUS();
 1437:     my %sectioncount = ();
 1438:     while (my ($student,$data) = each %$classlist) {
 1439:         my ($section,$status) = ($data->[$sec_index],
 1440:                                  $data->[$status_index]);
 1441:         unless ($section eq '' || $section =~ /^\s*$/) {
 1442:             if (!defined($sectioncount{$section})) {
 1443:                 $sectioncount{$section} = 1;
 1444:                 $numsections ++;
 1445:             } else {
 1446:                 $sectioncount{$section} ++;
 1447:             }
 1448:         }
 1449:     }
 1450:                                                                                    
 1451:     if ($ENV{'request.course.sec'} !~ /^\s*$/) {
 1452:         @sections = ($ENV{'request.course.sec'});
 1453:         $numvisible = 1;
 1454:     } else {
 1455:         @sections = sort {$a cmp $b} keys(%sectioncount);
 1456:         unshift(@sections,'all'); # Put 'all' at the front of the list
 1457:         if ($numsections < 4) {
 1458:             $numvisible = $numsections + 1;
 1459:         }
 1460:     }
 1461:     foreach (@sections) {
 1462:         $section_sel .= "  <option value=\"$_\" />$_\n";
 1463:     }
 1464:                                                                                    
 1465:     my $function = &Apache::loncommon::get_users_function();
 1466:     my $tabcolor = &Apache::loncommon::designparm($function.'.tabbg',
 1467:                                                     $ENV{'user.domain'});
 1468:     my $bodytag=&Apache::loncommon::bodytag('Discussion options',
 1469:                                           '','');
 1470:     my %lt = &Apache::lonlocal::texthash(
 1471:         'diso' => 'Discussion sorting and filtering options',
 1472:         'diop' => 'Display Options',
 1473:         'curr' => 'Current setting ',
 1474:         'actn' => 'Action',
 1475:         'prca' => 'Options can be set that control the sort order of the posts, in addition to which posts are displayed.',
 1476:         'soor' => 'Sort order',
 1477:         'disp' => 'Specific user roles',
 1478:         'actv' => 'Specific role status',
 1479:         'spse' => 'Specific sections',
 1480:         'psub' => 'Pick specific users (by name)',
 1481:         'shal' => 'Show a list of current posters'
 1482:     );
 1483:     $r->print(<<END);
 1484: <html>
 1485: <head>
 1486: <title>$lt{'diso'}</title>
 1487: <meta http-equiv="pragma" content="no-cache" />
 1488: </head>
 1489: $bodytag
 1490: <form name="modifyshown" method="post" action="/adm/feedback">
 1491: <b>$lt{'diso'}</b><br/> $lt{'prca'}
 1492: <br /><br />
 1493: <table border="0">
 1494:  <tr>
 1495:   <td><b>$lt{'soor'}</b></td>
 1496:   <td>&nbsp;</td>
 1497:   <td><b>$lt{'disp'}</b></td>
 1498:   <td>&nbsp;</td>
 1499:   <td><b>$lt{'actv'}</b></td>
 1500:   <td>&nbsp;</td>
 1501:   <td><b>$lt{'spse'}</b></td>
 1502:   <td>&nbsp;</td>
 1503:   <td><b>$lt{'psub'}</b></td>
 1504:  </tr>
 1505:  <tr>
 1506:   <td>
 1507:    <select name="sortposts">
 1508:     <option value="ascdate" />Date order - oldest first
 1509:     <option value="descdate" />Date order - newest first
 1510:     <option value="thread" />Threaded
 1511:     <option value="subject" />By subject
 1512:     <option value="username" />By domain and username
 1513:     <option value="lastfirst" />By last name, first name
 1514:    </select>
 1515:   </td>
 1516:   <td>&nbsp;</td>
 1517:   <td>
 1518:    <select name="rolefilter" multiple="true" size="5">
 1519:     <option value="all" />All users
 1520:     <option value="st" />Students
 1521:     <option value="cc" />Course Coordinators
 1522:     <option value="in" />Instructors
 1523:     <option value="ta" />TAs
 1524:     <option value="pr" />Exam proctors
 1525:     <option value="cr" />Custom roles
 1526:    </select>
 1527:   </td>
 1528:   <td>&nbsp;</td>
 1529:   <td>
 1530:    <select name="statusfilter">
 1531:     <option value="all" />Roles of any status
 1532:     <option value="Active" />Only active roles
 1533:     <option value="Expired" />Only inactive roles
 1534:    </select>
 1535:   </td>
 1536:   <td>&nbsp;</td>
 1537:   <td>
 1538:    <select name="sectionpick" multiple="true" size="$numvisible">
 1539:     $section_sel
 1540:    </select>
 1541:   </td>
 1542:   <td>&nbsp;</td>
 1543:   <td><input type="checkbox" name="posterlist" value="$symb" />$lt{'shal'}</td>
 1544:  </tr>
 1545: </table>
 1546: <br />
 1547: <br />
 1548: <input type="hidden" name="previous" value="$previous" />
 1549: <input type="hidden" name="applysort" value="$symb" />
 1550: <input type="button" name="sub" value="Store Changes" onClick="javascript:document.modifyshown.submit()" />
 1551: <br />
 1552: <br />
 1553: </form>
 1554: </body>
 1555: </html>
 1556: END
 1557: }
 1558: 
 1559: sub print_showposters {
 1560:     my ($r,$symb,$previous,$feedurl,$sortposts) = @_;
 1561:  # backward compatibility (bulletin boards used to be 'wrapped')
 1562:     if ($feedurl=~m|^/adm/wrapper/adm/.*/bulletinboard$|) {
 1563:         $feedurl=~s|^/adm/wrapper||;
 1564:     }
 1565: # backward compatibility (bulletin boards used to be 'wrapped')
 1566:     my $ressymb=$symb;
 1567:     if ($ressymb =~ /bulletin___\d+___/) {
 1568:         unless ($ressymb =~ m|bulletin___\d+___adm/wrapper|) {
 1569:             $ressymb=~s|(bulletin___\d+___)|$1adm/wrapper|;
 1570:         }
 1571:     }
 1572:     my $crs='/'.$ENV{'request.course.id'};
 1573:     if ($ENV{'request.course.sec'}) {
 1574:         $crs.='_'.$ENV{'request.course.sec'};
 1575:     }
 1576:     $crs=~s/\_/\//g;
 1577:     my $seeid=&Apache::lonnet::allowed('rin',$crs);
 1578:     my %contrib=&Apache::lonnet::restore($ressymb,$ENV{'request.course.id'},
 1579:                           $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
 1580:                           $ENV{'course.'.$ENV{'request.course.id'}.'.num'});
 1581:     my %namesort = ();
 1582:     my %postcounts = ();
 1583:     my %lt=&Apache::lonlocal::texthash(
 1584:                      'diso' => 'Discussion filtering options',
 1585:     );
 1586:     my $bodytag=&Apache::loncommon::bodytag('Discussion options',
 1587:                                           '','');
 1588:     if ($contrib{'version'}) {
 1589:         for (my $idx=1;$idx<=$contrib{'version'};$idx++) {
 1590:             my $hidden=($contrib{'hidden'}=~/\.$idx\./);
 1591:             my $deleted=($contrib{'deleted'}=~/\.$idx\./);
 1592:             unless ((($hidden) && (!$seeid)) || ($deleted)) {
 1593:                 if ((!$contrib{$idx.':anonymous'}) || ($seeid)) {
 1594:                     my %names = &Apache::lonnet::get('environment',['firstname','lastname'],$contrib{$idx.':senderdomain'},$contrib{$idx.':sendername'});
 1595:                     my $lastname = $names{'lastname'};
 1596:                     my $firstname = $names{'firstname'};
 1597:                     if ($lastname eq '') {
 1598:                         $lastname = '_';
 1599:                     }
 1600:                     if ($firstname eq '') {
 1601:                         $firstname = '_';
 1602:                     }
 1603:                     unless (defined($namesort{$lastname})) {
 1604:                         %{$namesort{$lastname}} = ();
 1605:                     }
 1606:                     my $poster =  $contrib{$idx.':sendername'}.':'.$contrib{$idx.':senderdomain'};
 1607:                     $postcounts{$poster} ++;
 1608:                     if (defined($namesort{$lastname}{$firstname})) {
 1609:                         if (!grep/^$poster$/,@{$namesort{$lastname}{$firstname}}) {
 1610:                             push @{$namesort{$lastname}{$firstname}}, $poster;
 1611:                         }
 1612:                     } else {
 1613:                         @{$namesort{$lastname}{$firstname}} = ("$poster");
 1614:                     }
 1615:                 }
 1616:             }
 1617:         }
 1618:     }
 1619:     $r->print(<<END);
 1620: <html>
 1621: <head>
 1622: <title>$lt{'diso'}</title>
 1623: <meta http-equiv="pragma" content="no-cache" />
 1624: </head>
 1625: $bodytag
 1626:  <form name="pickpostersform" method="post">
 1627:   <table border="0">
 1628:    <tr>
 1629:     <td bgcolor="#777777">
 1630:      <table border="0" cellpadding="3">
 1631:       <tr bgcolor="#e6ffff">
 1632:        <td><b>No.</b></td>
 1633:        <td><b>Select</b></td>
 1634:        <td><b>Fullname</b><font color="#999999">(Username/domain)</font></td>
 1635:        <td><b>Posts</td>
 1636:       </tr>
 1637: END
 1638:     my $count = 0;
 1639:     foreach my $last (sort keys %namesort) {
 1640:         foreach my $first (sort keys %{$namesort{$last}}) {
 1641:             foreach (sort @{$namesort{$last}{$first}}) {
 1642:                 my ($uname,$udom) = split/:/,$_;
 1643:                 if (!$uname || !$udom) { 
 1644:                     next;
 1645:                 } else {
 1646:                     $count ++;
 1647:                     $r->print('<tr bgcolor="#ffffe6"><td align="right">'.$count.'</td><td align="center"><input name="stuinfo" type="checkbox" value="'.$_.'" /></td><td>'.$last.', '.$first.' ('.$uname.','.$udom.')</td><td>'.$postcounts{$_}.'</td></tr>');
 1648:                 }
 1649:             }
 1650:         }
 1651:     }
 1652:     $r->print(<<END);
 1653:      </table>
 1654:     </td>
 1655:    </tr>
 1656:   </table>
 1657: <br />
 1658: <input type="hidden" name="sortposts" value="$sortposts" />
 1659: <input type="hidden" name="userpick" value="$symb" />
 1660: <input type="button" name="store" value="Display posts" onClick="javascript:document.pickpostersform.submit()" />
 1661: </form>
 1662: </body>
 1663: </html>
 1664: END
 1665: }
 1666: 
 1667: sub get_post_versions {
 1668:     my ($versions,$incoming,$numver) = @_;
 1669:     my $p = HTML::LCParser->new(\$incoming);
 1670:     my $done = 0;                                                                       
 1671:     while ( (my $token = $p->get_tag("version")) && (!$done)) {
 1672:         my $num = $token->[1]{num};
 1673:         my $text = $p->get_text("/version");
 1674:         if (defined($numver)) {
 1675:             if ($num == $numver) {
 1676:                 $$versions{$numver}=$text;
 1677:                 $done = 1;
 1678:             }
 1679:         } else {
 1680:             $$versions{$num}=$text;
 1681:         }
 1682:     }
 1683:     return;
 1684: }
 1685: 
 1686: sub get_post_attachments {
 1687:     my ($attachments,$attachmenturls) = @_;
 1688:     my $num;
 1689:     my $p = HTML::LCParser->new(\$attachmenturls);
 1690:     while (my $token = $p->get_tag("attachment","filename","post"))  {
 1691:         if ($token->[0] eq "attachment") {
 1692:             $num = $token->[1]{id};
 1693:             %{$$attachments{$num}} =();
 1694:         } elsif ($token->[0] eq "filename") {
 1695:             $$attachments{$num}{'filename'} = $p->get_text("/filename");
 1696:         } elsif ($token->[0] eq "post") {
 1697:             my $id = $token->[1]{id};
 1698:             $$attachments{$num}{$id} = $p->get_text("/post");
 1699:         }
 1700:     }
 1701:     return;
 1702: }
 1703: 
 1704: sub fail_redirect {;
 1705:   my ($r,$feedurl) = @_;
 1706:   if ($feedurl=~/^\/adm\//) { $feedurl.='?register=1' };
 1707:   $r->print (<<ENDFAILREDIR);
 1708: <html>
 1709: <head><title>Feedback not sent</title>
 1710: <meta http-equiv="pragma" content="no-cache" />
 1711: <meta HTTP-EQUIV="Refresh" CONTENT="2; url=$feedurl" />
 1712: </head>
 1713: <body bgcolor="#FFFFFF">
 1714: <img align="right" src="/adm/lonIcons/lonlogos.gif" />
 1715: <b>Sorry, no recipients  ...</b>
 1716: </body>
 1717: </html>
 1718: ENDFAILREDIR
 1719: }
 1720: 
 1721: sub redirect_back {
 1722:   my ($r,$feedurl,$typestyle,$sendsomething,$sendposts,$status,$previous,$sort,$rolefilter,$statusfilter,$secpick,$numpicks) = @_;
 1723:   my $sorttag = '';
 1724:   my $roletag = '';
 1725:   my $statustag = '';
 1726:   my $sectag = '';
 1727:   my $userpicktag = '';
 1728:   my $qrystr = '';
 1729:   my $prevtag = '';
 1730:  # backward compatibility (bulletin boards used to be 'wrapped')
 1731:   if ($feedurl=~m|^/adm/wrapper/adm/.*/bulletinboard$|) {
 1732:       $feedurl=~s|^/adm/wrapper||;
 1733:   }
 1734:   if ($feedurl=~/^\/adm\//) { $feedurl.='?register=1' };
 1735:   if ($previous > 0) {
 1736:       $qrystr = 'previous='.$previous;
 1737:       if ($feedurl =~ /\?register=1/) {
 1738:           $feedurl .= '&'.$qrystr;
 1739:       } else {
 1740:           $feedurl .= '?'.$qrystr;
 1741:       }
 1742:       $prevtag = '<input type="hidden" name="previous" value="'.$previous.'" />';
 1743:   }
 1744:   if (defined($sort)) {
 1745:       my $sortqry = 'sortposts='.$sort;
 1746:       if (($feedurl =~ /\?register=1/) || ($feedurl =~ /\?previous=/)) {
 1747:           $feedurl .= '&'.$sortqry;
 1748:       } else {
 1749:           $feedurl .= '?'.$sortqry;
 1750:       }
 1751:       $sorttag = '<input type="hidden" name="sortposts" value="'.$sort.'" />';
 1752:       if ( (defined($numpicks)) && ($numpicks > 0) ) {
 1753:           my $userpickqry = 'totposters='.$numpicks;
 1754:           $feedurl .= '&'.$userpickqry;
 1755:           $userpicktag = '<input type="hidden" name="totposters" value="'.$numpicks.'" />';
 1756:       } else {
 1757:           my $roleqry = 'rolefilter='.$rolefilter;
 1758:           $feedurl .= '&'.$roleqry;
 1759:           $roletag = '<input type="hidden" name="rolefilter" value="'.$rolefilter.'" />';
 1760:           $feedurl .= '&statusfilter='.$statusfilter;
 1761:           $statustag ='<input type="hidden" name="statusfilter" value="'.$statusfilter.'" />';
 1762:           $feedurl .= '&sectionpick='.$secpick;
 1763:           $sectag = '<input type="hidden" name="sectionpick" value="'.$secpick.'" />';
 1764:       }
 1765:   }
 1766:   $r->print (<<ENDREDIR);
 1767: <html>
 1768: <head>
 1769: <title>Feedback sent</title>
 1770: <meta http-equiv="pragma" content="no-cache" />
 1771: <meta HTTP-EQUIV="Refresh" CONTENT="2; url=$feedurl" />
 1772: </head>
 1773: <body bgcolor="#FFFFFF" onLoad='if (window.name!="loncapaclient") { this.document.reldt.submit(); self.close(); }'>
 1774: <img align="right" src="/adm/lonIcons/lonlogos.gif" />
 1775: $typestyle
 1776: <b>Sent $sendsomething message(s), and $sendposts post(s).</b>
 1777: <font color="red">$status</font>
 1778: <form name="reldt" action="$feedurl" target="loncapaclient">
 1779: $prevtag
 1780: $sorttag
 1781: $statustag
 1782: $roletag
 1783: $sectag
 1784: $userpicktag
 1785: </form>
 1786: </body>
 1787: </html>
 1788: ENDREDIR
 1789: }
 1790: 
 1791: sub no_redirect_back {
 1792:   my ($r,$feedurl) = @_;
 1793:   my $nofeed=&mt('Sorry, no feedback possible on this resource  ...');
 1794:   $r->print (<<ENDNOREDIR);
 1795: <html>
 1796: <head><title>Feedback not sent</title>
 1797: <meta http-equiv="pragma" content="no-cache" />
 1798: ENDNOREDIR
 1799: 
 1800:   if ($feedurl!~/^\/adm\/feedback/) { 
 1801:     $r->print('<meta HTTP-EQUIV="Refresh" CONTENT="2; url='.$feedurl.'">');
 1802:   }
 1803:   
 1804:   $r->print (<<ENDNOREDIRTWO);
 1805: </head>
 1806: <body bgcolor="#FFFFFF" onLoad='if (window.name!="loncapaclient") { self.close(); }'>
 1807: <img align="right" src="/adm/lonIcons/lonlogos.gif" />
 1808: <b>$nofeed</b>
 1809: </body>
 1810: </html>
 1811: ENDNOREDIRTWO
 1812: }
 1813: 
 1814: sub screen_header {
 1815:     my ($feedurl) = @_;
 1816:     my $msgoptions='';
 1817:     my $discussoptions='';
 1818:     unless (($ENV{'form.replydisc'}) || ($ENV{'form.editdisc'})) {
 1819: 	if (($feedurl=~/^\/res\//) && ($feedurl!~/^\/res\/adm/)) {
 1820: 	    $msgoptions= 
 1821: 		'<p><input type="checkbox" name="author" /> '.
 1822: 		&mt('Feedback to resource author').'</p>';
 1823: 	}
 1824: 	if (&feedback_available(1)) {
 1825: 	    $msgoptions.=
 1826: 		'<br /><input type="checkbox" name="question" /> '.
 1827: 		&mt('Question about resource content');
 1828: 	}
 1829: 	if (&feedback_available(0,1)) {
 1830: 	    $msgoptions.=
 1831: 		'<br /><input type="checkbox" name="course" /> '.
 1832: 		&mt('Question/Comment/Feedback about course content');
 1833: 	}
 1834: 	if (&feedback_available(0,0,1)) {
 1835: 	    $msgoptions.=
 1836: 		'<br /><input type="checkbox" name="policy" /> '.
 1837: 		&mt('Question/Comment/Feedback about course policy');
 1838: 	}
 1839:     }
 1840:     if ($ENV{'request.course.id'}) {
 1841: 	if (&discussion_open() &&
 1842: 	    &Apache::lonnet::allowed('pch',
 1843: 				     $ENV{'request.course.id'}.
 1844: 				     ($ENV{'request.course.sec'}?'/'.$ENV{'request.course.sec'}:''))) {
 1845: 	    $discussoptions='<input type="checkbox" name="discuss" onClick="this.form.anondiscuss.checked=false;" '.
 1846: 		($ENV{'form.replydisc'}?' checked="1"':'').' /> '.
 1847: 		&mt('Contribution to course discussion of resource');
 1848: 	    $discussoptions.='<br /><input type="checkbox" name="anondiscuss" onClick="this.form.discuss.checked=false;" /> '.
 1849: 		&mt('Anonymous contribution to course discussion of resource').
 1850: 		' <i>('.&mt('name only visible to course faculty').')</i>';
 1851:       }
 1852:     }
 1853:     if ($msgoptions) { $msgoptions='<h2><img src="/adm/lonMisc/feedback.gif" />'.&mt('Sending Messages').'</h2>'.$msgoptions; }
 1854:     if ($discussoptions) { 
 1855: 	$discussoptions='<h2><img src="/adm/lonMisc/chat.gif" />'.&mt('Discussion Contributions').'</h2>'.$discussoptions; }
 1856:     return $msgoptions.$discussoptions;
 1857: }
 1858: 
 1859: sub resource_output {
 1860:   my ($feedurl) = @_;
 1861:   my $usersaw=&Apache::lonnet::ssi_body($feedurl);
 1862:   $usersaw=~s/\<body[^\>]*\>//gi;
 1863:   $usersaw=~s/\<\/body\>//gi;
 1864:   $usersaw=~s/\<html\>//gi;
 1865:   $usersaw=~s/\<\/html\>//gi;
 1866:   $usersaw=~s/\<head\>//gi;
 1867:   $usersaw=~s/\<\/head\>//gi;
 1868:   $usersaw=~s/action\s*\=/would_be_action\=/gi;
 1869:   return $usersaw;
 1870: }
 1871: 
 1872: sub clear_out_html {
 1873:   my ($message,$override)=@_;
 1874:   unless (&Apache::lonhtmlcommon::htmlareablocked()) { return $message; }
 1875: # Always allow the <m>-tag
 1876:   my %html=(M=>1);
 1877: # Check if more is allowed
 1878:   my $cid=$ENV{'request.course.id'};
 1879:   if (($ENV{"course.$cid.allow_limited_html_in_feedback"} =~ m/yes/i) ||
 1880:       ($override)) {
 1881:       # allows <B> <I> <P> <A> <LI> <OL> <UL> <EM> <BR> <TT> <STRONG> 
 1882:       # <BLOCKQUOTE> <DIV .*> <DIV> <IMG> <M> <SPAN> <H1> <H2> <H3> <H4> <SUB>
 1883:       # <SUP>
 1884:       %html=(B=>1, I=>1, P=>1, A=>1, LI=>1, OL=>1, UL=>1, EM=>1,
 1885: 	     BR=>1, TT=>1, STRONG=>1, BLOCKQUOTE=>1, DIV=>1, IMG=>1,
 1886: 	     M=>1, SUB=>1, SUP=>1, SPAN=>1, 
 1887: 	     H1=>1, H2=>1, H3=>1, H4=>1, H5=>1);
 1888:   }
 1889: # Do the substitution of everything that is not explicitly allowed
 1890:   $message =~ s/\<(\/?\s*(\w+)[^\>\<]*)/
 1891: 	  {($html{uc($2)}&&(length($1)<1000))?"\<$1":"\&lt;$1"}/ge;
 1892:   $message =~ s/(\<?\s*(\w+)[^\<\>]*)\>/
 1893: 	  {($html{uc($2)}&&(length($1)<1000))?"$1\>":"$1\&gt;"}/ge;
 1894:   return $message;
 1895: }
 1896: 
 1897: sub assemble_email {
 1898:   my ($feedurl,$message,$prevattempts,$usersaw,$useranswer)=@_;
 1899:   my $email=<<"ENDEMAIL";
 1900: Refers to <a href="$feedurl">$feedurl</a>
 1901: 
 1902: $message
 1903: ENDEMAIL
 1904:     my $citations=<<"ENDCITE";
 1905: <h2>Previous attempts of student (if applicable)</h2>
 1906: $prevattempts
 1907: <br /><hr />
 1908: <h2>Original screen output (if applicable)</h2>
 1909: $usersaw
 1910: <h2>Correct Answer(s) (if applicable)</h2>
 1911: $useranswer
 1912: ENDCITE
 1913:   return ($email,$citations);
 1914: }
 1915: 
 1916: sub secapply {
 1917:     my $rec=shift;
 1918:     my $defaultflag=shift;
 1919:     $rec=~s/\s+//g;
 1920:     $rec=~s/\@/\:/g;
 1921:     my ($adr,$sections)=($rec=~/^([^\(]+)\(([^\)]+)\)/);
 1922:     if ($sections) {
 1923: 	foreach (split(/\;/,$sections)) {
 1924:             if (($_ eq $ENV{'request.course.sec'}) ||
 1925:                 ($defaultflag && ($_ eq '*'))) {
 1926:                 return $adr; 
 1927:             }
 1928:         }
 1929:     } else {
 1930:        return $rec;
 1931:     }
 1932:     return '';
 1933: }
 1934: 
 1935: sub decide_receiver {
 1936:   my ($feedurl,$author,$question,$course,$policy,$defaultflag) = @_;
 1937:   my $typestyle='';
 1938:   my %to=();
 1939:   if ($ENV{'form.author'}||$author) {
 1940:     $typestyle.='Submitting as Author Feedback<br>';
 1941:     $feedurl=~/^\/res\/(\w+)\/(\w+)\//;
 1942:     $to{$2.':'.$1}=1;
 1943:   }
 1944:   if ($ENV{'form.question'}||$question) {
 1945:     $typestyle.='Submitting as Question<br>';
 1946:     foreach (split(/\,/,
 1947: 		   $ENV{'course.'.$ENV{'request.course.id'}.'.question.email'})
 1948: 	     ) {
 1949: 	my $rec=&secapply($_,$defaultflag);
 1950:         if ($rec) { $to{$rec}=1; }
 1951:     } 
 1952:   }
 1953:   if ($ENV{'form.course'}||$course) {
 1954:     $typestyle.='Submitting as Comment<br />';
 1955:     foreach (split(/\,/,
 1956: 		   $ENV{'course.'.$ENV{'request.course.id'}.'.comment.email'})
 1957: 	     ) {
 1958: 	my $rec=&secapply($_,$defaultflag);
 1959:         if ($rec) { $to{$rec}=1; }
 1960:     } 
 1961:   }
 1962:   if ($ENV{'form.policy'}||$policy) {
 1963:     $typestyle.='Submitting as Policy Feedback<br />';
 1964:     foreach (split(/\,/,
 1965: 		   $ENV{'course.'.$ENV{'request.course.id'}.'.policy.email'})
 1966: 	     ) {
 1967: 	my $rec=&secapply($_,$defaultflag);
 1968:         if ($rec) { $to{$rec}=1; }
 1969:     } 
 1970:   }
 1971:   if ((scalar(%to) eq '0') && (!$defaultflag)) {
 1972:      ($typestyle,%to)=
 1973: 	 &decide_receiver($feedurl,$author,$question,$course,$policy,1);
 1974:   }
 1975:   return ($typestyle,%to);
 1976: }
 1977: 
 1978: sub feedback_available {
 1979:     my ($question,$course,$policy)=@_;
 1980:     my ($typestyle,%to)=&decide_receiver('',0,$question,$course,$policy);
 1981:     return scalar(%to);
 1982: }
 1983: 
 1984: sub send_msg {
 1985:   my ($feedurl,$email,$citations,$attachmenturl,%to)=@_;
 1986:   my $status='';
 1987:   my $sendsomething=0;
 1988:   foreach (keys %to) {
 1989:     if ($_) {
 1990:       my $declutter=&Apache::lonnet::declutter($feedurl);
 1991:       unless (&Apache::lonmsg::user_normal_msg(split(/\:/,$_),
 1992:                'Feedback ['.$declutter.']',$email,$citations,$feedurl,
 1993:                 $attachmenturl)=~/ok/) {
 1994: 	$status.='<br />'.&mt('Error sending message to').' '.$_.'<br />';
 1995:       } else {
 1996: 	$sendsomething++;
 1997:       }
 1998:     }
 1999:   }
 2000: 
 2001:     my %record=&Apache::lonnet::restore('_feedback');
 2002:     my ($temp)=keys %record;
 2003:     unless ($temp=~/^error\:/) {
 2004:        my %newrecord=();
 2005:        $newrecord{'resource'}=$feedurl;
 2006:        $newrecord{'subnumber'}=$record{'subnumber'}+1;
 2007:        unless (&Apache::lonnet::cstore(\%newrecord,'_feedback') eq 'ok') {
 2008: 	   $status.='<br />'.&mt('Not registered').'<br />';
 2009:        }
 2010:     }
 2011:        
 2012:   return ($status,$sendsomething);
 2013: }
 2014: 
 2015: sub adddiscuss {
 2016:     my ($symb,$email,$anon,$attachmenturl,$subject)=@_;
 2017:     my $status='';
 2018:     if (&discussion_open() &&
 2019: 	&Apache::lonnet::allowed('pch',$ENV{'request.course.id'}.
 2020:         ($ENV{'request.course.sec'}?'/'.$ENV{'request.course.sec'}:''))) {
 2021: 
 2022:     my %contrib=('message'      => $email,
 2023:                  'sendername'   => $ENV{'user.name'},
 2024:                  'senderdomain' => $ENV{'user.domain'},
 2025:                  'screenname'   => $ENV{'environment.screenname'},
 2026:                  'plainname'    => $ENV{'environment.firstname'}.' '.
 2027: 		                   $ENV{'environment.middlename'}.' '.
 2028:                                    $ENV{'environment.lastname'}.' '.
 2029:                                    $ENV{'enrironment.generation'},
 2030:                  'attachmenturl'=> $attachmenturl,
 2031:                  'subject'      => $subject);
 2032:     if ($ENV{'form.replydisc'}) {
 2033: 	$contrib{'replyto'}=(split(/\:\:\:/,$ENV{'form.replydisc'}))[1];
 2034:     }
 2035:     if ($anon) {
 2036: 	$contrib{'anonymous'}='true';
 2037:     }
 2038:     if (($symb) && ($email)) {
 2039:         if ($ENV{'form.editdisc'}) {
 2040:             my %newcontrib = ();
 2041:             $contrib{'ip'}=$ENV{'REMOTE_ADDR'};
 2042:             $contrib{'host'}=$Apache::lonnet::perlvar{'lonHostID'};
 2043:             $contrib{'timestamp'} = time;
 2044:             $contrib{'history'} = '';
 2045:             my $numoldver = 0;
 2046:             my ($oldsymb,$oldidx)=split(/\:\:\:/,$ENV{'form.editdisc'});
 2047:             $oldsymb=~s|(bulletin___\d+___)adm/wrapper/|$1|;
 2048: # get timestamp for last post and history
 2049:             my %oldcontrib=&Apache::lonnet::restore($oldsymb,$ENV{'request.course.id'},
 2050:                      $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
 2051:                      $ENV{'course.'.$ENV{'request.course.id'}.'.num'});
 2052:             if (defined($oldcontrib{$oldidx.':replyto'})) {
 2053:                 $contrib{'replyto'} = $oldcontrib{$oldidx.':replyto'};
 2054:             }
 2055:             if (defined($oldcontrib{$oldidx.':history'})) {
 2056:                 if ($oldcontrib{$oldidx.':history'} =~ /:/) {
 2057:                     my @oldversions = split/:/,$oldcontrib{$oldidx.':history'};
 2058:                     $numoldver = @oldversions;
 2059:                 } else {
 2060:                     $numoldver = 1;
 2061:                 }
 2062:                 $contrib{'history'} = $oldcontrib{$oldidx.':history'}.':';
 2063:             }
 2064:             my $numnewver = $numoldver + 1;
 2065:             if (defined($oldcontrib{$oldidx.':subject'})) {
 2066:                 if ($oldcontrib{$oldidx.':subject'} =~ /^<version num="0">/) {
 2067:                     $contrib{'subject'} = '<version num="'.$numnewver.'">'.&HTML::Entities::encode($contrib{'subject'},'<>&"').'</version>';
 2068:                     $contrib{'subject'} = $oldcontrib{$oldidx.':subject'}.$contrib{'subject'};
 2069:                 } else {
 2070:                     $contrib{'subject'} = '<version num="0">'.&HTML::Entities::encode($oldcontrib{$oldidx.':subject'},'<>&"').'</version><version num="1">'.&HTML::Entities::encode($contrib{'subject'},'<>&"').'</version>';
 2071:                 }
 2072:             } 
 2073:             if (defined($oldcontrib{$oldidx.':message'})) {
 2074:                 if ($oldcontrib{$oldidx.':message'} =~ /^<version num="0">/) {
 2075:                     $contrib{'message'} = '<version num="'.$numnewver.'">'.&HTML::Entities::encode($contrib{'message'},'<>&"').'</version>';
 2076:                     $contrib{'message'} = $oldcontrib{$oldidx.':message'}.$contrib{'message'};
 2077:                 } else {
 2078:                     $contrib{'message'} = '<version num="0">'.&HTML::Entities::encode($oldcontrib{$oldidx.':message'},'<>&"').'</version><version num="1">'.&HTML::Entities::encode($contrib{'message'},'<>&"').'</version>';
 2079:                 }
 2080:             }
 2081:             $contrib{'history'} .= $oldcontrib{$oldidx.':timestamp'};
 2082:             foreach (keys %contrib) {
 2083:                 my $key = $oldidx.':'.&Apache::lonnet::escape($oldsymb).':'.$_;                                                                               
 2084:                 $newcontrib{$key} = $contrib{$_};
 2085:             }
 2086:             my $put_reply = &Apache::lonnet::putstore($ENV{'request.course.id'},
 2087:                   \%newcontrib,
 2088:                   $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
 2089:                   $ENV{'course.'.$ENV{'request.course.id'}.'.num'});
 2090:             $status='Editing class discussion'.($anon?' (anonymous)':'');
 2091:         } else {
 2092:            $status='Adding to class discussion'.($anon?' (anonymous)':'').': '.
 2093:            &Apache::lonnet::store(\%contrib,$symb,$ENV{'request.course.id'},
 2094:                      $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
 2095: 		     $ENV{'course.'.$ENV{'request.course.id'}.'.num'});
 2096:         }
 2097:         my %storenewentry=($symb => time);
 2098:         $status.='<br />'.&mt('Updating discussion time').': '.
 2099:         &Apache::lonnet::put('discussiontimes',\%storenewentry,
 2100:                      $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
 2101: 		     $ENV{'course.'.$ENV{'request.course.id'}.'.num'});
 2102:     }
 2103:     my %record=&Apache::lonnet::restore('_discussion');
 2104:     my ($temp)=keys %record;
 2105:     unless ($temp=~/^error\:/) {
 2106:        my %newrecord=();
 2107:        $newrecord{'resource'}=$symb;
 2108:        $newrecord{'subnumber'}=$record{'subnumber'}+1;
 2109:        $status.='<br />'.&mt('Registering').': '.
 2110:                &Apache::lonnet::cstore(\%newrecord,'_discussion');
 2111:     }
 2112:     } else {
 2113: 	$status.='Failed.';
 2114:     }
 2115:     return $status.'<br />';   
 2116: }
 2117: 
 2118: # ----------------------------------------------------------- Preview function
 2119: 
 2120: sub show_preview {
 2121:     my $r=shift;
 2122:     my $message=&clear_out_html($ENV{'form.comment'});
 2123:     $message=~s/\n/\<br \/\>/g;
 2124:     $message=&Apache::lonspeller::markeduptext($message);
 2125:     $message=&Apache::lontexconvert::msgtexconverted($message);
 2126:     my $subject=&clear_out_html($ENV{'form.subject'});
 2127:     $subject=~s/\n/\<br \/\>/g;
 2128:     $subject=&Apache::lontexconvert::msgtexconverted($subject);
 2129:     $r->print('<table border="2"><tr><td>'.
 2130:        '<b>Subject:</b> '.$subject.'<br /><br />'.
 2131:        $message.'</td></tr></table>');
 2132: }
 2133: 
 2134: sub generate_preview_button {
 2135:     my $pre=&mt("Show Preview and Check Spelling");
 2136:     return(<<ENDPREVIEW);
 2137: <form name="preview" action="/adm/feedback?preview=1" method="post" target="preview">
 2138: <input type="hidden" name="subject">
 2139: <input type="hidden" name="comment" />
 2140: <input type="button" value="$pre"
 2141: onClick="if (typeof(document.mailform.onsubmit)!='undefined') {document.mailform.onsubmit();};this.form.comment.value=document.mailform.comment.value;this.form.subject.value=document.mailform.subject.value;this.form.submit();" />
 2142: </form>
 2143: ENDPREVIEW
 2144: }
 2145: 
 2146: sub modify_attachments {
 2147:     my ($r,$currnewattach,$currdelold,$symb,$idx,$attachmenturls)=@_;
 2148:     my $subject=&clear_out_html($ENV{'form.subject'});
 2149:     $subject=~s/\n/\<br \/\>/g;
 2150:     $subject=&Apache::lontexconvert::msgtexconverted($subject);
 2151:     my $timestamp=$ENV{'form.timestamp'};
 2152:     my $numoldver=$ENV{'form.numoldver'};
 2153:     my $bodytag=&Apache::loncommon::bodytag('Discussion Post Attachments',
 2154:                                           '','');
 2155:     my $msg = '';
 2156:     my %attachments = ();
 2157:     my %currattach = ();
 2158:     if ($idx) {
 2159:         &extract_attachments($attachmenturls,$idx,$numoldver,\$msg,\%attachments,\%currattach,$currdelold);
 2160:     }
 2161:     $r->print(<<END);
 2162: <html>
 2163: <head>
 2164: <title>Managing Attachments</title>
 2165: <script>
 2166:  function setAction () {
 2167:    document.modattachments.action = document.modattachments.origpage.value;
 2168:    document.modattachments.submit();
 2169:  }
 2170: </script> 
 2171: </head>
 2172: $bodytag
 2173: <form name="modattachments" method="post" enctype="multipart/form-data" action="/adm/feedback?attach=$symb">
 2174:  <table border="2">
 2175:   <tr>
 2176:    <td>
 2177:     <b>Subject:</b>$subject</b><br /><br />
 2178: END
 2179:     if ($idx) {
 2180:         if ($attachmenturls) {
 2181:             my @currold = keys %currattach;
 2182:             if (@currold > 0) {
 2183:                 $r->print("The following attachments were part of the most recent saved version of this posting.<br />Check the checkboxes for any you wish to remove<br />\n");  
 2184:                 foreach my $id (@currold) {
 2185:                     my $attachurl = &HTML::Entities::decode($attachments{$id}{'filename'}); 
 2186:                     $attachurl =~ m#/([^/]+)$#;
 2187:                     $r->print('<input type="checkbox" name="deloldattach" value="'.$id.'" />&nbsp;'.$1.'<br />'."\n");
 2188:                 }
 2189:                 $r->print("<br />");
 2190:             }
 2191:         }
 2192:     }
 2193:     if (@{$currnewattach} > 0) {
 2194:         $r->print("The following attachments have been uploaded for inclusion with this posting.<br />Check the checkboxes for any you wish to remove<br />\n");
 2195:         foreach (@{$currnewattach}) {
 2196:             $_ =~ m#/([^/]+)$#;
 2197:             $r->print('<input type="checkbox" name="delnewattach" value="'.$_.'" />&nbsp;'.$1.'<br />'."\n");
 2198:         }
 2199:         $r->print("<br />"); 
 2200:     }
 2201:     $r->print(<<END);
 2202:    Add a new attachment to this post.&nbsp;<input type="file" name="addnewattach" /><input type="button" name="upload" value="Upload" onClick="this.form.submit()" />    
 2203:    </td>
 2204:   </tr>
 2205:  </table>
 2206: <input type="hidden" name="subject" value="$ENV{'form.subject'}" />
 2207: <input type="hidden" name="comment" value="$ENV{'form.comment'}" />
 2208: <input type="hidden" name="timestamp" value="$ENV{'form.timestamp'}" />
 2209: <input type="hidden" name="idx" value="$ENV{'form.idx'}" />
 2210: <input type="hidden" name="numoldver" value="$ENV{'form.numoldver'}" />
 2211: <input type="hidden" name="origpage" value="$ENV{'form.origpage'}" />
 2212: <input type="hidden" name="anondiscuss" value="$ENV{'form.anondiscuss'}" />
 2213: <input type="hidden" name="discuss" value="$ENV{'form.discuss'}" />
 2214: END
 2215:     foreach (@{$currnewattach}) {
 2216:         $r->print('<input type="hidden" name="currnewattach" value="'.$_.'" />'."\n");
 2217:     }
 2218:     foreach (@{$currdelold}) {
 2219:         $r->print('<input type="hidden" name="deloldattach" value="'.$_.'" />'."\n");
 2220:     }
 2221:     $r->print(<<END);
 2222:  <input type="button" name="rtntoedit" value="Store Changes" onClick="setAction()"/>
 2223: </form>
 2224: </body>
 2225: </html>
 2226: END
 2227:     return;
 2228: }
 2229: 
 2230: sub process_attachments {
 2231:     my ($currnewattach,$currdelold,$keepold) = @_;
 2232:     if (exists($ENV{'form.currnewattach'})) {
 2233:         if (ref($ENV{'form.currnewattach'}) eq 'ARRAY') {
 2234:             @{$currnewattach} = @{$ENV{'form.currnewattach'}};
 2235:         } else {
 2236:             $$currnewattach[0] = $ENV{'form.currnewattach'};
 2237:         }
 2238:     }
 2239:     if (exists($ENV{'form.deloldattach'})) {
 2240:         if (ref($ENV{'form.deloldattach'}) eq 'ARRAY') {
 2241:             @{$currdelold} = @{$ENV{'form.deloldattach'}};
 2242:         } else {
 2243:             $$currdelold[0] = $ENV{'form.deloldattach'};
 2244:         }
 2245:     }
 2246:     if (exists($ENV{'form.delnewattach'})) {
 2247:         my @currdelnew = ();
 2248:         my @currnew = ();
 2249:         if (ref($ENV{'form.delnewattach'}) eq 'ARRAY') {
 2250:             @currdelnew = @{$ENV{'form.delnewattach'}};
 2251:         } else {
 2252:             $currdelnew[0] = $ENV{'form.delnewattach'};
 2253:         }
 2254:         foreach my $newone (@{$currnewattach}) {
 2255:             my $delflag = 0;
 2256:             foreach (@currdelnew) {
 2257:                 if ($newone eq $_) {
 2258:                     $delflag = 1;
 2259:                     last;
 2260:                 }
 2261:             }
 2262:             unless ($delflag) {
 2263:                 push @currnew, $newone;
 2264:             }
 2265:         }
 2266:         @{$currnewattach} = @currnew;
 2267:     }
 2268:     if (exists($ENV{'form.keepold'})) {
 2269:         if (ref($ENV{'form.keepold'}) eq 'ARRAY') {
 2270:             @{$keepold} = @{$ENV{'form.keepold'}};
 2271:         } else {
 2272:             $$keepold[0] = $ENV{'form.keepold'};
 2273:         }
 2274:     }
 2275: }
 2276: 
 2277: sub generate_attachments_button {
 2278:     my ($idx,$attachnum,$ressymb,$now,$currnewattach,$deloldattach,$numoldver,$mode) = @_;
 2279:     my $origpage = $ENV{'REQUEST_URI'};
 2280:     my $att=$attachnum.' '.&mt("attachments");
 2281:     my $response = (<<END);
 2282: <form name="attachment" action="/adm/feedback?attach=$ressymb" method="post">
 2283: Click to add/remove attachments:&nbsp;<input type="button" value="$att"
 2284: onClick="this.form.subject.value=document.mailform.subject.value;this.form.comment.value=document.mailform.comment.value;
 2285: END
 2286:     unless ($mode eq 'board') {
 2287:         $response .= 'javascript:anonchk();';
 2288:     }
 2289:     $response .= (<<ENDATTACH);
 2290: this.form.submit();" />
 2291: <input type="hidden" name="origpage" value="$origpage" />
 2292: <input type="hidden" name="idx" value="$idx" />
 2293: <input type="hidden" name="timestamp" value="$now" />
 2294: <input type="hidden" name="subject" />
 2295: <input type="hidden" name="comment" />
 2296: <input type="hidden" name="anondiscuss" value = "0";
 2297: <input type="hidden" name="discuss" value = "0";
 2298: <input type="hidden" name="numoldver" value="$numoldver" />
 2299: ENDATTACH
 2300:     if (defined($deloldattach)) {
 2301:         if (@{$deloldattach} > 0) {
 2302:             foreach (@{$deloldattach}) {
 2303:                 $response .= '<input type="hidden" name="deloldattach" value="'.$_.'" />'."\n";
 2304:             }
 2305:         }
 2306:     }
 2307:     if (defined($currnewattach)) {
 2308:         if (@{$currnewattach} > 0) {
 2309:             foreach (@{$currnewattach}) {
 2310:                 $response .= '<input type="hidden" name="currnewattach" value="'.$_.'" />'."\n";
 2311:             }
 2312:         }
 2313:     }
 2314:     $response .= '</form>';
 2315:     return $response;
 2316: }
 2317: 
 2318: sub extract_attachments {
 2319:     my ($attachmenturls,$idx,$numoldver,$message,$attachments,$currattach,$currdelold) = @_;
 2320:     if ($attachmenturls =~ m/^<attachment id="0">/) {
 2321:         &get_post_attachments($attachments,$attachmenturls);
 2322:         foreach my $id (sort keys %{$attachments}) {
 2323:             if (exists($$attachments{$id}{$numoldver})) {
 2324:                 if (defined($currdelold)) {
 2325:                     if (@{$currdelold} > 0) {
 2326:                         unless (grep/^$id$/,@{$currdelold}) {
 2327:                             $$currattach{$id} = $$attachments{$id}{$numoldver}; 
 2328:                         }
 2329:                     } else {
 2330:                         $$currattach{$id} = $$attachments{$id}{$numoldver};
 2331:                     }
 2332:                 } else {
 2333:                     $$currattach{$id} = $$attachments{$id}{$numoldver};
 2334:                 }
 2335:             }
 2336:         }
 2337:         my @attached = (sort { $a <=> $b } keys %{$currattach});
 2338:         if (@attached == 1) {
 2339:             my $id = $attached[0];
 2340:             my $attachurl = &HTML::Entities::decode($$attachments{$id}{'filename'});
 2341:             $attachurl=~m|/([^/]+)$|;
 2342:             $$message.='<br /><a href="'.$attachurl.'"><tt>'.
 2343:             $1.'</tt></a><br />';
 2344:             &Apache::lonnet::allowuploaded('/adm/feedback',
 2345:                                    $attachurl);
 2346:         } elsif (@attached > 1) {
 2347:             $$message.='<ol>';
 2348:             foreach (@attached) {
 2349:                 my $id = $_;
 2350:                 my $attachurl = &HTML::Entities::decode($$attachments{$id}{'filename'});
 2351:                 my ($fname)
 2352:                   =($attachurl=~m|/([^/]+)$|);
 2353:                 $$message .= '<li><a href="'.$attachurl.
 2354:                   '"><tt>'.
 2355:                   $fname.'</tt></a></li>';
 2356:                 &Apache::lonnet::allowuploaded('/adm/feedback',
 2357:                                  $attachurl);
 2358:             }
 2359:             $$message .= '</ol><br />';
 2360:         }
 2361:     } else {
 2362:         my ($fname)
 2363:            =($attachmenturls=~m|/([^/]+)$|);
 2364:         $$message .='<p>'.&mt('Attachment').
 2365:            ': <a href="'.$attachmenturls.
 2366:            '"><tt>'.
 2367:            $fname.'</tt></a></p>';
 2368:            $$attachments{0} = $attachmenturls;
 2369:            $$currattach{'0'} = 'n';
 2370:            &Apache::lonnet::allowuploaded('/adm/feedback',
 2371:                              $attachmenturls);
 2372:     }
 2373: }
 2374: 
 2375: sub construct_attachmenturl {
 2376:     my ($currnewattach,$keepold,$symb,$idx)=@_;
 2377:     my $oldattachmenturl;
 2378:     my $newattachmenturl;
 2379:     my $startnum = 0;
 2380:     my $currver = 0;
 2381:     if (($ENV{'form.editdisc'}) && ($idx)) {
 2382:         my %contrib=&Apache::lonnet::restore($symb,$ENV{'request.course.id'},
 2383:                        $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
 2384:                        $ENV{'course.'.$ENV{'request.course.id'}.'.num'});
 2385:         $oldattachmenturl = $contrib{$idx.':attachmenturl'};
 2386:         if ($contrib{$idx.':history'}) {
 2387:             if ($contrib{$idx.':history'} =~ /:/) {
 2388:                 my @oldversions = split/:/,$contrib{$idx.':history'};
 2389:                 $currver = 1 + scalar(@oldversions);
 2390:             } else {
 2391:                 $currver = 2;
 2392:             }
 2393:         } else {
 2394:             $currver = 1;
 2395:         }
 2396:         if ($oldattachmenturl) {
 2397:             if ($oldattachmenturl =~ m/^<attachment id="0">/) {
 2398:                 my %attachments = ();
 2399:                 my $prevver = $currver-1;
 2400:                 &get_post_attachments(\%attachments,$oldattachmenturl);
 2401:                 my $numattach = keys %attachments;
 2402:                 $startnum += $numattach;
 2403:                 foreach my $num (sort {$a <=> $b} keys %attachments) {
 2404:                     $newattachmenturl .= '<attachment id="'.$num.'"><filename>'.$attachments{$num}{'filename'}.'</filename>';
 2405:                     foreach (sort {$a <=> $b} keys %{$attachments{$num}}) { 
 2406:                         $newattachmenturl .= '<post id="'.$_.'">'.$attachments{$num}{$_}.'</post>';
 2407:                     }
 2408:                     if (grep/^$num$/,@{$keepold}) {
 2409:                         $newattachmenturl .= '<post id="'.$currver.'">'.$attachments{$num}{$prevver}.'</post>';
 2410:                     }
 2411:                     $newattachmenturl .= '</attachment>';
 2412:                 }
 2413:             } else {
 2414:                 $newattachmenturl = '<attachment id="0"><filename>'.&HTML::Entities::encode($oldattachmenturl).'<post id="0">n</post>';
 2415:                 unless (grep/^0$/,@{$keepold}) {
 2416:                     $newattachmenturl .= '<post id="1">n</post>';
 2417:                 }
 2418:                 $newattachmenturl .= '</attachment>';
 2419:                 $startnum ++;
 2420:             }
 2421:         }
 2422:     }
 2423:     for (my $i=0; $i<@{$currnewattach}; $i++) {
 2424:         my $attachnum = $startnum + $i;
 2425:         $newattachmenturl .= '<attachment id="'.$attachnum.'"><filename>'.&HTML::Entities::encode($$currnewattach[$i]).'</filename><post id="'.$currver.'">n</post></attachment>';
 2426:     }
 2427:     return $newattachmenturl; 
 2428: }
 2429:   
 2430: sub handler {
 2431:   my $r = shift;
 2432:   if ($r->header_only) {
 2433:      &Apache::loncommon::content_type($r,'text/html');
 2434:      $r->send_http_header;
 2435:      return OK;
 2436:   }
 2437: 
 2438: # --------------------------- Get query string for limited number of parameters
 2439: 
 2440:   &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
 2441:          ['hide','unhide','deldisc','postdata','preview','replydisc','editdisc','threadedon','threadedoff','onlyunread','allposts','onlyunmark','previous','markread','markonread','markondisp','toggoff','toggon','modifydisp','changes','navmaps','navurl','sortfilter','sortposts','applysort','rolefilter','statusfilter','sectionpick','posterlist','userpick','attach','origpage','currnewattach','deloldattach','keepold','allversions']);
 2442:   if ($ENV{'form.discsymb'}) {
 2443:       my $symb = $ENV{'form.discsymb'};
 2444:       my $readkey = $symb.'_read';
 2445:       my %readinghash = ();
 2446:       my $chgcount = 0;
 2447:       %readinghash = &Apache::lonnet::get('nohist_'.$ENV{'request.course.id'}.'_discuss',[$readkey],$ENV{'user.domain'},$ENV{'user.name'});
 2448:       foreach my $key (keys %ENV) {
 2449:           if ($key =~ m/^form\.postunread_(\d+)/) {
 2450:               if ($readinghash{$readkey} =~ /\.$1\./) {
 2451:                   $readinghash{$readkey} =~ s/\.$1\.//;
 2452:                   $chgcount ++;
 2453:               }
 2454:           } elsif ($key =~ m/^form\.postread_(\d+)/) {
 2455:               unless ($readinghash{$readkey} =~ /\.$1\./) {
 2456:                   $readinghash{$readkey} .= '.'.$1.'.';
 2457:                   $chgcount ++;
 2458:               }
 2459:           }
 2460:       }
 2461:       if ($chgcount > 0) {
 2462:           &Apache::lonnet::put('nohist_'.$ENV{'request.course.id'}.'_discuss',\%readinghash,$ENV{'user.domain'},$ENV{'user.name'});
 2463:       }
 2464:       &Apache::loncommon::content_type($r,'text/html');
 2465:       $r->send_http_header;
 2466:       my ($map,$ind,$url)=&Apache::lonnet::decode_symb($symb);
 2467:       my $previous=$ENV{'form.previous'};
 2468:       my $feedurl = &Apache::lonnet::clutter($url);
 2469:       &redirect_back($r,$feedurl,&mt('Marked postings read/unread').'<br />','0','0','',$previous,'','','',);
 2470:       return OK;
 2471:   }
 2472:   if ($ENV{'form.allversions'}) {
 2473:       &Apache::loncommon::content_type($r,'text/html');
 2474:       $r->send_http_header;
 2475:       my $bodytag=&Apache::loncommon::bodytag('Discussion Post Versions',
 2476:                                           '','');
 2477:       $r->print (<<END);
 2478: <html>
 2479: <head>
 2480: <title>Post Versions</title>
 2481: <meta http-equiv="pragma" content="no-cache" />
 2482: </head>
 2483: $bodytag
 2484: END
 2485:       my $crs='/'.$ENV{'request.course.id'};
 2486:       if ($ENV{'request.course.sec'}) {
 2487:           $crs.='_'.$ENV{'request.course.sec'};
 2488:       }
 2489:       $crs=~s/\_/\//g;
 2490:       my $seeid=&Apache::lonnet::allowed('rin',$crs);
 2491:       my ($symb,$idx)=split(/\:\:\:/,$ENV{'form.allversions'});
 2492:       my $ressymb=$symb;
 2493:       unless ($ressymb =~ m|bulletin___\d+___adm/wrapper|) {
 2494:           $ressymb=~s|(bulletin___\d+___)|$1adm/wrapper|;
 2495:       }
 2496:       if ($idx > 0) {
 2497:           my %contrib=&Apache::lonnet::restore($ressymb,$ENV{'request.course.id'},
 2498:                $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
 2499:                $ENV{'course.'.$ENV{'request.course.id'}.'.num'});
 2500:           if ($contrib{$idx.':history'}) {
 2501:               my $attachmenturls = $contrib{$idx.':attachmenturl'};
 2502:               my @postversions = ();
 2503:               my %messages = ();
 2504:               my %subjects = ();
 2505:               if ($contrib{$idx.':history'} =~ m/:/) {
 2506:                   @postversions = split/:/,$contrib{$idx.':history'};
 2507:               } else {
 2508:                   @postversions = ("$contrib{$idx.':history'}");
 2509:               }
 2510:               if (@postversions > 0) {
 2511:                   &get_post_versions(\%messages,$contrib{$idx.':message'});
 2512:                   &get_post_versions(\%subjects,$contrib{$idx.':subject'});
 2513:                   push @postversions,$contrib{$idx.':timestamp'};
 2514:                   my $screenname=&Apache::loncommon::screenname(
 2515:                                             $contrib{$idx.':sendername'},
 2516:                                             $contrib{$idx.':senderdomain'});
 2517:                   my $plainname=&Apache::loncommon::nickname(
 2518:                                             $contrib{$idx.':sendername'},
 2519:                                             $contrib{$idx.':senderdomain'});
 2520:                   my $sender=&Apache::loncommon::aboutmewrapper(
 2521:                                      $plainname,
 2522:                                      $contrib{$idx.':sendername'},
 2523:                                      $contrib{$idx.':senderdomain'}).' ('.
 2524:                                      $contrib{$idx.':sendername'}.' at '.
 2525:                                      $contrib{$idx.':senderdomain'}.')';
 2526:                   if ($contrib{$idx.':anonymous'}) {
 2527:                       $sender.=' ['.&mt('anonymous').'] '.$screenname;
 2528:                   }
 2529:                   $r->print('<b>'.$sender.'</b><br /><ul>');
 2530:                   for (my $i=0; $i<@postversions; $i++) {
 2531:                       my ($timesent,$message,$subject,$attachmsg);
 2532:                       $timesent = &Apache::lonlocal::locallocaltime($postversions[$i]);
 2533:                       $message=&HTML::Entities::decode($messages{$i});
 2534:                       $subject=&HTML::Entities::decode($subjects{$i});
 2535:                       $message=~s/\n/\<br \/\>/g;
 2536:                       $message=&Apache::lontexconvert::msgtexconverted($message);
 2537:                       $subject=~s/\n/\<br \/\>/g;
 2538:                       $subject=&Apache::lontexconvert::msgtexconverted($subject);
 2539:                       if ($attachmenturls) {
 2540:                           my %attachments = ();
 2541:                           my %currattach = ();
 2542:                           &extract_attachments($attachmenturls,$idx,$i,\$attachmsg,\%attachments,\%currattach);
 2543:                       }
 2544:                       if ($attachmsg) {
 2545:                           $attachmsg = '<br />Attachments:'.$attachmsg.'<br />';
 2546:                       } else {
 2547:                           $attachmsg = '<br />';
 2548:                       }
 2549:                       $r->print (<<END);
 2550: <li><b>$subject</b>, $timesent<br />
 2551: $message<br />
 2552: $attachmsg</li>
 2553: END
 2554:                   }
 2555:                   $r->print('</ul></body></html>');
 2556:               }
 2557:           }
 2558:       }
 2559:       return OK;
 2560:   }
 2561:   if ($ENV{'form.posterlist'}) {
 2562:       &Apache::loncommon::content_type($r,'text/html');
 2563:       $r->send_http_header;
 2564:       my $symb=$ENV{'form.posterlist'};
 2565:       my $sortposts = $ENV{'form.sortposts'};
 2566:       my ($map,$ind,$url)=&Apache::lonnet::decode_symb($symb);
 2567:       my $previous=$ENV{'form.previous'};
 2568:       my $feedurl = &Apache::lonnet::clutter($url);
 2569:  # backward compatibility (bulletin boards used to be 'wrapped')
 2570:       if ($feedurl=~m|^/adm/wrapper/adm/.*/bulletinboard$|) {
 2571:           $feedurl=~s|^/adm/wrapper||;
 2572:       }
 2573:       &print_showposters($r,$symb,$previous,$feedurl,$sortposts);
 2574:       return OK;
 2575:   }
 2576:   if ($ENV{'form.userpick'}) {
 2577:       &Apache::loncommon::content_type($r,'text/html');
 2578:       $r->send_http_header;
 2579:       my $symb=$ENV{'form.userpick'};
 2580:       my ($map,$ind,$url)=&Apache::lonnet::decode_symb($symb);
 2581:       my $previous=$ENV{'form.previous'};
 2582: # backward compatibility (bulletin boards used to be 'wrapped')
 2583:       my $ressymb=$symb;
 2584:       unless ($ressymb =~ m|bulletin___\d+___adm/wrapper|) {
 2585:           $ressymb=~s|(bulletin___\d+___)|$1adm/wrapper|;
 2586:       }
 2587:       my $sort=$ENV{'form.sortposts'};
 2588:       my @posters = ();
 2589:       if (ref($ENV{'form.stuinfo'}) eq 'ARRAY') {
 2590:           @posters = $ENV{'form.stuinfo'};
 2591:       } else {
 2592:           $posters[0] = $ENV{'form.stuinfo'};
 2593:       }
 2594:       my $numpicks = @posters;
 2595:       if (defined($ENV{'form.userpick'})) {
 2596:           my %discinfo = ();
 2597:           $discinfo{$ressymb.'_userpick'} = join('&',@posters);
 2598:           &Apache::lonnet::put('nohist_'.$ENV{'request.course.id'}.'_discuss',\%discinfo,$ENV{'user.domain'},$ENV{'user.name'});
 2599:       }
 2600:       my $feedurl = &Apache::lonnet::clutter($url);
 2601:  # backward compatibility (bulletin boards used to be 'wrapped')
 2602:       if ($feedurl=~m|^/adm/wrapper/adm/.*/bulletinboard$|) {
 2603:           $feedurl=~s|^/adm/wrapper||;
 2604:       }
 2605:       &redirect_back($r,$feedurl,&mt('Changed sort/filter').'<br />','0','0','',$previous,$sort,'','','',$numpicks);
 2606:       return OK;
 2607:   }
 2608:   if ($ENV{'form.applysort'}) {
 2609:       &Apache::loncommon::content_type($r,'text/html');
 2610:       $r->send_http_header;
 2611:       my $symb=$ENV{'form.applysort'};
 2612:       my ($map,$ind,$url)=&Apache::lonnet::decode_symb($symb);
 2613:       my $previous=$ENV{'form.previous'};
 2614:       my $sort = $ENV{'form.sortposts'};
 2615:       my $rolefilter = $ENV{'form.rolefilter'};
 2616:       my $statusfilter = $ENV{'form.statusfilter'};
 2617:       my $secpick = $ENV{'form.sectionpick'};
 2618:       my $feedurl = &Apache::lonnet::clutter($url);
 2619:  # backward compatibility (bulletin boards used to be 'wrapped')
 2620:       if ($feedurl=~m|^/adm/wrapper/adm/.*/bulletinboard$|) {
 2621:           $feedurl=~s|^/adm/wrapper||;
 2622:       }
 2623:       &redirect_back($r,$feedurl,&mt('Changed sort/filter').'<br />','0','0','',$previous,$sort,$rolefilter,$statusfilter,$secpick);
 2624:       return OK;
 2625:   } elsif ($ENV{'form.sortfilter'}) {
 2626:       &Apache::loncommon::content_type($r,'text/html');
 2627:       $r->send_http_header;
 2628:       my $symb=$ENV{'form.sortfilter'};
 2629:       my ($map,$ind,$url)=&Apache::lonnet::decode_symb($symb);
 2630:       my $previous=$ENV{'form.previous'};
 2631:       my $feedurl = &Apache::lonnet::clutter($url);
 2632:  # backward compatibility (bulletin boards used to be 'wrapped')
 2633:       if ($feedurl=~m|^/adm/wrapper/adm/.*/bulletinboard$|) {
 2634:           $feedurl=~s|^/adm/wrapper||;
 2635:       }
 2636:       &print_sortfilter_options($r,$symb,$previous,$feedurl);
 2637:       return OK;
 2638:   } elsif ($ENV{'form.navmaps'}) {
 2639:       my %discinfo = ();
 2640:       my @resources = ();
 2641:       if ($ENV{'form.navmaps'} =~ /:/) {
 2642:           @resources = split/:/,$ENV{'form.navmaps'};
 2643:       } else {
 2644:           @resources = ("$ENV{'form.navmaps'}");
 2645:       }
 2646:       my $numitems = @resources;
 2647:       my $feedurl = '/adm/navmaps';
 2648:       if ($ENV{'form.navurl'}) {
 2649:           $feedurl .= '?'.$ENV{'form.navurl'};
 2650:       }
 2651:       my %lt = &Apache::lonlocal::texthash(
 2652:           'mnpa' => 'Marked "New" posts as read in a total of',
 2653:           'robb' => 'resources/bulletin boards.'
 2654:       );       
 2655:       foreach (@resources) {
 2656: # backward compatibility (bulletin boards used to be 'wrapped')
 2657:           my $ressymb=$_;
 2658:           if ($ressymb =~ m/bulletin___\d+___/) {
 2659:               unless ($ressymb =~ m|bulletin___\d+___adm/wrapper|) {
 2660:                   $ressymb=~s|(bulletin___\d+___)|$1adm/wrapper/|;
 2661:               }
 2662:           }
 2663:           my $lastkey = $ressymb.'_lastread';
 2664:           $discinfo{$lastkey} = time;
 2665:       }
 2666:       &Apache::lonnet::put('nohist_'.$ENV{'request.course.id'}.'_discuss',\%discinfo,$ENV{'user.domain'},$ENV{'user.name'});
 2667:       &Apache::loncommon::content_type($r,'text/html');
 2668:       $r->send_http_header;
 2669:       $r->print (<<ENDREDIR);
 2670: <html>
 2671: <head>
 2672: <title>New posts marked as read</title>
 2673: <meta http-equiv="pragma" content="no-cache" />
 2674: <meta HTTP-EQUIV="Refresh" CONTENT="2; url=$feedurl" />
 2675: </head>
 2676: <body bgcolor="#FFFFFF" onLoad='if (window.name!="loncapaclient") { this.document.reldt.submit(); self.close(); }'>
 2677: <img align="right" src="/adm/lonIcons/lonlogos.gif" />
 2678: <b>$lt{'mnpa'} $numitems $lt{'robb'}</b>
 2679: <form name="reldt" action="$feedurl" target="loncapaclient">
 2680: </form>
 2681: </body>
 2682: </html>
 2683: ENDREDIR
 2684:       return OK;
 2685:   } elsif ($ENV{'form.modifydisp'}) {
 2686:       &Apache::loncommon::content_type($r,'text/html');
 2687:       $r->send_http_header;
 2688:       my $symb=$ENV{'form.modifydisp'};
 2689:       my ($map,$ind,$url)=&Apache::lonnet::decode_symb($symb);
 2690:       my $previous=$ENV{'form.previous'};
 2691:       my ($dispchgA,$dispchgB,$markchg,$toggchg) = split/_/,$ENV{'form.changes'};
 2692:       my $feedurl = &Apache::lonnet::clutter($url);
 2693:  # backward compatibility (bulletin boards used to be 'wrapped')  
 2694:       if ($feedurl=~m|^/adm/wrapper/adm/.*/bulletinboard$|) {
 2695:           $feedurl=~s|^/adm/wrapper||;
 2696:       }
 2697:       &print_display_options($r,$symb,$previous,$dispchgA,$dispchgB,$markchg,$toggchg,$feedurl);
 2698:       return OK;
 2699:   } elsif (($ENV{'form.markondisp'}) || ($ENV{'form.markonread'}) || ($ENV{'form.allposts'}) || ($ENV{'form.onlyunread'}) || $ENV{'form.onlyunmark'} || $ENV{'form.toggoff'} || $ENV{'form.toggon'} ) {
 2700:       &Apache::loncommon::content_type($r,'text/html');
 2701:       $r->send_http_header;
 2702:       my $previous=$ENV{'form.previous'};
 2703:       my ($map,$ind,$url);
 2704:       if ( ($ENV{'form.toggoff'}) || ($ENV{'form.toggon'}) ) {
 2705: # ------------------------------ Modify setting for read/unread toggle for each post 
 2706:           my $symb=$ENV{'form.toggoff'}?$ENV{'form.toggoff'}:$ENV{'form.toggon'};
 2707:           my $ressymb = $symb;
 2708:           ($map,$ind,$url)=&Apache::lonnet::decode_symb($symb);
 2709:           unless ($ressymb =~ m|bulletin___\d+___adm/wrapper|) {
 2710:               $ressymb=~s|(bulletin___\d+___)|$1adm/wrapper|;
 2711:           }
 2712:           my %discinfo = ();
 2713:           my $toggkey = $ressymb.'_readtoggle';
 2714:           if ($ENV{'form.toggon'}) {
 2715:               $discinfo{$toggkey} = 1;
 2716:           } elsif ($ENV{'form.toggoff'}) {
 2717:               $discinfo{$toggkey} = 0;
 2718:           }
 2719:           &Apache::lonnet::put('nohist_'.$ENV{'request.course.id'}.'_discuss',\%discinfo,$ENV{'user.domain'},$ENV{'user.name'});
 2720:       }
 2721:       if (($ENV{'form.markondisp'}) || ($ENV{'form.markonread'})) {
 2722: # ---------------------- Modify setting for identification of 'NEW' posts in this discussion
 2723:           my $symb=$ENV{'form.markondisp'}?$ENV{'form.markondisp'}:$ENV{'form.markonread'};
 2724:           my $ressymb = $symb;
 2725:           ($map,$ind,$url)=&Apache::lonnet::decode_symb($symb);
 2726:           unless ($ressymb =~ m|bulletin___\d+___adm/wrapper|) {
 2727:               $ressymb=~s|(bulletin___\d+___)|$1adm/wrapper|;
 2728:           }
 2729:           my %discinfo = ();
 2730:           my $lastkey = $ressymb.'_lastread';
 2731:           my $ondispkey = $ressymb.'_markondisp';
 2732:           if ($ENV{'form.markondisp'}) {
 2733:               $discinfo{$lastkey} = time;
 2734:               $discinfo{$ondispkey} = 1;
 2735:           } elsif ($ENV{'form.markonread'}) {
 2736:               if ( $previous > 0 ) {
 2737:                   $discinfo{$lastkey} = $previous;
 2738:               }
 2739:               $discinfo{$ondispkey} = 0;
 2740:           }
 2741:           &Apache::lonnet::put('nohist_'.$ENV{'request.course.id'}.'_discuss',\%discinfo,$ENV{'user.domain'},$ENV{'user.name'});
 2742:       }
 2743:       if (($ENV{'form.allposts'}) || ($ENV{'form.onlyunread'}) || ($ENV{'form.onlyunmark'}) ) {
 2744: # ----------------------------------------------------------------- Modify display setting for this discussion 
 2745:           my $symb;
 2746:           if ($ENV{'form.allposts'}) {
 2747:               $symb = $ENV{'form.allposts'};
 2748:           } elsif ($ENV{'form.onlyunread'}) {
 2749:               $symb = $ENV{'form.onlyunread'};
 2750:           } else {
 2751:               $symb = $ENV{'form.onlyunmark'};
 2752:           }
 2753:           my $ressymb = $symb;
 2754:           ($map,$ind,$url)=&Apache::lonnet::decode_symb($symb);
 2755:           unless ($ressymb =~ m|bulletin___\d+___adm/wrapper|) {
 2756:               $ressymb=~s|(bulletin___\d+___)|$1adm/wrapper|;
 2757:           }
 2758:           my %discinfo = ();
 2759:           if ($ENV{'form.allposts'}) {
 2760:               $discinfo{$ressymb.'_showonlyunread'} = 0;
 2761:               $discinfo{$ressymb.'_showonlyunmark'} = 0;
 2762:           } elsif ($ENV{'form.onlyunread'}) {
 2763:               $discinfo{$ressymb.'_showonlyunread'} = 1;
 2764:           } else {
 2765:               $discinfo{$ressymb.'_showonlyunmark'} = 1;
 2766:           }
 2767:           &Apache::lonnet::put('nohist_'.$ENV{'request.course.id'}.'_discuss',\%discinfo,$ENV{'user.domain'},$ENV{'user.name'});
 2768:       }
 2769:       if (($ENV{'form.markonread'}) || ($ENV{'form.allposts'}) || ($ENV{'form.onlyunread'}) || ($ENV{'form.onlyunmark'}) ||($ENV{'form.toggoff'}) || ($ENV{'form.toggon'}) ) {
 2770:           &redirect_back($r,&Apache::lonnet::clutter($url),&mt('Changed display status').'<br />','0','0','',$previous);
 2771:       } else {
 2772:           &redirect_back($r,&Apache::lonnet::clutter($url),&mt('Changed display status').'<br />','0','0');
 2773:       }
 2774:       return OK;
 2775:   } elsif ($ENV{'form.markread'}) {
 2776: # ----------------------------------------------------------------- Mark new posts not NEW 
 2777:       &Apache::loncommon::content_type($r,'text/html');
 2778:       $r->send_http_header;
 2779:       my $symb=$ENV{'form.markread'};
 2780:       my $ressymb = $symb;
 2781:       my ($map,$ind,$url)=&Apache::lonnet::decode_symb($symb);
 2782:       unless ($ressymb =~ m|bulletin___\d+___adm/wrapper|) {
 2783:           $ressymb=~s|(bulletin___\d+___)|$1adm/wrapper|;
 2784:       }
 2785:       my %discinfo = ();
 2786:       my $lastkey = $ressymb.'_lastread';
 2787:       $discinfo{$lastkey} = time;
 2788:       &Apache::lonnet::put('nohist_'.$ENV{'request.course.id'}.'_discuss',\%discinfo,$ENV{'user.domain'},$ENV{'user.name'});
 2789:       &redirect_back($r,&Apache::lonnet::clutter($url),&mt('Changed reading status').'<br />','0','0');
 2790:       return OK;
 2791:   } elsif (($ENV{'form.hide'}) || ($ENV{'form.unhide'})) {
 2792: # ----------------------------------------------------------------- Hide/unhide
 2793:     &Apache::loncommon::content_type($r,'text/html');
 2794:     $r->send_http_header;
 2795: 
 2796:     my $entry=$ENV{'form.hide'}?$ENV{'form.hide'}:$ENV{'form.unhide'};
 2797: 
 2798:     my ($symb,$idx)=split(/\:\:\:/,$entry);
 2799:     my ($map,$ind,$url)=&Apache::lonnet::decode_symb($symb);
 2800: 
 2801:     my %contrib=&Apache::lonnet::restore($symb,$ENV{'request.course.id'},
 2802:                      $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
 2803: 		     $ENV{'course.'.$ENV{'request.course.id'}.'.num'});
 2804: 
 2805:         
 2806:     my $currenthidden=$contrib{'hidden'};
 2807:     my $currentstudenthidden=$contrib{'studenthidden'};
 2808: 
 2809:     my $crs='/'.$ENV{'request.course.id'};
 2810:     if ($ENV{'request.course.sec'}) {
 2811:         $crs.='_'.$ENV{'request.course.sec'};
 2812:     }
 2813:     $crs=~s/\_/\//g;
 2814:     my $seeid=&Apache::lonnet::allowed('rin',$crs);
 2815:     
 2816:     if ($ENV{'form.hide'}) {
 2817: 	$currenthidden.='.'.$idx.'.';
 2818:         unless ($seeid) {
 2819:             $currentstudenthidden.='.'.$idx.'.';
 2820:         }
 2821:     } else {
 2822:         $currenthidden=~s/\.$idx\.//g;
 2823:     }
 2824:     my %newhash=('hidden' => $currenthidden);
 2825:     if ( ($ENV{'form.hide'}) && (!$seeid) ) {
 2826:         $newhash{'studenthidden'} = $currentstudenthidden;
 2827:     }
 2828: 
 2829:     &Apache::lonnet::store(\%newhash,$symb,$ENV{'request.course.id'},
 2830:                      $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
 2831: 		     $ENV{'course.'.$ENV{'request.course.id'}.'.num'});
 2832: 
 2833:     &redirect_back($r,&Apache::lonnet::clutter($url),
 2834:        &mt('Changed discussion status').'<br />','0','0','',$ENV{'form.previous'});
 2835:   } elsif (($ENV{'form.threadedon'}) || ($ENV{'form.threadedoff'})) {
 2836:       &Apache::loncommon::content_type($r,'text/html');
 2837:       $r->send_http_header;
 2838:       if ($ENV{'form.threadedon'}) {
 2839: 	  &Apache::lonnet::put('environment',{'threadeddiscussion' => 'on'});
 2840: 	  &Apache::lonnet::appenv('environment.threadeddiscussion' => 'on');
 2841:       } else {
 2842:  	  &Apache::lonnet::del('environment',['threadeddiscussion']);
 2843: 	  &Apache::lonnet::delenv('environment\.threadeddiscussion');
 2844:       }
 2845:       my $symb=$ENV{'form.threadedon'}?$ENV{'form.threadedon'}:$ENV{'form.threadedoff'};
 2846:       my ($map,$ind,$url)=&Apache::lonnet::decode_symb($symb);
 2847:       &redirect_back($r,&Apache::lonnet::clutter($url),
 2848: 		     &mt('Changed discussion view mode').'<br />','0','0','',$ENV{'form.previous'});
 2849:   } elsif ($ENV{'form.deldisc'}) {
 2850: # --------------------------------------------------------------- Hide for good
 2851:     &Apache::loncommon::content_type($r,'text/html');
 2852:     $r->send_http_header;
 2853: 
 2854:     my $entry=$ENV{'form.deldisc'};
 2855: 
 2856:     my ($symb,$idx)=split(/\:\:\:/,$entry);
 2857:     my ($map,$ind,$url)=&Apache::lonnet::decode_symb($symb);
 2858: 
 2859:     my %contrib=&Apache::lonnet::restore($symb,$ENV{'request.course.id'},
 2860:                      $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
 2861: 		     $ENV{'course.'.$ENV{'request.course.id'}.'.num'});
 2862: 
 2863:         
 2864:     my $currentdeleted=$contrib{'deleted'};
 2865:     
 2866:     $currentdeleted.='.'.$idx.'.';
 2867: 
 2868:     my %newhash=('deleted' => $currentdeleted);
 2869: 
 2870:     &Apache::lonnet::store(\%newhash,$symb,$ENV{'request.course.id'},
 2871:                      $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
 2872: 		     $ENV{'course.'.$ENV{'request.course.id'}.'.num'});
 2873: 
 2874:     &redirect_back($r,&Apache::lonnet::clutter($url),
 2875:        &mt('Changed discussion status').'<br />','0','0','',$ENV{'form.previous'});
 2876:   } elsif ($ENV{'form.preview'}) {
 2877: # -------------------------------------------------------- User wants a preview
 2878:       $r->content_type('text/html');
 2879:       $r->send_http_header;
 2880:       &show_preview($r);
 2881:   } elsif ($ENV{'form.attach'}) {
 2882: # -------------------------------------------------------- Work on attachments
 2883:       &Apache::loncommon::content_type($r,'text/html');
 2884:       $r->send_http_header;
 2885:       &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},['subject','comment','addnewattach','delnewattach','timestamp','numoldver','idx','anondiscuss','discuss']);
 2886:       my @currnewattach = ();
 2887:       my @currdelold = ();
 2888:       my @keepold = ();
 2889:       &process_attachments(\@currnewattach,\@currdelold,\@keepold);
 2890:       if (exists($ENV{'form.addnewattach.filename'})) {
 2891:           unless (length($ENV{'form.addnewattach'})>131072) {
 2892:               my $subdir = 'feedback/'.$ENV{'form.timestamp'};
 2893:               my $newattachment=&Apache::lonnet::userfileupload('addnewattach',undef,$subdir);
 2894:               push @currnewattach, $newattachment;
 2895:           }
 2896:       }
 2897:       my $attachmenturls = '';
 2898:       my $idx = $ENV{'form.idx'};
 2899:       my $symb = $ENV{'form.attach'};
 2900:       if ($idx) {
 2901:           my %contrib=&Apache::lonnet::restore($symb,$ENV{'request.course.id'},
 2902:                          $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
 2903:                          $ENV{'course.'.$ENV{'request.course.id'}.'.num'});
 2904:           $attachmenturls = $contrib{$idx.':attachmenturl'};
 2905:       }
 2906:       &modify_attachments($r,\@currnewattach,\@currdelold,$symb,$idx,$attachmenturls);
 2907:   } elsif ($ENV{'form.chgreads'}) {
 2908:       &Apache::loncommon::content_type($r,'text/html');
 2909:       $r->send_http_header;
 2910:       my ($map,$ind,$url)=&Apache::lonnet::decode_symb($ENV{'form.chgreads'});
 2911:       &redirect_back($r,&Apache::lonnet::clutter($url),
 2912:        &mt('Changed read status').'<br />','0','0');
 2913:   } else {
 2914: # ------------------------------------------------------------- Normal feedback
 2915:   my $feedurl=$ENV{'form.postdata'};
 2916:   $feedurl=~s/^http\:\/\///;
 2917:   $feedurl=~s/^$ENV{'SERVER_NAME'}//;
 2918:   $feedurl=~s/^$ENV{'HTTP_HOST'}//;
 2919:   $feedurl=~s/\?.+$//;
 2920: 
 2921:   my $symb;
 2922:   if ($ENV{'form.replydisc'}) {
 2923:       $symb=(split(/\:\:\:/,$ENV{'form.replydisc'}))[0];
 2924:       my ($map,$id,$url)=&Apache::lonnet::decode_symb($symb);
 2925:       $feedurl=&Apache::lonnet::clutter($url);
 2926:   } elsif ($ENV{'form.editdisc'}) {
 2927:       $symb=(split(/\:\:\:/,$ENV{'form.editdisc'}))[0];
 2928:       my ($map,$id,$url)=&Apache::lonnet::decode_symb($symb);
 2929:       $feedurl=&Apache::lonnet::clutter($url);
 2930:   } elsif ($ENV{'form.origpage'}) {
 2931:       $symb=""; 
 2932:   } else {
 2933:       $symb=&Apache::lonnet::symbread($feedurl);
 2934:   }
 2935:   unless ($symb) {
 2936:       $symb=$ENV{'form.symb'};
 2937:       if ($symb) {
 2938: 	  my ($map,$id,$url)=&Apache::lonnet::decode_symb($symb);
 2939:           $feedurl=&Apache::lonnet::clutter($url);
 2940:       }
 2941:   }
 2942:   my $goahead=1;
 2943:   if ($feedurl=~/\.(problem|exam|quiz|assess|survey|form)$/) {
 2944:       unless ($symb) { $goahead=0; }
 2945:   }
 2946:   # backward compatibility (bulletin boards used to be 'wrapped')
 2947:   if ($feedurl=~m|^/adm/wrapper/adm/.*/bulletinboard$|) {
 2948:       $feedurl=~s|^/adm/wrapper||;
 2949:   }
 2950:   if ($goahead) {
 2951: # Go ahead with feedback, no ambiguous reference
 2952:     &Apache::loncommon::content_type($r,'text/html');
 2953:     $r->send_http_header;
 2954:   
 2955:     if (
 2956:       (
 2957:        ($feedurl=~m:^/res:) && ($feedurl!~m:^/res/adm:)
 2958:       ) 
 2959:       || 
 2960:       ($ENV{'request.course.id'} && ($feedurl!~m:^/adm:))
 2961:       ||
 2962:       ($ENV{'request.course.id'} && ($symb=~/^bulletin\_\_\_/))
 2963:      ) {
 2964: # --------------------------------------------------- Print login screen header
 2965:     unless ($ENV{'form.sendit'}) {
 2966:       my $options=&screen_header($feedurl);
 2967:       if ($options) {
 2968:         &mail_screen($r,$feedurl,$options);
 2969:       } else {
 2970: 	&fail_redirect($r,$feedurl);
 2971:       }
 2972:     } else {
 2973:       
 2974: # Get previous user input
 2975:       my $prevattempts=&Apache::loncommon::get_previous_attempt(
 2976:             $symb,$ENV{'user.name'},$ENV{'user.domain'},
 2977:             $ENV{'request.course.id'});
 2978: 
 2979: # Get output from resource
 2980:       my $usersaw=&resource_output($feedurl);
 2981: 
 2982: # Get resource answer (need to allow student to view grades for this to work)
 2983:       &Apache::lonnet::appenv(('allowed.vgr'=>'F'));
 2984:       my $useranswer=&Apache::loncommon::get_student_answers(
 2985:                        $symb,$ENV{'user.name'},$ENV{'user.domain'},
 2986: 		       $ENV{'request.course.id'});
 2987:       &Apache::lonnet::delenv('allowed.vgr');
 2988: # Get attachments, if any, and not too large
 2989:       my $attachmenturl='';
 2990:       if (($ENV{'form.origpage'}) || ($ENV{'form.editdisc'}) || ($ENV{'form.replydisc'})) {
 2991:           my ($symb,$idx);
 2992:           if ($ENV{'form.replydisc'}) {
 2993:               ($symb,$idx)=split(/\:\:\:/,$ENV{'form.replydisc'});
 2994:           } elsif ($ENV{'form.editdisc'}) {
 2995:               ($symb,$idx)=split(/\:\:\:/,$ENV{'form.editdisc'});
 2996:           } elsif ($ENV{'form.origpage'}) {
 2997:               $symb = $ENV{'form.symb'};
 2998:           }
 2999:           my @currnewattach = ();
 3000:           my @deloldattach = ();
 3001:           my @keepold = ();
 3002:           &process_attachments(\@currnewattach,\@deloldattach,\@keepold);
 3003:           $symb=~s|(bulletin___\d+___)adm/wrapper/|$1|;
 3004:           $attachmenturl=&construct_attachmenturl(\@currnewattach,\@keepold,$symb,$idx);
 3005:       } elsif ($ENV{'form.attachment.filename'}) {
 3006: 	  unless (length($ENV{'form.attachment'})>131072) {
 3007: 	      $attachmenturl=&Apache::lonnet::userfileupload('attachment',undef,'feedback');
 3008: 	  }
 3009:       }
 3010: # Filter HTML out of message (could be nasty)
 3011:       my $message=&clear_out_html($ENV{'form.comment'});
 3012: 
 3013: # Assemble email
 3014:       my ($email,$citations)=&assemble_email($feedurl,$message,$prevattempts,
 3015:           $usersaw,$useranswer);
 3016:  
 3017: # Who gets this?
 3018:       my ($typestyle,%to) = &decide_receiver($feedurl);
 3019: 
 3020: # Actually send mail
 3021:       my ($status,$numsent)=&send_msg($feedurl,$email,$citations,
 3022:           $attachmenturl,%to);
 3023: 
 3024: # Discussion? Store that.
 3025: 
 3026:       my $numpost=0;
 3027:       if ($ENV{'form.discuss'}) {
 3028:           my $subject = &clear_out_html($ENV{'form.subject'});
 3029: 	  $typestyle.=&adddiscuss($symb,$message,0,$attachmenturl,$subject);
 3030: 	  $numpost++;
 3031:       }
 3032: 
 3033:       if ($ENV{'form.anondiscuss'}) {
 3034:           my $subject = &clear_out_html($ENV{'form.subject'});
 3035: 	  $typestyle.=&adddiscuss($symb,$message,1,$attachmenturl,$subject);
 3036: 	  $numpost++;
 3037:       }
 3038: 
 3039: 
 3040: # Receipt screen and redirect back to where came from
 3041:       &redirect_back($r,$feedurl,$typestyle,$numsent,$numpost,$status,$ENV{'form.previous'});
 3042: 
 3043:     }
 3044:    } else {
 3045: # Unable to give feedback
 3046:     &no_redirect_back($r,$feedurl);
 3047:    }
 3048:   } else {
 3049: # Ambiguous Problem Resource
 3050:       if ( &Apache::lonnet::mod_perl_version() == 2 ) {
 3051: 	  &Apache::lonnet::cleanenv();
 3052:       }
 3053:       $r->internal_redirect('/adm/ambiguous');
 3054:   }
 3055: }
 3056:   return OK;
 3057: } 
 3058: 
 3059: 1;
 3060: __END__

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