File:  [LON-CAPA] / loncom / interface / lonfeedback.pm
Revision 1.23: download - view: text, annotated - select for diffs
Wed Jan 16 15:38:38 2002 UTC (22 years, 9 months ago) by www
Branches: MAIN
CVS tags: stable_2002_spring, HEAD
Discussion should now also work for students in sections

    1: # The LearningOnline Network
    2: # Feedback
    3: #
    4: # $Id: lonfeedback.pm,v 1.23 2002/01/16 15:38:38 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: 
   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',
  203:         $ENV{'request.course.id'}.
  204:         ($ENV{'request.course.sec'}?'/'.$ENV{'request.course.sec'}:''))) {
  205:     $options.='<br><input type=checkbox name=discuss> '.
  206: 	'<b>Contribution to course discussion of resource</b>';
  207:     $options.='<br><input type=checkbox name=anondiscuss> '.
  208: 	'<b>Anonymous contribution to course discussion of resource</b>'.
  209:         ' (name only visible to course faculty)';
  210:       }
  211:   }
  212:   return $options;
  213: }
  214: 
  215: sub resource_output {
  216:   my ($feedurl) = @_;
  217:   my $usersaw=&Apache::lonnet::ssi($feedurl);
  218:   $usersaw=~s/\<body[^\>]*\>//gi;
  219:   $usersaw=~s/\<\/body\>//gi;
  220:   $usersaw=~s/\<html\>//gi;
  221:   $usersaw=~s/\<\/html\>//gi;
  222:   $usersaw=~s/\<head\>//gi;
  223:   $usersaw=~s/\<\/head\>//gi;
  224:   $usersaw=~s/action\s*\=/would_be_action\=/gi;
  225:   return $usersaw;
  226: }
  227: 
  228: sub clear_out_html {
  229:   my $message=$ENV{'form.comment'};
  230:   $message=~s/\</\&lt\;/g;
  231:   $message=~s/\>/\&gt\;/g;
  232:   return $message;
  233: }
  234: 
  235: sub assemble_email {
  236:   my ($feedurl,$message,$prevattempts,$usersaw)=@_;
  237:   my $email=<<"ENDEMAIL";
  238: Refers to <a href="$feedurl">$feedurl</a>
  239: 
  240: $message
  241: ENDEMAIL
  242:     my $citations=<<"ENDCITE";
  243: <h2>Previous attempts of student (if applicable)</h2>
  244: $prevattempts
  245: <p><hr>
  246: <h2>Original screen output (if applicable)</h2>
  247: $usersaw
  248: ENDCITE
  249:   return ($email,$citations);
  250: }
  251: 
  252: sub decide_receiver {
  253:   my ($feedurl) = @_;
  254:   my $typestyle='';
  255:   my %to=();
  256:   if ($ENV{'form.author'}) {
  257:     $typestyle.='Submitting as Author Feedback<br>';
  258:     $feedurl=~/^\/res\/(\w+)\/(\w+)\//;
  259:     $to{$2.':'.$1}=1;
  260:   }
  261:   if ($ENV{'form.question'}) {
  262:     $typestyle.='Submitting as Question<br>';
  263:     map {
  264:       $to{$_}=1;
  265:     } split(/\,/,
  266: 	    $ENV{'course.'.$ENV{'request.course.id'}.'.question.email'});
  267:   }
  268:   if ($ENV{'form.course'}) {
  269:     $typestyle.='Submitting as Comment<br>';
  270:     map {
  271:       $to{$_}=1;
  272:     } split(/\,/,
  273: 	    $ENV{'course.'.$ENV{'request.course.id'}.'.comment.email'});
  274:   }
  275:   if ($ENV{'form.policy'}) {
  276:     $typestyle.='Submitting as Policy Feedback<br>';
  277:     map {
  278:       $to{$_}=1;
  279:     } split(/\,/,
  280: 	    $ENV{'course.'.$ENV{'request.course.id'}.'.policy.email'});
  281:   }
  282:   return ($typestyle,%to);
  283: }
  284: 
  285: sub send_msg {
  286:   my ($feedurl,$email,$citations,%to)=@_;
  287:   my $status='';
  288:   my $sendsomething=0;
  289:   map {
  290:     if ($_) {
  291:       my $declutter=&Apache::lonnet::declutter($feedurl);
  292:       unless (&Apache::lonmsg::user_normal_msg(split(/\:/,$_),
  293:                'Feedback ['.$declutter.']',$email,$citations) eq 'ok') {
  294: 	$status.='<br>Error sending message to '.$_.'<br>';
  295:       } else {
  296: 	$sendsomething++;
  297:       }
  298:     }
  299:   } keys %to;
  300: 
  301:     my %record=&Apache::lonnet::restore('_feedback');
  302:     my ($temp)=keys %record;
  303:     unless ($temp=~/^error\:/) {
  304:        my %newrecord=();
  305:        $newrecord{'resource'}=$feedurl;
  306:        $newrecord{'subnumber'}=$record{'subnumber'}+1;
  307:        unless (&Apache::lonnet::cstore(\%newrecord,'_feedback') eq 'ok') {
  308: 	   $status.='<br>Not registered<br>';
  309:        }
  310:     }
  311:        
  312:   return ($status,$sendsomething);
  313: }
  314: 
  315: sub adddiscuss {
  316:     my ($symb,$email,$anon)=@_;
  317:     my $status='';
  318:     if (&Apache::lonnet::allowed('pch',$ENV{'request.course.id'}.
  319:         ($ENV{'request.course.sec'}?'/'.$ENV{'request.course.sec'}:''))) {
  320: 
  321:     my %contrib=('message'      => $email,
  322:                  'sendername'   => $ENV{'user.name'},
  323:                  'senderdomain' => $ENV{'user.domain'});
  324:     if ($anon) {
  325: 	$contrib{'anonymous'}='true';
  326:     }
  327:     if (($symb) && ($email)) {
  328:        $status='Adding to class discussion'.($anon?' (anonymous)':'').': '.
  329:         &Apache::lonnet::store(\%contrib,$symb,$ENV{'request.course.id'},
  330:                      $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
  331: 		     $ENV{'course.'.$ENV{'request.course.id'}.'.num'});
  332:         my %storenewentry=($symb => time);
  333:         $status.='<br>Updating discussion time: '.
  334:         &Apache::lonnet::put('discussiontimes',\%storenewentry,
  335:                      $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
  336: 		     $ENV{'course.'.$ENV{'request.course.id'}.'.num'});
  337:     }
  338:     my %record=&Apache::lonnet::restore('_discussion');
  339:     my ($temp)=keys %record;
  340:     unless ($temp=~/^error\:/) {
  341:        my %newrecord=();
  342:        $newrecord{'resource'}=$symb;
  343:        $newrecord{'subnumber'}=$record{'subnumber'}+1;
  344:        $status.='<br>Registering: '.
  345:                &Apache::lonnet::cstore(\%newrecord,'_discussion');
  346:     }
  347:     } else {
  348: 	$status.='Failed.';
  349:     }
  350:     return $status.'<br>';   
  351: }
  352: 
  353: sub handler {
  354:   my $r = shift;
  355:   if ($r->header_only) {
  356:      $r->content_type('text/html');
  357:      $r->send_http_header;
  358:      return OK;
  359:   }
  360: 
  361: # --------------------------- Get query string for limited number of parameters
  362: 
  363:     map {
  364:        my ($name, $value) = split(/=/,$_);
  365:        $value =~ tr/+/ /;
  366:        $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
  367:        if (($name eq 'hide') || ($name eq 'unhide')) {
  368:            unless ($ENV{'form.'.$name}) {
  369:               $ENV{'form.'.$name}=$value;
  370: 	   }
  371:        }
  372:     } (split(/&/,$ENV{'QUERY_STRING'}));
  373: 
  374:   if (($ENV{'form.hide'}) || ($ENV{'form.unhide'})) {
  375: # ----------------------------------------------------------------- Hide/unhide
  376:     $r->content_type('text/html');
  377:     $r->send_http_header;
  378: 
  379:     my $entry=$ENV{'form.hide'}?$ENV{'form.hide'}:$ENV{'form.unhide'};
  380: 
  381:     my ($symb,$idx)=split(/\:\:\:/,$entry);
  382:     my ($map,$ind,$url)=split(/\_\_\_/,$symb);
  383: 
  384:     my %contrib=&Apache::lonnet::restore($symb,$ENV{'request.course.id'},
  385:                      $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
  386: 		     $ENV{'course.'.$ENV{'request.course.id'}.'.num'});
  387: 
  388:         
  389:     my $currenthidden=$contrib{'hidden'};
  390:     
  391:     if ($ENV{'form.hide'}) {
  392: 	$currenthidden.='.'.$idx.'.';
  393:     } else {
  394:         $currenthidden=~s/\.$idx\.//g;
  395:     }
  396:     my %newhash=('hidden' => $currenthidden);
  397: 
  398:     &Apache::lonnet::store(\%newhash,$symb,$ENV{'request.course.id'},
  399:                      $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
  400: 		     $ENV{'course.'.$ENV{'request.course.id'}.'.num'});
  401: 
  402:     &redirect_back($r,'/res/'.$url,'Changed discussion status<p>','0');
  403: 
  404:       
  405:   } else {
  406: # ------------------------------------------------------------- Normal feedback
  407:   my $feedurl=$ENV{'form.postdata'};
  408:   $feedurl=~s/^http\:\/\///;
  409:   $feedurl=~s/^$ENV{'SERVER_NAME'}//;
  410:   $feedurl=~s/^$ENV{'HTTP_HOST'}//;
  411: 
  412:   my $symb=&Apache::lonnet::symbread($feedurl);
  413:   my $goahead=1;
  414:   if ($feedurl=~/\.(problem|exam|quiz|assess|survey|form)$/) {
  415:       unless ($symb) { $goahead=0; }
  416:   }
  417: 
  418:   if ($goahead) {
  419: # Go ahead with feedback, no ambiguous reference
  420:     $r->content_type('text/html');
  421:     $r->send_http_header;
  422:   
  423:     if (
  424:       (
  425:        ($feedurl=~m:^/res:) && ($feedurl!~m:^/res/adm:)
  426:       ) 
  427:       || 
  428:       ($ENV{'request.course.id'} && ($feedurl!~m:^/adm:))
  429:      ) {
  430: # --------------------------------------------------- Print login screen header
  431:     unless ($ENV{'form.sendit'}) {
  432:       my $options=&screen_header($feedurl);
  433:       if ($options) {
  434: 	&mail_screen($r,$feedurl,$options);
  435:       } else {
  436: 	&fail_redirect($r,$feedurl);
  437:       }
  438:     } else {
  439:       
  440: # Get previous user input
  441:       my $prevattempts=&Apache::loncommon::get_previous_attempt(
  442:             $symb,$ENV{'user.name'},$ENV{'user.domain'},
  443:             $ENV{'request.course.id'});
  444: 
  445: # Get output from resource
  446:       my $usersaw=&resource_output($feedurl);
  447: 
  448: # Filter HTML out of message (could be nasty)
  449:       my $message=&clear_out_html;
  450: 
  451: # Assemble email
  452:       my ($email,$citations)=&assemble_email($feedurl,$message,$prevattempts,
  453:           $usersaw);
  454: 
  455: # Who gets this?
  456:       my ($typestyle,%to) = &decide_receiver($feedurl);
  457: 
  458: # Actually send mail
  459:       my ($status,$numsent)=&send_msg($feedurl,$email,$citations,%to);
  460: 
  461: # Discussion? Store that.
  462: 
  463:       if ($ENV{'form.discuss'}) {
  464: 	  $typestyle.=&adddiscuss($symb,$message);
  465:       }
  466: 
  467:       if ($ENV{'form.anondiscuss'}) {
  468: 	  $typestyle.=&adddiscuss($symb,$message,1);
  469:       }
  470: 
  471: 
  472: # Receipt screen and redirect back to where came from
  473:       &redirect_back($r,$feedurl,$typestyle,$numsent,$status);
  474: 
  475:     }
  476:    } else {
  477: # Unable to give feedback
  478:     &no_redirect_back($r,$feedurl);
  479:    }
  480:   } else {
  481: # Ambiguous Problem Resource
  482:     $r->internal_redirect('/adm/ambiguous');
  483:   }
  484: }
  485:   return OK;
  486: } 
  487: 
  488: 1;
  489: __END__
  490: 
  491: 
  492: 
  493: 

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