File:  [LON-CAPA] / loncom / interface / lonfeedback.pm
Revision 1.73: download - view: text, annotated - select for diffs
Thu Nov 27 09:43:42 2003 UTC (20 years, 6 months ago) by albertel
Branches: MAIN
CVS tags: version_1_1_X, version_1_1_1, version_1_1_0, version_1_0_99_3, version_1_0_99_2, version_1_0_99_1, HEAD
- Part of BUG#2416, make the new bulletin boards backward compatabile with the old ones

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

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