File:  [LON-CAPA] / loncom / interface / lonfeedback.pm
Revision 1.21: download - view: text, annotated - select for diffs
Tue Jan 1 15:02:31 2002 UTC (22 years, 5 months ago) by www
Branches: MAIN
CVS tags: HEAD
Stores discussion time

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

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