File:  [LON-CAPA] / loncom / interface / lonfeedback.pm
Revision 1.68: download - view: text, annotated - select for diffs
Fri Nov 7 16:37:23 2003 UTC (20 years, 6 months ago) by www
Branches: MAIN
CVS tags: HEAD
Saving my work on threaded discussions.

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

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