File:  [LON-CAPA] / loncom / interface / lonfeedback.pm
Revision 1.70: download - view: text, annotated - select for diffs
Mon Nov 24 16:00:45 2003 UTC (20 years, 6 months ago) by www
Branches: MAIN
CVS tags: HEAD
Bug #2321: Bulletin Board was framed, conflict in non-remote mode.

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

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