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

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

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