Annotation of loncom/interface/lonfeedback.pm, revision 1.17

1.1       www         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: #
1.5       www        12: # 3/1,2/3,2/5,2/6,2/8 Gerd Kortemeyer
1.7       albertel   13: # 2/9 Guy Albertelli
1.8       www        14: # 2/10 Gerd Kortemeyer
1.9       albertel   15: # 2/13 Guy Albertelli
1.10      www        16: # 7/25 Gerd Kortemeyer
1.13      www        17: # 7/26 Guy Albertelli
1.17    ! www        18: # 7/26,8/10,10/1,11/5 Gerd Kortemeyer
1.13      www        19: 
1.7       albertel   20: 
1.1       www        21: package Apache::lonfeedback;
                     22: 
                     23: use strict;
                     24: use Apache::Constants qw(:common);
1.3       www        25: use Apache::lonmsg();
1.9       albertel   26: use Apache::loncommon();
1.1       www        27: 
1.6       albertel   28: sub mail_screen {
                     29:   my ($r,$feedurl,$options) = @_;
                     30:   $r->print(<<ENDDOCUMENT);
1.1       www        31: <html>
                     32: <head>
                     33: <title>The LearningOnline Network with CAPA</title>
1.7       albertel   34: <meta http-equiv="pragma" content="no-cache"></meta>
1.5       www        35: <script>
                     36:     function gosubmit() {
                     37:         var rec=0;
1.12      albertel   38:         if (typeof(document.mailform.elements.author)!="undefined") {
1.5       www        39:           if (document.mailform.elements.author.checked) {
                     40:              rec=1;
                     41:           } 
                     42:         }
1.12      albertel   43:         if (typeof(document.mailform.elements.question)!="undefined") {
1.5       www        44:           if (document.mailform.elements.question.checked) {
                     45:              rec=1;
                     46:           } 
                     47:         }
1.12      albertel   48:         if (typeof(document.mailform.elements.course)!="undefined") {
1.5       www        49:           if (document.mailform.elements.course.checked) {
                     50:              rec=1;
                     51:           } 
                     52:         }
1.12      albertel   53:         if (typeof(document.mailform.elements.policy)!="undefined") {
1.5       www        54:           if (document.mailform.elements.policy.checked) {
                     55:              rec=1;
                     56:           } 
                     57:         }
1.12      albertel   58:         if (typeof(document.mailform.elements.discuss)!="undefined") {
1.10      www        59:           if (document.mailform.elements.discuss.checked) {
                     60:              rec=1;
                     61:           } 
                     62:         }
1.14      www        63:         if (typeof(document.mailform.elements.anondiscuss)!="undefined") {
                     64:           if (document.mailform.elements.anondiscuss.checked) {
                     65:              rec=1;
                     66:           } 
                     67:         }
1.5       www        68: 
                     69:         if (rec) {
                     70: 	    document.mailform.submit();
                     71:         } else {
                     72:             alert('Please check a feedback type.');
                     73: 	}
                     74:     }
                     75: </script>
1.1       www        76: </head>
1.5       www        77: <body bgcolor="#FFFFFF" onLoad="window.focus();">
1.2       www        78: <img align=right src=/adm/lonIcons/lonlogos.gif>
1.1       www        79: <h1>Feedback</h1>
1.2       www        80: <h2><tt>$feedurl</tt></h2>
1.5       www        81: <form action="/adm/feedback" method=post name=mailform>
1.2       www        82: <input type=hidden name=postdata value="$feedurl">
1.5       www        83: Please check at least one of the following feedback types:
1.2       www        84: $options<hr>
                     85: My question/comment/feedback:<p>
1.16      www        86: <textarea name=comment cols=60 rows=10 wrap=hard>
1.2       www        87: </textarea><p>
1.5       www        88: <input type=hidden name=sendit value=1>
                     89: <input type=button value="Send Feedback" onClick='gosubmit();'></input>
1.2       www        90: </form>
1.1       www        91: </body>
                     92: </html>
                     93: ENDDOCUMENT
1.6       albertel   94: }
                     95: 
                     96: sub fail_redirect {
                     97:   my ($r,$feedurl) = @_;
                     98:   $r->print (<<ENDFAILREDIR);
1.5       www        99: <head><title>Feedback not sent</title>
1.7       albertel  100: <meta http-equiv="pragma" content="no-cache"></meta>
1.5       www       101: <meta HTTP-EQUIV="Refresh" CONTENT="2; url=$feedurl">
                    102: </head>
                    103: <html>
                    104: <body bgcolor="#FFFFFF">
1.8       www       105: <img align=right src=/adm/lonIcons/lonlogos.gif>
                    106: <b>Sorry, no recipients  ...</b>
1.5       www       107: </body>
                    108: </html>
                    109: ENDFAILREDIR
                    110: }
1.4       www       111: 
1.6       albertel  112: sub redirect_back {
                    113:   my ($r,$feedurl,$typestyle,$sendsomething,$status) = @_;
                    114:   $r->print (<<ENDREDIR);
1.3       www       115: <head>
                    116: <title>Feedback sent</title>
1.7       albertel  117: <meta http-equiv="pragma" content="no-cache"></meta>
1.5       www       118: <meta HTTP-EQUIV="Refresh" CONTENT="2; url=$feedurl">
1.2       www       119: </head>
                    120: <html>
                    121: <body bgcolor="#FFFFFF">
1.8       www       122: <img align=right src=/adm/lonIcons/lonlogos.gif>
1.5       www       123: $typestyle
                    124: <b>Sent $sendsomething message(s).</b>
1.3       www       125: <font color=red>$status</font>
1.2       www       126: </body>
                    127: </html>
                    128: ENDREDIR
                    129: }
1.6       albertel  130: 
                    131: sub no_redirect_back {
                    132:   my ($r,$feedurl) = @_;
                    133:   $r->print (<<ENDNOREDIR);
1.2       www       134: <head><title>Feedback not sent</title>
1.7       albertel  135: <meta http-equiv="pragma" content="no-cache"></meta>
                    136: ENDNOREDIR
                    137: 
1.8       www       138:   if ($feedurl!~/^\/adm\/feedback/) { 
1.7       albertel  139:     $r->print('<meta HTTP-EQUIV="Refresh" CONTENT="2; url='.$feedurl.'">');
                    140:   }
                    141:   
1.8       www       142:   $r->print (<<ENDNOREDIRTWO);
1.2       www       143: </head>
                    144: <html>
                    145: <body bgcolor="#FFFFFF">
1.8       www       146: <img align=right src=/adm/lonIcons/lonlogos.gif>
                    147: <b>Sorry, no feedback possible on this resource  ...</b>
1.2       www       148: </body>
                    149: </html>
1.8       www       150: ENDNOREDIRTWO
1.2       www       151: }
1.6       albertel  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.=
1.8       www       162:     '<br><input type=checkbox name=question> Question about resource content';
1.6       albertel  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';
1.10      www       173:   }
                    174:   if ($ENV{'request.course.id'}) {
                    175:     $options.='<br><input type=checkbox name=discuss> '.
                    176: 	'<b>Contribution to course discussion of resource</b>';
1.6       albertel  177:   }
1.14      www       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:   }
1.6       albertel  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'}) {
1.8       www       228:     $typestyle.='Submitting as Author Feedback<br>';
1.6       albertel  229:     $feedurl=~/^\/res\/(\w+)\/(\w+)\//;
                    230:     $to{$2.':'.$1}=1;
                    231:   }
                    232:   if ($ENV{'form.question'}) {
1.8       www       233:     $typestyle.='Submitting as Question<br>';
1.6       albertel  234:     map {
                    235:       $to{$_}=1;
                    236:     } split(/\,/,
                    237: 	    $ENV{'course.'.$ENV{'request.course.id'}.'.question.email'});
                    238:   }
                    239:   if ($ENV{'form.course'}) {
1.8       www       240:     $typestyle.='Submitting as Comment<br>';
1.6       albertel  241:     map {
                    242:       $to{$_}=1;
                    243:     } split(/\,/,
                    244: 	    $ENV{'course.'.$ENV{'request.course.id'}.'.comment.email'});
                    245:   }
                    246:   if ($ENV{'form.policy'}) {
1.8       www       247:     $typestyle.='Submitting as Policy Feedback<br>';
1.6       albertel  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 ($_) {
1.8       www       262:       unless (&Apache::lonmsg::user_normal_msg(split(/\:/,$_),
                    263:                'Feedback '.$feedurl,$email,$citations) eq 'ok') {
1.6       albertel  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: 
1.13      www       274: sub adddiscuss {
1.14      www       275:     my ($symb,$email,$anon)=@_;
1.13      www       276:     my $status='';
                    277:     my %contrib=('message'      => $email,
                    278:                  'sendername'   => $ENV{'user.name'},
                    279:                  'senderdomain' => $ENV{'user.domain'});
1.14      www       280:     if ($anon) {
                    281: 	$contrib{'anonymous'}='true';
                    282:     }
1.13      www       283:     if (($symb) && ($email)) {
1.14      www       284:        $status='Adding to class discussion'.($anon?' (anonymous)':'').': '.
1.13      www       285:         &Apache::lonnet::store(\%contrib,$symb,$ENV{'request.course.id'},
                    286:                      $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
1.17    ! www       287: 		     $ENV{'course.'.$ENV{'request.course.id'}.'.num'});
1.13      www       288:     }
1.17    ! www       289:     my $now=time;
        !           290:     my %record=&Apache::lonnet::restore('_discussion');
        !           291:     my ($temp)=keys %record;
        !           292:     unless ($temp=~/^error\:/) {
        !           293:        my %newrecord=();
        !           294:        $newrecord{'resource'}=$symb;
        !           295:        $newrecord{'subnumber'}=$record{'subnumber'}+1;
        !           296:        $status.=' '.&Apache::lonnet::cstore(\%newrecord,'_discussion');
        !           297:     }
        !           298:     return $status.'<br>';   
1.13      www       299: }
                    300: 
1.6       albertel  301: sub handler {
                    302:   my $r = shift;
1.8       www       303:   if ($r->header_only) {
                    304:      $r->content_type('text/html');
                    305:      $r->send_http_header;
                    306:      return OK;
                    307:   }
1.15      www       308: 
                    309: # --------------------------- Get query string for limited number of parameters
                    310: 
                    311:     map {
                    312:        my ($name, $value) = split(/=/,$_);
                    313:        $value =~ tr/+/ /;
                    314:        $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
                    315:        if (($name eq 'hide') || ($name eq 'unhide')) {
                    316:            unless ($ENV{'form.'.$name}) {
                    317:               $ENV{'form.'.$name}=$value;
                    318: 	   }
                    319:        }
                    320:     } (split(/&/,$ENV{'QUERY_STRING'}));
                    321: 
                    322:   if (($ENV{'form.hide'}) || ($ENV{'form.unhide'})) {
                    323: # ----------------------------------------------------------------- Hide/unhide
                    324:     $r->content_type('text/html');
                    325:     $r->send_http_header;
                    326: 
                    327:     my $entry=$ENV{'form.hide'}?$ENV{'form.hide'}:$ENV{'form.unhide'};
                    328: 
                    329:     my ($symb,$idx)=split(/\:\:\:/,$entry);
                    330:     my ($map,$ind,$url)=split(/\_\_\_/,$symb);
                    331: 
                    332:     my %contrib=&Apache::lonnet::restore($symb,$ENV{'request.course.id'},
                    333:                      $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
                    334: 		     $ENV{'course.'.$ENV{'request.course.id'}.'.num'});
                    335: 
                    336:         
                    337:     my $currenthidden=$contrib{'hidden'};
                    338:     
                    339:     if ($ENV{'form.hide'}) {
                    340: 	$currenthidden.='.'.$idx.'.';
                    341:     } else {
                    342:         $currenthidden=~s/\.$idx\.//g;
                    343:     }
                    344:     my %newhash=('hidden' => $currenthidden);
                    345: 
                    346:     &Apache::lonnet::store(\%newhash,$symb,$ENV{'request.course.id'},
                    347:                      $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
                    348: 		     $ENV{'course.'.$ENV{'request.course.id'}.'.num'});
                    349: 
                    350:     &redirect_back($r,'/res/'.$url,'Changed discussion status<p>','0');
                    351: 
                    352:       
                    353:   } else {
                    354: # ------------------------------------------------------------- Normal feedback
1.6       albertel  355:   my $feedurl=$ENV{'form.postdata'};
                    356:   $feedurl=~s/^http\:\/\///;
                    357:   $feedurl=~s/^$ENV{'SERVER_NAME'}//;
                    358:   $feedurl=~s/^$ENV{'HTTP_HOST'}//;
1.8       www       359: 
                    360:   my $symb=&Apache::lonnet::symbread($feedurl);
                    361:   my $goahead=1;
                    362:   if ($feedurl=~/\.(problem|exam|quiz|assess|survey|form)$/) {
                    363:       unless ($symb) { $goahead=0; }
                    364:   }
                    365: 
                    366:   if ($goahead) {
                    367: # Go ahead with feedback, no ambiguous reference
                    368:     $r->content_type('text/html');
                    369:     $r->send_http_header;
1.6       albertel  370:   
1.8       www       371:     if (
1.7       albertel  372:       (
                    373:        ($feedurl=~m:^/res:) && ($feedurl!~m:^/res/adm:)
                    374:       ) 
                    375:       || 
                    376:       ($ENV{'request.course.id'} && ($feedurl!~m:^/adm:))
                    377:      ) {
1.6       albertel  378: # --------------------------------------------------- Print login screen header
                    379:     unless ($ENV{'form.sendit'}) {
                    380:       my $options=&screen_header($feedurl);
                    381:       if ($options) {
                    382: 	&mail_screen($r,$feedurl,$options);
                    383:       } else {
                    384: 	&fail_redirect($r,$feedurl);
                    385:       }
                    386:     } else {
                    387:       
                    388: # Get previous user input
1.9       albertel  389:       my $prevattempts=&Apache::loncommon::get_previous_attempt(
1.11      albertel  390:             $symb,$ENV{'user.name'},$ENV{'user.domain'},
1.9       albertel  391:             $ENV{'request.course.id'});
1.6       albertel  392: 
                    393: # Get output from resource
                    394:       my $usersaw=&resource_output($feedurl);
                    395: 
                    396: # Filter HTML out of message (could be nasty)
                    397:       my $message=&clear_out_html;
                    398: 
                    399: # Assemble email
1.8       www       400:       my ($email,$citations)=&assemble_email($feedurl,$message,$prevattempts,
                    401:           $usersaw);
1.6       albertel  402: 
                    403: # Who gets this?
                    404:       my ($typestyle,%to) = &decide_receiver($feedurl);
                    405: 
                    406: # Actually send mail
                    407:       my ($status,$numsent)=&send_msg($feedurl,$email,$citations,%to);
1.13      www       408: 
                    409: # Discussion? Store that.
                    410: 
                    411:       if ($ENV{'form.discuss'}) {
                    412: 	  $typestyle.=&adddiscuss($symb,$message);
                    413:       }
1.6       albertel  414: 
1.14      www       415:       if ($ENV{'form.anondiscuss'}) {
                    416: 	  $typestyle.=&adddiscuss($symb,$message,1);
                    417:       }
                    418: 
                    419: 
1.6       albertel  420: # Receipt screen and redirect back to where came from
1.8       www       421:       &redirect_back($r,$feedurl,$typestyle,$numsent,$status);
1.6       albertel  422: 
                    423:     }
1.8       www       424:    } else {
1.7       albertel  425: # Unable to give feedback
1.6       albertel  426:     &no_redirect_back($r,$feedurl);
1.8       www       427:    }
                    428:   } else {
                    429: # Ambiguous Problem Resource
                    430:     $r->internal_redirect('/adm/ambiguous');
1.6       albertel  431:   }
1.15      www       432: }
1.6       albertel  433:   return OK;
1.1       www       434: } 
                    435: 
                    436: 1;
                    437: __END__
1.14      www       438: 
1.2       www       439: 
                    440: 
                    441: 

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