Annotation of loncom/interface/lonfeedback.pm, revision 1.136

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

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