File:  [LON-CAPA] / loncom / interface / lonfeedback.pm
Revision 1.22: download - view: text, annotated - select for diffs
Tue Jan 1 16:39:32 2002 UTC (22 years, 5 months ago) by www
Branches: MAIN
CVS tags: HEAD
Standardize subjects

    1: # The LearningOnline Network
    2: # Feedback
    3: #
    4: # $Id: lonfeedback.pm,v 1.22 2002/01/01 16:39:32 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 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: 
   54: sub mail_screen {
   55:   my ($r,$feedurl,$options) = @_;
   56:   $r->print(<<ENDDOCUMENT);
   57: <html>
   58: <head>
   59: <title>The LearningOnline Network with CAPA</title>
   60: <meta http-equiv="pragma" content="no-cache"></meta>
   61: <script>
   62:     function gosubmit() {
   63:         var rec=0;
   64:         if (typeof(document.mailform.elements.author)!="undefined") {
   65:           if (document.mailform.elements.author.checked) {
   66:              rec=1;
   67:           } 
   68:         }
   69:         if (typeof(document.mailform.elements.question)!="undefined") {
   70:           if (document.mailform.elements.question.checked) {
   71:              rec=1;
   72:           } 
   73:         }
   74:         if (typeof(document.mailform.elements.course)!="undefined") {
   75:           if (document.mailform.elements.course.checked) {
   76:              rec=1;
   77:           } 
   78:         }
   79:         if (typeof(document.mailform.elements.policy)!="undefined") {
   80:           if (document.mailform.elements.policy.checked) {
   81:              rec=1;
   82:           } 
   83:         }
   84:         if (typeof(document.mailform.elements.discuss)!="undefined") {
   85:           if (document.mailform.elements.discuss.checked) {
   86:              rec=1;
   87:           } 
   88:         }
   89:         if (typeof(document.mailform.elements.anondiscuss)!="undefined") {
   90:           if (document.mailform.elements.anondiscuss.checked) {
   91:              rec=1;
   92:           } 
   93:         }
   94: 
   95:         if (rec) {
   96: 	    document.mailform.submit();
   97:         } else {
   98:             alert('Please check a feedback type.');
   99: 	}
  100:     }
  101: </script>
  102: </head>
  103: <body bgcolor="#FFFFFF" onLoad="window.focus();">
  104: <img align=right src=/adm/lonIcons/lonlogos.gif>
  105: <h1>Feedback</h1>
  106: <h2><tt>$feedurl</tt></h2>
  107: <form action="/adm/feedback" method=post name=mailform>
  108: <input type=hidden name=postdata value="$feedurl">
  109: Please check at least one of the following feedback types:
  110: $options<hr>
  111: My question/comment/feedback:<p>
  112: <textarea name=comment cols=60 rows=10 wrap=hard>
  113: </textarea><p>
  114: <input type=hidden name=sendit value=1>
  115: <input type=button value="Send Feedback" onClick='gosubmit();'></input>
  116: </form>
  117: </body>
  118: </html>
  119: ENDDOCUMENT
  120: }
  121: 
  122: sub fail_redirect {
  123:   my ($r,$feedurl) = @_;
  124:   $r->print (<<ENDFAILREDIR);
  125: <head><title>Feedback not sent</title>
  126: <meta http-equiv="pragma" content="no-cache"></meta>
  127: <meta HTTP-EQUIV="Refresh" CONTENT="2; url=$feedurl">
  128: </head>
  129: <html>
  130: <body bgcolor="#FFFFFF">
  131: <img align=right src=/adm/lonIcons/lonlogos.gif>
  132: <b>Sorry, no recipients  ...</b>
  133: </body>
  134: </html>
  135: ENDFAILREDIR
  136: }
  137: 
  138: sub redirect_back {
  139:   my ($r,$feedurl,$typestyle,$sendsomething,$status) = @_;
  140:   $r->print (<<ENDREDIR);
  141: <head>
  142: <title>Feedback sent</title>
  143: <meta http-equiv="pragma" content="no-cache"></meta>
  144: <meta HTTP-EQUIV="Refresh" CONTENT="2; url=$feedurl">
  145: </head>
  146: <html>
  147: <body bgcolor="#FFFFFF">
  148: <img align=right src=/adm/lonIcons/lonlogos.gif>
  149: $typestyle
  150: <b>Sent $sendsomething message(s).</b>
  151: <font color=red>$status</font>
  152: </body>
  153: </html>
  154: ENDREDIR
  155: }
  156: 
  157: sub no_redirect_back {
  158:   my ($r,$feedurl) = @_;
  159:   $r->print (<<ENDNOREDIR);
  160: <head><title>Feedback not sent</title>
  161: <meta http-equiv="pragma" content="no-cache"></meta>
  162: ENDNOREDIR
  163: 
  164:   if ($feedurl!~/^\/adm\/feedback/) { 
  165:     $r->print('<meta HTTP-EQUIV="Refresh" CONTENT="2; url='.$feedurl.'">');
  166:   }
  167:   
  168:   $r->print (<<ENDNOREDIRTWO);
  169: </head>
  170: <html>
  171: <body bgcolor="#FFFFFF">
  172: <img align=right src=/adm/lonIcons/lonlogos.gif>
  173: <b>Sorry, no feedback possible on this resource  ...</b>
  174: </body>
  175: </html>
  176: ENDNOREDIRTWO
  177: }
  178: 
  179: sub screen_header {
  180:   my ($feedurl) = @_;
  181:   my $options='';
  182:   if (($feedurl=~/^\/res/) && ($feedurl!~/^\/res\/adm/)) {
  183:     $options= 
  184:       '<p><input type=checkbox name=author> Feedback to resource author';
  185:   }
  186:   if ($ENV{'course.'.$ENV{'request.course.id'}.'.question.email'}) {
  187:     $options.=
  188:     '<br><input type=checkbox name=question> Question about resource content';
  189:   }
  190:   if ($ENV{'course.'.$ENV{'request.course.id'}.'.comment.email'}) {
  191:     $options.=
  192:       '<br><input type=checkbox name=course> '.
  193: 	'Question/Comment/Feedback about course content';
  194:   }
  195:   if ($ENV{'course.'.$ENV{'request.course.id'}.'.policy.email'}) {
  196:     $options.=
  197:       '<br><input type=checkbox name=policy> '.
  198: 	'Question/Comment/Feedback about course policy';
  199:   }
  200: 
  201:   if ($ENV{'request.course.id'}) {
  202:       if (&Apache::lonnet::allowed('pch',$ENV{'request.course.id'})) {
  203:     $options.='<br><input type=checkbox name=discuss> '.
  204: 	'<b>Contribution to course discussion of resource</b>';
  205:     $options.='<br><input type=checkbox name=anondiscuss> '.
  206: 	'<b>Anonymous contribution to course discussion of resource</b>'.
  207:         ' (name only visible to course faculty)';
  208:       }
  209:   }
  210:   return $options;
  211: }
  212: 
  213: sub resource_output {
  214:   my ($feedurl) = @_;
  215:   my $usersaw=&Apache::lonnet::ssi($feedurl);
  216:   $usersaw=~s/\<body[^\>]*\>//gi;
  217:   $usersaw=~s/\<\/body\>//gi;
  218:   $usersaw=~s/\<html\>//gi;
  219:   $usersaw=~s/\<\/html\>//gi;
  220:   $usersaw=~s/\<head\>//gi;
  221:   $usersaw=~s/\<\/head\>//gi;
  222:   $usersaw=~s/action\s*\=/would_be_action\=/gi;
  223:   return $usersaw;
  224: }
  225: 
  226: sub clear_out_html {
  227:   my $message=$ENV{'form.comment'};
  228:   $message=~s/\</\&lt\;/g;
  229:   $message=~s/\>/\&gt\;/g;
  230:   return $message;
  231: }
  232: 
  233: sub assemble_email {
  234:   my ($feedurl,$message,$prevattempts,$usersaw)=@_;
  235:   my $email=<<"ENDEMAIL";
  236: Refers to <a href="$feedurl">$feedurl</a>
  237: 
  238: $message
  239: ENDEMAIL
  240:     my $citations=<<"ENDCITE";
  241: <h2>Previous attempts of student (if applicable)</h2>
  242: $prevattempts
  243: <p><hr>
  244: <h2>Original screen output (if applicable)</h2>
  245: $usersaw
  246: ENDCITE
  247:   return ($email,$citations);
  248: }
  249: 
  250: sub decide_receiver {
  251:   my ($feedurl) = @_;
  252:   my $typestyle='';
  253:   my %to=();
  254:   if ($ENV{'form.author'}) {
  255:     $typestyle.='Submitting as Author Feedback<br>';
  256:     $feedurl=~/^\/res\/(\w+)\/(\w+)\//;
  257:     $to{$2.':'.$1}=1;
  258:   }
  259:   if ($ENV{'form.question'}) {
  260:     $typestyle.='Submitting as Question<br>';
  261:     map {
  262:       $to{$_}=1;
  263:     } split(/\,/,
  264: 	    $ENV{'course.'.$ENV{'request.course.id'}.'.question.email'});
  265:   }
  266:   if ($ENV{'form.course'}) {
  267:     $typestyle.='Submitting as Comment<br>';
  268:     map {
  269:       $to{$_}=1;
  270:     } split(/\,/,
  271: 	    $ENV{'course.'.$ENV{'request.course.id'}.'.comment.email'});
  272:   }
  273:   if ($ENV{'form.policy'}) {
  274:     $typestyle.='Submitting as Policy Feedback<br>';
  275:     map {
  276:       $to{$_}=1;
  277:     } split(/\,/,
  278: 	    $ENV{'course.'.$ENV{'request.course.id'}.'.policy.email'});
  279:   }
  280:   return ($typestyle,%to);
  281: }
  282: 
  283: sub send_msg {
  284:   my ($feedurl,$email,$citations,%to)=@_;
  285:   my $status='';
  286:   my $sendsomething=0;
  287:   map {
  288:     if ($_) {
  289:       my $declutter=&Apache::lonnet::declutter($feedurl);
  290:       unless (&Apache::lonmsg::user_normal_msg(split(/\:/,$_),
  291:                'Feedback ['.$declutter.']',$email,$citations) eq 'ok') {
  292: 	$status.='<br>Error sending message to '.$_.'<br>';
  293:       } else {
  294: 	$sendsomething++;
  295:       }
  296:     }
  297:   } keys %to;
  298: 
  299:     my %record=&Apache::lonnet::restore('_feedback');
  300:     my ($temp)=keys %record;
  301:     unless ($temp=~/^error\:/) {
  302:        my %newrecord=();
  303:        $newrecord{'resource'}=$feedurl;
  304:        $newrecord{'subnumber'}=$record{'subnumber'}+1;
  305:        unless (&Apache::lonnet::cstore(\%newrecord,'_feedback') eq 'ok') {
  306: 	   $status.='<br>Not registered<br>';
  307:        }
  308:     }
  309:        
  310:   return ($status,$sendsomething);
  311: }
  312: 
  313: sub adddiscuss {
  314:     my ($symb,$email,$anon)=@_;
  315:     my $status='';
  316:     if (&Apache::lonnet::allowed('pch',$ENV{'request.course.id'})) {
  317: 
  318:     my %contrib=('message'      => $email,
  319:                  'sendername'   => $ENV{'user.name'},
  320:                  'senderdomain' => $ENV{'user.domain'});
  321:     if ($anon) {
  322: 	$contrib{'anonymous'}='true';
  323:     }
  324:     if (($symb) && ($email)) {
  325:        $status='Adding to class discussion'.($anon?' (anonymous)':'').': '.
  326:         &Apache::lonnet::store(\%contrib,$symb,$ENV{'request.course.id'},
  327:                      $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
  328: 		     $ENV{'course.'.$ENV{'request.course.id'}.'.num'});
  329:         my %storenewentry=($symb => time);
  330:         $status.='<br>Updating discussion time: '.
  331:         &Apache::lonnet::put('discussiontimes',\%storenewentry,
  332:                      $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
  333: 		     $ENV{'course.'.$ENV{'request.course.id'}.'.num'});
  334:     }
  335:     my %record=&Apache::lonnet::restore('_discussion');
  336:     my ($temp)=keys %record;
  337:     unless ($temp=~/^error\:/) {
  338:        my %newrecord=();
  339:        $newrecord{'resource'}=$symb;
  340:        $newrecord{'subnumber'}=$record{'subnumber'}+1;
  341:        $status.='<br>Registering: '.
  342:                &Apache::lonnet::cstore(\%newrecord,'_discussion');
  343:     }
  344:     } else {
  345: 	$status.='Failed.';
  346:     }
  347:     return $status.'<br>';   
  348: }
  349: 
  350: sub handler {
  351:   my $r = shift;
  352:   if ($r->header_only) {
  353:      $r->content_type('text/html');
  354:      $r->send_http_header;
  355:      return OK;
  356:   }
  357: 
  358: # --------------------------- Get query string for limited number of parameters
  359: 
  360:     map {
  361:        my ($name, $value) = split(/=/,$_);
  362:        $value =~ tr/+/ /;
  363:        $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
  364:        if (($name eq 'hide') || ($name eq 'unhide')) {
  365:            unless ($ENV{'form.'.$name}) {
  366:               $ENV{'form.'.$name}=$value;
  367: 	   }
  368:        }
  369:     } (split(/&/,$ENV{'QUERY_STRING'}));
  370: 
  371:   if (($ENV{'form.hide'}) || ($ENV{'form.unhide'})) {
  372: # ----------------------------------------------------------------- Hide/unhide
  373:     $r->content_type('text/html');
  374:     $r->send_http_header;
  375: 
  376:     my $entry=$ENV{'form.hide'}?$ENV{'form.hide'}:$ENV{'form.unhide'};
  377: 
  378:     my ($symb,$idx)=split(/\:\:\:/,$entry);
  379:     my ($map,$ind,$url)=split(/\_\_\_/,$symb);
  380: 
  381:     my %contrib=&Apache::lonnet::restore($symb,$ENV{'request.course.id'},
  382:                      $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
  383: 		     $ENV{'course.'.$ENV{'request.course.id'}.'.num'});
  384: 
  385:         
  386:     my $currenthidden=$contrib{'hidden'};
  387:     
  388:     if ($ENV{'form.hide'}) {
  389: 	$currenthidden.='.'.$idx.'.';
  390:     } else {
  391:         $currenthidden=~s/\.$idx\.//g;
  392:     }
  393:     my %newhash=('hidden' => $currenthidden);
  394: 
  395:     &Apache::lonnet::store(\%newhash,$symb,$ENV{'request.course.id'},
  396:                      $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
  397: 		     $ENV{'course.'.$ENV{'request.course.id'}.'.num'});
  398: 
  399:     &redirect_back($r,'/res/'.$url,'Changed discussion status<p>','0');
  400: 
  401:       
  402:   } else {
  403: # ------------------------------------------------------------- Normal feedback
  404:   my $feedurl=$ENV{'form.postdata'};
  405:   $feedurl=~s/^http\:\/\///;
  406:   $feedurl=~s/^$ENV{'SERVER_NAME'}//;
  407:   $feedurl=~s/^$ENV{'HTTP_HOST'}//;
  408: 
  409:   my $symb=&Apache::lonnet::symbread($feedurl);
  410:   my $goahead=1;
  411:   if ($feedurl=~/\.(problem|exam|quiz|assess|survey|form)$/) {
  412:       unless ($symb) { $goahead=0; }
  413:   }
  414: 
  415:   if ($goahead) {
  416: # Go ahead with feedback, no ambiguous reference
  417:     $r->content_type('text/html');
  418:     $r->send_http_header;
  419:   
  420:     if (
  421:       (
  422:        ($feedurl=~m:^/res:) && ($feedurl!~m:^/res/adm:)
  423:       ) 
  424:       || 
  425:       ($ENV{'request.course.id'} && ($feedurl!~m:^/adm:))
  426:      ) {
  427: # --------------------------------------------------- Print login screen header
  428:     unless ($ENV{'form.sendit'}) {
  429:       my $options=&screen_header($feedurl);
  430:       if ($options) {
  431: 	&mail_screen($r,$feedurl,$options);
  432:       } else {
  433: 	&fail_redirect($r,$feedurl);
  434:       }
  435:     } else {
  436:       
  437: # Get previous user input
  438:       my $prevattempts=&Apache::loncommon::get_previous_attempt(
  439:             $symb,$ENV{'user.name'},$ENV{'user.domain'},
  440:             $ENV{'request.course.id'});
  441: 
  442: # Get output from resource
  443:       my $usersaw=&resource_output($feedurl);
  444: 
  445: # Filter HTML out of message (could be nasty)
  446:       my $message=&clear_out_html;
  447: 
  448: # Assemble email
  449:       my ($email,$citations)=&assemble_email($feedurl,$message,$prevattempts,
  450:           $usersaw);
  451: 
  452: # Who gets this?
  453:       my ($typestyle,%to) = &decide_receiver($feedurl);
  454: 
  455: # Actually send mail
  456:       my ($status,$numsent)=&send_msg($feedurl,$email,$citations,%to);
  457: 
  458: # Discussion? Store that.
  459: 
  460:       if ($ENV{'form.discuss'}) {
  461: 	  $typestyle.=&adddiscuss($symb,$message);
  462:       }
  463: 
  464:       if ($ENV{'form.anondiscuss'}) {
  465: 	  $typestyle.=&adddiscuss($symb,$message,1);
  466:       }
  467: 
  468: 
  469: # Receipt screen and redirect back to where came from
  470:       &redirect_back($r,$feedurl,$typestyle,$numsent,$status);
  471: 
  472:     }
  473:    } else {
  474: # Unable to give feedback
  475:     &no_redirect_back($r,$feedurl);
  476:    }
  477:   } else {
  478: # Ambiguous Problem Resource
  479:     $r->internal_redirect('/adm/ambiguous');
  480:   }
  481: }
  482:   return OK;
  483: } 
  484: 
  485: 1;
  486: __END__
  487: 
  488: 
  489: 
  490: 

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