File:  [LON-CAPA] / loncom / interface / lonfeedback.pm
Revision 1.109: download - view: text, annotated - select for diffs
Thu Jul 29 18:21:44 2004 UTC (19 years, 10 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
Course Coordinators (and others with lonnet::allowed('rin',$crs)) can now display all versions of a discussion post.

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

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