File:  [LON-CAPA] / loncom / interface / lonfeedback.pm
Revision 1.15: download - view: text, annotated - select for diffs
Fri Aug 10 18:39:58 2001 UTC (22 years, 9 months ago) by www
Branches: MAIN
CVS tags: HEAD
Hide/unhide

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

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