File:  [LON-CAPA] / loncom / interface / lonfeedback.pm
Revision 1.76: download - view: text, annotated - select for diffs
Wed Feb 11 22:14:10 2004 UTC (20 years, 4 months ago) by albertel
Branches: MAIN
CVS tags: HEAD

- sets the http header  BUG#2723

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

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