File:  [LON-CAPA] / loncom / interface / lonfeedback.pm
Revision 1.74: download - view: text, annotated - select for diffs
Tue Jan 13 02:31:09 2004 UTC (20 years, 4 months ago) by www
Branches: MAIN
CVS tags: HEAD
Working through some of the FDBK/COM comments, bug #2187

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

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