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

1.1       www         1: # The LearningOnline Network
                      2: # Feedback
                      3: #
1.96    ! albertel    4: # $Id: lonfeedback.pm,v 1.95 2004/06/23 17:42:58 sakharuk 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.54      www        39: 
1.92      albertel   40: sub discussion_open {
1.90      albertel   41:     my ($status)=@_;
1.92      albertel   42:     if (defined($status) &&
                     43: 	!($status eq 'CAN_ANSWER' || $status eq 'CANNOT_ANSWER'
1.77      www        44: 	  || $status eq 'OPEN')) {
1.92      albertel   45: 	return 0;
1.75      albertel   46:     }
1.89      albertel   47:     my $close=&Apache::lonnet::EXT('resource.0.discussend');
                     48:     if (defined($close) && $close ne '' && $close < time) {
1.92      albertel   49: 	return 0;
1.89      albertel   50:     }
1.92      albertel   51:     return 1;
                     52: }
                     53: 
                     54: sub discussion_visible {
                     55:     my ($status)=@_;
                     56:     if (not &discussion_open($status)) {
                     57: 	my $hidden=&Apache::lonnet::EXT('resource.0.discusshide');
                     58: 	if (lc($hidden) eq 'yes' or $hidden eq '' or !defined($hidden))  {
                     59: 	    return 0;
                     60: 	}
                     61:     }
                     62:     return 1;
1.90      albertel   63: }
1.84      raeburn    64: 
1.90      albertel   65: sub list_discussion {
                     66:     my ($mode,$status,$symb)=@_;
                     67: 
1.95      sakharuk   68:     my $outputtarget=$ENV{'form.grade_target'};
1.92      albertel   69:     if (not &discussion_visible($status)) { return ''; }
1.84      raeburn    70:     my @bgcols = ("#cccccc","#eeeeee");
1.57      www        71:     my $discussiononly=0;
                     72:     if ($mode eq 'board') { $discussiononly=1; }
1.55      www        73:     unless ($ENV{'request.course.id'}) { return ''; }
                     74:     my $crs='/'.$ENV{'request.course.id'};
                     75:     if ($ENV{'request.course.sec'}) {
                     76: 	$crs.='_'.$ENV{'request.course.sec'};
                     77:     }                 
                     78:     $crs=~s/\_/\//g;
1.54      www        79:     unless ($symb) {
                     80: 	$symb=&Apache::lonnet::symbread();
                     81:     }
                     82:     unless ($symb) { return ''; }
1.78      raeburn    83: 
1.80      raeburn    84: # backward compatibility (bulletin boards used to be 'wrapped')
                     85:     my $ressymb=$symb;
                     86:     if ($mode eq 'board') {
                     87:         unless ($ressymb =~ m|bulletin___\d+___adm/wrapper|) {
                     88:             $ressymb=~s|(bulletin___\d+___)|$1adm/wrapper|;
                     89:         }
                     90:     }
                     91: 
                     92: # Get discussion display settings for this discussion
                     93:     my $lastkey = $ressymb.'_lastread';
                     94:     my $showkey = $ressymb.'_showonlyunread';
                     95:     my $visitkey = $ressymb.'_visit';
1.84      raeburn    96:     my $ondispkey = $ressymb.'_markondisp';
                     97:     my %dischash = &Apache::lonnet::get('nohist_'.$ENV{'request.course.id'}.'_discuss',[$lastkey,$showkey,$visitkey,$ondispkey],$ENV{'user.domain'},$ENV{'user.name'});
                     98:     my %discinfo = ();
1.80      raeburn    99:     my $showonlyunread = 0;
1.84      raeburn   100:     my $markondisp = 0;
1.79      raeburn   101:     my $prevread = 0;
1.81      raeburn   102:     my $previous = 0;
1.80      raeburn   103:     my $visit = 0;
                    104:     my $newpostsflag = 0;
                    105: 
1.81      raeburn   106: # Retain identification of "NEW" posts identified in last display, if continuing 'previous' browsing of posts.
                    107:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},['previous']);
                    108:     $previous = $ENV{'form.previous'};
1.80      raeburn   109:     if ($previous > 0) {
                    110:         $prevread = $previous;
                    111:     } elsif (defined($dischash{$lastkey})) {
1.84      raeburn   112:         unless ($dischash{$lastkey} eq '') {
                    113:             $prevread = $dischash{$lastkey};
                    114:         }
1.80      raeburn   115:     }
1.79      raeburn   116: 
1.84      raeburn   117: # Get discussion display default settings for user
                    118:     my %userenv = &Apache::lonnet::get('environment',['discdisplay','discmarkread'],$ENV{'user.domain'},$ENV{'user.name'});
1.83      raeburn   119:     my $discdisplay=$userenv{'discdisplay'};
                    120:     if ($discdisplay eq 'unread') {
                    121:         $showonlyunread = 1;
                    122:     }
1.84      raeburn   123:     my $discmarkread=$userenv{'discmarkread'};
                    124:     if ($discmarkread eq 'ondisp') {
                    125:         $markondisp = 1;
                    126:     }
                    127: 
                    128: # Override user's default if user specified display setting for this discussion
                    129:     if (defined($dischash{$ondispkey})) {
                    130:         $markondisp = $dischash{$ondispkey};
                    131:     }
                    132:     if ($markondisp) {
                    133:         $discinfo{$lastkey} = time;
                    134:     }
1.83      raeburn   135: 
1.80      raeburn   136:     if (defined($dischash{$showkey})) {
                    137:         $showonlyunread = $dischash{$showkey};
                    138:     }
                    139: 
                    140:     if (defined($dischash{$visitkey})) {
                    141:         $visit = $dischash{$visitkey};
1.78      raeburn   142:     }
1.80      raeburn   143:     $visit ++;
1.78      raeburn   144: 
1.54      www       145:     my $seeid=&Apache::lonnet::allowed('rin',$crs);
1.77      www       146:     my $viewgrades=(&Apache::lonnet::allowed('vgr',$crs)
                    147: 	&& ($symb=~/\.(problem|exam|quiz|assess|survey|form)$/));
1.68      www       148:     my @discussionitems=();
1.73      albertel  149:     my %contrib=&Apache::lonnet::restore($ressymb,$ENV{'request.course.id'},
1.54      www       150: 			  $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
                    151: 			  $ENV{'course.'.$ENV{'request.course.id'}.'.num'});
1.67      www       152:     my $visible=0;
1.68      www       153:     my @depth=();
                    154:     my @original=();
                    155:     my @index=();
                    156:     my @replies=();
                    157:     my %alldiscussion=();
1.80      raeburn   158:     my %notshown = ();
1.84      raeburn   159:     my %newitem = ();
1.68      www       160:     my $maxdepth=0;
                    161: 
1.69      www       162:     my $target='';
                    163:     unless ($ENV{'browser.interface'} eq 'textual' ||
                    164: 	    $ENV{'environment.remote'} eq 'off' ) {
                    165: 	$target='target="LONcom"';
                    166:     }
1.79      raeburn   167:     
                    168:     my $now = time;
1.80      raeburn   169:     $discinfo{$visitkey} = $visit;
                    170: 
                    171:     &Apache::lonnet::put('nohist_'.$ENV{'request.course.id'}.'_discuss',\%discinfo,$ENV{'user.domain'},$ENV{'user.name'});
1.79      raeburn   172: 
1.54      www       173:     if ($contrib{'version'}) {
1.84      raeburn   174:         my $oldest = $contrib{'1:timestamp'};
                    175:         if ($prevread eq '0') {
                    176:             $prevread = $oldest-1;
                    177:         }
1.64      www       178: 	for (my $id=1;$id<=$contrib{'version'};$id++) {
                    179: 	    my $idx=$id;
1.80      raeburn   180:             my $posttime = $contrib{$idx.':timestamp'};
1.84      raeburn   181:             if ($prevread <= $posttime) {
1.80      raeburn   182:                 $newpostsflag = 1;
                    183:             }
1.54      www       184: 	    my $hidden=($contrib{'hidden'}=~/\.$idx\./);
                    185: 	    my $deleted=($contrib{'deleted'}=~/\.$idx\./);
1.68      www       186: 	    my $origindex='0.';
1.69      www       187: 	    if (($contrib{$idx.':replyto'}) && ($ENV{'environment.threadeddiscussion'})) {
1.68      www       188: # this is a follow-up message
                    189: 		$original[$idx]=$original[$contrib{$idx.':replyto'}];
                    190: 		$depth[$idx]=$depth[$contrib{$idx.':replyto'}]+1;
                    191: 		$origindex=$index[$contrib{$idx.':replyto'}];
                    192: 		if ($depth[$idx]>$maxdepth) { $maxdepth=$depth[$idx]; }
                    193: 	    } else {
                    194: # this is an original message
                    195: 		$original[$idx]=0;
                    196: 		$depth[$idx]=0;
                    197: 	    }
                    198: 	    if ($replies[$depth[$idx]]) {
                    199: 		$replies[$depth[$idx]]++;
                    200: 	    } else {
                    201: 		$replies[$depth[$idx]]=1;
                    202: 	    }
1.54      www       203: 	    unless ((($hidden) && (!$seeid)) || ($deleted)) {
1.67      www       204: 		$visible++;
1.54      www       205: 		my $message=$contrib{$idx.':message'};
                    206: 		$message=~s/\n/\<br \/\>/g;
                    207: 		$message=&Apache::lontexconvert::msgtexconverted($message);
1.78      raeburn   208:                 my $subject=$contrib{$idx.':subject'};
                    209:                 if (defined($subject)) {
                    210:                     $subject=~s/\n/\<br \/\>/g;
                    211:                     $subject=&Apache::lontexconvert::msgtexconverted($subject);
                    212:                 }
1.54      www       213: 		if ($contrib{$idx.':attachmenturl'}) {
1.82      albertel  214: 		    my ($fname)
                    215:                         =($contrib{$idx.':attachmenturl'}=~m|/([^/]+)$|);
                    216: 		    &Apache::lonnet::allowuploaded('/adm/feedback',
                    217: 					   $contrib{$idx.':attachmenturl'});
                    218: 		    $message.='<p>'.&mt('Attachment').
                    219: 			': <a href="'.$contrib{$idx.':attachmenturl'}.'"><tt>'.
                    220: 			$fname.'</tt></a></p>';
1.54      www       221: 		}
                    222: 		if ($message) {
                    223: 		    if ($hidden) {
                    224: 			$message='<font color="#888888">'.$message.'</font>';
                    225: 		    }
                    226: 		    my $screenname=&Apache::loncommon::screenname(
                    227: 					    $contrib{$idx.':sendername'},
                    228: 					    $contrib{$idx.':senderdomain'});
                    229: 		    my $plainname=&Apache::loncommon::nickname(
                    230: 					    $contrib{$idx.':sendername'},
                    231: 					    $contrib{$idx.':senderdomain'});
                    232: 		    
1.62      www       233: 		    my $sender=&mt('Anonymous');
1.54      www       234: 		    if ((!$contrib{$idx.':anonymous'}) || ($seeid)) {
                    235: 			$sender=&Apache::loncommon::aboutmewrapper(
                    236: 					 $plainname,
                    237: 					 $contrib{$idx.':sendername'},
                    238: 					 $contrib{$idx.':senderdomain'}).' ('.
                    239: 					 $contrib{$idx.':sendername'}.' at '.
                    240: 					 $contrib{$idx.':senderdomain'}.')';
                    241: 			if ($contrib{$idx.':anonymous'}) {
1.62      www       242: 			    $sender.=' ['.&mt('anonymous').'] '.
1.54      www       243: 				$screenname;
                    244: 			}
                    245: 			if ($seeid) {
                    246: 			    if ($hidden) {
                    247: 				$sender.=' <a href="/adm/feedback?unhide='.
1.80      raeburn   248: 				    $ressymb.':::'.$idx;
                    249:                                 if ($newpostsflag) {
                    250:                                     $sender .= '&previous='.$prevread;
                    251:                                 }
                    252:                                 $sender .= '">'.&mt('Make Visible').'</a>';
1.54      www       253: 			    } else {
                    254: 				$sender.=' <a href="/adm/feedback?hide='.
1.80      raeburn   255: 				    $ressymb.':::'.$idx;
                    256:                                 if ($newpostsflag) {
                    257:                                     $sender .= '&previous='.$prevread;
                    258:                                 }
                    259:                                 $sender .= '">'.&mt('Hide').'</a>';
1.54      www       260: 			    }                     
                    261: 			    $sender.=' <a href="/adm/feedback?deldisc='.
1.80      raeburn   262: 				$ressymb.':::'.$idx;
                    263:                                 if ($newpostsflag) {
                    264:                                     $sender .= '&previous='.$prevread;
                    265:                                 }
                    266:                                 $sender .= '">'.&mt('Delete').'</a>';
1.64      www       267: 			}
1.54      www       268: 		    } else {
                    269: 			if ($screenname) {
                    270: 			    $sender='<i>'.$screenname.'</i>';
                    271: 			}
1.77      www       272: 		    }
1.92      albertel  273: 		    if (&discussion_open($status) &&
1.90      albertel  274: 			&Apache::lonnet::allowed('pch',
1.77      www       275: 						 $ENV{'request.course.id'}.
                    276: 						 ($ENV{'request.course.sec'}?'/'.$ENV{'request.course.sec'}:''))) {
                    277: 			$sender.=' <a href="/adm/feedback?replydisc='.
1.80      raeburn   278: 			    $ressymb.':::'.$idx;
                    279:                         if ($newpostsflag) {
                    280:                             $sender .= '&previous='.$prevread;
                    281:                         }
                    282:                         $sender .= '" '.$target.'>'.&mt('Reply').'</a>';
1.54      www       283: 		    }
                    284: 		    my $vgrlink;
                    285: 		    if ($viewgrades) {
                    286: 			$vgrlink=&Apache::loncommon::submlink('Submissions',
                    287:             $contrib{$idx.':sendername'},$contrib{$idx.':senderdomain'},$symb);
                    288: 		    }
1.68      www       289: #figure out at what position this needs to print
                    290: 		    my $thisindex=$idx;
1.69      www       291: 		    if ($ENV{'environment.threadeddiscussion'}) {
1.68      www       292: 			$thisindex=$origindex.substr('00'.$replies[$depth[$idx]],-2,2);	
                    293: 		    }
                    294: 		    $alldiscussion{$thisindex}=$idx;
                    295: 		    $index[$idx]=$thisindex;
1.79      raeburn   296:                     my $spansize = 2;
1.80      raeburn   297:                     if ($showonlyunread && $prevread > $posttime) {
                    298:                         $notshown{$idx} = 1;
1.78      raeburn   299:                     } else {
1.80      raeburn   300:                         if ($prevread > 0 && $prevread <= $posttime) {
1.84      raeburn   301:                             $newitem{$idx} = 1;
                    302:                             $discussionitems[$idx] .= '
                    303:                              <p><table border="0" width="100%">
                    304:                               <tr><td align="left"><font color="#FF0000"><b>NEW</b></font></td>';
                    305:                         } else {
                    306:                             $newitem{$idx} = 0;
                    307:                             $discussionitems[$idx] .= '
                    308:                              <p><table border="0" width="100%">
                    309:                               <tr><td align="left">&nbsp;</td>';
1.80      raeburn   310:                         }
                    311:                         $discussionitems[$idx] .= '<td align ="left">&nbsp;&nbsp;'.
                    312:                             '<b>'.$subject.'</b>&nbsp;&nbsp;'.
                    313:                             $sender.'</b> '.$vgrlink.' ('.
                    314:                             localtime($posttime).')</td></tr>'.
                    315:                             '</table><blockquote>'.$message.'</blockquote></p>';
1.78      raeburn   316:                     }
                    317:                 }
                    318:             }
1.54      www       319: 	}
1.64      www       320:     }
1.80      raeburn   321: 
1.67      www       322:     my $discussion='';
1.84      raeburn   323: 
                    324:     my $function = &Apache::loncommon::get_users_function();
                    325:     my $color = &Apache::loncommon::designparm($function.'.tabbg',
                    326:                                                     $ENV{'user.domain'});
                    327:     my %lt = &Apache::lonlocal::texthash(
                    328:         'cuse' => 'Current settings for this discussion',
                    329:         'allposts' => 'All posts',
                    330:         'unread' => 'New posts only',
                    331:         'ondisp' => 'Once displayed',
                    332:         'onmark' => 'Once marked read',
                    333:         'disa' => 'Posts to be displayed',
                    334:         'npce' => 'Posts cease to be marked "NEW"',
                    335:         'chgt' => 'Change to ',
                    336:     );
                    337: 
                    338:     my $currdisp = $lt{'allposts'};
                    339:     my $currmark = $lt{'onmark'};
                    340:     my $dispchange = $lt{'unread'};
                    341:     my $markchange = $lt{'ondisp'};
                    342:     my $displink = '/adm/feedback?onlyunread='.$ressymb;
                    343:     my $marklink = '/adm/feedback?markondisp='.$ressymb;
                    344: 
                    345:     if ($markondisp) {
                    346:         $currmark = $lt{'ondisp'};
                    347:         $markchange = $lt{'onmark'};
                    348:         $marklink = '/adm/feedback?markonread='.$ressymb;
                    349:         if ($newpostsflag) {
                    350:             $marklink .= '&previous='.$prevread;
                    351:         }
                    352:     }
                    353: 
                    354:     if ($showonlyunread) {
                    355:         $currdisp = $lt{'unread'};
                    356:         $dispchange = $lt{'allposts'};
                    357:         $displink = '/adm/feedback?allposts='.$ressymb;
                    358:     }
                    359: 
                    360:     if ($newpostsflag) {
                    361:         $displink .= '&previous='.$prevread;
                    362:     }
                    363: 
1.67      www       364:     if ($visible) {
1.80      raeburn   365: # Print the discusssion
1.95      sakharuk  366: 	if ($outputtarget ne 'tex') {
                    367: 	    $discussion.='<table bgcolor="#AAAAAA" cellpadding="2" cellspacing="2" border="0">';
                    368: 	    my $colspan=$maxdepth+1;
                    369: 	    $discussion .= '<tr bgcolor="#FFFFFF"><td colspan="'.$colspan.'" valign="top">'.
                    370: 		'<table border="0" bgcolor="#FFFFFF" width="100%" cellspacing="2" cellpadding="2">'.
                    371: 		'<tr><td align="left"><b>'.$lt{'cuse'}.'</b></td><td>&nbsp;&nbsp;&nbsp;&nbsp;</td><td align="right"><b>'.$lt{'chgt'}.'</b></td></tr>'.
                    372: 		'<tr><td>'.$lt{'disa'}.':&nbsp;<i>'.$currdisp.'</i></td><td>&nbsp;&nbsp;&nbsp;&nbsp;</td><td align="right"><a href="'.$displink.'">'.$dispchange.'</a></td></tr>'.
                    373: 		'<tr><td>'.$lt{'npce'}.':&nbsp;<i>'.$currmark.'</i></td><td>&nbsp;&nbsp;&nbsp;&nbsp;</td><td align="right"><a href="'.$marklink.'">'.$markchange.'</a></td></tr>'.
                    374: 		'</table></td></tr>'.
                    375: 		'<tr><td bgcolor="#DDDDBB" colspan="'.$colspan.'">'.
                    376: 		'<table border="0" width="100%" bgcolor="#DDDDBB"><tr>';
                    377: 	    if ($visible>2) {
                    378: 		$discussion.='<td align="left">'.
                    379: 		    '<a href="/adm/feedback?threadedon='.$ressymb;
                    380: 		if ($newpostsflag) {
                    381: 		    $discussion .= '&previous='.$prevread;
                    382: 		}
                    383: 		$discussion .='">'.&mt('Threaded View').'</a>&nbsp;&nbsp;'.
                    384: 		    '<a href="/adm/feedback?threadedoff='.$ressymb;
                    385: 		if ($newpostsflag) {
                    386: 		    $discussion .= '&previous='.$prevread;
                    387: 		}
                    388: 		$discussion .='">'.&mt('Chronological View').'</a>&nbsp;&nbsp;</td>';
                    389: 	    } 
                    390: 	    if ($newpostsflag) {
                    391: 		if (!$markondisp) {
                    392: 		    $discussion .='<td align="right"><a href="/adm/feedback?markread='.$ressymb.'">'.&mt('Mark new posts as read').'</a>&nbsp;&nbsp;';
                    393: 		} else {
                    394: 		    $discussion .= '<td>&nbsp;</td>';
                    395: 		}
                    396: 	    } else {
                    397: 		$discussion .= '<td>&nbsp;</td>';
                    398: 	    }
                    399: 	    $discussion .= '</tr></table></td></tr>';
                    400: 	} else {
                    401: 	    $discussion.='\vskip 0 mm\noindent\makebox[2 cm][b]{\hrulefill}'.
                    402:                          '\textbf{DISCUSSIONS}\makebox[2 cm][b]{\hrulefill}'.
                    403:                          '\vskip 0 mm\noindent\textbf{'.$lt{'cuse'}.'}:\vskip 0 mm'.
                    404:                          '\noindent\textbf{'.$lt{'disa'}.'}: \textit{'.$currdisp.'}\vskip 0 mm'.
                    405:                          '\noindent\textbf{'.$lt{'npce'}.'}: \textit{'.$currmark.'}';
                    406: 	}
1.80      raeburn   407:         my $numhidden = keys %notshown;
                    408:         if ($numhidden > 0) {
                    409:             my $colspan = $maxdepth+1;
                    410:             $discussion.="\n".'<tr><td bgcolor="#CCCCCC" colspan="'.$colspan.'">'.
                    411:                          '<a href="/adm/feedback?allposts='.$ressymb;
                    412:             if ($newpostsflag) {
                    413:                 $discussion .= '&previous='.$prevread;
                    414:             }
                    415:             $discussion .= '">'.&mt('Show all posts').'</a> '.&mt('to display').' '.
                    416:                          $numhidden.' '.&mt('previously viewed posts').
                    417:                          '<br/></td></tr>';
                    418:         }
1.68      www       419: 	foreach (sort { $a <=> $b } keys %alldiscussion) {
1.80      raeburn   420:             unless ($notshown{$alldiscussion{$_}} eq '1') {
1.95      sakharuk  421:                 if ($outputtarget ne 'tex') {
                    422: 		    $discussion.="\n<tr>";
                    423: 		} else {
                    424: 		    $discussion.='\vskip 0 mm\noindent\makebox[2 cm][b]{\hrulefill}';
                    425: 		}
1.80      raeburn   426: 	        my $thisdepth=$depth[$alldiscussion{$_}];
1.95      sakharuk  427:                 if ($outputtarget ne 'tex') {
                    428: 		    for (1..$thisdepth) {
                    429: 			$discussion.='<td>&nbsp;&nbsp;&nbsp;</td>';
                    430: 		    }
                    431: 		}
1.80      raeburn   432: 	        my $colspan=$maxdepth-$thisdepth+1;
1.95      sakharuk  433:                 if ($outputtarget ne 'tex') {
                    434: 		    $discussion.='<td  bgcolor="'.$bgcols[$newitem{$alldiscussion{$_}}].'" colspan="'.$colspan.'">'.
1.80      raeburn   435:                              $discussionitems[$alldiscussion{$_}].
                    436: 	                     '</td></tr>';
1.95      sakharuk  437: 		} else {
                    438: 		    #cleanup block
                    439: 		    $discussionitems[$alldiscussion{$_}]=~s/<table([^>]*)>/<table TeXwidth="90 mm">/;
                    440: 		    $discussionitems[$alldiscussion{$_}]=~s/<tr([^>]*)><td([^>]*)>/<tr><td TeXwidth="20 mm" align="left">/;
                    441:                     my $threadinsert='';
                    442:                     if ($thisdepth > 0) {
                    443: 			$threadinsert='<br /><strong>Reply: '.$thisdepth.'</strong>';
                    444: 		    }
                    445: 		    $discussionitems[$alldiscussion{$_}]=~s/<\/td><td([^>]*)>/$threadinsert<\/td><td TeXwidth="65 mm" align="left">/;
                    446: 		    $discussionitems[$alldiscussion{$_}]=~s/<a([^>]+)>(Hide|Delete|Reply|Submissions)<\/a>//g;
                    447:                     $discussionitems[$alldiscussion{$_}]=~s/(<b>|<\/b>|<\/a>|<a([^>]+)>)//g;
1.96    ! albertel  448: 		    
        !           449:                     #FIXME xmlparse can't be safely called from inside xmlparse
        !           450:                     #   due to the global variables that are use, the safe
        !           451:                     #   space etc. I expect this has unforseen issues that
        !           452:                     #   need resolving.
        !           453: 		    
        !           454:                     $discussion.=&Apache::lonxml::xmlparse('','tex',$discussionitems[$alldiscussion{$_}]);
1.95      sakharuk  455: 		}
1.69      www       456: 	    }
1.80      raeburn   457:         }
1.95      sakharuk  458: 	if ($outputtarget ne 'tex') {
                    459: 	    $discussion.='</table><br /><br />';
                    460: 	}
1.54      www       461:     }
                    462:     if ($discussiononly) {
                    463: 	$discussion.=(<<ENDDISCUSS);
                    464: <form action="/adm/feedback" method="post" name="mailform" enctype="multipart/form-data">
                    465: <input type="submit" name="discuss" value="Post Discussion" />
                    466: <input type="submit" name="anondiscuss" value="Post Anonymous Discussion" />
1.73      albertel  467: <input type="hidden" name="symb" value="$ressymb" />
1.54      www       468: <input type="hidden" name="sendit" value="true" />
                    469: <br />
                    470: <font size="1">Note: in anonymous discussion, your name is visible only to
                    471: course faculty</font><br />
1.78      raeburn   472: <b>Title:</b>&nbsp;<input type="text" name="subject" value="" size="30" /><br /><br />
1.94      www       473: <textarea name="comment" cols="80" rows="14" wrap="hard"></textarea>
1.54      www       474: <p>
                    475: Attachment (128 KB max size): <input type="file" name="attachment" />
                    476: </p>
                    477: </form>
                    478: ENDDISCUSS
1.95      sakharuk  479:         if ($outputtarget ne 'tex') {
                    480: 	    $discussion.=&generate_preview_button();
                    481: 	}
1.74      www       482:     } else {
1.92      albertel  483: 	if (&discussion_open($status) &&
1.90      albertel  484: 	    &Apache::lonnet::allowed('pch',
1.74      www       485: 				   $ENV{'request.course.id'}.
                    486: 	($ENV{'request.course.sec'}?'/'.$ENV{'request.course.sec'}:''))) {
1.95      sakharuk  487: 	    if ($outputtarget ne 'tex') {
                    488: 		$discussion.='<table bgcolor="#BBBBBB"><tr><td><a href="/adm/feedback?replydisc='.
                    489: 		    $symb.':::" '.$target.'>'.
                    490: 		    '<img src="/adm/lonMisc/chat.gif" border="0" />'.
                    491: 		    &mt('Post Discussion').'</a></td></tr></table>';
                    492: 	    }
1.74      www       493: 			}
                    494:     }
1.54      www       495:    return $discussion;
                    496: }
1.1       www       497: 
1.6       albertel  498: sub mail_screen {
                    499:   my ($r,$feedurl,$options) = @_;
1.45      www       500:   my $bodytag=&Apache::loncommon::bodytag('Resource Feedback and Discussion',
                    501:                                           '','onLoad="window.focus();"');
1.51      albertel  502:   my $title=&Apache::lonnet::gettitle($feedurl);
                    503:   if (!$title) { $title = $feedurl; }
1.69      www       504:   my $quote='';
1.78      raeburn   505:   my $subject = '';
1.80      raeburn   506:   my $prevtag = '';
1.69      www       507:   if ($ENV{'form.replydisc'}) {
                    508:       my ($symb,$idx)=split(/\:\:\:/,$ENV{'form.replydisc'});
                    509:       my %contrib=&Apache::lonnet::restore($symb,$ENV{'request.course.id'},
                    510: 					   $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
                    511: 					   $ENV{'course.'.$ENV{'request.course.id'}.'.num'});
1.80      raeburn   512:       unless (($contrib{'hidden'}=~/\.$idx\./) || ($contrib{'deleted'}=~/\.$idx\./)) {
1.69      www       513: 	  my $message=$contrib{$idx.':message'};
                    514: 	  $message=~s/\n/\<br \/\>/g;
                    515: 	  $quote='<blockquote>'.&Apache::lontexconvert::msgtexconverted($message).'</blockquote>';
1.79      raeburn   516:           if ($idx > 0) {
                    517:               $subject = 'Re: '.$contrib{$idx.':subject'};
                    518:           }
1.69      www       519:       }
1.80      raeburn   520:       if ($ENV{'form.previous'}) {
                    521:           $prevtag = '<input type="hidden" name="previous" value="'.$ENV{'form.previous'}.'" />';
                    522:       }
1.69      www       523:   }
1.85      www       524:   my $latexHelp=&Apache::loncommon::helpLatexCheatsheet();
1.86      www       525:   my $htmlheader=&Apache::lonhtmlcommon::htmlareaheaders();
1.94      www       526:   my $onsubmit='';
                    527:   if ((&Apache::lonhtmlcommon::htmlareabrowser()) &&
                    528:       (!&Apache::lonhtmlcommon::htmlareablocked())) {
                    529:       $onsubmit='document.mailform.onsubmit();';
                    530:   }
1.74      www       531:   my $send=&mt('Send');
1.6       albertel  532:   $r->print(<<ENDDOCUMENT);
1.1       www       533: <html>
                    534: <head>
                    535: <title>The LearningOnline Network with CAPA</title>
1.7       albertel  536: <meta http-equiv="pragma" content="no-cache"></meta>
1.85      www       537: $htmlheader
1.63      albertel  538: <script type="text/javascript">
                    539: //<!--
1.5       www       540:     function gosubmit() {
                    541:         var rec=0;
1.12      albertel  542:         if (typeof(document.mailform.elements.author)!="undefined") {
1.5       www       543:           if (document.mailform.elements.author.checked) {
                    544:              rec=1;
                    545:           } 
                    546:         }
1.12      albertel  547:         if (typeof(document.mailform.elements.question)!="undefined") {
1.5       www       548:           if (document.mailform.elements.question.checked) {
                    549:              rec=1;
                    550:           } 
                    551:         }
1.12      albertel  552:         if (typeof(document.mailform.elements.course)!="undefined") {
1.5       www       553:           if (document.mailform.elements.course.checked) {
                    554:              rec=1;
                    555:           } 
                    556:         }
1.12      albertel  557:         if (typeof(document.mailform.elements.policy)!="undefined") {
1.5       www       558:           if (document.mailform.elements.policy.checked) {
                    559:              rec=1;
                    560:           } 
                    561:         }
1.12      albertel  562:         if (typeof(document.mailform.elements.discuss)!="undefined") {
1.10      www       563:           if (document.mailform.elements.discuss.checked) {
                    564:              rec=1;
                    565:           } 
                    566:         }
1.14      www       567:         if (typeof(document.mailform.elements.anondiscuss)!="undefined") {
                    568:           if (document.mailform.elements.anondiscuss.checked) {
                    569:              rec=1;
                    570:           } 
                    571:         }
1.5       www       572: 
                    573:         if (rec) {
1.94      www       574:             $onsubmit
1.5       www       575: 	    document.mailform.submit();
                    576:         } else {
                    577:             alert('Please check a feedback type.');
                    578: 	}
                    579:     }
1.63      albertel  580: //-->
1.5       www       581: </script>
1.1       www       582: </head>
1.29      www       583: $bodytag
1.51      albertel  584: <h2><tt>$title</tt></h2>
1.43      www       585: <form action="/adm/feedback" method="post" name="mailform"
                    586: enctype="multipart/form-data">
1.80      raeburn   587: $prevtag
1.63      albertel  588: <input type="hidden" name="postdata" value="$feedurl" />
1.68      www       589: <input type="hidden" name="replydisc" value="$ENV{'form.replydisc'}" />
1.5       www       590: Please check at least one of the following feedback types:
1.63      albertel  591: $options<hr />
1.69      www       592: $quote
1.63      albertel  593: <p>My question/comment/feedback:</p>
                    594: <p>
1.47      bowersj2  595: $latexHelp
1.78      raeburn   596: Title: <input type="text" name="subject" size="30" value="$subject" /></p>
                    597: <p>
1.94      www       598: <textarea name="comment" id="comment" cols="60" rows="10" wrap="hard">
1.63      albertel  599: </textarea></p>
                    600: <p>
1.42      www       601: Attachment (128 KB max size): <input type="file" name="attachment" />
                    602: </p>
                    603: <p>
                    604: <input type="hidden" name="sendit" value="1" />
1.74      www       605: <input type="button" value="$send" onClick='gosubmit();' />
1.42      www       606: </p>
1.2       www       607: </form>
1.1       www       608: ENDDOCUMENT
1.85      www       609: $r->print(&generate_preview_button().
1.94      www       610: &Apache::lonhtmlcommon::htmlareaselectactive('comment').
1.85      www       611: '</body></html>');
1.6       albertel  612: }
                    613: 
                    614: sub fail_redirect {
                    615:   my ($r,$feedurl) = @_;
1.70      www       616:   if ($feedurl=~/^\/adm\//) { $feedurl.='?register=1' };
1.6       albertel  617:   $r->print (<<ENDFAILREDIR);
1.72      albertel  618: <html>
1.5       www       619: <head><title>Feedback not sent</title>
1.63      albertel  620: <meta http-equiv="pragma" content="no-cache" />
                    621: <meta HTTP-EQUIV="Refresh" CONTENT="2; url=$feedurl" />
1.5       www       622: </head>
                    623: <body bgcolor="#FFFFFF">
1.63      albertel  624: <img align="right" src="/adm/lonIcons/lonlogos.gif" />
1.8       www       625: <b>Sorry, no recipients  ...</b>
1.5       www       626: </body>
                    627: </html>
                    628: ENDFAILREDIR
                    629: }
1.4       www       630: 
1.6       albertel  631: sub redirect_back {
1.80      raeburn   632:   my ($r,$feedurl,$typestyle,$sendsomething,$sendposts,$status,$previous) = @_;
                    633:   my $prevtag = '';
                    634:   my $qrystr = '';
1.70      www       635:   if ($feedurl=~/^\/adm\//) { $feedurl.='?register=1' };
1.80      raeburn   636:   if ($previous > 0) {
                    637:       $qrystr = 'previous='.$previous;
                    638:       if ($feedurl =~ /\?register=1/) {
                    639:           $feedurl .= '&'.$qrystr;
                    640:       } else {
                    641:           $feedurl .= '?'.$qrystr;
                    642:       }
                    643:       $prevtag = '<input type="hidden" name="previous" value="'.$previous.'" />';
                    644:   }
1.6       albertel  645:   $r->print (<<ENDREDIR);
1.72      albertel  646: <html>
1.3       www       647: <head>
                    648: <title>Feedback sent</title>
1.63      albertel  649: <meta http-equiv="pragma" content="no-cache" />
1.80      raeburn   650: <meta HTTP-EQUIV="Refresh" CONTENT="2; url=$feedurl" />
1.2       www       651: </head>
1.49      www       652: <body bgcolor="#FFFFFF" onLoad='if (window.name!="loncapaclient") { this.document.reldt.submit(); self.close(); }'>
1.63      albertel  653: <img align="right" src="/adm/lonIcons/lonlogos.gif" />
1.5       www       654: $typestyle
1.32      albertel  655: <b>Sent $sendsomething message(s), and $sendposts post(s).</b>
1.63      albertel  656: <font color="red">$status</font>
1.49      www       657: <form name="reldt" action="$feedurl" target="loncapaclient">
1.80      raeburn   658: $prevtag
1.49      www       659: </form>
1.2       www       660: </body>
                    661: </html>
                    662: ENDREDIR
                    663: }
1.6       albertel  664: 
                    665: sub no_redirect_back {
                    666:   my ($r,$feedurl) = @_;
                    667:   $r->print (<<ENDNOREDIR);
1.72      albertel  668: <html>
1.2       www       669: <head><title>Feedback not sent</title>
1.63      albertel  670: <meta http-equiv="pragma" content="no-cache" />
1.7       albertel  671: ENDNOREDIR
                    672: 
1.8       www       673:   if ($feedurl!~/^\/adm\/feedback/) { 
1.7       albertel  674:     $r->print('<meta HTTP-EQUIV="Refresh" CONTENT="2; url='.$feedurl.'">');
                    675:   }
                    676:   
1.8       www       677:   $r->print (<<ENDNOREDIRTWO);
1.2       www       678: </head>
1.49      www       679: <body bgcolor="#FFFFFF" onLoad='if (window.name!="loncapaclient") { self.close(); }'>
1.63      albertel  680: <img align="right" src="/adm/lonIcons/lonlogos.gif" />
1.8       www       681: <b>Sorry, no feedback possible on this resource  ...</b>
1.2       www       682: </body>
                    683: </html>
1.8       www       684: ENDNOREDIRTWO
1.2       www       685: }
1.6       albertel  686: 
                    687: sub screen_header {
1.65      www       688:     my ($feedurl) = @_;
                    689:     my $msgoptions='';
                    690:     my $discussoptions='';
                    691:     unless ($ENV{'form.replydisc'}) {
                    692: 	if (($feedurl=~/^\/res\//) && ($feedurl!~/^\/res\/adm/)) {
                    693: 	    $msgoptions= 
                    694: 		'<p><input type="checkbox" name="author" /> '.
                    695: 		&mt('Feedback to resource author').'</p>';
                    696: 	}
                    697: 	if (&feedback_available(1)) {
                    698: 	    $msgoptions.=
                    699: 		'<br /><input type="checkbox" name="question" /> '.
                    700: 		&mt('Question about resource content');
                    701: 	}
                    702: 	if (&feedback_available(0,1)) {
                    703: 	    $msgoptions.=
                    704: 		'<br /><input type="checkbox" name="course" /> '.
                    705: 		&mt('Question/Comment/Feedback about course content');
                    706: 	}
                    707: 	if (&feedback_available(0,0,1)) {
                    708: 	    $msgoptions.=
                    709: 		'<br /><input type="checkbox" name="policy" /> '.
                    710: 		&mt('Question/Comment/Feedback about course policy');
                    711: 	}
                    712:     }
                    713:     if ($ENV{'request.course.id'}) {
1.92      albertel  714: 	if (&discussion_open() &&
1.90      albertel  715: 	    &Apache::lonnet::allowed('pch',
1.65      www       716: 				     $ENV{'request.course.id'}.
                    717: 				     ($ENV{'request.course.sec'}?'/'.$ENV{'request.course.sec'}:''))) {
1.74      www       718: 	    $discussoptions='<input type="checkbox" name="discuss" onClick="this.form.anondiscuss.checked=false;" '.
                    719: 		($ENV{'form.replydisc'}?' checked="1"':'').' /> '.
1.65      www       720: 		&mt('Contribution to course discussion of resource');
                    721: 	    $discussoptions.='<br /><input type="checkbox" name="anondiscuss" onClick="this.form.discuss.checked=false;" /> '.
                    722: 		&mt('Anonymous contribution to course discussion of resource').
                    723: 		' <i>('.&mt('name only visible to course faculty').')</i>';
1.20      www       724:       }
1.65      www       725:     }
1.74      www       726:     if ($msgoptions) { $msgoptions='<h2><img src="/adm/lonMisc/feedback.gif" />'.&mt('Sending Messages').'</h2>'.$msgoptions; }
1.65      www       727:     if ($discussoptions) { 
1.74      www       728: 	$discussoptions='<h2><img src="/adm/lonMisc/chat.gif" />'.&mt('Discussion Contributions').'</h2>'.$discussoptions; }
1.65      www       729:     return $msgoptions.$discussoptions;
1.6       albertel  730: }
                    731: 
                    732: sub resource_output {
                    733:   my ($feedurl) = @_;
1.46      albertel  734:   my $usersaw=&Apache::lonnet::ssi_body($feedurl);
1.6       albertel  735:   $usersaw=~s/\<body[^\>]*\>//gi;
                    736:   $usersaw=~s/\<\/body\>//gi;
                    737:   $usersaw=~s/\<html\>//gi;
                    738:   $usersaw=~s/\<\/html\>//gi;
                    739:   $usersaw=~s/\<head\>//gi;
                    740:   $usersaw=~s/\<\/head\>//gi;
                    741:   $usersaw=~s/action\s*\=/would_be_action\=/gi;
                    742:   return $usersaw;
                    743: }
                    744: 
                    745: sub clear_out_html {
1.39      www       746:   my ($message,$override)=@_;
1.88      www       747:   unless (&Apache::lonhtmlcommon::htmlareablocked()) { return $message; }
1.37      albertel  748:   my $cid=$ENV{'request.course.id'};
1.39      www       749:   if (($ENV{"course.$cid.allow_limited_html_in_feedback"} =~ m/yes/i) ||
                    750:       ($override)) {
1.37      albertel  751:       # allows <B> <I> <P> <A> <LI> <OL> <UL> <EM> <BR> <TT> <STRONG> 
1.88      www       752:       # <BLOCKQUOTE> <DIV .*> <DIV> <IMG> <M> <SPAN> <H1> <H2> <H3> <H4> <SUB>
                    753:       # <SUP>
1.37      albertel  754:       my %html=(B=>1, I=>1, P=>1, A=>1, LI=>1, OL=>1, UL=>1, EM=>1,
1.61      www       755: 		BR=>1, TT=>1, STRONG=>1, BLOCKQUOTE=>1, DIV=>1, IMG=>1,
1.88      www       756:                 M=>1, SUB=>1, SUP=>1, SPAN=>1, 
                    757: 		H1=>1, H2=>1, H3=>1, H4=>1, H5=>1);
1.37      albertel  758: 
                    759:       $message =~ s/\<(\/?\s*(\w+)[^\>\<]*)/
1.48      albertel  760: 	  {($html{uc($2)}&&(length($1)<1000))?"\<$1":"\&lt;$1"}/ge;
1.37      albertel  761:       $message =~ s/(\<?\s*(\w+)[^\<\>]*)\>/
1.48      albertel  762: 	  {($html{uc($2)}&&(length($1)<1000))?"$1\>":"$1\&gt;"}/ge;
1.37      albertel  763:   } else {
                    764:       $message=~s/\</\&lt\;/g;
                    765:       $message=~s/\>/\&gt\;/g;
                    766:   }
1.6       albertel  767:   return $message;
                    768: }
                    769: 
                    770: sub assemble_email {
1.40      albertel  771:   my ($feedurl,$message,$prevattempts,$usersaw,$useranswer)=@_;
1.6       albertel  772:   my $email=<<"ENDEMAIL";
                    773: Refers to <a href="$feedurl">$feedurl</a>
                    774: 
                    775: $message
                    776: ENDEMAIL
                    777:     my $citations=<<"ENDCITE";
                    778: <h2>Previous attempts of student (if applicable)</h2>
                    779: $prevattempts
1.63      albertel  780: <br /><hr />
1.6       albertel  781: <h2>Original screen output (if applicable)</h2>
                    782: $usersaw
1.40      albertel  783: <h2>Correct Answer(s) (if applicable)</h2>
                    784: $useranswer
1.6       albertel  785: ENDCITE
                    786:   return ($email,$citations);
                    787: }
                    788: 
1.35      www       789: sub secapply {
                    790:     my $rec=shift;
1.36      www       791:     my $defaultflag=shift;
                    792:     $rec=~s/\s+//g;
                    793:     $rec=~s/\@/\:/g;
                    794:     my ($adr,$sections)=($rec=~/^([^\(]+)\(([^\)]+)\)/);
                    795:     if ($sections) {
                    796: 	foreach (split(/\;/,$sections)) {
                    797:             if (($_ eq $ENV{'request.course.sec'}) ||
                    798:                 ($defaultflag && ($_ eq '*'))) {
                    799:                 return $adr; 
                    800:             }
                    801:         }
                    802:     } else {
                    803:        return $rec;
                    804:     }
                    805:     return '';
1.35      www       806: }
                    807: 
1.6       albertel  808: sub decide_receiver {
1.36      www       809:   my ($feedurl,$author,$question,$course,$policy,$defaultflag) = @_;
1.6       albertel  810:   my $typestyle='';
                    811:   my %to=();
1.36      www       812:   if ($ENV{'form.author'}||$author) {
1.8       www       813:     $typestyle.='Submitting as Author Feedback<br>';
1.6       albertel  814:     $feedurl=~/^\/res\/(\w+)\/(\w+)\//;
                    815:     $to{$2.':'.$1}=1;
                    816:   }
1.36      www       817:   if ($ENV{'form.question'}||$question) {
1.8       www       818:     $typestyle.='Submitting as Question<br>';
1.24      harris41  819:     foreach (split(/\,/,
                    820: 		   $ENV{'course.'.$ENV{'request.course.id'}.'.question.email'})
                    821: 	     ) {
1.36      www       822: 	my $rec=&secapply($_,$defaultflag);
                    823:         if ($rec) { $to{$rec}=1; }
1.24      harris41  824:     } 
1.6       albertel  825:   }
1.36      www       826:   if ($ENV{'form.course'}||$course) {
1.63      albertel  827:     $typestyle.='Submitting as Comment<br />';
1.24      harris41  828:     foreach (split(/\,/,
                    829: 		   $ENV{'course.'.$ENV{'request.course.id'}.'.comment.email'})
                    830: 	     ) {
1.36      www       831: 	my $rec=&secapply($_,$defaultflag);
                    832:         if ($rec) { $to{$rec}=1; }
1.24      harris41  833:     } 
1.6       albertel  834:   }
1.36      www       835:   if ($ENV{'form.policy'}||$policy) {
1.63      albertel  836:     $typestyle.='Submitting as Policy Feedback<br />';
1.24      harris41  837:     foreach (split(/\,/,
                    838: 		   $ENV{'course.'.$ENV{'request.course.id'}.'.policy.email'})
                    839: 	     ) {
1.36      www       840: 	my $rec=&secapply($_,$defaultflag);
                    841:         if ($rec) { $to{$rec}=1; }
1.24      harris41  842:     } 
1.6       albertel  843:   }
1.36      www       844:   if ((scalar(%to) eq '0') && (!$defaultflag)) {
                    845:      ($typestyle,%to)=
                    846: 	 &decide_receiver($feedurl,$author,$question,$course,$policy,1);
                    847:   }
1.6       albertel  848:   return ($typestyle,%to);
1.36      www       849: }
                    850: 
                    851: sub feedback_available {
                    852:     my ($question,$course,$policy)=@_;
                    853:     my ($typestyle,%to)=&decide_receiver('',0,$question,$course,$policy);
                    854:     return scalar(%to);
1.6       albertel  855: }
                    856: 
                    857: sub send_msg {
1.43      www       858:   my ($feedurl,$email,$citations,$attachmenturl,%to)=@_;
1.6       albertel  859:   my $status='';
                    860:   my $sendsomething=0;
1.24      harris41  861:   foreach (keys %to) {
1.6       albertel  862:     if ($_) {
1.22      www       863:       my $declutter=&Apache::lonnet::declutter($feedurl);
1.8       www       864:       unless (&Apache::lonmsg::user_normal_msg(split(/\:/,$_),
1.43      www       865:                'Feedback ['.$declutter.']',$email,$citations,$feedurl,
                    866:                 $attachmenturl)=~/ok/) {
1.63      albertel  867: 	$status.='<br />'.&mt('Error sending message to').' '.$_.'<br />';
1.6       albertel  868:       } else {
                    869: 	$sendsomething++;
                    870:       }
                    871:     }
1.24      harris41  872:   }
1.18      www       873: 
                    874:     my %record=&Apache::lonnet::restore('_feedback');
                    875:     my ($temp)=keys %record;
                    876:     unless ($temp=~/^error\:/) {
                    877:        my %newrecord=();
                    878:        $newrecord{'resource'}=$feedurl;
                    879:        $newrecord{'subnumber'}=$record{'subnumber'}+1;
                    880:        unless (&Apache::lonnet::cstore(\%newrecord,'_feedback') eq 'ok') {
1.63      albertel  881: 	   $status.='<br />'.&mt('Not registered').'<br />';
1.18      www       882:        }
                    883:     }
                    884:        
1.6       albertel  885:   return ($status,$sendsomething);
                    886: }
                    887: 
1.13      www       888: sub adddiscuss {
1.78      raeburn   889:     my ($symb,$email,$anon,$attachmenturl,$subject)=@_;
1.13      www       890:     my $status='';
1.92      albertel  891:     if (&discussion_open() &&
1.90      albertel  892: 	&Apache::lonnet::allowed('pch',$ENV{'request.course.id'}.
1.23      www       893:         ($ENV{'request.course.sec'}?'/'.$ENV{'request.course.sec'}:''))) {
1.20      www       894: 
1.13      www       895:     my %contrib=('message'      => $email,
                    896:                  'sendername'   => $ENV{'user.name'},
1.26      www       897:                  'senderdomain' => $ENV{'user.domain'},
                    898:                  'screenname'   => $ENV{'environment.screenname'},
                    899:                  'plainname'    => $ENV{'environment.firstname'}.' '.
                    900: 		                   $ENV{'environment.middlename'}.' '.
                    901:                                    $ENV{'environment.lastname'}.' '.
1.42      www       902:                                    $ENV{'enrironment.generation'},
1.78      raeburn   903:                  'attachmenturl'=> $attachmenturl,
                    904:                  'subject'      => $subject);
1.65      www       905:     if ($ENV{'form.replydisc'}) {
1.66      www       906: 	$contrib{'replyto'}=(split(/\:\:\:/,$ENV{'form.replydisc'}))[1];
1.65      www       907:     }
1.14      www       908:     if ($anon) {
                    909: 	$contrib{'anonymous'}='true';
                    910:     }
1.13      www       911:     if (($symb) && ($email)) {
1.14      www       912:        $status='Adding to class discussion'.($anon?' (anonymous)':'').': '.
1.13      www       913:         &Apache::lonnet::store(\%contrib,$symb,$ENV{'request.course.id'},
                    914:                      $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
1.17      www       915: 		     $ENV{'course.'.$ENV{'request.course.id'}.'.num'});
1.21      www       916:         my %storenewentry=($symb => time);
1.63      albertel  917:         $status.='<br />'.&mt('Updating discussion time').': '.
1.21      www       918:         &Apache::lonnet::put('discussiontimes',\%storenewentry,
                    919:                      $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
                    920: 		     $ENV{'course.'.$ENV{'request.course.id'}.'.num'});
1.13      www       921:     }
1.17      www       922:     my %record=&Apache::lonnet::restore('_discussion');
                    923:     my ($temp)=keys %record;
                    924:     unless ($temp=~/^error\:/) {
                    925:        my %newrecord=();
                    926:        $newrecord{'resource'}=$symb;
                    927:        $newrecord{'subnumber'}=$record{'subnumber'}+1;
1.63      albertel  928:        $status.='<br />'.&mt('Registering').': '.
1.21      www       929:                &Apache::lonnet::cstore(\%newrecord,'_discussion');
1.20      www       930:     }
                    931:     } else {
                    932: 	$status.='Failed.';
1.17      www       933:     }
1.63      albertel  934:     return $status.'<br />';   
1.13      www       935: }
                    936: 
1.33      www       937: # ----------------------------------------------------------- Preview function
                    938: 
                    939: sub show_preview {
                    940:     my $r=shift;
                    941:     my $message=&clear_out_html($ENV{'form.comment'});
                    942:     $message=~s/\n/\<br \/\>/g;
                    943:     $message=&Apache::lontexconvert::msgtexconverted($message);
1.78      raeburn   944:     my $subject=&clear_out_html($ENV{'form.subject'});
                    945:     $subject=~s/\n/\<br \/\>/g;
                    946:     $subject=&Apache::lontexconvert::msgtexconverted($subject);
1.33      www       947:     $r->print('<table border="2"><tr><td>'.
1.78      raeburn   948:        '<b>Subject:</b> '.$subject.'<br /><br />'.
1.33      www       949:        $message.'</td></tr></table>');
                    950: }
                    951: 
                    952: sub generate_preview_button {
1.65      www       953:     my $pre=&mt("Show Preview");
1.33      www       954:     return(<<ENDPREVIEW);
                    955: <form name="preview" action="/adm/feedback?preview=1" method="post" target="preview">
1.78      raeburn   956: <input type="hidden" name="subject">
1.33      www       957: <input type="hidden" name="comment" />
1.65      www       958: <input type="button" value="$pre"
1.87      www       959: onClick="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       960: </form>
                    961: ENDPREVIEW
                    962: }
1.71      www       963: 
1.6       albertel  964: sub handler {
                    965:   my $r = shift;
1.8       www       966:   if ($r->header_only) {
1.71      www       967:      &Apache::loncommon::content_type($r,'text/html');
1.8       www       968:      $r->send_http_header;
                    969:      return OK;
                    970:   }
1.15      www       971: 
                    972: # --------------------------- Get query string for limited number of parameters
                    973: 
1.27      stredwic  974:    &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
1.84      raeburn   975:          ['hide','unhide','deldisc','postdata','preview','replydisc','threadedon','threadedoff','onlyunread','allposts','previous','markread','markonread','markondisp']);
                    976: 
                    977:   if (($ENV{'form.markondisp'}) || ($ENV{'form.markonread'})) {
                    978: # ---------------------- Modify setting for identification of 'NEW' posts in this discussion
1.15      www       979: 
1.84      raeburn   980:       &Apache::loncommon::content_type($r,'text/html');
                    981:       $r->send_http_header;
                    982:       my $symb=$ENV{'form.markondisp'}?$ENV{'form.markondisp'}:$ENV{'form.markonread'};
                    983:       my $ressymb = $symb;
                    984:       my ($map,$ind,$url)=&Apache::lonnet::decode_symb($symb);
                    985:       unless ($ressymb =~ m|bulletin___\d+___adm/wrapper|) {
                    986:           $ressymb=~s|(bulletin___\d+___)|$1adm/wrapper|;
                    987:       }
                    988:                                                                                           
                    989:       my %discinfo = ();
                    990:       my $lastkey = $ressymb.'_lastread';
                    991:       my $ondispkey = $ressymb.'_markondisp';
                    992:       if ($ENV{'form.markondisp'}) {
                    993:           $discinfo{$lastkey} = time;
                    994:           $discinfo{$ondispkey} = 1;
                    995:       } elsif ($ENV{'form.markonread'}) {
                    996:           if ( defined($ENV{'previous'}) ) {
                    997:               $discinfo{$lastkey} = $ENV{'previous'};
                    998:           }
                    999:           $discinfo{$ondispkey} = 0;
                   1000:       }
                   1001:       &Apache::lonnet::put('nohist_'.$ENV{'request.course.id'}.'_discuss',\%discinfo,$ENV{'user.domain'},$ENV{'user.name'});
                   1002:       if ($ENV{'form.markondisp'}) {
                   1003:           &redirect_back($r,&Apache::lonnet::clutter($url),&mt('Changed display status').'<br />','0','0');
                   1004:       } else {
                   1005:           &redirect_back($r,&Apache::lonnet::clutter($url),&mt('Changed display status').'<br />','0','0','',$ENV{'form.previous'});
                   1006:       }
                   1007:       return OK;
                   1008:   } elsif (($ENV{'form.allposts'}) || ($ENV{'form.onlyunread'})) {
1.80      raeburn  1009: # ----------------------------------------------------------------- Modify display setting for this discussion 
1.78      raeburn  1010:       &Apache::loncommon::content_type($r,'text/html');
                   1011:       $r->send_http_header;
1.80      raeburn  1012:       my $symb=$ENV{'form.allposts'}?$ENV{'form.allposts'}:$ENV{'form.onlyunread'};
1.84      raeburn  1013:       my $ressymb = $symb;
1.78      raeburn  1014:       my ($map,$ind,$url)=&Apache::lonnet::decode_symb($symb);
1.84      raeburn  1015:       unless ($ressymb =~ m|bulletin___\d+___adm/wrapper|) {
                   1016:           $ressymb=~s|(bulletin___\d+___)|$1adm/wrapper|;
                   1017:       }
                   1018:       my %discinfo = ();
                   1019:       if ($ENV{'form.allposts'}) {
                   1020:           $discinfo{$ressymb.'_showonlyunread'} = 0;
                   1021:       } elsif ($ENV{'form.onlyunread'}) {
                   1022:           $discinfo{$ressymb.'_showonlyunread'} = 1;
                   1023:       }
                   1024:       &Apache::lonnet::put('nohist_'.$ENV{'request.course.id'}.'_discuss',\%discinfo,$ENV{'user.domain'},$ENV{'user.name'});
                   1025:       &redirect_back($r,&Apache::lonnet::clutter($url),&mt('Changed display status').'<br />','0','0','',$ENV{'form.previous'});
                   1026:       return OK;
                   1027:   } elsif ($ENV{'form.markread'}) {
                   1028: # ----------------------------------------------------------------- Mark new posts as read
                   1029:       &Apache::loncommon::content_type($r,'text/html');
                   1030:       $r->send_http_header;
                   1031:       my $symb=$ENV{'form.markread'};
                   1032:       my $ressymb = $symb;
                   1033:       my ($map,$ind,$url)=&Apache::lonnet::decode_symb($symb);
                   1034:       unless ($ressymb =~ m|bulletin___\d+___adm/wrapper|) {
                   1035:           $ressymb=~s|(bulletin___\d+___)|$1adm/wrapper|;
1.78      raeburn  1036:       }
1.84      raeburn  1037:       my %discinfo = ();
                   1038:       my $lastkey = $ressymb.'_lastread';
                   1039:       $discinfo{$lastkey} = time;
                   1040:       &Apache::lonnet::put('nohist_'.$ENV{'request.course.id'}.'_discuss',\%discinfo,$ENV{'user.domain'},$ENV{'user.name'});
                   1041:       &redirect_back($r,&Apache::lonnet::clutter($url),&mt('Changed reading status').'<br />','0','0');
1.78      raeburn  1042:       return OK;
                   1043:   } elsif (($ENV{'form.hide'}) || ($ENV{'form.unhide'})) {
1.15      www      1044: # ----------------------------------------------------------------- Hide/unhide
1.71      www      1045:     &Apache::loncommon::content_type($r,'text/html');
1.15      www      1046:     $r->send_http_header;
                   1047: 
                   1048:     my $entry=$ENV{'form.hide'}?$ENV{'form.hide'}:$ENV{'form.unhide'};
                   1049: 
                   1050:     my ($symb,$idx)=split(/\:\:\:/,$entry);
1.52      www      1051:     my ($map,$ind,$url)=&Apache::lonnet::decode_symb($symb);
1.15      www      1052: 
                   1053:     my %contrib=&Apache::lonnet::restore($symb,$ENV{'request.course.id'},
                   1054:                      $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
                   1055: 		     $ENV{'course.'.$ENV{'request.course.id'}.'.num'});
                   1056: 
                   1057:         
                   1058:     my $currenthidden=$contrib{'hidden'};
                   1059:     
                   1060:     if ($ENV{'form.hide'}) {
                   1061: 	$currenthidden.='.'.$idx.'.';
                   1062:     } else {
                   1063:         $currenthidden=~s/\.$idx\.//g;
                   1064:     }
                   1065:     my %newhash=('hidden' => $currenthidden);
1.38      www      1066: 
                   1067:     &Apache::lonnet::store(\%newhash,$symb,$ENV{'request.course.id'},
                   1068:                      $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
                   1069: 		     $ENV{'course.'.$ENV{'request.course.id'}.'.num'});
                   1070: 
                   1071:     &redirect_back($r,&Apache::lonnet::clutter($url),
1.80      raeburn  1072:        &mt('Changed discussion status').'<br />','0','0','',$ENV{'form.previous'});
1.69      www      1073:   } elsif (($ENV{'form.threadedon'}) || ($ENV{'form.threadedoff'})) {
1.72      albertel 1074:       &Apache::loncommon::content_type($r,'text/html');
                   1075:       $r->send_http_header;
1.69      www      1076:       if ($ENV{'form.threadedon'}) {
                   1077: 	  &Apache::lonnet::put('environment',{'threadeddiscussion' => 'on'});
                   1078: 	  &Apache::lonnet::appenv('environment.threadeddiscussion' => 'on');
                   1079:       } else {
                   1080:  	  &Apache::lonnet::del('environment',['threadeddiscussion']);
                   1081: 	  &Apache::lonnet::delenv('environment\.threadeddiscussion');
1.72      albertel 1082:       }
1.69      www      1083:       my $symb=$ENV{'form.threadedon'}?$ENV{'form.threadedon'}:$ENV{'form.threadedoff'};
                   1084:       my ($map,$ind,$url)=&Apache::lonnet::decode_symb($symb);
                   1085:       &redirect_back($r,&Apache::lonnet::clutter($url),
1.80      raeburn  1086: 		     &mt('Changed discussion view mode').'<br />','0','0','',$ENV{'form.previous'});
1.38      www      1087:   } elsif ($ENV{'form.deldisc'}) {
                   1088: # --------------------------------------------------------------- Hide for good
1.71      www      1089:     &Apache::loncommon::content_type($r,'text/html');
1.38      www      1090:     $r->send_http_header;
                   1091: 
                   1092:     my $entry=$ENV{'form.deldisc'};
                   1093: 
                   1094:     my ($symb,$idx)=split(/\:\:\:/,$entry);
1.52      www      1095:     my ($map,$ind,$url)=&Apache::lonnet::decode_symb($symb);
1.38      www      1096: 
                   1097:     my %contrib=&Apache::lonnet::restore($symb,$ENV{'request.course.id'},
                   1098:                      $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
                   1099: 		     $ENV{'course.'.$ENV{'request.course.id'}.'.num'});
                   1100: 
                   1101:         
                   1102:     my $currentdeleted=$contrib{'deleted'};
                   1103:     
                   1104:     $currentdeleted.='.'.$idx.'.';
                   1105: 
                   1106:     my %newhash=('deleted' => $currentdeleted);
1.15      www      1107: 
                   1108:     &Apache::lonnet::store(\%newhash,$symb,$ENV{'request.course.id'},
                   1109:                      $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
                   1110: 		     $ENV{'course.'.$ENV{'request.course.id'}.'.num'});
                   1111: 
1.30      www      1112:     &redirect_back($r,&Apache::lonnet::clutter($url),
1.80      raeburn  1113:        &mt('Changed discussion status').'<br />','0','0','',$ENV{'form.previous'});
1.33      www      1114:   } elsif ($ENV{'form.preview'}) {
                   1115: # -------------------------------------------------------- User wants a preview
1.76      albertel 1116:       $r->content_type('text/html');
                   1117:       $r->send_http_header;
1.33      www      1118:       &show_preview($r);
1.15      www      1119:   } else {
                   1120: # ------------------------------------------------------------- Normal feedback
1.6       albertel 1121:   my $feedurl=$ENV{'form.postdata'};
                   1122:   $feedurl=~s/^http\:\/\///;
                   1123:   $feedurl=~s/^$ENV{'SERVER_NAME'}//;
                   1124:   $feedurl=~s/^$ENV{'HTTP_HOST'}//;
1.62      www      1125:   $feedurl=~s/\?.+$//;
1.8       www      1126: 
1.66      www      1127:   my $symb;
                   1128:   if ($ENV{'form.replydisc'}) {
                   1129:       $symb=(split(/\:\:\:/,$ENV{'form.replydisc'}))[0];
                   1130:       my ($map,$id,$url)=&Apache::lonnet::decode_symb($symb);
                   1131:       $feedurl=&Apache::lonnet::clutter($url);
                   1132:   } else {
                   1133:       $symb=&Apache::lonnet::symbread($feedurl);
                   1134:   }
1.31      www      1135:   unless ($symb) {
                   1136:       $symb=$ENV{'form.symb'};
                   1137:       if ($symb) {
1.52      www      1138: 	  my ($map,$id,$url)=&Apache::lonnet::decode_symb($symb);
1.31      www      1139:           $feedurl=&Apache::lonnet::clutter($url);
                   1140:       }
                   1141:   }
1.8       www      1142:   my $goahead=1;
                   1143:   if ($feedurl=~/\.(problem|exam|quiz|assess|survey|form)$/) {
                   1144:       unless ($symb) { $goahead=0; }
                   1145:   }
1.74      www      1146:   # backward compatibility (bulltin boards used to be 'wrapped')
1.73      albertel 1147:   if ($feedurl=~m|^/adm/wrapper/adm/.*/bulletinboard$|) {
                   1148:       $feedurl=~s|^/adm/wrapper||;
                   1149:   }
1.8       www      1150:   if ($goahead) {
                   1151: # Go ahead with feedback, no ambiguous reference
1.71      www      1152:     &Apache::loncommon::content_type($r,'text/html');
1.8       www      1153:     $r->send_http_header;
1.6       albertel 1154:   
1.8       www      1155:     if (
1.7       albertel 1156:       (
                   1157:        ($feedurl=~m:^/res:) && ($feedurl!~m:^/res/adm:)
                   1158:       ) 
                   1159:       || 
                   1160:       ($ENV{'request.course.id'} && ($feedurl!~m:^/adm:))
1.31      www      1161:       ||
                   1162:       ($ENV{'request.course.id'} && ($symb=~/^bulletin\_\_\_/))
1.7       albertel 1163:      ) {
1.6       albertel 1164: # --------------------------------------------------- Print login screen header
                   1165:     unless ($ENV{'form.sendit'}) {
                   1166:       my $options=&screen_header($feedurl);
                   1167:       if ($options) {
                   1168: 	&mail_screen($r,$feedurl,$options);
                   1169:       } else {
                   1170: 	&fail_redirect($r,$feedurl);
                   1171:       }
                   1172:     } else {
                   1173:       
                   1174: # Get previous user input
1.9       albertel 1175:       my $prevattempts=&Apache::loncommon::get_previous_attempt(
1.11      albertel 1176:             $symb,$ENV{'user.name'},$ENV{'user.domain'},
1.9       albertel 1177:             $ENV{'request.course.id'});
1.6       albertel 1178: 
                   1179: # Get output from resource
                   1180:       my $usersaw=&resource_output($feedurl);
                   1181: 
1.50      albertel 1182: # Get resource answer (need to allow student to view grades for this to work)
                   1183:       &Apache::lonnet::appenv(('allowed.vgr'=>'F'));
1.40      albertel 1184:       my $useranswer=&Apache::loncommon::get_student_answers(
                   1185:                        $symb,$ENV{'user.name'},$ENV{'user.domain'},
                   1186: 		       $ENV{'request.course.id'});
1.50      albertel 1187:       &Apache::lonnet::delenv('allowed.vgr');
1.42      www      1188: # Get attachments, if any, and not too large
                   1189:       my $attachmenturl='';
                   1190:       if ($ENV{'form.attachment.filename'}) {
                   1191: 	  unless (length($ENV{'form.attachment'})>131072) {
1.82      albertel 1192: 	      $attachmenturl=&Apache::lonnet::userfileupload('attachment',undef,'feedback');
1.42      www      1193: 	  }
                   1194:       }
1.6       albertel 1195: # Filter HTML out of message (could be nasty)
1.39      www      1196:       my $message=&clear_out_html($ENV{'form.comment'});
1.6       albertel 1197: 
                   1198: # Assemble email
1.8       www      1199:       my ($email,$citations)=&assemble_email($feedurl,$message,$prevattempts,
1.40      albertel 1200:           $usersaw,$useranswer);
                   1201:  
1.6       albertel 1202: # Who gets this?
                   1203:       my ($typestyle,%to) = &decide_receiver($feedurl);
                   1204: 
                   1205: # Actually send mail
1.43      www      1206:       my ($status,$numsent)=&send_msg($feedurl,$email,$citations,
                   1207:           $attachmenturl,%to);
1.13      www      1208: 
                   1209: # Discussion? Store that.
                   1210: 
1.32      albertel 1211:       my $numpost=0;
1.13      www      1212:       if ($ENV{'form.discuss'}) {
1.78      raeburn  1213:           my $subject = &clear_out_html($ENV{'form.subject'});
                   1214: 	  $typestyle.=&adddiscuss($symb,$message,0,$attachmenturl,$subject);
1.32      albertel 1215: 	  $numpost++;
1.13      www      1216:       }
1.6       albertel 1217: 
1.14      www      1218:       if ($ENV{'form.anondiscuss'}) {
1.78      raeburn  1219:           my $subject = &clear_out_html($ENV{'form.subject'});
                   1220: 	  $typestyle.=&adddiscuss($symb,$message,1,$attachmenturl,$subject);
1.32      albertel 1221: 	  $numpost++;
1.14      www      1222:       }
                   1223: 
                   1224: 
1.6       albertel 1225: # Receipt screen and redirect back to where came from
1.80      raeburn  1226:       &redirect_back($r,$feedurl,$typestyle,$numsent,$numpost,$status,$ENV{'form.previous'});
1.6       albertel 1227: 
                   1228:     }
1.8       www      1229:    } else {
1.7       albertel 1230: # Unable to give feedback
1.6       albertel 1231:     &no_redirect_back($r,$feedurl);
1.8       www      1232:    }
                   1233:   } else {
                   1234: # Ambiguous Problem Resource
1.60      albertel 1235:       if ( &Apache::lonnet::mod_perl_version() == 2 ) {
1.53      albertel 1236: 	  &Apache::lonnet::cleanenv();
1.58      albertel 1237:       }
1.53      albertel 1238:       $r->internal_redirect('/adm/ambiguous');
1.6       albertel 1239:   }
1.15      www      1240: }
1.6       albertel 1241:   return OK;
1.1       www      1242: } 
                   1243: 
                   1244: 1;
                   1245: __END__

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