File:  [LON-CAPA] / loncom / interface / lonfeedback.pm
Revision 1.64: download - view: text, annotated - select for diffs
Tue Oct 21 00:22:23 2003 UTC (20 years, 8 months ago) by www
Branches: MAIN
CVS tags: HEAD
Tiny steps toward threaded discussion. The "Reply" link does nothing at all
yet.

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

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