File:  [LON-CAPA] / loncom / interface / lonfeedback.pm
Revision 1.108: download - view: text, annotated - select for diffs
Wed Jul 28 18:50:26 2004 UTC (19 years, 11 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
Allow multiple attachments in discussion posts.  Different edited versions can include different (or the same) attachments.
Work still required: clean-up interface, change storage of versioned attachmenturl, message and subject in .db file to XML, add interface for course coordinators (i.e., for lonnet::allowed('rin',$crs)) to display old versions of posts.
Known issues: Edits are not currently stored for bulletin board posts.

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

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