File:  [LON-CAPA] / loncom / interface / lonfeedback.pm
Revision 1.54: download - view: text, annotated - select for diffs
Sun Sep 14 23:57:18 2003 UTC (20 years, 9 months ago) by www
Branches: MAIN
CVS tags: HEAD
In preparation for implementing threaded discussion and disentangling COM
from FDBK, disentagle the subroutines - there is really no reason for the
discussion display to be embedded into lonxml.

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

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