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

1.1       www         1: # The LearningOnline Network
                      2: # Feedback
                      3: #
1.77    ! www         4: # $Id: lonfeedback.pm,v 1.76 2004/02/11 22:14:10 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.62      www        37: use Apache::lonlocal;
1.54      www        38: 
                     39: sub list_discussion {
1.57      www        40:     my ($mode,$status,$symb)=@_;
1.77    ! www        41: #    &Apache::lonnet::logthis("status is $status");
        !            42:     if (!($status eq 'CAN_ANSWER' || $status eq 'CANNOT_ANSWER'
        !            43: 	  || $status eq 'OPEN')) {
1.75      albertel   44: 	return '';
                     45:     }
1.57      www        46:     my $discussiononly=0;
                     47:     if ($mode eq 'board') { $discussiononly=1; }
1.55      www        48:     unless ($ENV{'request.course.id'}) { return ''; }
                     49:     my $crs='/'.$ENV{'request.course.id'};
                     50:     if ($ENV{'request.course.sec'}) {
                     51: 	$crs.='_'.$ENV{'request.course.sec'};
                     52:     }                 
                     53:     $crs=~s/\_/\//g;
1.54      www        54:     unless ($symb) {
                     55: 	$symb=&Apache::lonnet::symbread();
                     56:     }
                     57:     unless ($symb) { return ''; }
                     58:     my $seeid=&Apache::lonnet::allowed('rin',$crs);
1.77    ! www        59:     my $viewgrades=(&Apache::lonnet::allowed('vgr',$crs)
        !            60: 	&& ($symb=~/\.(problem|exam|quiz|assess|survey|form)$/));
1.68      www        61:     my @discussionitems=();
1.74      www        62:     # backward compatibility (bulletin boards used to be 'wrapped')
1.73      albertel   63:     my $ressymb=$symb;
                     64:     if ($mode eq 'board') {
                     65: 	$ressymb=~s|(bulletin___\d+___)|$1adm/wrapper|;
                     66:     }
                     67:     my %contrib=&Apache::lonnet::restore($ressymb,$ENV{'request.course.id'},
1.54      www        68: 			  $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
                     69: 			  $ENV{'course.'.$ENV{'request.course.id'}.'.num'});
1.67      www        70:     my $visible=0;
1.68      www        71:     my @depth=();
                     72:     my @original=();
                     73:     my @index=();
                     74:     my @replies=();
                     75:     my %alldiscussion=();
                     76:     my $maxdepth=0;
                     77: 
1.69      www        78:     my $target='';
                     79:     unless ($ENV{'browser.interface'} eq 'textual' ||
                     80: 	    $ENV{'environment.remote'} eq 'off' ) {
                     81: 	$target='target="LONcom"';
                     82:     }
1.54      www        83:     if ($contrib{'version'}) {
1.64      www        84: 	for (my $id=1;$id<=$contrib{'version'};$id++) {
                     85: 	    my $idx=$id;
1.54      www        86: 	    my $hidden=($contrib{'hidden'}=~/\.$idx\./);
                     87: 	    my $deleted=($contrib{'deleted'}=~/\.$idx\./);
1.68      www        88: 	    my $origindex='0.';
1.69      www        89: 	    if (($contrib{$idx.':replyto'}) && ($ENV{'environment.threadeddiscussion'})) {
1.68      www        90: # this is a follow-up message
                     91: 		$original[$idx]=$original[$contrib{$idx.':replyto'}];
                     92: 		$depth[$idx]=$depth[$contrib{$idx.':replyto'}]+1;
                     93: 		$origindex=$index[$contrib{$idx.':replyto'}];
                     94: 		if ($depth[$idx]>$maxdepth) { $maxdepth=$depth[$idx]; }
                     95: 	    } else {
                     96: # this is an original message
                     97: 		$original[$idx]=0;
                     98: 		$depth[$idx]=0;
                     99: 	    }
                    100: 	    if ($replies[$depth[$idx]]) {
                    101: 		$replies[$depth[$idx]]++;
                    102: 	    } else {
                    103: 		$replies[$depth[$idx]]=1;
                    104: 	    }
1.54      www       105: 	    unless ((($hidden) && (!$seeid)) || ($deleted)) {
1.67      www       106: 		$visible++;
1.54      www       107: 		my $message=$contrib{$idx.':message'};
                    108: 		$message=~s/\n/\<br \/\>/g;
                    109: 		$message=&Apache::lontexconvert::msgtexconverted($message);
                    110: 		if ($contrib{$idx.':attachmenturl'}) {
                    111: 		    my ($fname,$ft)
                    112:                         =($contrib{$idx.':attachmenturl'}=~/\/(\w+)\.(\w+)$/);
1.62      www       113: 		    $message.='<p>'.&mt('Attachment').': <a href="'.
1.54      www       114: 			&Apache::lonnet::tokenwrapper(
                    115:                                              $contrib{$idx.':attachmenturl'}).
1.63      albertel  116: 			'"><tt>'.$fname.'.'.$ft.'</tt></a></p>';
1.54      www       117: 		}
                    118: 		if ($message) {
                    119: 		    if ($hidden) {
                    120: 			$message='<font color="#888888">'.$message.'</font>';
                    121: 		    }
                    122: 		    my $screenname=&Apache::loncommon::screenname(
                    123: 					    $contrib{$idx.':sendername'},
                    124: 					    $contrib{$idx.':senderdomain'});
                    125: 		    my $plainname=&Apache::loncommon::nickname(
                    126: 					    $contrib{$idx.':sendername'},
                    127: 					    $contrib{$idx.':senderdomain'});
                    128: 		    
1.62      www       129: 		    my $sender=&mt('Anonymous');
1.54      www       130: 		    if ((!$contrib{$idx.':anonymous'}) || ($seeid)) {
                    131: 			$sender=&Apache::loncommon::aboutmewrapper(
                    132: 					 $plainname,
                    133: 					 $contrib{$idx.':sendername'},
                    134: 					 $contrib{$idx.':senderdomain'}).' ('.
                    135: 					 $contrib{$idx.':sendername'}.' at '.
                    136: 					 $contrib{$idx.':senderdomain'}.')';
                    137: 			if ($contrib{$idx.':anonymous'}) {
1.62      www       138: 			    $sender.=' ['.&mt('anonymous').'] '.
1.54      www       139: 				$screenname;
                    140: 			}
                    141: 			if ($seeid) {
                    142: 			    if ($hidden) {
                    143: 				$sender.=' <a href="/adm/feedback?unhide='.
1.77    ! www       144: 				    $ressymb.':::'.$idx.'">'.&mt('Make Visible').'</a>';
1.54      www       145: 			    } else {
                    146: 				$sender.=' <a href="/adm/feedback?hide='.
1.77    ! www       147: 				    $ressymb.':::'.$idx.'">'.&mt('Hide').'</a>';
1.54      www       148: 			    }                     
                    149: 			    $sender.=' <a href="/adm/feedback?deldisc='.
1.77    ! www       150: 				$ressymb.':::'.$idx.'">'.&mt('Delete').'</a>';
1.64      www       151: 			}
1.54      www       152: 		    } else {
                    153: 			if ($screenname) {
                    154: 			    $sender='<i>'.$screenname.'</i>';
                    155: 			}
1.77    ! www       156: 		    }
        !           157: 		    if (&Apache::lonnet::allowed('pch',
        !           158: 						 $ENV{'request.course.id'}.
        !           159: 						 ($ENV{'request.course.sec'}?'/'.$ENV{'request.course.sec'}:''))) {
        !           160: 			$sender.=' <a href="/adm/feedback?replydisc='.
        !           161: 			    $ressymb.':::'.$idx.'" '.$target.'>'.&mt('Reply').'</a>';
1.54      www       162: 		    }
                    163: 		    my $vgrlink;
                    164: 		    if ($viewgrades) {
                    165: 			$vgrlink=&Apache::loncommon::submlink('Submissions',
                    166:             $contrib{$idx.':sendername'},$contrib{$idx.':senderdomain'},$symb);
                    167: 		    }
1.68      www       168: #figure out at what position this needs to print
                    169: 		    my $thisindex=$idx;
1.69      www       170: 		    if ($ENV{'environment.threadeddiscussion'}) {
1.68      www       171: 			$thisindex=$origindex.substr('00'.$replies[$depth[$idx]],-2,2);	
                    172: 		    }
                    173: 		    $alldiscussion{$thisindex}=$idx;
                    174: 		    $index[$idx]=$thisindex;
                    175: 		    $discussionitems[$idx]='<p><b>'.$sender.'</b> '.$vgrlink.' ('.
1.54      www       176: 			localtime($contrib{$idx.':timestamp'}).
                    177: 			'):<blockquote>'.$message.
                    178: 			'</blockquote></p>';
                    179: 	        }
                    180: 	    } 
                    181: 	}
1.64      www       182:     }
1.67      www       183:     my $discussion='';
                    184:     if ($visible) {
1.69      www       185: # Print a the discusssion
1.67      www       186: 	$discussion.='<table bgcolor="#AAAAAA" cellpadding="2" cellspacing="2" border="0">';
1.69      www       187: 	if ($visible>2) {
                    188: 	    my $colspan=$maxdepth+1;
                    189: 	    $discussion.='<tr><td bgcolor="DDDDBB" colspan="'.$colspan.'">'.
                    190:   '<a href="/adm/feedback?threadedon='.$symb.'">'.&mt('Threaded View').'</a>&nbsp;&nbsp;'.
                    191:   '<a href="/adm/feedback?threadedoff='.$symb.'">'.&mt('Chronological View').'</a>'.
                    192:   '</td></tr>';
                    193: 	}
                    194: 
1.68      www       195: 	foreach (sort { $a <=> $b } keys %alldiscussion) {
1.69      www       196: 	    $discussion.="\n<tr>";
                    197: 	    my $thisdepth=$depth[$alldiscussion{$_}];
                    198: 	    for (1..$thisdepth) {
                    199: 		$discussion.='<td>&nbsp;&nbsp;&nbsp;</td>';
                    200: 	    }
                    201: 	    my $colspan=$maxdepth-$thisdepth+1;
                    202:             $discussion.="<td  bgcolor='#CCCCCC' colspan='".$colspan."'>".$discussionitems[$alldiscussion{$_}].
1.67      www       203: 		"</td></tr>";
                    204: 	}
                    205:         $discussion.='</table>';
1.54      www       206:     }
                    207:     if ($discussiononly) {
                    208: 	$discussion.=(<<ENDDISCUSS);
                    209: <form action="/adm/feedback" method="post" name="mailform" enctype="multipart/form-data">
                    210: <input type="submit" name="discuss" value="Post Discussion" />
                    211: <input type="submit" name="anondiscuss" value="Post Anonymous Discussion" />
1.73      albertel  212: <input type="hidden" name="symb" value="$ressymb" />
1.54      www       213: <input type="hidden" name="sendit" value="true" />
                    214: <br />
                    215: <font size="1">Note: in anonymous discussion, your name is visible only to
                    216: course faculty</font><br />
1.63      albertel  217: <textarea name="comment" cols="60" rows="10" wrap="hard"></textarea>
1.54      www       218: <p>
                    219: Attachment (128 KB max size): <input type="file" name="attachment" />
                    220: </p>
                    221: </form>
                    222: ENDDISCUSS
                    223:       $discussion.=&generate_preview_button();
1.74      www       224:     } else {
                    225: 	if (&Apache::lonnet::allowed('pch',
                    226: 				   $ENV{'request.course.id'}.
                    227: 	($ENV{'request.course.sec'}?'/'.$ENV{'request.course.sec'}:''))) {
                    228: 			    $discussion.='<table bgcolor="#BBBBBB"><tr><td><a href="/adm/feedback?replydisc='.
                    229: 				$symb.':::" '.$target.'>'.
                    230: 				'<img src="/adm/lonMisc/chat.gif" border="0" />'.
                    231: 				&mt('Post Discussion').'</a></td></tr></table>';
                    232: 			}
                    233:     }
1.54      www       234:    return $discussion;
                    235: }
1.1       www       236: 
1.6       albertel  237: sub mail_screen {
                    238:   my ($r,$feedurl,$options) = @_;
1.45      www       239:   my $bodytag=&Apache::loncommon::bodytag('Resource Feedback and Discussion',
                    240:                                           '','onLoad="window.focus();"');
1.51      albertel  241:   my $title=&Apache::lonnet::gettitle($feedurl);
                    242:   if (!$title) { $title = $feedurl; }
1.69      www       243:   my $quote='';
                    244:   if ($ENV{'form.replydisc'}) {
                    245:       my ($symb,$idx)=split(/\:\:\:/,$ENV{'form.replydisc'});
                    246:       my %contrib=&Apache::lonnet::restore($symb,$ENV{'request.course.id'},
                    247: 					   $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
                    248: 					   $ENV{'course.'.$ENV{'request.course.id'}.'.num'});
                    249:       unless (($contrib{'hidden'}=~/\.$idx\./) || ($contrib{'deleted'}=~/\.$idx\./)) { 
                    250: 	  my $message=$contrib{$idx.':message'};
                    251: 	  $message=~s/\n/\<br \/\>/g;
                    252: 	  $quote='<blockquote>'.&Apache::lontexconvert::msgtexconverted($message).'</blockquote>';
                    253:       }
                    254:   }
1.47      bowersj2  255:   my $latexHelp = Apache::loncommon::helpLatexCheatsheet();
1.74      www       256:   my $send=&mt('Send');
1.6       albertel  257:   $r->print(<<ENDDOCUMENT);
1.1       www       258: <html>
                    259: <head>
                    260: <title>The LearningOnline Network with CAPA</title>
1.7       albertel  261: <meta http-equiv="pragma" content="no-cache"></meta>
1.63      albertel  262: <script type="text/javascript">
                    263: //<!--
1.5       www       264:     function gosubmit() {
                    265:         var rec=0;
1.12      albertel  266:         if (typeof(document.mailform.elements.author)!="undefined") {
1.5       www       267:           if (document.mailform.elements.author.checked) {
                    268:              rec=1;
                    269:           } 
                    270:         }
1.12      albertel  271:         if (typeof(document.mailform.elements.question)!="undefined") {
1.5       www       272:           if (document.mailform.elements.question.checked) {
                    273:              rec=1;
                    274:           } 
                    275:         }
1.12      albertel  276:         if (typeof(document.mailform.elements.course)!="undefined") {
1.5       www       277:           if (document.mailform.elements.course.checked) {
                    278:              rec=1;
                    279:           } 
                    280:         }
1.12      albertel  281:         if (typeof(document.mailform.elements.policy)!="undefined") {
1.5       www       282:           if (document.mailform.elements.policy.checked) {
                    283:              rec=1;
                    284:           } 
                    285:         }
1.12      albertel  286:         if (typeof(document.mailform.elements.discuss)!="undefined") {
1.10      www       287:           if (document.mailform.elements.discuss.checked) {
                    288:              rec=1;
                    289:           } 
                    290:         }
1.14      www       291:         if (typeof(document.mailform.elements.anondiscuss)!="undefined") {
                    292:           if (document.mailform.elements.anondiscuss.checked) {
                    293:              rec=1;
                    294:           } 
                    295:         }
1.5       www       296: 
                    297:         if (rec) {
                    298: 	    document.mailform.submit();
                    299:         } else {
                    300:             alert('Please check a feedback type.');
                    301: 	}
                    302:     }
1.63      albertel  303: //-->
1.5       www       304: </script>
1.1       www       305: </head>
1.29      www       306: $bodytag
1.51      albertel  307: <h2><tt>$title</tt></h2>
1.43      www       308: <form action="/adm/feedback" method="post" name="mailform"
                    309: enctype="multipart/form-data">
1.63      albertel  310: <input type="hidden" name="postdata" value="$feedurl" />
1.68      www       311: <input type="hidden" name="replydisc" value="$ENV{'form.replydisc'}" />
1.5       www       312: Please check at least one of the following feedback types:
1.63      albertel  313: $options<hr />
1.69      www       314: $quote
1.63      albertel  315: <p>My question/comment/feedback:</p>
                    316: <p>
1.47      bowersj2  317: $latexHelp
1.63      albertel  318: <textarea name="comment" cols="60" rows="10" wrap="hard">
                    319: </textarea></p>
                    320: <p>
1.42      www       321: Attachment (128 KB max size): <input type="file" name="attachment" />
                    322: </p>
                    323: <p>
                    324: <input type="hidden" name="sendit" value="1" />
1.74      www       325: <input type="button" value="$send" onClick='gosubmit();' />
1.42      www       326: </p>
1.2       www       327: </form>
1.1       www       328: ENDDOCUMENT
1.33      www       329: $r->print(&generate_preview_button().'</body></html>');
1.6       albertel  330: }
                    331: 
                    332: sub fail_redirect {
                    333:   my ($r,$feedurl) = @_;
1.70      www       334:   if ($feedurl=~/^\/adm\//) { $feedurl.='?register=1' };
1.6       albertel  335:   $r->print (<<ENDFAILREDIR);
1.72      albertel  336: <html>
1.5       www       337: <head><title>Feedback not sent</title>
1.63      albertel  338: <meta http-equiv="pragma" content="no-cache" />
                    339: <meta HTTP-EQUIV="Refresh" CONTENT="2; url=$feedurl" />
1.5       www       340: </head>
                    341: <body bgcolor="#FFFFFF">
1.63      albertel  342: <img align="right" src="/adm/lonIcons/lonlogos.gif" />
1.8       www       343: <b>Sorry, no recipients  ...</b>
1.5       www       344: </body>
                    345: </html>
                    346: ENDFAILREDIR
                    347: }
1.4       www       348: 
1.6       albertel  349: sub redirect_back {
1.32      albertel  350:   my ($r,$feedurl,$typestyle,$sendsomething,$sendposts,$status) = @_;
1.70      www       351:   if ($feedurl=~/^\/adm\//) { $feedurl.='?register=1' };
1.6       albertel  352:   $r->print (<<ENDREDIR);
1.72      albertel  353: <html>
1.3       www       354: <head>
                    355: <title>Feedback sent</title>
1.63      albertel  356: <meta http-equiv="pragma" content="no-cache" />
1.5       www       357: <meta HTTP-EQUIV="Refresh" CONTENT="2; url=$feedurl">
1.2       www       358: </head>
1.49      www       359: <body bgcolor="#FFFFFF" onLoad='if (window.name!="loncapaclient") { this.document.reldt.submit(); self.close(); }'>
1.63      albertel  360: <img align="right" src="/adm/lonIcons/lonlogos.gif" />
1.5       www       361: $typestyle
1.32      albertel  362: <b>Sent $sendsomething message(s), and $sendposts post(s).</b>
1.63      albertel  363: <font color="red">$status</font>
1.49      www       364: <form name="reldt" action="$feedurl" target="loncapaclient">
                    365: </form>
1.2       www       366: </body>
                    367: </html>
                    368: ENDREDIR
                    369: }
1.6       albertel  370: 
                    371: sub no_redirect_back {
                    372:   my ($r,$feedurl) = @_;
                    373:   $r->print (<<ENDNOREDIR);
1.72      albertel  374: <html>
1.2       www       375: <head><title>Feedback not sent</title>
1.63      albertel  376: <meta http-equiv="pragma" content="no-cache" />
1.7       albertel  377: ENDNOREDIR
                    378: 
1.8       www       379:   if ($feedurl!~/^\/adm\/feedback/) { 
1.7       albertel  380:     $r->print('<meta HTTP-EQUIV="Refresh" CONTENT="2; url='.$feedurl.'">');
                    381:   }
                    382:   
1.8       www       383:   $r->print (<<ENDNOREDIRTWO);
1.2       www       384: </head>
1.49      www       385: <body bgcolor="#FFFFFF" onLoad='if (window.name!="loncapaclient") { self.close(); }'>
1.63      albertel  386: <img align="right" src="/adm/lonIcons/lonlogos.gif" />
1.8       www       387: <b>Sorry, no feedback possible on this resource  ...</b>
1.2       www       388: </body>
                    389: </html>
1.8       www       390: ENDNOREDIRTWO
1.2       www       391: }
1.6       albertel  392: 
                    393: sub screen_header {
1.65      www       394:     my ($feedurl) = @_;
                    395:     my $msgoptions='';
                    396:     my $discussoptions='';
                    397:     unless ($ENV{'form.replydisc'}) {
                    398: 	if (($feedurl=~/^\/res\//) && ($feedurl!~/^\/res\/adm/)) {
                    399: 	    $msgoptions= 
                    400: 		'<p><input type="checkbox" name="author" /> '.
                    401: 		&mt('Feedback to resource author').'</p>';
                    402: 	}
                    403: 	if (&feedback_available(1)) {
                    404: 	    $msgoptions.=
                    405: 		'<br /><input type="checkbox" name="question" /> '.
                    406: 		&mt('Question about resource content');
                    407: 	}
                    408: 	if (&feedback_available(0,1)) {
                    409: 	    $msgoptions.=
                    410: 		'<br /><input type="checkbox" name="course" /> '.
                    411: 		&mt('Question/Comment/Feedback about course content');
                    412: 	}
                    413: 	if (&feedback_available(0,0,1)) {
                    414: 	    $msgoptions.=
                    415: 		'<br /><input type="checkbox" name="policy" /> '.
                    416: 		&mt('Question/Comment/Feedback about course policy');
                    417: 	}
                    418:     }
                    419:     if ($ENV{'request.course.id'}) {
                    420: 	if (&Apache::lonnet::allowed('pch',
                    421: 				     $ENV{'request.course.id'}.
                    422: 				     ($ENV{'request.course.sec'}?'/'.$ENV{'request.course.sec'}:''))) {
1.74      www       423: 	    $discussoptions='<input type="checkbox" name="discuss" onClick="this.form.anondiscuss.checked=false;" '.
                    424: 		($ENV{'form.replydisc'}?' checked="1"':'').' /> '.
1.65      www       425: 		&mt('Contribution to course discussion of resource');
                    426: 	    $discussoptions.='<br /><input type="checkbox" name="anondiscuss" onClick="this.form.discuss.checked=false;" /> '.
                    427: 		&mt('Anonymous contribution to course discussion of resource').
                    428: 		' <i>('.&mt('name only visible to course faculty').')</i>';
1.20      www       429:       }
1.65      www       430:     }
1.74      www       431:     if ($msgoptions) { $msgoptions='<h2><img src="/adm/lonMisc/feedback.gif" />'.&mt('Sending Messages').'</h2>'.$msgoptions; }
1.65      www       432:     if ($discussoptions) { 
1.74      www       433: 	$discussoptions='<h2><img src="/adm/lonMisc/chat.gif" />'.&mt('Discussion Contributions').'</h2>'.$discussoptions; }
1.65      www       434:     return $msgoptions.$discussoptions;
1.6       albertel  435: }
                    436: 
                    437: sub resource_output {
                    438:   my ($feedurl) = @_;
1.46      albertel  439:   my $usersaw=&Apache::lonnet::ssi_body($feedurl);
1.6       albertel  440:   $usersaw=~s/\<body[^\>]*\>//gi;
                    441:   $usersaw=~s/\<\/body\>//gi;
                    442:   $usersaw=~s/\<html\>//gi;
                    443:   $usersaw=~s/\<\/html\>//gi;
                    444:   $usersaw=~s/\<head\>//gi;
                    445:   $usersaw=~s/\<\/head\>//gi;
                    446:   $usersaw=~s/action\s*\=/would_be_action\=/gi;
                    447:   return $usersaw;
                    448: }
                    449: 
                    450: sub clear_out_html {
1.39      www       451:   my ($message,$override)=@_;
1.37      albertel  452:   my $cid=$ENV{'request.course.id'};
1.39      www       453:   if (($ENV{"course.$cid.allow_limited_html_in_feedback"} =~ m/yes/i) ||
                    454:       ($override)) {
1.37      albertel  455:       # allows <B> <I> <P> <A> <LI> <OL> <UL> <EM> <BR> <TT> <STRONG> 
                    456:       # <BLOCKQUOTE> <DIV .*> <DIV> <IMG>
                    457:       my %html=(B=>1, I=>1, P=>1, A=>1, LI=>1, OL=>1, UL=>1, EM=>1,
1.61      www       458: 		BR=>1, TT=>1, STRONG=>1, BLOCKQUOTE=>1, DIV=>1, IMG=>1,
                    459:                 M=>1);
1.37      albertel  460: 
                    461:       $message =~ s/\<(\/?\s*(\w+)[^\>\<]*)/
1.48      albertel  462: 	  {($html{uc($2)}&&(length($1)<1000))?"\<$1":"\&lt;$1"}/ge;
1.37      albertel  463:       $message =~ s/(\<?\s*(\w+)[^\<\>]*)\>/
1.48      albertel  464: 	  {($html{uc($2)}&&(length($1)<1000))?"$1\>":"$1\&gt;"}/ge;
1.37      albertel  465:   } else {
                    466:       $message=~s/\</\&lt\;/g;
                    467:       $message=~s/\>/\&gt\;/g;
                    468:   }
1.6       albertel  469:   return $message;
                    470: }
                    471: 
                    472: sub assemble_email {
1.40      albertel  473:   my ($feedurl,$message,$prevattempts,$usersaw,$useranswer)=@_;
1.6       albertel  474:   my $email=<<"ENDEMAIL";
                    475: Refers to <a href="$feedurl">$feedurl</a>
                    476: 
                    477: $message
                    478: ENDEMAIL
                    479:     my $citations=<<"ENDCITE";
                    480: <h2>Previous attempts of student (if applicable)</h2>
                    481: $prevattempts
1.63      albertel  482: <br /><hr />
1.6       albertel  483: <h2>Original screen output (if applicable)</h2>
                    484: $usersaw
1.40      albertel  485: <h2>Correct Answer(s) (if applicable)</h2>
                    486: $useranswer
1.6       albertel  487: ENDCITE
                    488:   return ($email,$citations);
                    489: }
                    490: 
1.35      www       491: sub secapply {
                    492:     my $rec=shift;
1.36      www       493:     my $defaultflag=shift;
                    494:     $rec=~s/\s+//g;
                    495:     $rec=~s/\@/\:/g;
                    496:     my ($adr,$sections)=($rec=~/^([^\(]+)\(([^\)]+)\)/);
                    497:     if ($sections) {
                    498: 	foreach (split(/\;/,$sections)) {
                    499:             if (($_ eq $ENV{'request.course.sec'}) ||
                    500:                 ($defaultflag && ($_ eq '*'))) {
                    501:                 return $adr; 
                    502:             }
                    503:         }
                    504:     } else {
                    505:        return $rec;
                    506:     }
                    507:     return '';
1.35      www       508: }
                    509: 
1.6       albertel  510: sub decide_receiver {
1.36      www       511:   my ($feedurl,$author,$question,$course,$policy,$defaultflag) = @_;
1.6       albertel  512:   my $typestyle='';
                    513:   my %to=();
1.36      www       514:   if ($ENV{'form.author'}||$author) {
1.8       www       515:     $typestyle.='Submitting as Author Feedback<br>';
1.6       albertel  516:     $feedurl=~/^\/res\/(\w+)\/(\w+)\//;
                    517:     $to{$2.':'.$1}=1;
                    518:   }
1.36      www       519:   if ($ENV{'form.question'}||$question) {
1.8       www       520:     $typestyle.='Submitting as Question<br>';
1.24      harris41  521:     foreach (split(/\,/,
                    522: 		   $ENV{'course.'.$ENV{'request.course.id'}.'.question.email'})
                    523: 	     ) {
1.36      www       524: 	my $rec=&secapply($_,$defaultflag);
                    525:         if ($rec) { $to{$rec}=1; }
1.24      harris41  526:     } 
1.6       albertel  527:   }
1.36      www       528:   if ($ENV{'form.course'}||$course) {
1.63      albertel  529:     $typestyle.='Submitting as Comment<br />';
1.24      harris41  530:     foreach (split(/\,/,
                    531: 		   $ENV{'course.'.$ENV{'request.course.id'}.'.comment.email'})
                    532: 	     ) {
1.36      www       533: 	my $rec=&secapply($_,$defaultflag);
                    534:         if ($rec) { $to{$rec}=1; }
1.24      harris41  535:     } 
1.6       albertel  536:   }
1.36      www       537:   if ($ENV{'form.policy'}||$policy) {
1.63      albertel  538:     $typestyle.='Submitting as Policy Feedback<br />';
1.24      harris41  539:     foreach (split(/\,/,
                    540: 		   $ENV{'course.'.$ENV{'request.course.id'}.'.policy.email'})
                    541: 	     ) {
1.36      www       542: 	my $rec=&secapply($_,$defaultflag);
                    543:         if ($rec) { $to{$rec}=1; }
1.24      harris41  544:     } 
1.6       albertel  545:   }
1.36      www       546:   if ((scalar(%to) eq '0') && (!$defaultflag)) {
                    547:      ($typestyle,%to)=
                    548: 	 &decide_receiver($feedurl,$author,$question,$course,$policy,1);
                    549:   }
1.6       albertel  550:   return ($typestyle,%to);
1.36      www       551: }
                    552: 
                    553: sub feedback_available {
                    554:     my ($question,$course,$policy)=@_;
                    555:     my ($typestyle,%to)=&decide_receiver('',0,$question,$course,$policy);
                    556:     return scalar(%to);
1.6       albertel  557: }
                    558: 
                    559: sub send_msg {
1.43      www       560:   my ($feedurl,$email,$citations,$attachmenturl,%to)=@_;
1.6       albertel  561:   my $status='';
                    562:   my $sendsomething=0;
1.24      harris41  563:   foreach (keys %to) {
1.6       albertel  564:     if ($_) {
1.22      www       565:       my $declutter=&Apache::lonnet::declutter($feedurl);
1.8       www       566:       unless (&Apache::lonmsg::user_normal_msg(split(/\:/,$_),
1.43      www       567:                'Feedback ['.$declutter.']',$email,$citations,$feedurl,
                    568:                 $attachmenturl)=~/ok/) {
1.63      albertel  569: 	$status.='<br />'.&mt('Error sending message to').' '.$_.'<br />';
1.6       albertel  570:       } else {
                    571: 	$sendsomething++;
                    572:       }
                    573:     }
1.24      harris41  574:   }
1.18      www       575: 
                    576:     my %record=&Apache::lonnet::restore('_feedback');
                    577:     my ($temp)=keys %record;
                    578:     unless ($temp=~/^error\:/) {
                    579:        my %newrecord=();
                    580:        $newrecord{'resource'}=$feedurl;
                    581:        $newrecord{'subnumber'}=$record{'subnumber'}+1;
                    582:        unless (&Apache::lonnet::cstore(\%newrecord,'_feedback') eq 'ok') {
1.63      albertel  583: 	   $status.='<br />'.&mt('Not registered').'<br />';
1.18      www       584:        }
                    585:     }
                    586:        
1.6       albertel  587:   return ($status,$sendsomething);
                    588: }
                    589: 
1.13      www       590: sub adddiscuss {
1.42      www       591:     my ($symb,$email,$anon,$attachmenturl)=@_;
1.13      www       592:     my $status='';
1.23      www       593:     if (&Apache::lonnet::allowed('pch',$ENV{'request.course.id'}.
                    594:         ($ENV{'request.course.sec'}?'/'.$ENV{'request.course.sec'}:''))) {
1.20      www       595: 
1.13      www       596:     my %contrib=('message'      => $email,
                    597:                  'sendername'   => $ENV{'user.name'},
1.26      www       598:                  'senderdomain' => $ENV{'user.domain'},
                    599:                  'screenname'   => $ENV{'environment.screenname'},
                    600:                  'plainname'    => $ENV{'environment.firstname'}.' '.
                    601: 		                   $ENV{'environment.middlename'}.' '.
                    602:                                    $ENV{'environment.lastname'}.' '.
1.42      www       603:                                    $ENV{'enrironment.generation'},
1.43      www       604:                  'attachmenturl'=> $attachmenturl);
1.65      www       605:     if ($ENV{'form.replydisc'}) {
1.66      www       606: 	$contrib{'replyto'}=(split(/\:\:\:/,$ENV{'form.replydisc'}))[1];
1.65      www       607:     }
1.14      www       608:     if ($anon) {
                    609: 	$contrib{'anonymous'}='true';
                    610:     }
1.13      www       611:     if (($symb) && ($email)) {
1.14      www       612:        $status='Adding to class discussion'.($anon?' (anonymous)':'').': '.
1.13      www       613:         &Apache::lonnet::store(\%contrib,$symb,$ENV{'request.course.id'},
                    614:                      $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
1.17      www       615: 		     $ENV{'course.'.$ENV{'request.course.id'}.'.num'});
1.21      www       616:         my %storenewentry=($symb => time);
1.63      albertel  617:         $status.='<br />'.&mt('Updating discussion time').': '.
1.21      www       618:         &Apache::lonnet::put('discussiontimes',\%storenewentry,
                    619:                      $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
                    620: 		     $ENV{'course.'.$ENV{'request.course.id'}.'.num'});
1.13      www       621:     }
1.17      www       622:     my %record=&Apache::lonnet::restore('_discussion');
                    623:     my ($temp)=keys %record;
                    624:     unless ($temp=~/^error\:/) {
                    625:        my %newrecord=();
                    626:        $newrecord{'resource'}=$symb;
                    627:        $newrecord{'subnumber'}=$record{'subnumber'}+1;
1.63      albertel  628:        $status.='<br />'.&mt('Registering').': '.
1.21      www       629:                &Apache::lonnet::cstore(\%newrecord,'_discussion');
1.20      www       630:     }
                    631:     } else {
                    632: 	$status.='Failed.';
1.17      www       633:     }
1.63      albertel  634:     return $status.'<br />';   
1.13      www       635: }
                    636: 
1.33      www       637: # ----------------------------------------------------------- Preview function
                    638: 
                    639: sub show_preview {
                    640:     my $r=shift;
                    641:     my $message=&clear_out_html($ENV{'form.comment'});
                    642:     $message=~s/\n/\<br \/\>/g;
                    643:     $message=&Apache::lontexconvert::msgtexconverted($message);
                    644:     $r->print('<table border="2"><tr><td>'.
                    645:        $message.'</td></tr></table>');
                    646: }
                    647: 
                    648: sub generate_preview_button {
1.65      www       649:     my $pre=&mt("Show Preview");
1.33      www       650:     return(<<ENDPREVIEW);
                    651: <form name="preview" action="/adm/feedback?preview=1" method="post" target="preview">
                    652: <input type="hidden" name="comment" />
1.65      www       653: <input type="button" value="$pre"
1.33      www       654: onClick="this.form.comment.value=document.mailform.comment.value;this.form.submit();" />
                    655: </form>
                    656: ENDPREVIEW
                    657: }
1.71      www       658: 
1.6       albertel  659: sub handler {
                    660:   my $r = shift;
1.8       www       661:   if ($r->header_only) {
1.71      www       662:      &Apache::loncommon::content_type($r,'text/html');
1.8       www       663:      $r->send_http_header;
                    664:      return OK;
                    665:   }
1.15      www       666: 
                    667: # --------------------------- Get query string for limited number of parameters
                    668: 
1.27      stredwic  669:    &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
1.69      www       670:          ['hide','unhide','deldisc','postdata','preview','replydisc','threadedon','threadedoff']);
1.15      www       671: 
                    672:   if (($ENV{'form.hide'}) || ($ENV{'form.unhide'})) {
                    673: # ----------------------------------------------------------------- Hide/unhide
1.71      www       674:     &Apache::loncommon::content_type($r,'text/html');
1.15      www       675:     $r->send_http_header;
                    676: 
                    677:     my $entry=$ENV{'form.hide'}?$ENV{'form.hide'}:$ENV{'form.unhide'};
                    678: 
                    679:     my ($symb,$idx)=split(/\:\:\:/,$entry);
1.52      www       680:     my ($map,$ind,$url)=&Apache::lonnet::decode_symb($symb);
1.15      www       681: 
                    682:     my %contrib=&Apache::lonnet::restore($symb,$ENV{'request.course.id'},
                    683:                      $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
                    684: 		     $ENV{'course.'.$ENV{'request.course.id'}.'.num'});
                    685: 
                    686:         
                    687:     my $currenthidden=$contrib{'hidden'};
                    688:     
                    689:     if ($ENV{'form.hide'}) {
                    690: 	$currenthidden.='.'.$idx.'.';
                    691:     } else {
                    692:         $currenthidden=~s/\.$idx\.//g;
                    693:     }
                    694:     my %newhash=('hidden' => $currenthidden);
1.38      www       695: 
                    696:     &Apache::lonnet::store(\%newhash,$symb,$ENV{'request.course.id'},
                    697:                      $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
                    698: 		     $ENV{'course.'.$ENV{'request.course.id'}.'.num'});
                    699: 
                    700:     &redirect_back($r,&Apache::lonnet::clutter($url),
1.63      albertel  701:        &mt('Changed discussion status').'<br />','0','0');
1.69      www       702:   } elsif (($ENV{'form.threadedon'}) || ($ENV{'form.threadedoff'})) {
1.72      albertel  703:       &Apache::loncommon::content_type($r,'text/html');
                    704:       $r->send_http_header;
1.69      www       705:       if ($ENV{'form.threadedon'}) {
                    706: 	  &Apache::lonnet::put('environment',{'threadeddiscussion' => 'on'});
                    707: 	  &Apache::lonnet::appenv('environment.threadeddiscussion' => 'on');
                    708:       } else {
                    709:  	  &Apache::lonnet::del('environment',['threadeddiscussion']);
                    710: 	  &Apache::lonnet::delenv('environment\.threadeddiscussion');
1.72      albertel  711:       }
1.69      www       712:       my $symb=$ENV{'form.threadedon'}?$ENV{'form.threadedon'}:$ENV{'form.threadedoff'};
                    713:       my ($map,$ind,$url)=&Apache::lonnet::decode_symb($symb);
                    714:       &redirect_back($r,&Apache::lonnet::clutter($url),
                    715: 		     &mt('Changed discussion view mode').'<br />','0','0');
1.38      www       716:   } elsif ($ENV{'form.deldisc'}) {
                    717: # --------------------------------------------------------------- Hide for good
1.71      www       718:     &Apache::loncommon::content_type($r,'text/html');
1.38      www       719:     $r->send_http_header;
                    720: 
                    721:     my $entry=$ENV{'form.deldisc'};
                    722: 
                    723:     my ($symb,$idx)=split(/\:\:\:/,$entry);
1.52      www       724:     my ($map,$ind,$url)=&Apache::lonnet::decode_symb($symb);
1.38      www       725: 
                    726:     my %contrib=&Apache::lonnet::restore($symb,$ENV{'request.course.id'},
                    727:                      $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
                    728: 		     $ENV{'course.'.$ENV{'request.course.id'}.'.num'});
                    729: 
                    730:         
                    731:     my $currentdeleted=$contrib{'deleted'};
                    732:     
                    733:     $currentdeleted.='.'.$idx.'.';
                    734: 
                    735:     my %newhash=('deleted' => $currentdeleted);
1.15      www       736: 
                    737:     &Apache::lonnet::store(\%newhash,$symb,$ENV{'request.course.id'},
                    738:                      $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
                    739: 		     $ENV{'course.'.$ENV{'request.course.id'}.'.num'});
                    740: 
1.30      www       741:     &redirect_back($r,&Apache::lonnet::clutter($url),
1.63      albertel  742:        &mt('Changed discussion status').'<br />','0','0');
1.33      www       743:   } elsif ($ENV{'form.preview'}) {
                    744: # -------------------------------------------------------- User wants a preview
1.76      albertel  745:       $r->content_type('text/html');
                    746:       $r->send_http_header;
1.33      www       747:       &show_preview($r);
1.15      www       748:   } else {
                    749: # ------------------------------------------------------------- Normal feedback
1.6       albertel  750:   my $feedurl=$ENV{'form.postdata'};
                    751:   $feedurl=~s/^http\:\/\///;
                    752:   $feedurl=~s/^$ENV{'SERVER_NAME'}//;
                    753:   $feedurl=~s/^$ENV{'HTTP_HOST'}//;
1.62      www       754:   $feedurl=~s/\?.+$//;
1.8       www       755: 
1.66      www       756:   my $symb;
                    757:   if ($ENV{'form.replydisc'}) {
                    758:       $symb=(split(/\:\:\:/,$ENV{'form.replydisc'}))[0];
                    759:       my ($map,$id,$url)=&Apache::lonnet::decode_symb($symb);
                    760:       $feedurl=&Apache::lonnet::clutter($url);
                    761:   } else {
                    762:       $symb=&Apache::lonnet::symbread($feedurl);
                    763:   }
1.31      www       764:   unless ($symb) {
                    765:       $symb=$ENV{'form.symb'};
                    766:       if ($symb) {
1.52      www       767: 	  my ($map,$id,$url)=&Apache::lonnet::decode_symb($symb);
1.31      www       768:           $feedurl=&Apache::lonnet::clutter($url);
                    769:       }
                    770:   }
1.8       www       771:   my $goahead=1;
                    772:   if ($feedurl=~/\.(problem|exam|quiz|assess|survey|form)$/) {
                    773:       unless ($symb) { $goahead=0; }
                    774:   }
1.74      www       775:   # backward compatibility (bulltin boards used to be 'wrapped')
1.73      albertel  776:   if ($feedurl=~m|^/adm/wrapper/adm/.*/bulletinboard$|) {
                    777:       $feedurl=~s|^/adm/wrapper||;
                    778:   }
1.8       www       779:   if ($goahead) {
                    780: # Go ahead with feedback, no ambiguous reference
1.71      www       781:     &Apache::loncommon::content_type($r,'text/html');
1.8       www       782:     $r->send_http_header;
1.6       albertel  783:   
1.8       www       784:     if (
1.7       albertel  785:       (
                    786:        ($feedurl=~m:^/res:) && ($feedurl!~m:^/res/adm:)
                    787:       ) 
                    788:       || 
                    789:       ($ENV{'request.course.id'} && ($feedurl!~m:^/adm:))
1.31      www       790:       ||
                    791:       ($ENV{'request.course.id'} && ($symb=~/^bulletin\_\_\_/))
1.7       albertel  792:      ) {
1.6       albertel  793: # --------------------------------------------------- Print login screen header
                    794:     unless ($ENV{'form.sendit'}) {
                    795:       my $options=&screen_header($feedurl);
                    796:       if ($options) {
                    797: 	&mail_screen($r,$feedurl,$options);
                    798:       } else {
                    799: 	&fail_redirect($r,$feedurl);
                    800:       }
                    801:     } else {
                    802:       
                    803: # Get previous user input
1.9       albertel  804:       my $prevattempts=&Apache::loncommon::get_previous_attempt(
1.11      albertel  805:             $symb,$ENV{'user.name'},$ENV{'user.domain'},
1.9       albertel  806:             $ENV{'request.course.id'});
1.6       albertel  807: 
                    808: # Get output from resource
                    809:       my $usersaw=&resource_output($feedurl);
                    810: 
1.50      albertel  811: # Get resource answer (need to allow student to view grades for this to work)
                    812:       &Apache::lonnet::appenv(('allowed.vgr'=>'F'));
1.40      albertel  813:       my $useranswer=&Apache::loncommon::get_student_answers(
                    814:                        $symb,$ENV{'user.name'},$ENV{'user.domain'},
                    815: 		       $ENV{'request.course.id'});
1.50      albertel  816:       &Apache::lonnet::delenv('allowed.vgr');
1.42      www       817: # Get attachments, if any, and not too large
                    818:       my $attachmenturl='';
                    819:       if ($ENV{'form.attachment.filename'}) {
                    820: 	  unless (length($ENV{'form.attachment'})>131072) {
1.43      www       821: 	      $attachmenturl=&Apache::lonnet::userfileupload('attachment');
1.42      www       822: 	  }
                    823:       }
1.6       albertel  824: # Filter HTML out of message (could be nasty)
1.39      www       825:       my $message=&clear_out_html($ENV{'form.comment'});
1.6       albertel  826: 
                    827: # Assemble email
1.8       www       828:       my ($email,$citations)=&assemble_email($feedurl,$message,$prevattempts,
1.40      albertel  829:           $usersaw,$useranswer);
                    830:  
1.6       albertel  831: # Who gets this?
                    832:       my ($typestyle,%to) = &decide_receiver($feedurl);
                    833: 
                    834: # Actually send mail
1.43      www       835:       my ($status,$numsent)=&send_msg($feedurl,$email,$citations,
                    836:           $attachmenturl,%to);
1.13      www       837: 
                    838: # Discussion? Store that.
                    839: 
1.32      albertel  840:       my $numpost=0;
1.13      www       841:       if ($ENV{'form.discuss'}) {
1.42      www       842: 	  $typestyle.=&adddiscuss($symb,$message,0,$attachmenturl);
1.32      albertel  843: 	  $numpost++;
1.13      www       844:       }
1.6       albertel  845: 
1.14      www       846:       if ($ENV{'form.anondiscuss'}) {
1.42      www       847: 	  $typestyle.=&adddiscuss($symb,$message,1,$attachmenturl);
1.32      albertel  848: 	  $numpost++;
1.14      www       849:       }
                    850: 
                    851: 
1.6       albertel  852: # Receipt screen and redirect back to where came from
1.32      albertel  853:       &redirect_back($r,$feedurl,$typestyle,$numsent,$numpost,$status);
1.6       albertel  854: 
                    855:     }
1.8       www       856:    } else {
1.7       albertel  857: # Unable to give feedback
1.6       albertel  858:     &no_redirect_back($r,$feedurl);
1.8       www       859:    }
                    860:   } else {
                    861: # Ambiguous Problem Resource
1.60      albertel  862:       if ( &Apache::lonnet::mod_perl_version() == 2 ) {
1.53      albertel  863: 	  &Apache::lonnet::cleanenv();
1.58      albertel  864:       }
1.53      albertel  865:       $r->internal_redirect('/adm/ambiguous');
1.6       albertel  866:   }
1.15      www       867: }
1.6       albertel  868:   return OK;
1.1       www       869: } 
                    870: 
                    871: 1;
                    872: __END__

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