File:  [LON-CAPA] / loncom / interface / lonfeedback.pm
Revision 1.63: download - view: text, annotated - select for diffs
Wed Oct 15 20:13:31 2003 UTC (20 years, 8 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- xhtmlize

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

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